Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ integ-test:
@echo "Building ecs-cli..."
./scripts/build_binary.sh ./bin/local
@echo "Running integration tests..."
go test -tags integ -v ./ecs-cli/integ/...
go test -tags integ -v ./ecs-cli/integ/e2e/...

.PHONY: generate
generate: $(SOURCES)
Expand Down
12 changes: 8 additions & 4 deletions ecs-cli/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-cli/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"
version = "=1.3.0"

[[constraint]]
name = "github.com/urfave/cli"
Expand Down
4 changes: 1 addition & 3 deletions ecs-cli/integ/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ You may be charged for the AWS resources utilized while running these tests. It'

## Local test setup

1. Some tests assume the existence of an "ecs-cli-integ" cluster in the currently configured region.
Prior to running these tests, create this cluster using the template in `/resources/ecs_cli_integ_template.json` via the CloudFormation console.
2. Set the following environment variables:
1. Set the following environment variables:
```bash
# hardcode the CodeBuild ID since we're not using codebuild to run the tests
export CODEBUILD_BUILD_ID="local-integ-test"
Expand Down
66 changes: 66 additions & 0 deletions ecs-cli/integ/cfn/cfn.go
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)
}
200 changes: 200 additions & 0 deletions ecs-cli/integ/cmd/compose.go
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
}
68 changes: 68 additions & 0 deletions ecs-cli/integ/cmd/configure.go
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"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra whitespace

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯

"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),
})
}
Loading