From d49d4473c31ceb7c53a2dd716a773f28a0130858 Mon Sep 17 00:00:00 2001
From: Guillaume Tardif <guillaume.tardif@docker.com>
Date: Thu, 20 Aug 2020 09:56:26 +0200
Subject: [PATCH] Allow running ECS E2E tests locally

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
---
 README.md                     |  8 ++++++++
 tests/ecs-e2e/e2e-ecs_test.go | 35 +++++++++++++++++++++++++----------
 2 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index fedae97c1..1ddd20bee 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,7 @@ make e2e-local
 ```
 This requires a local Docker Engine running
 
+Local ACI E2E tests: 
 ```
 AZURE_TENANT_ID="xxx" AZURE_CLIENT_ID="yyy" AZURE_CLIENT_SECRET="yyy" make e2e-aci
 ```
@@ -65,6 +66,13 @@ You can also run a single ACI test from the test suite:
 AZURE_TENANT_ID="xxx" AZURE_CLIENT_ID="yyy" AZURE_CLIENT_SECRET="yyy" make E2E_TEST=TestContainerRun e2e-aci
 ```
 
+Local ECS E2E tests: 
+```
+TEST_AWS_PROFILE=myProfile TEST_AWS_REGION=eu-west-3 make e2e-ecs
+```
+
+This requires a valid AWS profile defined in ~/.aws/credentials.
+
 ## Release
 
 To create a new release:
diff --git a/tests/ecs-e2e/e2e-ecs_test.go b/tests/ecs-e2e/e2e-ecs_test.go
index 98f20f860..c28112221 100644
--- a/tests/ecs-e2e/e2e-ecs_test.go
+++ b/tests/ecs-e2e/e2e-ecs_test.go
@@ -18,7 +18,6 @@ package main
 
 import (
 	"fmt"
-	"gotest.tools/v3/assert"
 	"io/ioutil"
 	"net/http"
 	"os"
@@ -27,6 +26,8 @@ import (
 	"testing"
 	"time"
 
+	"gotest.tools/v3/assert"
+
 	. "github.com/docker/api/tests/framework"
 	"gotest.tools/v3/icmd"
 )
@@ -52,12 +53,24 @@ func TestCompose(t *testing.T) {
 	stack := contextName
 
 	t.Run("create context", func(t *testing.T) {
-		profile := contextName
-		region := os.Getenv("AWS_DEFAULT_REGION")
-		secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
-		keyID := os.Getenv("AWS_ACCESS_KEY_ID")
-		res := c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", profile, "--region", region, "--secret-key", secretKey, "--key-id", keyID)
-		res.Assert(t, icmd.Success)
+		localTestProfile := os.Getenv("TEST_AWS_PROFILE")
+		var res *icmd.Result
+		if localTestProfile != "" {
+			region := os.Getenv("TEST_AWS_REGION")
+			assert.Check(t, region != "")
+			res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", localTestProfile, "--region", region)
+			res.Assert(t, icmd.Success)
+		} else {
+			profile := contextName
+			region := os.Getenv("AWS_DEFAULT_REGION")
+			secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
+			keyID := os.Getenv("AWS_ACCESS_KEY_ID")
+			assert.Check(t, keyID != "")
+			assert.Check(t, secretKey != "")
+			assert.Check(t, region != "")
+			res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", profile, "--region", region, "--secret-key", secretKey, "--key-id", keyID)
+			res.Assert(t, icmd.Success)
+		}
 		res = c.RunDockerCmd("context", "use", contextName)
 		res.Assert(t, icmd.Expected{Out: contextName})
 		res = c.RunDockerCmd("context", "ls")
@@ -69,21 +82,23 @@ func TestCompose(t *testing.T) {
 		res.Assert(t, icmd.Success)
 	})
 
-
+	var url string
 	t.Run("compose ps", func(t *testing.T) {
 		res := c.RunDockerCmd("compose", "ps", "--project-name", stack)
 		res.Assert(t, icmd.Success)
 		lines := strings.Split(res.Stdout(), "\n")
 
-		assert.Equal(t,3,  len(lines))
+		assert.Equal(t, 3, len(lines))
 		fields := strings.Fields(lines[1])
 		assert.Equal(t, 4, len(fields))
 		assert.Check(t, strings.Contains(fields[0], stack))
 		assert.Equal(t, "nginx", fields[1])
 		assert.Equal(t, "1/1", fields[2])
 		assert.Check(t, strings.Contains(fields[3], "->80/http"))
-		url := "http://" + strings.Replace(fields[3], "->80/http", "", 1)
+		url = "http://" + strings.Replace(fields[3], "->80/http", "", 1)
+	})
 
+	t.Run("nginx GET", func(t *testing.T) {
 		r, err := http.Get(url)
 		assert.NilError(t, err)
 		assert.Equal(t, r.StatusCode, http.StatusOK)