traefik/provider/rancher/config_test.go

599 lines
14 KiB
Go
Raw Normal View History

package rancher
2017-02-05 23:58:05 +00:00
import (
"testing"
"time"
"github.com/containous/flaeg"
"github.com/containous/traefik/provider/label"
"github.com/containous/traefik/types"
2017-10-12 15:50:03 +00:00
"github.com/stretchr/testify/assert"
2017-12-15 10:48:03 +00:00
"github.com/stretchr/testify/require"
2017-02-05 23:58:05 +00:00
)
2017-12-16 13:25:10 +00:00
func TestProviderBuildConfiguration(t *testing.T) {
provider := &Provider{
Domain: "rancher.localhost",
ExposedByDefault: true,
}
testCases := []struct {
desc string
services []rancherData
expectedFrontends map[string]*types.Frontend
expectedBackends map[string]*types.Backend
}{
{
desc: "without services",
services: []rancherData{},
expectedFrontends: map[string]*types.Frontend{},
expectedBackends: map[string]*types.Backend{},
},
{
desc: "with services",
services: []rancherData{
{
Name: "test/service",
Labels: map[string]string{
label.TraefikPort: "80",
label.TraefikFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
label.TraefikFrontendRedirectEntryPoint: "https",
},
Health: "healthy",
Containers: []string{"127.0.0.1"},
},
},
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test-service-rancher-localhost": {
Backend: "backend-test-service",
PassHostHeader: true,
EntryPoints: []string{},
BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
Priority: 0,
Redirect: &types.Redirect{
EntryPoint: "https",
},
Routes: map[string]types.Route{
"route-frontend-Host-test-service-rancher-localhost": {
Rule: "Host:test.service.rancher.localhost",
},
},
},
},
expectedBackends: map[string]*types.Backend{
"backend-test-service": {
Servers: map[string]types.Server{
"server-0": {
URL: "http://127.0.0.1:80",
Weight: 0,
},
},
CircuitBreaker: nil,
},
},
},
{
desc: "with Error Pages",
services: []rancherData{
{
Name: "test/service",
Labels: map[string]string{
label.TraefikPort: "80",
label.Prefix + label.BaseFrontendErrorPage + "foo." + label.SuffixErrorPageStatus: "404",
label.Prefix + label.BaseFrontendErrorPage + "foo." + label.SuffixErrorPageBackend: "foobar",
label.Prefix + label.BaseFrontendErrorPage + "foo." + label.SuffixErrorPageQuery: "foo_query",
label.Prefix + label.BaseFrontendErrorPage + "bar." + label.SuffixErrorPageStatus: "500,600",
label.Prefix + label.BaseFrontendErrorPage + "bar." + label.SuffixErrorPageBackend: "foobar",
label.Prefix + label.BaseFrontendErrorPage + "bar." + label.SuffixErrorPageQuery: "bar_query",
},
Health: "healthy",
Containers: []string{"127.0.0.1"},
},
},
expectedBackends: map[string]*types.Backend{
"backend-test-service": {
Servers: map[string]types.Server{
"server-0": {
URL: "http://127.0.0.1:80",
Weight: 0,
},
},
},
},
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test-service-rancher-localhost": {
EntryPoints: []string{},
BasicAuth: []string{},
Backend: "backend-test-service",
Routes: map[string]types.Route{
"route-frontend-Host-test-service-rancher-localhost": {
Rule: "Host:test.service.rancher.localhost",
},
},
PassHostHeader: true,
Errors: map[string]*types.ErrorPage{
"foo": {
Status: []string{"404"},
Query: "foo_query",
Backend: "foobar",
},
"bar": {
Status: []string{"500", "600"},
Query: "bar_query",
Backend: "foobar",
},
},
},
},
},
{
desc: "with rate Limits",
services: []rancherData{
{
Name: "test/service",
Labels: map[string]string{
label.TraefikPort: "80",
label.TraefikFrontendRateLimitExtractorFunc: "client.ip",
label.Prefix + label.BaseFrontendRateLimit + "foo." + label.SuffixRateLimitPeriod: "6",
label.Prefix + label.BaseFrontendRateLimit + "foo." + label.SuffixRateLimitAverage: "12",
label.Prefix + label.BaseFrontendRateLimit + "foo." + label.SuffixRateLimitBurst: "18",
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitPeriod: "3",
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitAverage: "6",
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitBurst: "9",
},
Health: "healthy",
Containers: []string{"127.0.0.1"},
},
},
expectedBackends: map[string]*types.Backend{
"backend-test-service": {
Servers: map[string]types.Server{
"server-0": {
URL: "http://127.0.0.1:80",
Weight: 0,
},
},
},
},
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test-service-rancher-localhost": {
EntryPoints: []string{},
BasicAuth: []string{},
Backend: "backend-test-service",
Routes: map[string]types.Route{
"route-frontend-Host-test-service-rancher-localhost": {
Rule: "Host:test.service.rancher.localhost",
},
},
PassHostHeader: true,
RateLimit: &types.RateLimit{
ExtractorFunc: "client.ip",
RateSet: map[string]*types.Rate{
"foo": {
Period: flaeg.Duration(6 * time.Second),
Average: 12,
Burst: 18,
},
"bar": {
Period: flaeg.Duration(3 * time.Second),
Average: 6,
Burst: 9,
},
},
},
},
},
},
2017-12-16 13:25:10 +00:00
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actualConfig := provider.buildConfiguration(test.services)
require.NotNil(t, actualConfig)
assert.EqualValues(t, test.expectedBackends, actualConfig.Backends)
assert.EqualValues(t, test.expectedFrontends, actualConfig.Frontends)
})
}
}
2017-11-20 10:40:04 +00:00
func TestProviderServiceFilter(t *testing.T) {
provider := &Provider{
Domain: "rancher.localhost",
EnableServiceHealthFilter: true,
}
constraint, _ := types.NewConstraint("tag==ch*se")
provider.Constraints = types.Constraints{constraint}
2017-11-20 10:40:04 +00:00
testCases := []struct {
desc string
service rancherData
expected bool
}{
{
2017-11-20 10:40:04 +00:00
desc: "missing Port labels, don't respect constraint",
service: rancherData{
Labels: map[string]string{
label.TraefikEnable: "true",
},
Health: "healthy",
State: "active",
},
expected: false,
},
{
2017-11-20 10:40:04 +00:00
desc: "don't respect constraint",
service: rancherData{
Labels: map[string]string{
label.TraefikPort: "80",
label.TraefikEnable: "false",
},
Health: "healthy",
State: "active",
},
expected: false,
},
{
2017-11-20 10:40:04 +00:00
desc: "unhealthy",
service: rancherData{
Labels: map[string]string{
label.TraefikTags: "cheese",
label.TraefikPort: "80",
label.TraefikEnable: "true",
},
Health: "unhealthy",
State: "active",
},
expected: false,
},
{
2017-11-20 10:40:04 +00:00
desc: "inactive",
service: rancherData{
Labels: map[string]string{
label.TraefikTags: "not-cheesy",
label.TraefikPort: "80",
label.TraefikEnable: "true",
},
Health: "healthy",
State: "inactive",
},
expected: false,
},
{
2017-11-20 10:40:04 +00:00
desc: "healthy & active, tag: cheese",
service: rancherData{
Labels: map[string]string{
label.TraefikTags: "cheese",
label.TraefikPort: "80",
label.TraefikEnable: "true",
},
Health: "healthy",
State: "active",
},
expected: true,
},
{
2017-11-20 10:40:04 +00:00
desc: "healthy & active, tag: chose",
service: rancherData{
Labels: map[string]string{
label.TraefikTags: "chose",
label.TraefikPort: "80",
label.TraefikEnable: "true",
},
Health: "healthy",
2017-11-20 10:40:04 +00:00
State: "active",
},
expected: true,
},
{
2017-11-20 10:40:04 +00:00
desc: "healthy & upgraded",
service: rancherData{
Labels: map[string]string{
label.TraefikTags: "cheeeeese",
label.TraefikPort: "80",
label.TraefikEnable: "true",
},
Health: "healthy",
2017-11-20 10:40:04 +00:00
State: "upgraded",
},
expected: true,
},
}
2017-11-20 10:40:04 +00:00
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actual := provider.serviceFilter(test.service)
assert.Equal(t, test.expected, actual)
})
}
}
2017-11-20 10:40:04 +00:00
func TestContainerFilter(t *testing.T) {
testCases := []struct {
name string
healthState string
state string
expected bool
}{
{
healthState: "unhealthy",
state: "running",
expected: false,
},
{
healthState: "healthy",
state: "stopped",
expected: false,
},
{
state: "stopped",
expected: false,
},
{
healthState: "healthy",
state: "running",
expected: true,
},
{
healthState: "updating-healthy",
state: "updating-running",
expected: true,
},
}
2017-11-20 10:40:04 +00:00
for _, test := range testCases {
test := test
t.Run(test.healthState+" "+test.state, func(t *testing.T) {
t.Parallel()
actual := containerFilter(test.name, test.healthState, test.state)
assert.Equal(t, test.expected, actual)
})
}
}
2017-11-20 10:40:04 +00:00
func TestProviderGetFrontendName(t *testing.T) {
provider := &Provider{Domain: "rancher.localhost"}
2017-02-05 23:58:05 +00:00
2017-11-20 10:40:04 +00:00
testCases := []struct {
desc string
2017-02-05 23:58:05 +00:00
service rancherData
expected string
}{
{
2017-11-20 10:40:04 +00:00
desc: "default",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "foo",
},
expected: "Host-foo-rancher-localhost",
},
{
2017-11-20 10:40:04 +00:00
desc: "with Headers label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "Headers:User-Agent,bat/0.1.0",
2017-02-05 23:58:05 +00:00
},
},
expected: "Headers-User-Agent-bat-0-1-0",
},
{
2017-11-20 10:40:04 +00:00
desc: "with Host label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "Host:foo.bar",
2017-02-05 23:58:05 +00:00
},
},
expected: "Host-foo-bar",
},
{
2017-11-20 10:40:04 +00:00
desc: "with Path label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "Path:/test",
2017-02-05 23:58:05 +00:00
},
},
expected: "Path-test",
},
{
2017-11-20 10:40:04 +00:00
desc: "with PathPrefix label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "PathPrefix:/test2",
2017-02-05 23:58:05 +00:00
},
},
expected: "PathPrefix-test2",
},
}
2017-11-20 10:40:04 +00:00
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actual := provider.getFrontendName(test.service)
assert.Equal(t, test.expected, actual)
})
2017-02-05 23:58:05 +00:00
}
}
2017-11-20 10:40:04 +00:00
func TestProviderGetFrontendRule(t *testing.T) {
provider := &Provider{Domain: "rancher.localhost"}
2017-02-05 23:58:05 +00:00
2017-11-20 10:40:04 +00:00
testCases := []struct {
desc string
2017-02-05 23:58:05 +00:00
service rancherData
expected string
}{
{
2017-11-20 10:40:04 +00:00
desc: "host",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "foo",
},
expected: "Host:foo.rancher.localhost",
},
2017-02-20 19:41:28 +00:00
{
2017-11-20 10:40:04 +00:00
desc: "host with /",
2017-02-20 19:41:28 +00:00
service: rancherData{
Name: "foo/bar",
},
expected: "Host:foo.bar.rancher.localhost",
},
2017-02-05 23:58:05 +00:00
{
2017-11-20 10:40:04 +00:00
desc: "with Host label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "Host:foo.bar.com",
2017-02-05 23:58:05 +00:00
},
},
expected: "Host:foo.bar.com",
},
{
2017-11-20 10:40:04 +00:00
desc: "with Path label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "Path:/test",
2017-02-05 23:58:05 +00:00
},
},
expected: "Path:/test",
},
{
2017-11-20 10:40:04 +00:00
desc: "with PathPrefix label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikFrontendRule: "PathPrefix:/test2",
2017-02-05 23:58:05 +00:00
},
},
expected: "PathPrefix:/test2",
},
}
2017-11-20 10:40:04 +00:00
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actual := provider.getFrontendRule(test.service)
assert.Equal(t, test.expected, actual)
})
2017-02-05 23:58:05 +00:00
}
}
func TestGetBackend(t *testing.T) {
2017-11-20 10:40:04 +00:00
testCases := []struct {
desc string
2017-02-05 23:58:05 +00:00
service rancherData
expected string
}{
{
2017-11-20 10:40:04 +00:00
desc: "without label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
},
expected: "test-service",
},
{
2017-11-20 10:40:04 +00:00
desc: "with label",
2017-02-05 23:58:05 +00:00
service: rancherData{
Name: "test-service",
Labels: map[string]string{
label.TraefikBackend: "foobar",
2017-02-05 23:58:05 +00:00
},
},
expected: "foobar",
},
}
2017-11-20 10:40:04 +00:00
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actual := getBackend(test.service)
2017-11-20 10:40:04 +00:00
assert.Equal(t, test.expected, actual)
})
}
}
2017-12-15 10:48:03 +00:00
func TestHasRedirect(t *testing.T) {
testCases := []struct {
desc string
service rancherData
expected bool
}{
{
desc: "without redirect labels",
service: rancherData{
Name: "test-service",
},
expected: false,
},
{
desc: "with Redirect EntryPoint label",
service: rancherData{
Name: "test-service",
Labels: map[string]string{
2017-12-15 21:16:48 +00:00
label.TraefikFrontendRedirectEntryPoint: "https",
2017-12-15 10:48:03 +00:00
},
},
expected: true,
},
{
desc: "with Redirect regex label",
service: rancherData{
Name: "test-service",
Labels: map[string]string{
2017-12-15 21:16:48 +00:00
label.TraefikFrontendRedirectRegex: `(.+)`,
2017-12-15 10:48:03 +00:00
},
},
expected: false,
},
{
desc: "with Redirect replacement label",
service: rancherData{
Name: "test-service",
Labels: map[string]string{
2017-12-15 21:16:48 +00:00
label.TraefikFrontendRedirectReplacement: "$1",
2017-12-15 10:48:03 +00:00
},
},
expected: false,
},
{
desc: "with Redirect regex & replacement labels",
service: rancherData{
Name: "test-service",
Labels: map[string]string{
2017-12-15 21:16:48 +00:00
label.TraefikFrontendRedirectRegex: `(.+)`,
label.TraefikFrontendRedirectReplacement: "$1",
2017-12-15 10:48:03 +00:00
},
},
expected: true,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actual := hasRedirect(test.service)
assert.Equal(t, test.expected, actual)
})
}
}