Skip to content

Commit

Permalink
integ tests: port forwarding
Browse files Browse the repository at this point in the history
always verify that the task is stopped at the end of every test, in case
unrelated state change events are received that may trigger a test to
exit before it has been cleaned up.

also get a new port for each test to avoid potential collisions.
  • Loading branch information
sparrc committed May 14, 2020
1 parent 7de7eef commit a75477b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 67 deletions.
14 changes: 14 additions & 0 deletions agent/engine/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -48,8 +49,21 @@ const (

var (
defaultDockerClientAPIVersion = dockerclient.Version_1_17
// some unassigned ports to use for tests
// see https://www.speedguide.net/port.php?port=24685
unassignedPort int64 = 24685
)

// getUnassignedPort returns a NEW unassigned port each time it's called.
func getUnassignedPort() int {
port := int(atomic.LoadInt64(&unassignedPort))
if port > 24752 {
// 24753 is max, see https://www.speedguide.net/port.php?port=24753
atomic.StoreInt64(&unassignedPort, 24685)
}
return port
}

func discardEvents(from interface{}) func() {
done := make(chan bool)

Expand Down
5 changes: 3 additions & 2 deletions agent/engine/engine_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"

Expand All @@ -44,11 +45,11 @@ import (
"github.com/stretchr/testify/require"
)



const (
testDockerStopTimeout = 5 * time.Second
credentialsIDIntegTest = "credsid"
containerPortOne = 24751
containerPortTwo = 24752
serverContent = "ecs test container"
dialTimeout = 200 * time.Millisecond
localhost = "127.0.0.1"
Expand Down
63 changes: 33 additions & 30 deletions agent/engine/engine_unix_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,8 @@ func TestPortForward(t *testing.T) {

testArn := "testPortForwardFail"
testTask := createTestTask(testArn)
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "-serve", serverContent}
port1 := getUnassignedPort()
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", port1), "-serve", serverContent}

// Port not forwarded; verify we can't access it
go taskEngine.AddTask(testTask)
Expand All @@ -659,8 +660,8 @@ func TestPortForward(t *testing.T) {
require.NoError(t, err)

time.Sleep(waitForDockerDuration) // wait for Docker
_, err = net.DialTimeout("tcp", fmt.Sprintf("%s:%d", localhost, containerPortOne), dialTimeout)
assert.Error(t, err, "Did not expect to be able to dial %s:%d but didn't get error", localhost, containerPortOne)
_, err = net.DialTimeout("tcp", fmt.Sprintf("%s:%d", localhost, port1), dialTimeout)
assert.Error(t, err, "Did not expect to be able to dial %s:%d but didn't get error", localhost, port1)

// Kill the existing container now to make the test run more quickly.
containerMap, _ := taskEngine.(*DockerTaskEngine).state.ContainerMapByArn(testTask.Arn)
Expand All @@ -674,8 +675,9 @@ func TestPortForward(t *testing.T) {
// Now forward it and make sure that works
testArn = "testPortForwardWorking"
testTask = createTestTask(testArn)
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "-serve", serverContent}
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: containerPortOne, HostPort: containerPortOne}}
port2 := getUnassignedPort()
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", port2), "-serve", serverContent}
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: port2, HostPort: port2}}

taskEngine.AddTask(testTask)

Expand All @@ -685,7 +687,7 @@ func TestPortForward(t *testing.T) {
}

time.Sleep(waitForDockerDuration) // wait for Docker
conn, err := dialWithRetries("tcp", fmt.Sprintf("%s:%d", localhost, containerPortOne), 10, dialTimeout)
conn, err := dialWithRetries("tcp", fmt.Sprintf("%s:%d", localhost, port2), 10, dialTimeout)
if err != nil {
t.Fatal("Error dialing simple container " + err.Error())
}
Expand Down Expand Up @@ -727,13 +729,15 @@ func TestMultiplePortForwards(t *testing.T) {
// Forward it and make sure that works
testArn := "testMultiplePortForwards"
testTask := createTestTask(testArn)
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "-serve", serverContent + "1"}
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: containerPortOne, HostPort: containerPortOne}}
port1 := getUnassignedPort()
port2 := getUnassignedPort()
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", port1), "-serve", serverContent + "1"}
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: port1, HostPort: port1}}
testTask.Containers[0].Essential = false
testTask.Containers = append(testTask.Containers, createTestContainer())
testTask.Containers[1].Name = "nc2"
testTask.Containers[1].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "-serve", serverContent + "2"}
testTask.Containers[1].Ports = []apicontainer.PortBinding{{ContainerPort: containerPortOne, HostPort: containerPortTwo}}
testTask.Containers[1].Command = []string{fmt.Sprintf("-l=%d", port1), "-serve", serverContent + "2"}
testTask.Containers[1].Ports = []apicontainer.PortBinding{{ContainerPort: port1, HostPort: port2}}

go taskEngine.AddTask(testTask)

Expand All @@ -743,7 +747,7 @@ func TestMultiplePortForwards(t *testing.T) {
}

time.Sleep(waitForDockerDuration) // wait for Docker
conn, err := dialWithRetries("tcp", fmt.Sprintf("%s:%d", localhost, containerPortOne), 10, dialTimeout)
conn, err := dialWithRetries("tcp", fmt.Sprintf("%s:%d", localhost, port1), 10, dialTimeout)
if err != nil {
t.Fatal("Error dialing simple container 1 " + err.Error())
}
Expand All @@ -753,7 +757,7 @@ func TestMultiplePortForwards(t *testing.T) {
t.Error("Got response: " + string(response) + " instead of" + serverContent + "1")
}
t.Log("Read first container")
conn, err = dialWithRetries("tcp", fmt.Sprintf("%s:%d", localhost, containerPortTwo), 10, dialTimeout)
conn, err = dialWithRetries("tcp", fmt.Sprintf("%s:%d", localhost, port2), 10, dialTimeout)
if err != nil {
t.Fatal("Error dialing simple container 2 " + err.Error())
}
Expand All @@ -780,9 +784,10 @@ func TestDynamicPortForward(t *testing.T) {

testArn := "testDynamicPortForward"
testTask := createTestTask(testArn)
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "-serve", serverContent}
port := getUnassignedPort()
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", port), "-serve", serverContent}
// No HostPort = docker should pick
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: containerPortOne}}
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: port}}

go taskEngine.AddTask(testTask)

Expand All @@ -798,12 +803,12 @@ func TestDynamicPortForward(t *testing.T) {
}
var bindingForcontainerPortOne uint16
for _, binding := range portBindings {
if binding.ContainerPort == containerPortOne {
if binding.ContainerPort == port {
bindingForcontainerPortOne = binding.HostPort
}
}
if bindingForcontainerPortOne == 0 {
t.Errorf("Could not find the port mapping for %d!", containerPortOne)
t.Errorf("Could not find the port mapping for %d!", port)
}

time.Sleep(waitForDockerDuration) // wait for Docker
Expand All @@ -821,9 +826,7 @@ func TestDynamicPortForward(t *testing.T) {
taskUpdate := *testTask
taskUpdate.SetDesiredStatus(apitaskstatus.TaskStopped)
go taskEngine.AddTask(&taskUpdate)

verifyContainerStoppedStateChange(t, taskEngine)
verifyTaskStoppedStateChange(t, taskEngine)
verifyTaskIsStopped(stateChangeEvents, testTask)
}

func TestMultipleDynamicPortForward(t *testing.T) {
Expand All @@ -834,9 +837,10 @@ func TestMultipleDynamicPortForward(t *testing.T) {

testArn := "testDynamicPortForward2"
testTask := createTestTask(testArn)
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "-serve", serverContent, `-loop`}
port := getUnassignedPort()
testTask.Containers[0].Command = []string{fmt.Sprintf("-l=%d", port), "-serve", serverContent, `-loop`}
// No HostPort or 0 hostport; docker should pick two ports for us
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: containerPortOne}, {ContainerPort: containerPortOne, HostPort: 0}}
testTask.Containers[0].Ports = []apicontainer.PortBinding{{ContainerPort: port}, {ContainerPort: port, HostPort: 0}}

go taskEngine.AddTask(testTask)

Expand All @@ -853,7 +857,7 @@ func TestMultipleDynamicPortForward(t *testing.T) {
var bindingForcontainerPortOne_1 uint16
var bindingForcontainerPortOne_2 uint16
for _, binding := range portBindings {
if binding.ContainerPort == containerPortOne {
if binding.ContainerPort == port {
if bindingForcontainerPortOne_1 == 0 {
bindingForcontainerPortOne_1 = binding.HostPort
} else {
Expand All @@ -862,10 +866,10 @@ func TestMultipleDynamicPortForward(t *testing.T) {
}
}
if bindingForcontainerPortOne_1 == 0 {
t.Errorf("Could not find the port mapping for %d!", containerPortOne)
t.Errorf("Could not find the port mapping for %d!", port)
}
if bindingForcontainerPortOne_2 == 0 {
t.Errorf("Could not find the port mapping for %d!", containerPortOne)
t.Errorf("Could not find the port mapping for %d!", port)
}

time.Sleep(waitForDockerDuration) // wait for Docker
Expand Down Expand Up @@ -893,9 +897,7 @@ func TestMultipleDynamicPortForward(t *testing.T) {
taskUpdate := *testTask
taskUpdate.SetDesiredStatus(apitaskstatus.TaskStopped)
go taskEngine.AddTask(&taskUpdate)

verifyContainerStoppedStateChange(t, taskEngine)
verifyTaskStoppedStateChange(t, taskEngine)
verifyTaskIsStopped(stateChangeEvents, testTask)
}

// TestLinking ensures that container linking does allow networking to go
Expand All @@ -910,9 +912,10 @@ func TestLinking(t *testing.T) {
testTask.Containers = append(testTask.Containers, createTestContainer())
testTask.Containers[0].Command = []string{"-l=80", "-serve", "hello linker"}
testTask.Containers[0].Name = "linkee"
testTask.Containers[1].Command = []string{fmt.Sprintf("-l=%d", containerPortOne), "linkee_alias:80"}
port := getUnassignedPort()
testTask.Containers[1].Command = []string{fmt.Sprintf("-l=%d", port), "linkee_alias:80"}
testTask.Containers[1].Links = []string{"linkee:linkee_alias"}
testTask.Containers[1].Ports = []apicontainer.PortBinding{{ContainerPort: containerPortOne, HostPort: containerPortOne}}
testTask.Containers[1].Ports = []apicontainer.PortBinding{{ContainerPort: port, HostPort: port}}

stateChangeEvents := taskEngine.StateChangeEvents()

Expand All @@ -927,7 +930,7 @@ func TestLinking(t *testing.T) {

var response []byte
for i := 0; i < 10; i++ {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", localhost, containerPortOne), dialTimeout)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", localhost, port), dialTimeout)
if err != nil {
t.Log("Error dialing simple container" + err.Error())
}
Expand Down
Loading

0 comments on commit a75477b

Please sign in to comment.