ECS provider refactoring

This commit is contained in:
Michael 2017-09-06 12:10:05 +02:00 committed by Traefiker
parent b0a0e16136
commit 9ce4f94818
4 changed files with 420 additions and 183 deletions

View file

@ -1,80 +1,144 @@
package ecs package ecs
import ( import (
"reflect" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
func TestClustersSet(t *testing.T) { func TestClustersSet(t *testing.T) {
checkMap := map[string]Clusters{ tests := []struct {
"cluster": {"cluster"}, desc string
"cluster1,cluster2": {"cluster1", "cluster2"}, value string
"cluster1;cluster2": {"cluster1", "cluster2"}, expected Clusters
"cluster1,cluster2;cluster3": {"cluster1", "cluster2", "cluster3"}, }{
{
desc: "One value should return Clusters of size 1",
value: "cluster",
expected: Clusters{"cluster"},
},
{
desc: "Two values separated by comma should return Clusters of size 2",
value: "cluster1,cluster2",
expected: Clusters{"cluster1", "cluster2"},
},
{
desc: "Two values separated by semicolon should return Clusters of size 2",
value: "cluster1;cluster2",
expected: Clusters{"cluster1", "cluster2"},
},
{
desc: "Three values separated by comma and semicolon should return Clusters of size 3",
value: "cluster1,cluster2;cluster3",
expected: Clusters{"cluster1", "cluster2", "cluster3"},
},
} }
for str, check := range checkMap {
var clusters Clusters for _, test := range tests {
if err := clusters.Set(str); err != nil { test := test
t.Fatalf("Error :%s", err) t.Run(test.desc, func(t *testing.T) {
} t.Parallel()
if !reflect.DeepEqual(clusters, check) { var clusters Clusters
t.Fatalf("Expected:%s\ngot:%s", check, clusters) err := clusters.Set(test.value)
} assert.Nil(t, err)
assert.Equal(t, test.expected, clusters)
})
} }
} }
func TestClustersGet(t *testing.T) { func TestClustersGet(t *testing.T) {
slices := []Clusters{ tests := []struct {
{"cluster"}, desc string
{"cluster1", "cluster2"}, clusters Clusters
{"cluster1", "cluster2", "cluster3"}, expected Clusters
}{
{
desc: "Should return 1 cluster",
clusters: Clusters{"cluster"},
expected: Clusters{"cluster"},
},
{
desc: "Should return 2 clusters",
clusters: Clusters{"cluster1", "cluster2"},
expected: Clusters{"cluster1", "cluster2"},
},
{
desc: "Should return 3 clusters",
clusters: Clusters{"cluster1", "cluster2", "cluster3"},
expected: Clusters{"cluster1", "cluster2", "cluster3"},
},
} }
check := []Clusters{
{"cluster"}, for _, test := range tests {
{"cluster1", "cluster2"}, test := test
{"cluster1", "cluster2", "cluster3"}, t.Run(test.desc, func(t *testing.T) {
} t.Parallel()
for i, slice := range slices { actual := test.clusters.Get()
if !reflect.DeepEqual(slice.Get(), check[i]) { assert.Equal(t, test.expected, actual)
t.Fatalf("Expected:%s\ngot:%s", check[i], slice) })
}
} }
} }
func TestClustersString(t *testing.T) { func TestClustersString(t *testing.T) {
slices := []Clusters{ tests := []struct {
{"cluster"}, desc string
{"cluster1", "cluster2"}, clusters Clusters
{"cluster1", "cluster2", "cluster3"}, expected string
}{
{
desc: "Should return 1 cluster",
clusters: Clusters{"cluster"},
expected: "[cluster]",
},
{
desc: "Should return 2 clusters",
clusters: Clusters{"cluster1", "cluster2"},
expected: "[cluster1 cluster2]",
},
{
desc: "Should return 3 clusters",
clusters: Clusters{"cluster1", "cluster2", "cluster3"},
expected: "[cluster1 cluster2 cluster3]",
},
} }
check := []string{ for _, test := range tests {
"[cluster]", test := test
"[cluster1 cluster2]", t.Run(test.desc, func(t *testing.T) {
"[cluster1 cluster2 cluster3]", t.Parallel()
} actual := test.clusters.String()
for i, slice := range slices { assert.Equal(t, test.expected, actual)
if !reflect.DeepEqual(slice.String(), check[i]) { })
t.Fatalf("Expected:%s\ngot:%s", check[i], slice)
}
} }
} }
func TestClustersSetValue(t *testing.T) { func TestClustersSetValue(t *testing.T) {
check := []Clusters{ tests := []struct {
{"cluster"}, desc string
{"cluster1", "cluster2"}, clusters Clusters
{"cluster1", "cluster2", "cluster3"}, expected Clusters
}{
{
desc: "Should return Clusters of size 1",
clusters: Clusters{"cluster"},
expected: Clusters{"cluster"},
},
{
desc: "Should return Clusters of size 2",
clusters: Clusters{"cluster1", "cluster2"},
expected: Clusters{"cluster1", "cluster2"},
},
{
desc: "Should return Clusters of size 3",
clusters: Clusters{"cluster1", "cluster2", "cluster3"},
expected: Clusters{"cluster1", "cluster2", "cluster3"},
},
} }
slices := []Clusters{ for _, test := range tests {
{"cluster"}, test := test
{"cluster1", "cluster2"}, t.Run(test.desc, func(t *testing.T) {
{"cluster1", "cluster2", "cluster3"}, t.Parallel()
} var slice Clusters
for i, s := range slices { slice.SetValue(test.clusters)
var slice Clusters assert.Equal(t, test.expected, slice)
slice.SetValue(s) })
if !reflect.DeepEqual(slice, check[i]) {
t.Fatalf("Expected:%s\ngot:%s", check[i], slice)
}
} }
} }

View file

@ -409,7 +409,7 @@ func (p *Provider) lookupTaskDefinitions(ctx context.Context, client *awsClient,
return taskDefinitions, nil return taskDefinitions, nil
} }
func (i ecsInstance) label(k string) string { func (p *Provider) label(i ecsInstance, k string) string {
if v, found := i.containerDefinition.DockerLabels[k]; found { if v, found := i.containerDefinition.DockerLabels[k]; found {
return *v return *v
} }
@ -439,7 +439,7 @@ func (p *Provider) filterInstance(i ecsInstance) bool {
return false return false
} }
label := i.label(types.LabelEnable) label := p.label(i, types.LabelEnable)
enabled := p.ExposedByDefault && label != "false" || label == "true" enabled := p.ExposedByDefault && label != "false" || label == "true"
if !enabled { if !enabled {
log.Debugf("Filtering disabled ecs instance %s (%s) (traefik.enabled = '%s')", i.Name, i.ID, label) log.Debugf("Filtering disabled ecs instance %s (%s) (traefik.enabled = '%s')", i.Name, i.ID, label)
@ -463,7 +463,7 @@ func (p *Provider) filterFrontends(instances []ecsInstance) []ecsInstance {
} }
func (p *Provider) getFrontendRule(i ecsInstance) string { func (p *Provider) getFrontendRule(i ecsInstance) string {
if label := i.label(types.LabelFrontendRule); label != "" { if label := p.label(i, types.LabelFrontendRule); label != "" {
return label return label
} }
return "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + "." + p.Domain return "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + "." + p.Domain
@ -471,7 +471,7 @@ func (p *Provider) getFrontendRule(i ecsInstance) string {
func (p *Provider) getLoadBalancerSticky(instances []ecsInstance) string { func (p *Provider) getLoadBalancerSticky(instances []ecsInstance) string {
if len(instances) > 0 { if len(instances) > 0 {
label := instances[0].label(types.LabelBackendLoadbalancerSticky) label := p.label(instances[0], types.LabelBackendLoadbalancerSticky)
if label != "" { if label != "" {
return label return label
} }
@ -481,7 +481,7 @@ func (p *Provider) getLoadBalancerSticky(instances []ecsInstance) string {
func (p *Provider) getLoadBalancerMethod(instances []ecsInstance) string { func (p *Provider) getLoadBalancerMethod(instances []ecsInstance) string {
if len(instances) > 0 { if len(instances) > 0 {
label := instances[0].label(types.LabelBackendLoadbalancerMethod) label := p.label(instances[0], types.LabelBackendLoadbalancerMethod)
if label != "" { if label != "" {
return label return label
} }
@ -505,44 +505,44 @@ func (p *Provider) chunkedTaskArns(tasks []*string) [][]*string {
return chunkedTasks return chunkedTasks
} }
func (i ecsInstance) Protocol() string { func (p *Provider) getProtocol(i ecsInstance) string {
if label := i.label(types.LabelProtocol); label != "" { if label := p.label(i, types.LabelProtocol); label != "" {
return label return label
} }
return "http" return "http"
} }
func (i ecsInstance) Host() string { func (p *Provider) getHost(i ecsInstance) string {
return *i.machine.PrivateIpAddress return *i.machine.PrivateIpAddress
} }
func (i ecsInstance) Port() string { func (p *Provider) getPort(i ecsInstance) string {
return strconv.FormatInt(*i.container.NetworkBindings[0].HostPort, 10) return strconv.FormatInt(*i.container.NetworkBindings[0].HostPort, 10)
} }
func (i ecsInstance) Weight() string { func (p *Provider) getWeight(i ecsInstance) string {
if label := i.label(types.LabelWeight); label != "" { if label := p.label(i, types.LabelWeight); label != "" {
return label return label
} }
return "0" return "0"
} }
func (i ecsInstance) PassHostHeader() string { func (p *Provider) getPassHostHeader(i ecsInstance) string {
if label := i.label(types.LabelFrontendPassHostHeader); label != "" { if label := p.label(i, types.LabelFrontendPassHostHeader); label != "" {
return label return label
} }
return "true" return "true"
} }
func (i ecsInstance) Priority() string { func (p *Provider) getPriority(i ecsInstance) string {
if label := i.label(types.LabelFrontendPriority); label != "" { if label := p.label(i, types.LabelFrontendPriority); label != "" {
return label return label
} }
return "0" return "0"
} }
func (i ecsInstance) EntryPoints() []string { func (p *Provider) getEntryPoints(i ecsInstance) []string {
if label := i.label(types.LabelFrontendEntryPoints); label != "" { if label := p.label(i, types.LabelFrontendEntryPoints); label != "" {
return strings.Split(label, ",") return strings.Split(label, ",")
} }
return []string{} return []string{}

View file

@ -1,13 +1,13 @@
package ecs package ecs
import ( import (
"reflect"
"testing" "testing"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs" "github.com/aws/aws-sdk-go/service/ecs"
"github.com/containous/traefik/types" "github.com/containous/traefik/types"
"github.com/stretchr/testify/assert"
) )
func makeEcsInstance(containerDef *ecs.ContainerDefinition) ecsInstance { func makeEcsInstance(containerDef *ecs.ContainerDefinition) ecsInstance {
@ -58,171 +58,265 @@ func simpleEcsInstance(labels map[string]*string) ecsInstance {
} }
func TestEcsProtocol(t *testing.T) { func TestEcsProtocol(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected string expected string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Protocol label is not set should return a string equals to http",
expected: "http", expected: "http",
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
{ {
desc: "Protocol label is set to http should return a string equals to http",
expected: "http",
instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelProtocol: aws.String("http"),
}),
provider: &Provider{},
},
{
desc: "Protocol label is set to https should return a string equals to https",
expected: "https", expected: "https",
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelProtocol: aws.String("https"), types.LabelProtocol: aws.String("https"),
}), }),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.Protocol() test := test
if value != c.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getProtocol(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestEcsHost(t *testing.T) { func TestEcsHost(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected string expected string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Default host should be 10.0.0.0",
expected: "10.0.0.0", expected: "10.0.0.0",
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.Host() test := test
if value != c.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getHost(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestEcsPort(t *testing.T) { func TestEcsPort(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected string expected string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Default port should be 80",
expected: "80", expected: "80",
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.Port() test := test
if value != c.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getPort(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestEcsWeight(t *testing.T) { func TestEcsWeight(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected string expected string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Weight label not set should return a string equals to 0",
expected: "0", expected: "0",
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
{ {
desc: "Weight label set 0 should return a string equals to 0",
expected: "0",
instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelWeight: aws.String("0"),
}),
provider: &Provider{},
},
{
desc: "Weight label set -1 should return a string equals to -1",
expected: "-1",
instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelWeight: aws.String("-1"),
}),
provider: &Provider{},
},
{
desc: "Weight label set 10 should return a string equals to 10",
expected: "10", expected: "10",
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelWeight: aws.String("10"), types.LabelWeight: aws.String("10"),
}), }),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.Weight() test := test
if value != c.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getWeight(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestEcsPassHostHeader(t *testing.T) { func TestEcsPassHostHeader(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected string expected string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Frontend pass host header label not set should return a string equals to true",
expected: "true", expected: "true",
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
{ {
desc: "Frontend pass host header label set to false should return a string equals to false",
expected: "false", expected: "false",
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelFrontendPassHostHeader: aws.String("false"), types.LabelFrontendPassHostHeader: aws.String("false"),
}), }),
provider: &Provider{},
},
{
desc: "Frontend pass host header label set to true should return a string equals to true",
expected: "true",
instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelFrontendPassHostHeader: aws.String("true"),
}),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.PassHostHeader() test := test
if value != c.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getPassHostHeader(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestEcsPriority(t *testing.T) { func TestEcsPriority(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected string expected string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Frontend priority label not set should return a string equals to 0",
expected: "0", expected: "0",
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
{ {
desc: "Frontend priority label set to 10 should return a string equals to 10",
expected: "10", expected: "10",
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelFrontendPriority: aws.String("10"), types.LabelFrontendPriority: aws.String("10"),
}), }),
provider: &Provider{},
},
{
desc: "Frontend priority label set to -1 should return a string equals to -1",
expected: "-1",
instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelFrontendPriority: aws.String("-1"),
}),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.Priority() test := test
if value != c.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getPriority(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestEcsEntryPoints(t *testing.T) { func TestEcsEntryPoints(t *testing.T) {
cases := []struct { tests := []struct {
desc string
expected []string expected []string
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
desc: "Frontend entrypoints label not set should return empty array",
expected: []string{}, expected: []string{},
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{},
}, },
{ {
desc: "Frontend entrypoints label set to http should return a string array of 1 element",
expected: []string{"http"}, expected: []string{"http"},
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelFrontendEntryPoints: aws.String("http"), types.LabelFrontendEntryPoints: aws.String("http"),
}), }),
provider: &Provider{},
}, },
{ {
desc: "Frontend entrypoints label set to http,https should return a string array of 2 elements",
expected: []string{"http", "https"}, expected: []string{"http", "https"},
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelFrontendEntryPoints: aws.String("http,https"), types.LabelFrontendEntryPoints: aws.String("http,https"),
}), }),
provider: &Provider{},
}, },
} }
for i, c := range cases { for _, test := range tests {
value := c.instanceInfo.EntryPoints() test := test
if !reflect.DeepEqual(value, c.expected) { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) t.Parallel()
} actual := test.provider.getEntryPoints(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
} }
} }
@ -243,108 +337,187 @@ func TestFilterInstance(t *testing.T) {
invalidMachineState := simpleEcsInstance(map[string]*string{}) invalidMachineState := simpleEcsInstance(map[string]*string{})
invalidMachineState.machine.State.Name = aws.String(ec2.InstanceStateNameStopped) invalidMachineState.machine.State.Name = aws.String(ec2.InstanceStateNameStopped)
cases := []struct { tests := []struct {
expected bool desc string
exposedByDefault bool expected bool
instanceInfo ecsInstance instanceInfo ecsInstance
provider *Provider
}{ }{
{ {
expected: true, desc: "Instance without enable label and exposed by default enabled should be not filtered",
exposedByDefault: true, expected: true,
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{
ExposedByDefault: true,
},
}, },
{ {
expected: false, desc: "Instance without enable label and exposed by default disabled should be filtered",
exposedByDefault: false, expected: false,
instanceInfo: simpleEcsInstance(map[string]*string{}), instanceInfo: simpleEcsInstance(map[string]*string{}),
provider: &Provider{
ExposedByDefault: false,
},
}, },
{ {
expected: false, desc: "Instance with enable label set to false and exposed by default enabled should be filtered",
exposedByDefault: true, expected: false,
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelEnable: aws.String("false"), types.LabelEnable: aws.String("false"),
}), }),
provider: &Provider{
ExposedByDefault: true,
},
}, },
{ {
expected: true, desc: "Instance with enable label set to true and exposed by default disabled should be not filtered",
exposedByDefault: false, expected: true,
instanceInfo: simpleEcsInstance(map[string]*string{ instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelEnable: aws.String("true"), types.LabelEnable: aws.String("true"),
}), }),
provider: &Provider{
ExposedByDefault: false,
},
}, },
{ {
expected: false, desc: "Instance with nil private ip and exposed by default enabled should be filtered",
exposedByDefault: true, expected: false,
instanceInfo: nilPrivateIP, instanceInfo: nilPrivateIP,
provider: &Provider{
ExposedByDefault: true,
},
}, },
{ {
expected: false, desc: "Instance with nil machine and exposed by default enabled should be filtered",
exposedByDefault: true, expected: false,
instanceInfo: nilMachine, instanceInfo: nilMachine,
provider: &Provider{
ExposedByDefault: true,
},
}, },
{ {
expected: false, desc: "Instance with nil machine state and exposed by default enabled should be filtered",
exposedByDefault: true, expected: false,
instanceInfo: nilMachineState, instanceInfo: nilMachineState,
provider: &Provider{
ExposedByDefault: true,
},
}, },
{ {
expected: false, desc: "Instance with nil machine state name and exposed by default enabled should be filtered",
exposedByDefault: true, expected: false,
instanceInfo: nilMachineStateName, instanceInfo: nilMachineStateName,
provider: &Provider{
ExposedByDefault: true,
},
}, },
{ {
expected: false, desc: "Instance with invalid machine state and exposed by default enabled should be filtered",
exposedByDefault: true, expected: false,
instanceInfo: invalidMachineState, instanceInfo: invalidMachineState,
provider: &Provider{
ExposedByDefault: true,
},
}, },
} }
for i, c := range cases { for _, test := range tests {
provider := &Provider{ test := test
ExposedByDefault: c.exposedByDefault, t.Run(test.desc, func(t *testing.T) {
} t.Parallel()
value := provider.filterInstance(c.instanceInfo) actual := test.provider.filterInstance(test.instanceInfo)
if value != c.expected { assert.Equal(t, test.expected, actual)
t.Fatalf("Should have been %v, got %v (case %d)", c.expected, value, i) })
}
} }
} }
func TestTaskChunking(t *testing.T) { func TestTaskChunking(t *testing.T) {
provider := &Provider{}
testval := "a" testval := "a"
cases := []struct { tests := []struct {
desc string
count int count int
expectedLengths []int expectedLengths []int
provider *Provider
}{ }{
{0, []int(nil)}, {
{1, []int{1}}, desc: "0 parameter should return nil",
{99, []int{99}}, count: 0,
{100, []int{100}}, expectedLengths: []int(nil),
{101, []int{100, 1}}, provider: &Provider{},
{199, []int{100, 99}}, },
{200, []int{100, 100}}, {
{201, []int{100, 100, 1}}, desc: "1 parameter should return 1 array of 1 element",
{555, []int{100, 100, 100, 100, 100, 55}}, count: 1,
{1001, []int{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 1}}, expectedLengths: []int{1},
provider: &Provider{},
},
{
desc: "99 parameters should return 1 array of 99 elements",
count: 99,
expectedLengths: []int{99},
provider: &Provider{},
},
{
desc: "100 parameters should return 1 array of 100 elements",
count: 100,
expectedLengths: []int{100},
provider: &Provider{},
},
{
desc: "101 parameters should return 1 array of 100 elements and 1 array of 1 element",
count: 101,
expectedLengths: []int{100, 1},
provider: &Provider{},
},
{
desc: "199 parameters should return 1 array of 100 elements and 1 array of 99 elements",
count: 199,
expectedLengths: []int{100, 99},
provider: &Provider{},
},
{
desc: "200 parameters should return 2 arrays of 100 elements each",
count: 200,
expectedLengths: []int{100, 100},
provider: &Provider{},
},
{
desc: "201 parameters should return 2 arrays of 100 elements each and 1 array of 1 element",
count: 201,
expectedLengths: []int{100, 100, 1},
provider: &Provider{},
},
{
desc: "555 parameters should return 5 arrays of 100 elements each and 1 array of 55 elements",
count: 555,
expectedLengths: []int{100, 100, 100, 100, 100, 55},
provider: &Provider{},
},
{
desc: "1001 parameters should return 10 arrays of 100 elements each and 1 array of 1 element",
count: 1001,
expectedLengths: []int{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 1},
provider: &Provider{},
},
} }
for _, c := range cases { for _, test := range tests {
var tasks []*string test := test
for v := 0; v < c.count; v++ { t.Run(test.desc, func(t *testing.T) {
tasks = append(tasks, &testval) var tasks []*string
} for v := 0; v < test.count; v++ {
tasks = append(tasks, &testval)
}
out := provider.chunkedTaskArns(tasks) out := test.provider.chunkedTaskArns(tasks)
var outCount []int var outCount []int
for _, el := range out { for _, el := range out {
outCount = append(outCount, len(el)) outCount = append(outCount, len(el))
} }
assert.Equal(t, test.expectedLengths, outCount, "Chunking %d elements", test.count)
})
if !reflect.DeepEqual(outCount, c.expectedLengths) {
t.Errorf("Chunking %d elements, expected %#v, got %#v", c.count, c.expectedLengths, outCount)
}
} }
} }

View file

@ -5,8 +5,8 @@
{{range $index, $i := $instances}} {{range $index, $i := $instances}}
[backends.backend-{{ $i.Name }}.servers.server-{{ $i.Name }}{{ $i.ID }}] [backends.backend-{{ $i.Name }}.servers.server-{{ $i.Name }}{{ $i.ID }}]
url = "{{ $i.Protocol }}://{{ $i.Host }}:{{ $i.Port }}" url = "{{ getProtocol $i }}://{{ getHost $i }}:{{ getPort $i }}"
weight = {{ $i.Weight }} weight = {{ getWeight $i}}
{{end}} {{end}}
{{end}} {{end}}
@ -14,9 +14,9 @@
{{range filterFrontends $instances}} {{range filterFrontends $instances}}
[frontends.frontend-{{ $serviceName }}] [frontends.frontend-{{ $serviceName }}]
backend = "backend-{{ $serviceName }}" backend = "backend-{{ $serviceName }}"
passHostHeader = {{ .PassHostHeader }} passHostHeader = {{ getPassHostHeader .}}
priority = {{ .Priority }} priority = {{ getPriority .}}
entryPoints = [{{range .EntryPoints }} entryPoints = [{{range getEntryPoints .}}
"{{.}}", "{{.}}",
{{end}}] {{end}}]
[frontends.frontend-{{ $serviceName }}.routes.route-frontend-{{ $serviceName }}] [frontends.frontend-{{ $serviceName }}.routes.route-frontend-{{ $serviceName }}]