Use index-based syntax in KV tests.

This commit is contained in:
Ludovic Fernandez 2018-05-22 12:30:03 +02:00 committed by Traefiker Bot
parent e2bac47a0a
commit 3c8c5ebb96
2 changed files with 31 additions and 19 deletions

View file

@ -2,6 +2,7 @@ package kv
import ( import (
"sort" "sort"
"strconv"
"strings" "strings"
"testing" "testing"
@ -81,7 +82,18 @@ func withPair(key string, value string) func(map[string]string) {
} }
} }
func withErrorPage(name string, backend, query, status string) func(map[string]string) { func withList(key string, values ...string) func(map[string]string) {
return func(pairs map[string]string) {
if len(key) == 0 {
return
}
for i, value := range values {
pairs[key+"/"+strconv.Itoa(i)] = value
}
}
}
func withErrorPage(name string, backend string, query string, statuses ...string) func(map[string]string) {
return func(pairs map[string]string) { return func(pairs map[string]string) {
if len(name) == 0 { if len(name) == 0 {
return return
@ -89,7 +101,7 @@ func withErrorPage(name string, backend, query, status string) func(map[string]s
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesBackend, backend)(pairs) withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesBackend, backend)(pairs)
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesQuery, query)(pairs) withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesQuery, query)(pairs)
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesStatus, status)(pairs) withList(pathFrontendErrorPages+name+pathFrontendErrorPagesStatus, statuses...)(pairs)
} }
} }
@ -125,11 +137,14 @@ func TestFiller(t *testing.T) {
{Key: "traefik/frontends/frontend.with.dot/errors/bar", Value: []byte("")}, {Key: "traefik/frontends/frontend.with.dot/errors/bar", Value: []byte("")},
{Key: "traefik/frontends/frontend.with.dot/errors/bar/backend", Value: []byte("error")}, {Key: "traefik/frontends/frontend.with.dot/errors/bar/backend", Value: []byte("error")},
{Key: "traefik/frontends/frontend.with.dot/errors/bar/query", Value: []byte("/test2")}, {Key: "traefik/frontends/frontend.with.dot/errors/bar/query", Value: []byte("/test2")},
{Key: "traefik/frontends/frontend.with.dot/errors/bar/status", Value: []byte("400-405")}, {Key: "traefik/frontends/frontend.with.dot/errors/bar/status", Value: []byte("")},
{Key: "traefik/frontends/frontend.with.dot/errors/bar/status/0", Value: []byte("400-405")},
{Key: "traefik/frontends/frontend.with.dot/errors/foo", Value: []byte("")}, {Key: "traefik/frontends/frontend.with.dot/errors/foo", Value: []byte("")},
{Key: "traefik/frontends/frontend.with.dot/errors/foo/backend", Value: []byte("error")}, {Key: "traefik/frontends/frontend.with.dot/errors/foo/backend", Value: []byte("error")},
{Key: "traefik/frontends/frontend.with.dot/errors/foo/query", Value: []byte("/test1")}, {Key: "traefik/frontends/frontend.with.dot/errors/foo/query", Value: []byte("/test1")},
{Key: "traefik/frontends/frontend.with.dot/errors/foo/status", Value: []byte("500-501, 503-599")}, {Key: "traefik/frontends/frontend.with.dot/errors/foo/status", Value: []byte("")},
{Key: "traefik/frontends/frontend.with.dot/errors/foo/status/0", Value: []byte("500-501")},
{Key: "traefik/frontends/frontend.with.dot/errors/foo/status/1", Value: []byte("503-599")},
{Key: "traefik/frontends/frontend.with.dot/ratelimit", Value: []byte("")}, {Key: "traefik/frontends/frontend.with.dot/ratelimit", Value: []byte("")},
{Key: "traefik/frontends/frontend.with.dot/ratelimit/extractorfunc", Value: []byte("client.ip")}, {Key: "traefik/frontends/frontend.with.dot/ratelimit/extractorfunc", Value: []byte("client.ip")},
{Key: "traefik/frontends/frontend.with.dot/ratelimit/rateset", Value: []byte("")}, {Key: "traefik/frontends/frontend.with.dot/ratelimit/rateset", Value: []byte("")},
@ -150,7 +165,7 @@ func TestFiller(t *testing.T) {
frontend("frontend.with.dot", frontend("frontend.with.dot",
withPair("backend", "backend.with.dot.too"), withPair("backend", "backend.with.dot.too"),
withPair("routes/route.with.dot/rule", "Host:test.localhost"), withPair("routes/route.with.dot/rule", "Host:test.localhost"),
withErrorPage("foo", "error", "/test1", "500-501, 503-599"), withErrorPage("foo", "error", "/test1", "500-501", "503-599"),
withErrorPage("bar", "error", "/test2", "400-405"), withErrorPage("bar", "error", "/test2", "400-405"),
withRateLimit("client.ip", withRateLimit("client.ip",
withLimit("foo", "6", "12", "18"), withLimit("foo", "6", "12", "18"),
@ -168,10 +183,10 @@ func TestFiller(t *testing.T) {
withPair("routes/route.with.dot/rule", "Host:test.localhost"), withPair("routes/route.with.dot/rule", "Host:test.localhost"),
withPair("errors/foo/backend", "error"), withPair("errors/foo/backend", "error"),
withPair("errors/foo/query", "/test1"), withPair("errors/foo/query", "/test1"),
withPair("errors/foo/status", "500-501, 503-599"), withList("errors/foo/status", "500-501", "503-599"),
withPair("errors/bar/backend", "error"), withPair("errors/bar/backend", "error"),
withPair("errors/bar/query", "/test2"), withPair("errors/bar/query", "/test2"),
withPair("errors/bar/status", "400-405"), withList("errors/bar/status", "400-405"),
withPair("ratelimit/extractorfunc", "client.ip"), withPair("ratelimit/extractorfunc", "client.ip"),
withPair("ratelimit/rateset/foo/average", "6"), withPair("ratelimit/rateset/foo/average", "6"),
withPair("ratelimit/rateset/foo/burst", "12"), withPair("ratelimit/rateset/foo/burst", "12"),

View file

@ -93,15 +93,15 @@ func TestProviderBuildConfiguration(t *testing.T) {
withPair(pathFrontendPriority, "6"), withPair(pathFrontendPriority, "6"),
withPair(pathFrontendPassHostHeader, "false"), withPair(pathFrontendPassHostHeader, "false"),
withPair(pathFrontendPassTLSCert, "true"), withPair(pathFrontendPassTLSCert, "true"),
withPair(pathFrontendEntryPoints, "http,https"), withList(pathFrontendEntryPoints, "http", "https"),
withPair(pathFrontendWhiteListSourceRange, "1.1.1.1/24, 1234:abcd::42/32"), withList(pathFrontendWhiteListSourceRange, "1.1.1.1/24", "1234:abcd::42/32"),
withPair(pathFrontendWhiteListUseXForwardedFor, "true"), withPair(pathFrontendWhiteListUseXForwardedFor, "true"),
withPair(pathFrontendBasicAuth, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/, test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"), withList(pathFrontendBasicAuth, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"),
withPair(pathFrontendRedirectEntryPoint, "https"), withPair(pathFrontendRedirectEntryPoint, "https"),
withPair(pathFrontendRedirectRegex, "nope"), withPair(pathFrontendRedirectRegex, "nope"),
withPair(pathFrontendRedirectReplacement, "nope"), withPair(pathFrontendRedirectReplacement, "nope"),
withPair(pathFrontendRedirectPermanent, "true"), withPair(pathFrontendRedirectPermanent, "true"),
withErrorPage("foo", "error", "/test1", "500-501, 503-599"), withErrorPage("foo", "error", "/test1", "500-501", "503-599"),
withErrorPage("bar", "error", "/test2", "400-405"), withErrorPage("bar", "error", "/test2", "400-405"),
withRateLimit("client.ip", withRateLimit("client.ip",
withLimit("foo", "6", "12", "18"), withLimit("foo", "6", "12", "18"),
@ -117,7 +117,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
withPair(pathFrontendSSLProxyHeaders+"Content-Type", "application/json; charset=utf-8"), withPair(pathFrontendSSLProxyHeaders+"Content-Type", "application/json; charset=utf-8"),
withPair(pathFrontendSSLProxyHeaders+"X-Custom-Header", "test"), withPair(pathFrontendSSLProxyHeaders+"X-Custom-Header", "test"),
withPair(pathFrontendAllowedHosts, "example.com, ssl.example.com"), withPair(pathFrontendAllowedHosts, "example.com, ssl.example.com"),
withPair(pathFrontendHostsProxyHeaders, "foo, bar, goo, hor"), withList(pathFrontendHostsProxyHeaders, "foo", "bar", "goo", "hor"),
withPair(pathFrontendSTSSeconds, "666"), withPair(pathFrontendSTSSeconds, "666"),
withPair(pathFrontendSSLHost, "foo"), withPair(pathFrontendSSLHost, "foo"),
withPair(pathFrontendCustomFrameOptionsValue, "foo"), withPair(pathFrontendCustomFrameOptionsValue, "foo"),
@ -139,11 +139,11 @@ func TestProviderBuildConfiguration(t *testing.T) {
withPair("routes/route1/rule", "Host:test.localhost"), withPair("routes/route1/rule", "Host:test.localhost"),
withPair("routes/route2/rule", "Path:/foo")), withPair("routes/route2/rule", "Path:/foo")),
entry("tls/foo", entry("tls/foo",
withPair("entrypoints", "http,https"), withList("entrypoints", "http", "https"),
withPair("certificate/certfile", "certfile1"), withPair("certificate/certfile", "certfile1"),
withPair("certificate/keyfile", "keyfile1")), withPair("certificate/keyfile", "keyfile1")),
entry("tls/bar", entry("tls/bar",
withPair("entrypoints", "http,https"), withList("entrypoints", "http", "https"),
withPair("certificate/certfile", "certfile2"), withPair("certificate/certfile", "certfile2"),
withPair("certificate/keyfile", "keyfile2")), withPair("certificate/keyfile", "keyfile2")),
), ),
@ -682,10 +682,7 @@ func TestProviderGetSlice(t *testing.T) {
desc: "multiple entries", desc: "multiple entries",
kvPairs: filler("traefik", kvPairs: filler("traefik",
frontend("foo", frontend("foo",
withPair("entrypoints/0", "courgette"), withList("entrypoints", "courgette", "carotte", "tomate", "aubergine"),
withPair("entrypoints/1", "carotte"),
withPair("entrypoints/2", "tomate"),
withPair("entrypoints/3", "aubergine"),
), ),
), ),
keyParts: []string{"traefik/frontends/foo/entrypoints"}, keyParts: []string{"traefik/frontends/foo/entrypoints"},
@ -1210,7 +1207,7 @@ func TestProviderGetErrorPages(t *testing.T) {
rootPath: "traefik/frontends/foo", rootPath: "traefik/frontends/foo",
kvPairs: filler("traefik", kvPairs: filler("traefik",
frontend("foo", frontend("foo",
withErrorPage("foo", "error", "/test1", "500-501, 503-599"), withErrorPage("foo", "error", "/test1", "500-501", "503-599"),
withErrorPage("bar", "error", "/test2", "400-405"))), withErrorPage("bar", "error", "/test2", "400-405"))),
expected: map[string]*types.ErrorPage{ expected: map[string]*types.ErrorPage{
"foo": { "foo": {