2017-04-17 10:50:02 +00:00
|
|
|
package ecs
|
2017-01-05 14:24:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ecs"
|
2017-07-10 14:58:12 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2017-09-07 15:34:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-01-05 14:24:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeEcsInstance(containerDef *ecs.ContainerDefinition) ecsInstance {
|
|
|
|
container := &ecs.Container{
|
|
|
|
Name: containerDef.Name,
|
|
|
|
NetworkBindings: make([]*ecs.NetworkBinding, len(containerDef.PortMappings)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, pm := range containerDef.PortMappings {
|
|
|
|
container.NetworkBindings[i] = &ecs.NetworkBinding{
|
|
|
|
HostPort: pm.HostPort,
|
|
|
|
ContainerPort: pm.ContainerPort,
|
|
|
|
Protocol: pm.Protocol,
|
|
|
|
BindIP: aws.String("0.0.0.0"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ecsInstance{
|
|
|
|
Name: "foo-http",
|
|
|
|
ID: "123456789abc",
|
|
|
|
task: &ecs.Task{
|
|
|
|
Containers: []*ecs.Container{container},
|
|
|
|
},
|
|
|
|
taskDefinition: &ecs.TaskDefinition{
|
|
|
|
ContainerDefinitions: []*ecs.ContainerDefinition{containerDef},
|
|
|
|
},
|
|
|
|
container: container,
|
|
|
|
containerDefinition: containerDef,
|
|
|
|
machine: &ec2.Instance{
|
|
|
|
PrivateIpAddress: aws.String("10.0.0.0"),
|
2017-02-08 23:18:38 +00:00
|
|
|
State: &ec2.InstanceState{
|
|
|
|
Name: aws.String(ec2.InstanceStateNameRunning),
|
|
|
|
},
|
2017-01-05 14:24:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func simpleEcsInstance(labels map[string]*string) ecsInstance {
|
|
|
|
return makeEcsInstance(&ecs.ContainerDefinition{
|
|
|
|
Name: aws.String("http"),
|
|
|
|
PortMappings: []*ecs.PortMapping{{
|
|
|
|
HostPort: aws.Int64(80),
|
|
|
|
ContainerPort: aws.Int64(80),
|
|
|
|
Protocol: aws.String("tcp"),
|
|
|
|
}},
|
|
|
|
DockerLabels: labels,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsProtocol(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: "http",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: "https",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelProtocol: aws.String("https"),
|
2017-01-05 14:24:17 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.Protocol()
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsHost(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: "10.0.0.0",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.Host()
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsPort(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: "80",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.Port()
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsWeight(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: "0",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: "10",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelWeight: aws.String("10"),
|
2017-01-05 14:24:17 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.Weight()
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsPassHostHeader(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: "true",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: "false",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendPassHostHeader: aws.String("false"),
|
2017-01-05 14:24:17 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.PassHostHeader()
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsPriority(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: "0",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: "10",
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendPriority: aws.String("10"),
|
2017-01-05 14:24:17 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.Priority()
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcsEntryPoints(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
expected []string
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: []string{},
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: []string{"http"},
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendEntryPoints: aws.String("http"),
|
2017-01-05 14:24:17 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: []string{"http", "https"},
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendEntryPoints: aws.String("http,https"),
|
2017-01-05 14:24:17 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
|
|
|
value := test.instanceInfo.EntryPoints()
|
|
|
|
if !reflect.DeepEqual(value, test.expected) {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-02-08 23:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterInstance(t *testing.T) {
|
|
|
|
|
|
|
|
nilPrivateIP := simpleEcsInstance(map[string]*string{})
|
|
|
|
nilPrivateIP.machine.PrivateIpAddress = nil
|
|
|
|
|
|
|
|
nilMachine := simpleEcsInstance(map[string]*string{})
|
|
|
|
nilMachine.machine = nil
|
|
|
|
|
|
|
|
nilMachineState := simpleEcsInstance(map[string]*string{})
|
|
|
|
nilMachineState.machine.State = nil
|
|
|
|
|
|
|
|
nilMachineStateName := simpleEcsInstance(map[string]*string{})
|
|
|
|
nilMachineStateName.machine.State.Name = nil
|
|
|
|
|
|
|
|
invalidMachineState := simpleEcsInstance(map[string]*string{})
|
|
|
|
invalidMachineState.machine.State.Name = aws.String(ec2.InstanceStateNameStopped)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
expected bool
|
|
|
|
exposedByDefault bool
|
|
|
|
instanceInfo ecsInstance
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: true,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: false,
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: aws.String("false"),
|
2017-02-08 23:18:38 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: true,
|
|
|
|
exposedByDefault: false,
|
|
|
|
instanceInfo: simpleEcsInstance(map[string]*string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: aws.String("true"),
|
2017-02-08 23:18:38 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: nilPrivateIP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: nilMachine,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: nilMachineState,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: nilMachineStateName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: false,
|
|
|
|
exposedByDefault: true,
|
|
|
|
instanceInfo: invalidMachineState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for i, test := range cases {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{
|
2017-09-07 15:34:03 +00:00
|
|
|
ExposedByDefault: test.exposedByDefault,
|
2017-02-08 23:18:38 +00:00
|
|
|
}
|
2017-09-07 15:34:03 +00:00
|
|
|
value := provider.filterInstance(test.instanceInfo)
|
|
|
|
if value != test.expected {
|
|
|
|
t.Fatalf("Should have been %v, got %v (case %d)", test.expected, value, i)
|
2017-01-05 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 18:06:34 +00:00
|
|
|
|
|
|
|
func TestTaskChunking(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-01 18:06:34 +00:00
|
|
|
|
|
|
|
testval := "a"
|
|
|
|
cases := []struct {
|
|
|
|
count int
|
|
|
|
expectedLengths []int
|
|
|
|
}{
|
|
|
|
{0, []int(nil)},
|
|
|
|
{1, []int{1}},
|
|
|
|
{99, []int{99}},
|
|
|
|
{100, []int{100}},
|
|
|
|
{101, []int{100, 1}},
|
|
|
|
{199, []int{100, 99}},
|
|
|
|
{200, []int{100, 100}},
|
|
|
|
{201, []int{100, 100, 1}},
|
|
|
|
{555, []int{100, 100, 100, 100, 100, 55}},
|
|
|
|
{1001, []int{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 1}},
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
for _, test := range cases {
|
2017-03-01 18:06:34 +00:00
|
|
|
var tasks []*string
|
2017-09-07 15:34:03 +00:00
|
|
|
for v := 0; v < test.count; v++ {
|
2017-03-01 18:06:34 +00:00
|
|
|
tasks = append(tasks, &testval)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := provider.chunkedTaskArns(tasks)
|
|
|
|
var outCount []int
|
|
|
|
|
|
|
|
for _, el := range out {
|
|
|
|
outCount = append(outCount, len(el))
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:34:03 +00:00
|
|
|
if !reflect.DeepEqual(outCount, test.expectedLengths) {
|
|
|
|
t.Errorf("Chunking %d elements, expected %#v, got %#v", test.count, test.expectedLengths, outCount)
|
2017-03-01 18:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 15:34:03 +00:00
|
|
|
|
|
|
|
func TestEcsGetBasicAuth(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
instance ecsInstance
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
instance: simpleEcsInstance(map[string]*string{}),
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
instance: simpleEcsInstance(map[string]*string{
|
|
|
|
types.LabelFrontendAuthBasic: aws.String("user:password"),
|
|
|
|
}),
|
|
|
|
expected: []string{"user:password"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getBasicAuth(test.instance)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|