2017-04-17 13:50:02 +03:00
package ecs
2017-01-05 17:24:17 +03:00
import (
"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 17:58:12 +03:00
"github.com/containous/traefik/types"
2017-09-06 13:10:05 +03:00
"github.com/stretchr/testify/assert"
2017-01-05 17:24:17 +03: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-09 02:18:38 +03:00
State : & ec2 . InstanceState {
Name : aws . String ( ec2 . InstanceStateNameRunning ) ,
} ,
2017-01-05 17:24:17 +03: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 ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Protocol label is not set should return a string equals to http" ,
2017-01-05 17:24:17 +03:00
expected : "http" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
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" ,
2017-01-05 17:24:17 +03:00
expected : "https" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelProtocol : aws . String ( "https" ) ,
2017-01-05 17:24:17 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getProtocol ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
func TestEcsHost ( t * testing . T ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Default host should be 10.0.0.0" ,
2017-01-05 17:24:17 +03:00
expected : "10.0.0.0" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getHost ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
func TestEcsPort ( t * testing . T ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Default port should be 80" ,
2017-01-05 17:24:17 +03:00
expected : "80" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getPort ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
func TestEcsWeight ( t * testing . T ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Weight label not set should return a string equals to 0" ,
2017-01-05 17:24:17 +03:00
expected : "0" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
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" ,
2017-01-05 17:24:17 +03:00
expected : "10" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelWeight : aws . String ( "10" ) ,
2017-01-05 17:24:17 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getWeight ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
func TestEcsPassHostHeader ( t * testing . T ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Frontend pass host header label not set should return a string equals to true" ,
2017-01-05 17:24:17 +03:00
expected : "true" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Frontend pass host header label set to false should return a string equals to false" ,
2017-01-05 17:24:17 +03:00
expected : "false" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelFrontendPassHostHeader : aws . String ( "false" ) ,
2017-01-05 17:24:17 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
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 { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getPassHostHeader ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
func TestEcsPriority ( t * testing . T ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Frontend priority label not set should return a string equals to 0" ,
2017-01-05 17:24:17 +03:00
expected : "0" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Frontend priority label set to 10 should return a string equals to 10" ,
2017-01-05 17:24:17 +03:00
expected : "10" ,
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelFrontendPriority : aws . String ( "10" ) ,
2017-01-05 17:24:17 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
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 { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getPriority ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
func TestEcsEntryPoints ( t * testing . T ) {
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-01-05 17:24:17 +03:00
expected [ ] string
instanceInfo ecsInstance
2017-09-06 13:10:05 +03:00
provider * Provider
2017-01-05 17:24:17 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Frontend entrypoints label not set should return empty array" ,
2017-01-05 17:24:17 +03:00
expected : [ ] string { } ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Frontend entrypoints label set to http should return a string array of 1 element" ,
2017-01-05 17:24:17 +03:00
expected : [ ] string { "http" } ,
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelFrontendEntryPoints : aws . String ( "http" ) ,
2017-01-05 17:24:17 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Frontend entrypoints label set to http,https should return a string array of 2 elements" ,
2017-01-05 17:24:17 +03:00
expected : [ ] string { "http" , "https" } ,
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelFrontendEntryPoints : aws . String ( "http,https" ) ,
2017-01-05 17:24:17 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider { } ,
2017-01-05 17:24:17 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . getEntryPoints ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-02-09 02:18:38 +03: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 )
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
expected bool
instanceInfo ecsInstance
provider * Provider
2017-02-09 02:18:38 +03:00
} {
{
2017-09-06 13:10:05 +03:00
desc : "Instance without enable label and exposed by default enabled should be not filtered" ,
expected : true ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance without enable label and exposed by default disabled should be filtered" ,
expected : false ,
instanceInfo : simpleEcsInstance ( map [ string ] * string { } ) ,
provider : & Provider {
ExposedByDefault : false ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with enable label set to false and exposed by default enabled should be filtered" ,
expected : false ,
2017-02-09 02:18:38 +03:00
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelEnable : aws . String ( "false" ) ,
2017-02-09 02:18:38 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with enable label set to true and exposed by default disabled should be not filtered" ,
expected : true ,
2017-02-09 02:18:38 +03:00
instanceInfo : simpleEcsInstance ( map [ string ] * string {
2017-07-10 17:58:12 +03:00
types . LabelEnable : aws . String ( "true" ) ,
2017-02-09 02:18:38 +03:00
} ) ,
2017-09-06 13:10:05 +03:00
provider : & Provider {
ExposedByDefault : false ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with nil private ip and exposed by default enabled should be filtered" ,
expected : false ,
instanceInfo : nilPrivateIP ,
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with nil machine and exposed by default enabled should be filtered" ,
expected : false ,
instanceInfo : nilMachine ,
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with nil machine state and exposed by default enabled should be filtered" ,
expected : false ,
instanceInfo : nilMachineState ,
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with nil machine state name and exposed by default enabled should be filtered" ,
expected : false ,
instanceInfo : nilMachineStateName ,
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
{
2017-09-06 13:10:05 +03:00
desc : "Instance with invalid machine state and exposed by default enabled should be filtered" ,
expected : false ,
instanceInfo : invalidMachineState ,
provider : & Provider {
ExposedByDefault : true ,
} ,
2017-02-09 02:18:38 +03:00
} ,
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
actual := test . provider . filterInstance ( test . instanceInfo )
assert . Equal ( t , test . expected , actual )
} )
2017-01-05 17:24:17 +03:00
}
}
2017-03-01 21:06:34 +03:00
func TestTaskChunking ( t * testing . T ) {
testval := "a"
2017-09-06 13:10:05 +03:00
tests := [ ] struct {
desc string
2017-03-01 21:06:34 +03:00
count int
expectedLengths [ ] int
2017-09-06 13:10:05 +03:00
provider * Provider
2017-03-01 21:06:34 +03:00
} {
2017-09-06 13:10:05 +03:00
{
desc : "0 parameter should return nil" ,
count : 0 ,
expectedLengths : [ ] int ( nil ) ,
provider : & Provider { } ,
} ,
{
desc : "1 parameter should return 1 array of 1 element" ,
count : 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 { } ,
} ,
2017-03-01 21:06:34 +03:00
}
2017-09-06 13:10:05 +03:00
for _ , test := range tests {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
var tasks [ ] * string
for v := 0 ; v < test . count ; v ++ {
tasks = append ( tasks , & testval )
}
2017-03-01 21:06:34 +03:00
2017-09-06 13:10:05 +03:00
out := test . provider . chunkedTaskArns ( tasks )
var outCount [ ] int
2017-03-01 21:06:34 +03:00
2017-09-06 13:10:05 +03:00
for _ , el := range out {
outCount = append ( outCount , len ( el ) )
}
assert . Equal ( t , test . expectedLengths , outCount , "Chunking %d elements" , test . count )
} )
2017-03-01 21:06:34 +03:00
}
}
2017-09-07 18:34:03 +03: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 )
} )
}
}