2017-04-17 10:50:02 +00:00
|
|
|
package rancher
|
2017-02-05 23:58:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-12-20 15:33:26 +00:00
|
|
|
"time"
|
2017-04-29 19:37:54 +00:00
|
|
|
|
2017-12-20 15:33:26 +00:00
|
|
|
"github.com/containous/flaeg"
|
2017-12-02 18:29:09 +00:00
|
|
|
"github.com/containous/traefik/provider/label"
|
2017-04-29 19:37:54 +00:00
|
|
|
"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{},
|
|
|
|
},
|
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "when all labels are set",
|
2017-12-16 13:25:10 +00:00
|
|
|
services: []rancherData{
|
|
|
|
{
|
|
|
|
Labels: map[string]string{
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikPort: "666",
|
|
|
|
label.TraefikProtocol: "https",
|
|
|
|
label.TraefikWeight: "12",
|
|
|
|
|
|
|
|
label.TraefikBackend: "foobar",
|
|
|
|
|
|
|
|
label.TraefikBackendCircuitBreakerExpression: "NetworkErrorRatio() > 0.5",
|
|
|
|
label.TraefikBackendHealthCheckPath: "/health",
|
|
|
|
label.TraefikBackendHealthCheckPort: "880",
|
|
|
|
label.TraefikBackendHealthCheckInterval: "6",
|
|
|
|
label.TraefikBackendLoadBalancerMethod: "drr",
|
|
|
|
label.TraefikBackendLoadBalancerSticky: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickiness: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickinessCookieName: "chocolate",
|
|
|
|
label.TraefikBackendMaxConnAmount: "666",
|
|
|
|
label.TraefikBackendMaxConnExtractorFunc: "client.ip",
|
2018-01-31 14:32:04 +00:00
|
|
|
label.TraefikBackendBufferingMaxResponseBodyBytes: "10485760",
|
|
|
|
label.TraefikBackendBufferingMemResponseBodyBytes: "2097152",
|
|
|
|
label.TraefikBackendBufferingMaxRequestBodyBytes: "10485760",
|
|
|
|
label.TraefikBackendBufferingMemRequestBodyBytes: "2097152",
|
|
|
|
label.TraefikBackendBufferingRetryExpression: "IsNetworkError() && Attempts() <= 2",
|
2018-01-10 17:08:03 +00:00
|
|
|
|
|
|
|
label.TraefikFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
label.TraefikFrontendEntryPoints: "http,https",
|
|
|
|
label.TraefikFrontendPassHostHeader: "true",
|
|
|
|
label.TraefikFrontendPassTLSCert: "true",
|
|
|
|
label.TraefikFrontendPriority: "666",
|
|
|
|
label.TraefikFrontendRedirectEntryPoint: "https",
|
|
|
|
label.TraefikFrontendRedirectRegex: "nope",
|
|
|
|
label.TraefikFrontendRedirectReplacement: "nope",
|
2018-01-31 18:10:04 +00:00
|
|
|
label.TraefikFrontendRedirectPermanent: "true",
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikFrontendRule: "Host:traefik.io",
|
|
|
|
label.TraefikFrontendWhitelistSourceRange: "10.10.10.10",
|
|
|
|
|
|
|
|
label.TraefikFrontendRequestHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendResponseHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendSSLProxyHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendAllowedHosts: "foo,bar,bor",
|
|
|
|
label.TraefikFrontendHostsProxyHeaders: "foo,bar,bor",
|
|
|
|
label.TraefikFrontendSSLHost: "foo",
|
|
|
|
label.TraefikFrontendCustomFrameOptionsValue: "foo",
|
|
|
|
label.TraefikFrontendContentSecurityPolicy: "foo",
|
|
|
|
label.TraefikFrontendPublicKey: "foo",
|
|
|
|
label.TraefikFrontendReferrerPolicy: "foo",
|
2018-03-02 13:24:03 +00:00
|
|
|
label.TraefikFrontendCustomBrowserXSSValue: "foo",
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikFrontendSTSSeconds: "666",
|
|
|
|
label.TraefikFrontendSSLRedirect: "true",
|
|
|
|
label.TraefikFrontendSSLTemporaryRedirect: "true",
|
|
|
|
label.TraefikFrontendSTSIncludeSubdomains: "true",
|
|
|
|
label.TraefikFrontendSTSPreload: "true",
|
|
|
|
label.TraefikFrontendForceSTSHeader: "true",
|
|
|
|
label.TraefikFrontendFrameDeny: "true",
|
|
|
|
label.TraefikFrontendContentTypeNosniff: "true",
|
|
|
|
label.TraefikFrontendBrowserXSSFilter: "true",
|
|
|
|
label.TraefikFrontendIsDevelopment: "true",
|
|
|
|
|
2017-12-20 15:33:26 +00:00
|
|
|
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",
|
2018-01-10 17:08:03 +00:00
|
|
|
|
|
|
|
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",
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
2018-01-10 17:08:03 +00:00
|
|
|
Containers: []string{"10.0.0.1", "10.0.0.2"},
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2018-01-10 17:08:03 +00:00
|
|
|
"frontend-Host-traefik-io": {
|
|
|
|
EntryPoints: []string{
|
|
|
|
"http",
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
Backend: "backend-foobar",
|
2017-12-20 15:33:26 +00:00
|
|
|
Routes: map[string]types.Route{
|
2018-01-10 17:08:03 +00:00
|
|
|
"route-frontend-Host-traefik-io": {
|
|
|
|
Rule: "Host:traefik.io",
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
PassHostHeader: true,
|
2018-01-10 17:08:03 +00:00
|
|
|
PassTLSCert: true,
|
|
|
|
Priority: 666,
|
|
|
|
BasicAuth: []string{
|
|
|
|
"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
|
|
|
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
},
|
|
|
|
WhitelistSourceRange: []string{
|
|
|
|
"10.10.10.10",
|
|
|
|
},
|
|
|
|
Headers: &types.Headers{
|
|
|
|
CustomRequestHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
CustomResponseHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
AllowedHosts: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bor",
|
|
|
|
},
|
|
|
|
HostsProxyHeaders: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bor",
|
|
|
|
},
|
|
|
|
SSLRedirect: true,
|
|
|
|
SSLTemporaryRedirect: true,
|
|
|
|
SSLHost: "foo",
|
|
|
|
SSLProxyHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
STSSeconds: 666,
|
|
|
|
STSIncludeSubdomains: true,
|
|
|
|
STSPreload: true,
|
|
|
|
ForceSTSHeader: true,
|
|
|
|
FrameDeny: true,
|
|
|
|
CustomFrameOptionsValue: "foo",
|
|
|
|
ContentTypeNosniff: true,
|
|
|
|
BrowserXSSFilter: true,
|
2018-03-02 13:24:03 +00:00
|
|
|
CustomBrowserXSSValue: "foo",
|
2018-01-10 17:08:03 +00:00
|
|
|
ContentSecurityPolicy: "foo",
|
|
|
|
PublicKey: "foo",
|
|
|
|
ReferrerPolicy: "foo",
|
|
|
|
IsDevelopment: true,
|
|
|
|
},
|
2017-12-20 15:33:26 +00:00
|
|
|
Errors: map[string]*types.ErrorPage{
|
|
|
|
"foo": {
|
|
|
|
Status: []string{"404"},
|
|
|
|
Query: "foo_query",
|
|
|
|
Backend: "foobar",
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Status: []string{"500", "600"},
|
|
|
|
Query: "bar_query",
|
|
|
|
Backend: "foobar",
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Redirect: &types.Redirect{
|
|
|
|
EntryPoint: "https",
|
|
|
|
Regex: "",
|
|
|
|
Replacement: "",
|
2018-01-31 18:10:04 +00:00
|
|
|
Permanent: true,
|
2018-01-10 17:08:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-foobar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-0": {
|
|
|
|
URL: "https://10.0.0.1:666",
|
|
|
|
Weight: 12,
|
|
|
|
},
|
|
|
|
"server-1": {
|
|
|
|
URL: "https://10.0.0.2:666",
|
|
|
|
Weight: 12,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
|
|
|
},
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
Sticky: true,
|
|
|
|
Stickiness: &types.Stickiness{
|
|
|
|
CookieName: "chocolate",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MaxConn: &types.MaxConn{
|
|
|
|
Amount: 666,
|
|
|
|
ExtractorFunc: "client.ip",
|
|
|
|
},
|
|
|
|
HealthCheck: &types.HealthCheck{
|
|
|
|
Path: "/health",
|
|
|
|
Port: 880,
|
|
|
|
Interval: "6",
|
|
|
|
},
|
2018-01-31 14:32:04 +00:00
|
|
|
Buffering: &types.Buffering{
|
|
|
|
MaxResponseBodyBytes: 10485760,
|
|
|
|
MemResponseBodyBytes: 2097152,
|
|
|
|
MaxRequestBodyBytes: 10485760,
|
|
|
|
MemRequestBodyBytes: 2097152,
|
|
|
|
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
|
|
|
},
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "with services",
|
2017-12-20 15:33:26 +00:00
|
|
|
services: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "test/service",
|
|
|
|
Labels: map[string]string{
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
label.TraefikFrontendRedirectEntryPoint: "https",
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-Host-test-service-rancher-localhost": {
|
2018-01-10 17:08:03 +00:00
|
|
|
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",
|
|
|
|
},
|
2017-12-20 15:33:26 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-frontend-Host-test-service-rancher-localhost": {
|
|
|
|
Rule: "Host:test.service.rancher.localhost",
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-test-service": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-0": {
|
|
|
|
URL: "http://127.0.0.1:80",
|
|
|
|
Weight: 0,
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
CircuitBreaker: nil,
|
2017-12-20 15:33:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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) {
|
2017-04-29 19:37:54 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "rancher.localhost",
|
|
|
|
EnableServiceHealthFilter: true,
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:51:02 +00:00
|
|
|
constraint, _ := types.NewConstraint("tag==ch*se")
|
|
|
|
provider.Constraints = types.Constraints{constraint}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-04-29 19:37:54 +00:00
|
|
|
service rancherData
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "missing Port labels, don't respect constraint",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "don't respect constraint",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikEnable: "false",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "unhealthy",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikTags: "cheese",
|
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "unhealthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "inactive",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikTags: "not-cheesy",
|
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "inactive",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "healthy & active, tag: cheese",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikTags: "cheese",
|
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "healthy & active, tag: chose",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikTags: "chose",
|
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
2017-11-20 10:40:04 +00:00
|
|
|
State: "active",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
2017-05-02 14:51:02 +00:00
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "healthy & upgraded",
|
2017-05-02 14:51:02 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:29:09 +00:00
|
|
|
label.TraefikTags: "cheeeeese",
|
|
|
|
label.TraefikPort: "80",
|
|
|
|
label.TraefikEnable: "true",
|
2017-05-02 14:51:02 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
2017-11-20 10:40:04 +00:00
|
|
|
State: "upgraded",
|
2017-05-02 14:51:02 +00:00
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
2017-04-29 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
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-04-29 19:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestContainerFilter(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2017-05-08 01:20:38 +00:00
|
|
|
name string
|
|
|
|
healthState string
|
|
|
|
state string
|
|
|
|
expected bool
|
2017-04-29 19:37:54 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "unhealthy",
|
|
|
|
state: "running",
|
|
|
|
expected: false,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "healthy",
|
|
|
|
state: "stopped",
|
|
|
|
expected: false,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
state: "stopped",
|
2017-04-29 19:37:54 +00:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "healthy",
|
|
|
|
state: "running",
|
|
|
|
expected: true,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "updating-healthy",
|
|
|
|
state: "updating-running",
|
|
|
|
expected: true,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-04-29 19:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:08:03 +00:00
|
|
|
func TestGetBackendName(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{
|
2017-12-02 18:29:09 +00:00
|
|
|
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()
|
|
|
|
|
2018-01-10 17:08:03 +00:00
|
|
|
actual := getBackendName(test.service)
|
2017-11-20 10:40:04 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-11-18 12:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:08:03 +00:00
|
|
|
func TestGetCircuitBreaker(t *testing.T) {
|
2017-12-15 10:48:03 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
2018-01-10 17:08:03 +00:00
|
|
|
expected *types.CircuitBreaker
|
2017-12-15 10:48:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "should return nil when no CB label",
|
2017-12-15 10:48:03 +00:00
|
|
|
service: rancherData{
|
2018-01-10 17:08:03 +00:00
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
expected: nil,
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "should return a struct when CB label is set",
|
2017-12-15 10:48:03 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikBackendCircuitBreakerExpression: "NetworkErrorRatio() > 0.5",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getCircuitBreaker(test.service)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLoadBalancer(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.LoadBalancer
|
|
|
|
}{
|
2017-12-15 10:48:03 +00:00
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "should return nil when no LB labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a struct when labels are set",
|
2017-12-15 10:48:03 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikBackendLoadBalancerMethod: "drr",
|
|
|
|
label.TraefikBackendLoadBalancerSticky: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickiness: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickinessCookieName: "foo",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
Sticky: true,
|
|
|
|
Stickiness: &types.Stickiness{
|
|
|
|
CookieName: "foo",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "should return a nil Stickiness when Stickiness is not set",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendLoadBalancerMethod: "drr",
|
|
|
|
label.TraefikBackendLoadBalancerSticky: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickinessCookieName: "foo",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
Sticky: true,
|
|
|
|
Stickiness: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getLoadBalancer(test.service)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMaxConn(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.MaxConn
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return nil when no max conn labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return nil when no amount label",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendMaxConnExtractorFunc: "client.ip",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return default when no empty extractorFunc label",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendMaxConnExtractorFunc: "",
|
|
|
|
label.TraefikBackendMaxConnAmount: "666",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.MaxConn{
|
|
|
|
ExtractorFunc: "request.host",
|
|
|
|
Amount: 666,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a struct when max conn labels are set",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendMaxConnExtractorFunc: "client.ip",
|
|
|
|
label.TraefikBackendMaxConnAmount: "666",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.MaxConn{
|
|
|
|
ExtractorFunc: "client.ip",
|
|
|
|
Amount: 666,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getMaxConn(test.service)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetHealthCheck(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.HealthCheck
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return nil when no health check labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return nil when no health check Path label",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendHealthCheckPort: "80",
|
|
|
|
label.TraefikBackendHealthCheckInterval: "6",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a struct when health check labels are set",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendHealthCheckPath: "/health",
|
|
|
|
label.TraefikBackendHealthCheckPort: "80",
|
|
|
|
label.TraefikBackendHealthCheckInterval: "6",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.HealthCheck{
|
|
|
|
Path: "/health",
|
|
|
|
Port: 80,
|
|
|
|
Interval: "6",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getHealthCheck(test.service)
|
|
|
|
|
2018-01-31 14:32:04 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBuffering(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.Buffering
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return nil when no buffering labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a struct when buffering labels are set",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikBackendBufferingMaxResponseBodyBytes: "10485760",
|
|
|
|
label.TraefikBackendBufferingMemResponseBodyBytes: "2097152",
|
|
|
|
label.TraefikBackendBufferingMaxRequestBodyBytes: "10485760",
|
|
|
|
label.TraefikBackendBufferingMemRequestBodyBytes: "2097152",
|
|
|
|
label.TraefikBackendBufferingRetryExpression: "IsNetworkError() && Attempts() <= 2",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Buffering{
|
|
|
|
MaxResponseBodyBytes: 10485760,
|
|
|
|
MemResponseBodyBytes: 2097152,
|
|
|
|
MaxRequestBodyBytes: 10485760,
|
|
|
|
MemRequestBodyBytes: 2097152,
|
|
|
|
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getBuffering(test.service)
|
|
|
|
|
2018-01-10 17:08:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetServers(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected map[string]types.Server
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return nil when no server labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return nil when no server IPs",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikWeight: "7",
|
|
|
|
},
|
|
|
|
Containers: []string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should use default weight when invalid weight value",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikWeight: "kls",
|
|
|
|
},
|
|
|
|
Containers: []string{"10.10.10.0"},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: map[string]types.Server{
|
|
|
|
"server-0": {
|
|
|
|
URL: "http://10.10.10.0:",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a map when configuration keys are defined",
|
2017-12-15 10:48:03 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikWeight: "6",
|
|
|
|
},
|
|
|
|
Containers: []string{"10.10.10.0", "10.10.10.1"},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: map[string]types.Server{
|
|
|
|
"server-0": {
|
|
|
|
URL: "http://10.10.10.0:",
|
|
|
|
Weight: 6,
|
|
|
|
},
|
|
|
|
"server-1": {
|
|
|
|
URL: "http://10.10.10.1:",
|
|
|
|
Weight: 6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getServers(test.service)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRedirect(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.Redirect
|
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: "should return nil when no redirect labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should use only entry point tag when mix regex redirect and entry point redirect",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikFrontendRedirectEntryPoint: "https",
|
|
|
|
label.TraefikFrontendRedirectRegex: "(.*)",
|
2017-12-15 21:16:48 +00:00
|
|
|
label.TraefikFrontendRedirectReplacement: "$1",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Redirect{
|
|
|
|
EntryPoint: "https",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-01-10 17:08:03 +00:00
|
|
|
desc: "should return a struct when entry point redirect label",
|
2017-12-15 10:48:03 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikFrontendRedirectEntryPoint: "https",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Redirect{
|
|
|
|
EntryPoint: "https",
|
|
|
|
},
|
|
|
|
},
|
2018-01-31 18:10:04 +00:00
|
|
|
{
|
|
|
|
desc: "should return a struct when entry point redirect label (permanent)",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikFrontendRedirectEntryPoint: "https",
|
|
|
|
label.TraefikFrontendRedirectPermanent: "true",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Redirect{
|
|
|
|
EntryPoint: "https",
|
|
|
|
Permanent: true,
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
{
|
|
|
|
desc: "should return a struct when regex redirect labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikFrontendRedirectRegex: "(.*)",
|
2017-12-15 21:16:48 +00:00
|
|
|
label.TraefikFrontendRedirectReplacement: "$1",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
2018-01-10 17:08:03 +00:00
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Redirect{
|
|
|
|
Regex: "(.*)",
|
|
|
|
Replacement: "$1",
|
2017-12-15 10:48:03 +00:00
|
|
|
},
|
|
|
|
},
|
2018-01-31 18:10:04 +00:00
|
|
|
{
|
|
|
|
desc: "should return a struct when regex redirect labels (permanent)",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikFrontendRedirectRegex: "(.*)",
|
|
|
|
label.TraefikFrontendRedirectReplacement: "$1",
|
|
|
|
label.TraefikFrontendRedirectPermanent: "true",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Redirect{
|
|
|
|
Regex: "(.*)",
|
|
|
|
Replacement: "$1",
|
|
|
|
Permanent: true,
|
|
|
|
},
|
|
|
|
},
|
2017-12-15 10:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-01-10 17:08:03 +00:00
|
|
|
actual := getRedirect(test.service)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRateLimit(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.RateLimit
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return nil when no rate limit labels",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a struct when rate limit labels are defined",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
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",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return nil when ExtractorFunc is missing",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
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",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getRateLimit(test.service)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetHeaders(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected *types.Headers
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return nil when no custom headers options are set",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should return a struct when all custom headers options are set",
|
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
|
|
|
label.TraefikFrontendRequestHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendResponseHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendSSLProxyHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendAllowedHosts: "foo,bar,bor",
|
|
|
|
label.TraefikFrontendHostsProxyHeaders: "foo,bar,bor",
|
|
|
|
label.TraefikFrontendSSLHost: "foo",
|
|
|
|
label.TraefikFrontendCustomFrameOptionsValue: "foo",
|
|
|
|
label.TraefikFrontendContentSecurityPolicy: "foo",
|
|
|
|
label.TraefikFrontendPublicKey: "foo",
|
|
|
|
label.TraefikFrontendReferrerPolicy: "foo",
|
2018-03-02 13:24:03 +00:00
|
|
|
label.TraefikFrontendCustomBrowserXSSValue: "foo",
|
2018-01-10 17:08:03 +00:00
|
|
|
label.TraefikFrontendSTSSeconds: "666",
|
|
|
|
label.TraefikFrontendSSLRedirect: "true",
|
|
|
|
label.TraefikFrontendSSLTemporaryRedirect: "true",
|
|
|
|
label.TraefikFrontendSTSIncludeSubdomains: "true",
|
|
|
|
label.TraefikFrontendSTSPreload: "true",
|
|
|
|
label.TraefikFrontendForceSTSHeader: "true",
|
|
|
|
label.TraefikFrontendFrameDeny: "true",
|
|
|
|
label.TraefikFrontendContentTypeNosniff: "true",
|
|
|
|
label.TraefikFrontendBrowserXSSFilter: "true",
|
|
|
|
label.TraefikFrontendIsDevelopment: "true",
|
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: &types.Headers{
|
|
|
|
CustomRequestHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
CustomResponseHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
SSLProxyHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
AllowedHosts: []string{"foo", "bar", "bor"},
|
|
|
|
HostsProxyHeaders: []string{"foo", "bar", "bor"},
|
|
|
|
SSLHost: "foo",
|
|
|
|
CustomFrameOptionsValue: "foo",
|
|
|
|
ContentSecurityPolicy: "foo",
|
|
|
|
PublicKey: "foo",
|
|
|
|
ReferrerPolicy: "foo",
|
2018-03-02 13:24:03 +00:00
|
|
|
CustomBrowserXSSValue: "foo",
|
2018-01-10 17:08:03 +00:00
|
|
|
STSSeconds: 666,
|
|
|
|
SSLRedirect: true,
|
|
|
|
SSLTemporaryRedirect: true,
|
|
|
|
STSIncludeSubdomains: true,
|
|
|
|
STSPreload: true,
|
|
|
|
ForceSTSHeader: true,
|
|
|
|
FrameDeny: true,
|
|
|
|
ContentTypeNosniff: true,
|
|
|
|
BrowserXSSFilter: true,
|
|
|
|
IsDevelopment: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := getHeaders(test.service)
|
|
|
|
|
2017-12-15 10:48:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|