2019-01-15 13:28:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/pkg/config"
|
2019-06-21 15:18:05 +00:00
|
|
|
"github.com/containous/traefik/pkg/tls"
|
2019-01-15 13:28:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAggregator(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
given config.Configurations
|
2019-03-14 08:30:04 +00:00
|
|
|
expected *config.HTTPConfiguration
|
2019-01-15 13:28:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Nil returns an empty configuration",
|
|
|
|
given: nil,
|
2019-03-14 08:30:04 +00:00
|
|
|
expected: &config.HTTPConfiguration{
|
2019-01-15 13:28:04 +00:00
|
|
|
Routers: make(map[string]*config.Router),
|
|
|
|
Middlewares: make(map[string]*config.Middleware),
|
|
|
|
Services: make(map[string]*config.Service),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a mono-provider configuration map",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
2019-03-14 08:30:04 +00:00
|
|
|
HTTP: &config.HTTPConfiguration{
|
|
|
|
Routers: map[string]*config.Router{
|
|
|
|
"router-1": {},
|
|
|
|
},
|
|
|
|
Middlewares: map[string]*config.Middleware{
|
|
|
|
"middleware-1": {},
|
|
|
|
},
|
|
|
|
Services: map[string]*config.Service{
|
|
|
|
"service-1": {},
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-03-14 08:30:04 +00:00
|
|
|
expected: &config.HTTPConfiguration{
|
2019-01-15 13:28:04 +00:00
|
|
|
Routers: map[string]*config.Router{
|
2019-06-21 07:54:04 +00:00
|
|
|
"router-1@provider-1": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
Middlewares: map[string]*config.Middleware{
|
2019-06-21 07:54:04 +00:00
|
|
|
"middleware-1@provider-1": {},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
Services: map[string]*config.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",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
2019-03-14 08:30:04 +00:00
|
|
|
HTTP: &config.HTTPConfiguration{
|
|
|
|
Routers: map[string]*config.Router{
|
|
|
|
"router-1": {},
|
|
|
|
},
|
|
|
|
Middlewares: map[string]*config.Middleware{
|
|
|
|
"middleware-1": {},
|
|
|
|
},
|
|
|
|
Services: map[string]*config.Service{
|
|
|
|
"service-1": {},
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"provider-2": &config.Configuration{
|
2019-03-14 08:30:04 +00:00
|
|
|
HTTP: &config.HTTPConfiguration{
|
|
|
|
Routers: map[string]*config.Router{
|
|
|
|
"router-1": {},
|
|
|
|
},
|
|
|
|
Middlewares: map[string]*config.Middleware{
|
|
|
|
"middleware-1": {},
|
|
|
|
},
|
|
|
|
Services: map[string]*config.Service{
|
|
|
|
"service-1": {},
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-03-14 08:30:04 +00:00
|
|
|
expected: &config.HTTPConfiguration{
|
2019-01-15 13:28:04 +00:00
|
|
|
Routers: map[string]*config.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
|
|
|
},
|
|
|
|
Middlewares: map[string]*config.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
|
|
|
},
|
|
|
|
Services: map[string]*config.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
|
|
|
|
given config.Configurations
|
|
|
|
expected map[string]tls.TLS
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Nil returns an empty configuration",
|
|
|
|
given: nil,
|
|
|
|
expected: map[string]tls.TLS{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a mono-provider configuration map",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]tls.TLS{
|
|
|
|
"default": {},
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Returns fully qualified elements from a multi-provider configuration map",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"provider-2": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]tls.TLS{
|
|
|
|
"default": {},
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"foo@provider-2": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Create a valid default tls option when appears only in one provider",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS11",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"provider-2": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]tls.TLS{
|
|
|
|
"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",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS11",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"provider-2": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]tls.TLS{
|
|
|
|
"foo@provider-1": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
"foo@provider-2": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Create a default TLS Options configuration if none was provided",
|
|
|
|
given: config.Configurations{
|
|
|
|
"provider-1": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"provider-2": &config.Configuration{
|
|
|
|
TLSOptions: map[string]tls.TLS{
|
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]tls.TLS{
|
|
|
|
"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)
|
|
|
|
assert.Equal(t, test.expected, actual.TLSOptions)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|