2020-01-14 14:48:06 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2020-01-14 14:48:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_parseRouterConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
annotations map[string]string
|
|
|
|
expected *RouterConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "router annotations",
|
|
|
|
annotations: map[string]string{
|
|
|
|
"ingress.kubernetes.io/foo": "bar",
|
|
|
|
"traefik.ingress.kubernetes.io/foo": "bar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.pathmatcher": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.entrypoints": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.middlewares": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.priority": "42",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls": "true",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.certresolver": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.0.main": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.0.sans": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.1.main": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.1.sans": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.options": "foobar",
|
|
|
|
},
|
|
|
|
expected: &RouterConfig{
|
|
|
|
Router: &RouterIng{
|
|
|
|
PathMatcher: "foobar",
|
|
|
|
EntryPoints: []string{"foobar", "foobar"},
|
|
|
|
Middlewares: []string{"foobar", "foobar"},
|
|
|
|
Priority: 42,
|
|
|
|
TLS: &dynamic.RouterTLSConfig{
|
|
|
|
CertResolver: "foobar",
|
|
|
|
Domains: []types.Domain{
|
|
|
|
{
|
|
|
|
Main: "foobar",
|
|
|
|
SANs: []string{"foobar", "foobar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Main: "foobar",
|
|
|
|
SANs: []string{"foobar", "foobar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Options: "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "simple TLS annotation",
|
|
|
|
annotations: map[string]string{
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls": "true",
|
|
|
|
},
|
|
|
|
expected: &RouterConfig{
|
|
|
|
Router: &RouterIng{
|
|
|
|
PathMatcher: "PathPrefix",
|
|
|
|
TLS: &dynamic.RouterTLSConfig{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "empty map",
|
|
|
|
annotations: nil,
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "nil map",
|
|
|
|
annotations: nil,
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cfg, err := parseRouterConfig(test.annotations)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, cfg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_parseServiceConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
annotations map[string]string
|
|
|
|
expected *ServiceConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "service annotations",
|
|
|
|
annotations: map[string]string{
|
|
|
|
"ingress.kubernetes.io/foo": "bar",
|
|
|
|
"traefik.ingress.kubernetes.io/foo": "bar",
|
|
|
|
"traefik.ingress.kubernetes.io/service.serversscheme": "protocol",
|
2021-04-20 15:19:29 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.serverstransport": "foobar@file",
|
2020-01-14 14:48:06 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.passhostheader": "true",
|
2020-07-01 10:58:05 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie": "true",
|
2020-01-14 14:48:06 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.httponly": "true",
|
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.name": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.secure": "true",
|
2020-03-24 13:02:58 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.samesite": "none",
|
2020-01-14 14:48:06 +00:00
|
|
|
},
|
|
|
|
expected: &ServiceConfig{
|
|
|
|
Service: &ServiceIng{
|
|
|
|
Sticky: &dynamic.Sticky{
|
|
|
|
Cookie: &dynamic.Cookie{
|
|
|
|
Name: "foobar",
|
|
|
|
Secure: true,
|
|
|
|
HTTPOnly: true,
|
2020-03-24 13:02:58 +00:00
|
|
|
SameSite: "none",
|
2020-01-14 14:48:06 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-20 15:19:29 +00:00
|
|
|
ServersScheme: "protocol",
|
|
|
|
ServersTransport: "foobar@file",
|
|
|
|
PassHostHeader: Bool(true),
|
2020-01-14 14:48:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "simple sticky annotation",
|
|
|
|
annotations: map[string]string{
|
2020-07-01 10:58:05 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie": "true",
|
2020-01-14 14:48:06 +00:00
|
|
|
},
|
|
|
|
expected: &ServiceConfig{
|
|
|
|
Service: &ServiceIng{
|
2020-07-01 10:58:05 +00:00
|
|
|
Sticky: &dynamic.Sticky{Cookie: &dynamic.Cookie{}},
|
2020-01-14 14:48:06 +00:00
|
|
|
PassHostHeader: Bool(true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "empty map",
|
|
|
|
annotations: map[string]string{},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "nil map",
|
|
|
|
annotations: nil,
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cfg, err := parseServiceConfig(test.annotations)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, cfg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_convertAnnotations(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
annotations map[string]string
|
|
|
|
expected map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "router annotations",
|
|
|
|
annotations: map[string]string{
|
|
|
|
"ingress.kubernetes.io/foo": "bar",
|
|
|
|
"traefik.ingress.kubernetes.io/foo": "bar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.pathmatcher": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.entrypoints": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.middlewares": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.priority": "42",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls": "true",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.certresolver": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.0.main": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.0.sans": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.1.main": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.domains.1.sans": "foobar,foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/router.tls.options": "foobar",
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"traefik.foo": "bar",
|
|
|
|
"traefik.router.pathmatcher": "foobar",
|
|
|
|
"traefik.router.entrypoints": "foobar,foobar",
|
|
|
|
"traefik.router.middlewares": "foobar,foobar",
|
|
|
|
"traefik.router.priority": "42",
|
|
|
|
"traefik.router.tls": "true",
|
|
|
|
"traefik.router.tls.certresolver": "foobar",
|
|
|
|
"traefik.router.tls.domains[0].main": "foobar",
|
|
|
|
"traefik.router.tls.domains[0].sans": "foobar,foobar",
|
|
|
|
"traefik.router.tls.domains[1].main": "foobar",
|
|
|
|
"traefik.router.tls.domains[1].sans": "foobar,foobar",
|
|
|
|
"traefik.router.tls.options": "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "service annotations",
|
|
|
|
annotations: map[string]string{
|
|
|
|
"traefik.ingress.kubernetes.io/service.serversscheme": "protocol",
|
2021-04-20 15:19:29 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.serverstransport": "foobar@file",
|
2020-01-14 14:48:06 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.passhostheader": "true",
|
2020-07-01 10:58:05 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie": "true",
|
2020-01-14 14:48:06 +00:00
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.httponly": "true",
|
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.name": "foobar",
|
|
|
|
"traefik.ingress.kubernetes.io/service.sticky.cookie.secure": "true",
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"traefik.service.passhostheader": "true",
|
|
|
|
"traefik.service.serversscheme": "protocol",
|
2021-04-20 15:19:29 +00:00
|
|
|
"traefik.service.serverstransport": "foobar@file",
|
2020-07-01 10:58:05 +00:00
|
|
|
"traefik.service.sticky.cookie": "true",
|
2020-01-14 14:48:06 +00:00
|
|
|
"traefik.service.sticky.cookie.httponly": "true",
|
|
|
|
"traefik.service.sticky.cookie.name": "foobar",
|
|
|
|
"traefik.service.sticky.cookie.secure": "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "empty map",
|
|
|
|
annotations: map[string]string{},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "nil map",
|
|
|
|
annotations: nil,
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
labels := convertAnnotations(test.annotations)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, labels)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|