2019-01-15 13:28:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tls"
|
2019-01-15 13:28:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAggregator(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
given dynamic.Configurations
|
|
|
|
expected *dynamic.HTTPConfiguration
|
2019-01-15 13:28:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Nil returns an empty configuration",
|
|
|
|
given: nil,
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: make(map[string]*dynamic.Router),
|
|
|
|
Middlewares: make(map[string]*dynamic.Middleware),
|
|
|
|
Services: make(map[string]*dynamic.Service),
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a mono-provider configuration map",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-03-14 08:30:04 +00:00
|
|
|
"router-1": {},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
2019-03-14 08:30:04 +00:00
|
|
|
"middleware-1": {},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.Service{
|
2019-03-14 08:30:04 +00:00
|
|
|
"service-1": {},
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-06-21 07:54:04 +00:00
|
|
|
"router-1@provider-1": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
2019-06-21 07:54:04 +00:00
|
|
|
"middleware-1@provider-1": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.Service{
|
2019-06-21 07:54:04 +00:00
|
|
|
"service-1@provider-1": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a multi-provider configuration map",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-03-14 08:30:04 +00:00
|
|
|
"router-1": {},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
2019-03-14 08:30:04 +00:00
|
|
|
"middleware-1": {},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.Service{
|
2019-03-14 08:30:04 +00:00
|
|
|
"service-1": {},
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
"provider-2": &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-03-14 08:30:04 +00:00
|
|
|
"router-1": {},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
2019-03-14 08:30:04 +00:00
|
|
|
"middleware-1": {},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.Service{
|
2019-03-14 08:30:04 +00:00
|
|
|
"service-1": {},
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-06-21 07:54:04 +00:00
|
|
|
"router-1@provider-1": {},
|
|
|
|
"router-1@provider-2": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
2019-06-21 07:54:04 +00:00
|
|
|
"middleware-1@provider-1": {},
|
|
|
|
"middleware-1@provider-2": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.Service{
|
2019-06-21 07:54:04 +00:00
|
|
|
"service-1@provider-1": {},
|
|
|
|
"service-1@provider-2": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2019-01-15 13:28:04 +00:00
|
|
|
actual := mergeConfiguration(test.given)
|
2019-03-14 08:30:04 +00:00
|
|
|
assert.Equal(t, test.expected, actual.HTTP)
|
2019-01-15 13:28:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 15:18:05 +00:00
|
|
|
|
|
|
|
func TestAggregator_tlsoptions(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
given dynamic.Configurations
|
2019-06-27 21:58:03 +00:00
|
|
|
expected map[string]tls.Options
|
2019-06-21 15:18:05 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Nil returns an empty configuration",
|
|
|
|
given: nil,
|
2019-06-27 21:58:03 +00:00
|
|
|
expected: map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a mono-provider configuration map",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-27 21:58:03 +00:00
|
|
|
expected: map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"default": {},
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a multi-provider configuration map",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
"provider-2": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-27 21:58:03 +00:00
|
|
|
expected: map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"default": {},
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"foo@provider-2": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Create a valid default tls option when appears only in one provider",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS11",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
"provider-2": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-27 21:58:03 +00:00
|
|
|
expected: map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS11",
|
|
|
|
},
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"foo@provider-2": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "No default tls option if it is defined in multiple providers",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS11",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
"provider-2": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-27 21:58:03 +00:00
|
|
|
expected: map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
"foo@provider-2": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Create a default TLS Options configuration if none was provided",
|
2019-07-10 07:26:04 +00:00
|
|
|
given: dynamic.Configurations{
|
|
|
|
"provider-1": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
"provider-2": &dynamic.Configuration{
|
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Options: map[string]tls.Options{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
2019-06-21 15:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-27 21:58:03 +00:00
|
|
|
expected: map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"default": {},
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
"foo@provider-2": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := mergeConfiguration(test.given)
|
2019-06-27 21:58:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual.TLS.Options)
|
2019-06-21 15:18:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|