This repository was archived by the owner on Nov 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
Integ tests following the Fargate tutorial #765
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
602fe85
Remove cmd_ps test
efekarakus fb329a6
Remove cmd_up test
efekarakus 04f96a1
Add end-to-end test following the Fargate tutorial
efekarakus 2d82f2f
Add logging to cmd tests
efekarakus ebe9a86
Use require lib instead of assert
efekarakus 9f9dbe4
Upgrade testify to 1.3.0 and create Compose file as a temp file
efekarakus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
| // not use this file except in compliance with the License. A copy of the | ||
| // License is located at | ||
| // | ||
| // http://aws.amazon.com/apache2.0/ | ||
| // | ||
| // or in the "license" file accompanying this file. This file 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 cfn contains validation functions against a CloudFormation stack. | ||
| package cfn | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-sdk-go/aws/awserr" | ||
|
|
||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/aws/session" | ||
| "github.com/aws/aws-sdk-go/service/cloudformation" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestStackNameExists fails if there is no CloudFormation stack with the name stackName. | ||
| func TestStackNameExists(t *testing.T, stackName string) { | ||
| client := newClient(t) | ||
| resp, err := client.DescribeStacks(&cloudformation.DescribeStacksInput{ | ||
| StackName: aws.String(stackName), | ||
| }) | ||
| require.NoError(t, err, "Unexpected CloudFormation error during DescribeStacks") | ||
| require.NotNil(t, resp.Stacks, "CloudFormation stacks should not be nil") | ||
| require.Equalf(t, 1, len(resp.Stacks), "Expected to receive only 1 stack", "got %v", resp.Stacks) | ||
| require.Equalf(t, stackName, *resp.Stacks[0].StackName, "Unexpected stack name", "wanted %s, got %s", stackName, *resp.Stacks[0].StackName) | ||
| } | ||
|
|
||
| // TestNoStackName fails if there is a CloudFormation stack with the name stackName. | ||
| func TestNoStackName(t *testing.T, stackName string) { | ||
| client := newClient(t) | ||
| resp, err := client.DescribeStacks(&cloudformation.DescribeStacksInput{ | ||
| StackName: aws.String(stackName), | ||
| }) | ||
| if err != nil { | ||
| if aerr, ok := err.(awserr.Error); ok { | ||
| switch aerr.Code() { | ||
| case "ValidationError": | ||
| t.Logf("Success: stack %s does not exist", stackName) | ||
| return | ||
| default: | ||
| require.NoError(t, err, "Unexpected CloudFormation error during DescribeStacks") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| require.Equalf(t, 0, len(resp.Stacks), "No stack with the name %s should exist", stackName) | ||
| } | ||
|
|
||
| func newClient(t *testing.T) *cloudformation.CloudFormation { | ||
| sess, err := session.NewSession() | ||
| require.NoError(t, err, "failed to create new session for upTest clients") | ||
| conf := aws.NewConfig() | ||
| return cloudformation.New(sess, conf) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
| // not use this file except in compliance with the License. A copy of the | ||
| // License is located at | ||
| // | ||
| // http://aws.amazon.com/apache2.0/ | ||
| // | ||
| // or in the "license" file accompanying this file. This file 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 cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/aws/amazon-ecs-cli/ecs-cli/integ" | ||
|
|
||
| "github.com/aws/amazon-ecs-cli/ecs-cli/integ/stdout" | ||
| ) | ||
|
|
||
| // A Project is the configuration needed to create an ECS Service. | ||
| type Project struct { | ||
| Name string | ||
| ComposeFileName string | ||
| ECSParamsFileName string | ||
| ConfigName string | ||
| } | ||
|
|
||
| // NewProject creates a new Project for an ECS Service. | ||
| func NewProject(name string, configName string) *Project { | ||
| return &Project{ | ||
| Name: name, | ||
| ConfigName: configName, | ||
| } | ||
| } | ||
|
|
||
| // TestServiceUp runs `ecs-cli compose service up` for a project. | ||
| func TestServiceUp(t *testing.T, p *Project) { | ||
| // Given | ||
| args := []string{ | ||
| "compose", | ||
| "--file", | ||
| p.ComposeFileName, | ||
| "--ecs-params", | ||
| p.ECSParamsFileName, | ||
| "--project-name", | ||
| p.Name, | ||
| "service", | ||
| "up", | ||
| "--cluster-config", | ||
| p.ConfigName, | ||
| } | ||
| cmd := integ.GetCommand(args) | ||
|
|
||
| // When | ||
| out, err := cmd.Output() | ||
| require.NoErrorf(t, err, "Failed to create service", "error %v, running %v, out: %s", err, args, string(out)) | ||
|
|
||
| // Then | ||
| stdout.Stdout(out).TestHasAllSubstrings(t, []string{ | ||
| "ECS Service has reached a stable state", | ||
| "desiredCount=1", | ||
| "serviceName=" + p.Name, | ||
| }) | ||
| t.Logf("Created service with name %s", p.Name) | ||
| } | ||
|
|
||
| // TestServicePs runs `ecs-cli compose service ps` for a project. | ||
| func TestServicePs(t *testing.T, p *Project, wantedNumOfContainers int) { | ||
| f := func(t *testing.T) bool { | ||
| return testServiceHasAllRunningContainers(t, p, wantedNumOfContainers) | ||
| } | ||
| timeoutInS := 120 * time.Second // 2 mins | ||
| sleepInS := 15 * time.Second | ||
| require.True(t, integ.RetryUntilTimeout(t, f, timeoutInS, sleepInS), "Failed to get RUNNING containers") | ||
| t.Logf("Project %s has %d running containers", p.Name, wantedNumOfContainers) | ||
| } | ||
|
|
||
| // TestServiceScale runs `ecs-cli compose service scale` for a project. | ||
| func TestServiceScale(t *testing.T, p *Project, scale int) { | ||
| // Given | ||
| args := []string{ | ||
| "compose", | ||
| "--file", | ||
| p.ComposeFileName, | ||
| "--ecs-params", | ||
| p.ECSParamsFileName, | ||
| "--project-name", | ||
| p.Name, | ||
| "service", | ||
| "scale", | ||
| strconv.Itoa(scale), | ||
| "--cluster-config", | ||
| p.ConfigName, | ||
| } | ||
| cmd := integ.GetCommand(args) | ||
|
|
||
| // When | ||
| out, err := cmd.Output() | ||
| require.NoErrorf(t, err, "Failed to scale service", "error %v, running %v, out: %s", err, args, string(out)) | ||
|
|
||
| // Then | ||
| stdout.Stdout(out).TestHasAllSubstrings(t, []string{ | ||
| "ECS Service has reached a stable state", | ||
| fmt.Sprintf("desiredCount=%d", scale), | ||
| fmt.Sprintf("runningCount=%d", scale), | ||
| "serviceName=" + p.Name, | ||
| }) | ||
| t.Logf("Scaled the service %s to %d tasks", p.Name, scale) | ||
| } | ||
|
|
||
| // TestServiceDown runs `ecs-cli compose service down` for a project. | ||
| func TestServiceDown(t *testing.T, p *Project) { | ||
| // Given | ||
| args := []string{ | ||
| "compose", | ||
| "--file", | ||
| p.ComposeFileName, | ||
| "--ecs-params", | ||
| p.ECSParamsFileName, | ||
| "--project-name", | ||
| p.Name, | ||
| "service", | ||
| "down", | ||
| "--cluster-config", | ||
| p.ConfigName, | ||
| } | ||
| cmd := integ.GetCommand(args) | ||
|
|
||
| // When | ||
| out, err := cmd.Output() | ||
| require.NoErrorf(t, err, "Failed to delete service", "error %v, running %v, out: %s", err, args, string(out)) | ||
|
|
||
| // Then | ||
| stdout.Stdout(out).TestHasAllSubstrings(t, []string{ | ||
| "Deleted ECS service", | ||
| "ECS Service has reached a stable state", | ||
| "desiredCount=0", | ||
| "runningCount=0", | ||
| "serviceName=" + p.Name, | ||
| }) | ||
| t.Logf("Deleted service %s", p.Name) | ||
| } | ||
|
|
||
| func testServiceHasAllRunningContainers(t *testing.T, p *Project, wantedNumOfContainers int) bool { | ||
| // Given | ||
| args := []string{ | ||
| "compose", | ||
| "--file", | ||
| p.ComposeFileName, | ||
| "--ecs-params", | ||
| p.ECSParamsFileName, | ||
| "--project-name", | ||
| p.Name, | ||
| "service", | ||
| "ps", | ||
| "--cluster-config", | ||
| p.ConfigName, | ||
| } | ||
| cmd := integ.GetCommand(args) | ||
|
|
||
| // When | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| t.Logf("Failed to list containers for service %v", args) | ||
| return false | ||
| } | ||
|
|
||
| // Then | ||
| lines := strings.Split(string(out), "\n") | ||
| if len(lines) < 2 { | ||
| t.Logf("No running containers yet, out = %v", lines) | ||
| return false | ||
| } | ||
| if lines[len(lines)-1] == "" { | ||
| lines = lines[:len(lines)-1] // Drop the last new line | ||
| } | ||
| containers := lines[1:] // Drop the headers | ||
| if wantedNumOfContainers != len(containers) { | ||
| t.Logf("Wanted = %d, got = %d running containers", wantedNumOfContainers, len(containers)) | ||
| return false | ||
| } | ||
| for _, container := range containers { | ||
| status := integ.GetRowValues(container)[1] | ||
| if status != "RUNNING" { | ||
| t.Logf("Container is not RUNNING: %s", container) | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
| // not use this file except in compliance with the License. A copy of the | ||
| // License is located at | ||
| // | ||
| // http://aws.amazon.com/apache2.0/ | ||
| // | ||
| // or in the "license" file accompanying this file. This file 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 cmd contains ECS CLI command tests. | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/aws/amazon-ecs-cli/ecs-cli/integ" | ||
|
|
||
| "github.com/aws/amazon-ecs-cli/ecs-cli/integ/stdout" | ||
| ) | ||
|
|
||
| // A CLIConfig holds the basic lookup information used by the ECS CLI for a cluster. | ||
| type CLIConfig struct { | ||
| ClusterName string | ||
| ConfigName string | ||
| } | ||
|
|
||
| // TestFargateConfig runs `ecs-cli configure` with a FARGATE launch type. | ||
| func TestFargateConfig(t *testing.T) *CLIConfig { | ||
| conf := CLIConfig{ | ||
| ClusterName: integ.SuggestedResourceName("fargate", "cluster"), | ||
| ConfigName: integ.SuggestedResourceName("fargate", "config"), | ||
| } | ||
| testConfig(t, conf.ClusterName, "FARGATE", conf.ConfigName) | ||
| t.Logf("Created config %s", conf.ConfigName) | ||
| return &conf | ||
| } | ||
|
|
||
| func testConfig(t *testing.T, clusterName, launchType, configName string) { | ||
| args := []string{ | ||
| "configure", | ||
| "--cluster", | ||
| clusterName, | ||
| "--region", | ||
| os.Getenv("AWS_REGION"), | ||
| "--default-launch-type", | ||
| launchType, | ||
| "--config-name", | ||
| configName, | ||
| } | ||
| cmd := integ.GetCommand(args) | ||
|
|
||
| // When | ||
| out, err := cmd.Output() | ||
| require.NoErrorf(t, err, "Failed to configure CLI", "error %v, running %v, out: %s", err, args, string(out)) | ||
|
|
||
| // Then | ||
| stdout.Stdout(out).TestHasAllSubstrings(t, []string{ | ||
| fmt.Sprintf("Saved ECS CLI cluster configuration %s", configName), | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what goimports formats it as for me :/
Kept it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
¯_(ツ)_/¯