From 3a83d85d7a816eec596816d7afcd46b8113a8be5 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Tue, 18 Aug 2020 17:06:47 +0200 Subject: [PATCH 1/3] First ECS E2E test Signed-off-by: Guillaume Tardif --- .github/PULL_REQUEST_TEMPLATE.md | 1 + .github/workflows/optional-ci.yml | 46 +++++++++++++- Makefile | 3 + tests/composefiles/nginx.yaml | 3 +- tests/ecs-e2e/e2e-ecs_test.go | 99 +++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 tests/ecs-e2e/e2e-ecs_test.go diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 28ce12ce8..31921a304 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -6,6 +6,7 @@ diff --git a/.github/workflows/optional-ci.yml b/.github/workflows/optional-ci.yml index c08083a22..18d2ff191 100644 --- a/.github/workflows/optional-ci.yml +++ b/.github/workflows/optional-ci.yml @@ -12,6 +12,7 @@ jobs: runs-on: ubuntu-latest outputs: trigger-aci: ${{steps.runacitest.outputs.triggered}} + trigger-ecs: ${{steps.runecstest.outputs.triggered}} trigger-windows: ${{steps.runwindowstest.outputs.triggered}} steps: - uses: khan/pull-request-comment-trigger@master @@ -26,8 +27,14 @@ jobs: id: runwindowstest with: trigger: '@test-windows' + - uses: khan/pull-request-comment-trigger@master + name: Check if test ECS + if: github.event_name == 'pull_request' + id: runecstest + with: + trigger: '@test-ecs' - build: + aci-tests: name: ACI e2e tests runs-on: ubuntu-latest env: @@ -59,6 +66,43 @@ jobs: AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} run: make e2e-aci + ecs-tests: + name: ECS e2e tests + runs-on: ubuntu-latest + env: + GO111MODULE: "on" + needs: check-optional-tests + if: github.ref == 'refs/heads/main' || needs.check-optional-tests.outputs.trigger-ecs == 'true' + steps: + - name: Set up Go 1.15 + uses: actions/setup-go@v1 + with: + go-version: 1.15 + id: go + + - name: Checkout code into the Go module directory + uses: actions/checkout@v2 + + - uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: go-${{ hashFiles('**/go.sum') }} + + - name: Build for ECS e2e tests + env: + BUILD_TAGS: ecs + run: make -f builder.Makefile cli + + - name: create aws config folder + run: mkdir -p ~/.aws + + - name: ECS e2e Test + env: + AWS_DEFAULT_REGION: us-west-2 + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} + run: make e2e-ecs + windows-build: name: Windows Build runs-on: windows-latest diff --git a/Makefile b/Makefile index 30d54ff84..287bb20c8 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,9 @@ e2e-win-ci: ## Run end to end local tests on Windows CI, no Docker for Linux con e2e-aci: ## Run End to end ACI tests. Set E2E_TEST=TestName to run a single test go test -count=1 -v $(TEST_FLAGS) ./tests/aci-e2e +e2e-ecs: ## Run End to end ECS tests. Set E2E_TEST=TestName to run a single test + go test -timeout 20m -count=1 -v $(TEST_FLAGS) ./tests/ecs-e2e + cross: ## Compile the CLI for linux, darwin and windows @docker build . --target cross \ --build-arg BUILD_TAGS \ diff --git a/tests/composefiles/nginx.yaml b/tests/composefiles/nginx.yaml index 07ac6d46c..5f8be2aba 100644 --- a/tests/composefiles/nginx.yaml +++ b/tests/composefiles/nginx.yaml @@ -3,4 +3,5 @@ version: "3.7" services: nginx: image: nginx - isolation: FARGATE + ports: + - "80:80" \ No newline at end of file diff --git a/tests/ecs-e2e/e2e-ecs_test.go b/tests/ecs-e2e/e2e-ecs_test.go new file mode 100644 index 000000000..98f20f860 --- /dev/null +++ b/tests/ecs-e2e/e2e-ecs_test.go @@ -0,0 +1,99 @@ +/* + Copyright 2020 Docker, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import ( + "fmt" + "gotest.tools/v3/assert" + "io/ioutil" + "net/http" + "os" + "strconv" + "strings" + "testing" + "time" + + . "github.com/docker/api/tests/framework" + "gotest.tools/v3/icmd" +) + +var binDir string + +func TestMain(m *testing.M) { + p, cleanup, err := SetupExistingCLI() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + binDir = p + exitCode := m.Run() + cleanup() + os.Exit(exitCode) +} + +func TestCompose(t *testing.T) { + startTime := strconv.Itoa(int(time.Now().UnixNano())) + c := NewE2eCLI(t, binDir) + contextName := "teste2e" + startTime + 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) + res = c.RunDockerCmd("context", "use", contextName) + res.Assert(t, icmd.Expected{Out: contextName}) + res = c.RunDockerCmd("context", "ls") + res.Assert(t, icmd.Expected{Out: contextName + " *"}) + }) + + t.Run("compose up", func(t *testing.T) { + res := c.RunDockerCmd("compose", "up", "--project-name", stack, "-f", "../composefiles/nginx.yaml") + res.Assert(t, icmd.Success) + }) + + + 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)) + 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) + + r, err := http.Get(url) + assert.NilError(t, err) + assert.Equal(t, r.StatusCode, http.StatusOK) + b, err := ioutil.ReadAll(r.Body) + assert.NilError(t, err) + assert.Assert(t, strings.Contains(string(b), "Welcome to nginx!")) + }) + + t.Run("compose down", func(t *testing.T) { + res := c.RunDockerCmd("compose", "down", "--project-name", stack, "-f", "../composefiles/nginx.yaml") + res.Assert(t, icmd.Success) + }) +} From d49d4473c31ceb7c53a2dd716a773f28a0130858 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Thu, 20 Aug 2020 09:56:26 +0200 Subject: [PATCH 2/3] Allow running ECS E2E tests locally Signed-off-by: Guillaume Tardif --- 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) From 9b0b1129a65fda199da6959d15a3dcb4fd6fb99a Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Thu, 20 Aug 2020 10:00:03 +0200 Subject: [PATCH 3/3] Build without ecs build flag, not required anymore. Fix context create ecs command not build Signed-off-by: Guillaume Tardif --- .github/workflows/optional-ci.yml | 2 -- cli/cmd/context/create_ecs.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/.github/workflows/optional-ci.yml b/.github/workflows/optional-ci.yml index 18d2ff191..bcc980822 100644 --- a/.github/workflows/optional-ci.yml +++ b/.github/workflows/optional-ci.yml @@ -89,8 +89,6 @@ jobs: key: go-${{ hashFiles('**/go.sum') }} - name: Build for ECS e2e tests - env: - BUILD_TAGS: ecs run: make -f builder.Makefile cli - name: create aws config folder diff --git a/cli/cmd/context/create_ecs.go b/cli/cmd/context/create_ecs.go index b26aeea21..43ec3a622 100644 --- a/cli/cmd/context/create_ecs.go +++ b/cli/cmd/context/create_ecs.go @@ -1,5 +1,3 @@ -// +build ecs - /* Copyright 2020 Docker, Inc.