Skip to content

Commit efa0b45

Browse files
committed
Add integ test resources, 'ps' test
1 parent d741ace commit efa0b45

File tree

5 files changed

+404
-0
lines changed

5 files changed

+404
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ $(LOCAL_BINARY): $(SOURCES)
3535
test:
3636
env -i PATH=$$PATH GOPATH=$$GOPATH GOROOT=$$GOROOT go test -timeout=120s -v -cover ./ecs-cli/modules/...
3737

38+
39+
.PHONY: integ-test
40+
integ-test:
41+
@echo "Building ecs-cli..."
42+
./scripts/build_binary.sh ./bin/local
43+
@echo "Running integration tests..."
44+
go test -tags integ -v ./ecs-cli/integ/...
45+
3846
.PHONY: generate
3947
generate: $(SOURCES)
4048
PATH=$(LOCAL_PATH) go generate ./ecs-cli/modules/...

ecs-cli/integ/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Amazon ECS CLI - Integration Tests
2+
3+
This directory contains tests intended to run against public Amazon Elastic Container Service endpoints and other AWS Services. They are meant to test end-to-end functionality by executing commands as an ecs-cli user would.
4+
5+
You may be charged for the AWS resources utilized while running these tests. It's not recommended to run these on an AWS account handling production work-loads.
6+
7+
## Test setup
8+
9+
Some tests assume the existance 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`
10+
11+
## Running the tests
12+
13+
The best way to run them on Linux is via the `make integ-test` target.
14+
15+
The best way to run them on Windows is by building and running the tests with `go` from the project root:
16+
* `go build -installsuffix cgo -a -ldflags '-s' -o ./bin/local/ecs-cli.exe ./ecs-cli/`
17+
* `go test -tags integ -v ./ecs-cli/integ/...`

ecs-cli/integ/cmd_ps.go/ps_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// +build integ
2+
3+
// Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
// not use this file except in compliance with the License. A copy of the
7+
// License is located at
8+
//
9+
// http://aws.amazon.com/apache2.0/
10+
//
11+
// or in the "license" file accompanying this file. This file is distributed
12+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
// express or implied. See the License for the specific language governing
14+
// permissions and limitations under the License.
15+
16+
package integ
17+
18+
import (
19+
"bytes"
20+
"io"
21+
"strings"
22+
"testing"
23+
24+
"github.com/aws/amazon-ecs-cli/ecs-cli/integ"
25+
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/container"
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
const (
30+
integClusterName = "ecs-cli-integ"
31+
)
32+
33+
// Test assumes binary has been built, named cluster contains 3 containers
34+
func TestCmd_PS(t *testing.T) {
35+
// set up command
36+
ps_args := []string{"ps", "--cluster", integClusterName}
37+
cmd := integ.GetCommand(ps_args)
38+
39+
var stdoutWriter bytes.Buffer
40+
writer := io.MultiWriter(&stdoutWriter)
41+
42+
cmd.Stdout = writer
43+
44+
// execute command
45+
err := cmd.Run()
46+
assert.NoError(t, err, "Unexpected error starting 'ps'")
47+
48+
// assert on result
49+
actualStdout := stdoutWriter.String()
50+
assert.NotEmpty(t, actualStdout)
51+
52+
stdoutLines := strings.Split(actualStdout, "\n")
53+
length := len(stdoutLines)
54+
55+
// trim off empty last row is needed
56+
if stdoutLines[length-1] == "" {
57+
stdoutLines = stdoutLines[:length-1]
58+
}
59+
60+
headers := integ.GetRowValues(stdoutLines[0])
61+
assert.Equal(t, container.ContainerInfoColumns, headers)
62+
63+
runningContainers := stdoutLines[1:]
64+
assert.Equal(t, 3, len(runningContainers))
65+
}
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Description": "AWS CloudFormation template for ecs-cli integ resources.",
4+
"Conditions": {
5+
"IsCNRegion": {
6+
"Fn::Or" : [
7+
{"Fn::Equals": [ { "Ref": "AWS::Region" }, "cn-north-1" ]},
8+
{"Fn::Equals": [ { "Ref": "AWS::Region" }, "cn-northwest-1" ]}
9+
]
10+
}
11+
},
12+
"Parameters": {
13+
"EcsAmiId": {
14+
"Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>",
15+
"Description": "ECS EC2 AMI id",
16+
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
17+
},
18+
"EcsPort": {
19+
"Type" : "String",
20+
"Description" : "Security Group port to open on ECS instances",
21+
"Default" : "80"
22+
},
23+
"EcsServiceCount": {
24+
"Type" : "Number",
25+
"Description" : "Desired task count for the ECS Service",
26+
"Default" : "3"
27+
},
28+
"AsgMinSize": {
29+
"Type": "Number",
30+
"Description": "Minimum size of ECS Auto Scaling Group",
31+
"Default": "0"
32+
},
33+
"AsgMaxSize": {
34+
"Type": "Number",
35+
"Description": "Maximum size of ECS Auto Scaling Group",
36+
"Default": "10"
37+
},
38+
"AsgDesiredCapacity": {
39+
"Type": "Number",
40+
"Description": "The initial desired capacity of ECS Auto Scaling Group",
41+
"Default": "3"
42+
}
43+
},
44+
"Resources": {
45+
"Vpc": {
46+
"Type": "AWS::EC2::VPC",
47+
"Properties" : {
48+
"CidrBlock" : "10.0.0.0/16",
49+
"EnableDnsSupport" : true,
50+
"EnableDnsHostnames" : true,
51+
"Tags" : [ {"Key" : "Name", "Value" : "ECS CLI Integ cluster"} ]
52+
}
53+
},
54+
"PubSubnetAz1": {
55+
"Type": "AWS::EC2::Subnet",
56+
"Properties" : {
57+
"CidrBlock" : "10.0.0.0/24",
58+
"Tags" : [ {"Key" : "Name", "Value" : "ECS CLI Integ cluster"} ],
59+
"VpcId" : {
60+
"Ref": "Vpc"
61+
}
62+
}
63+
},
64+
"PubSubnetAz2": {
65+
"Type": "AWS::EC2::Subnet",
66+
"Properties" : {
67+
"CidrBlock" : "10.0.1.0/24",
68+
"Tags" : [ {"Key" : "Name", "Value" : "ECS CLI Integ cluster"} ],
69+
"VpcId" : {
70+
"Ref": "Vpc"
71+
}
72+
}
73+
},
74+
"InternetGateway": {
75+
"Type": "AWS::EC2::InternetGateway",
76+
"Properties": {
77+
"Tags": [ {"Key" : "Name", "Value" : "ECS CLI Integ cluster"} ]
78+
}
79+
},
80+
"AttachGateway": {
81+
"Type": "AWS::EC2::VPCGatewayAttachment",
82+
"Properties": {
83+
"VpcId": {
84+
"Ref": "Vpc"
85+
},
86+
"InternetGatewayId": {
87+
"Ref": "InternetGateway"
88+
}
89+
}
90+
},
91+
"RouteViaIgw": {
92+
"Type": "AWS::EC2::RouteTable",
93+
"Properties": {
94+
"VpcId": {
95+
"Ref": "Vpc"
96+
}
97+
}
98+
},
99+
"PublicRouteViaIgw": {
100+
"Type": "AWS::EC2::Route",
101+
"Properties": {
102+
"RouteTableId": {
103+
"Ref": "RouteViaIgw"
104+
},
105+
"DestinationCidrBlock": "0.0.0.0/0",
106+
"GatewayId": {
107+
"Ref": "InternetGateway"
108+
}
109+
}
110+
},
111+
"PubSubnet1RouteTableAssociation": {
112+
"Type": "AWS::EC2::SubnetRouteTableAssociation",
113+
"Properties": {
114+
"SubnetId": {
115+
"Ref": "PubSubnetAz1"
116+
},
117+
"RouteTableId": {
118+
"Ref": "RouteViaIgw"
119+
}
120+
}
121+
},
122+
"PubSubnet2RouteTableAssociation": {
123+
"Type": "AWS::EC2::SubnetRouteTableAssociation",
124+
"Properties": {
125+
"SubnetId": {
126+
"Ref": "PubSubnetAz2"
127+
},
128+
"RouteTableId": {
129+
"Ref": "RouteViaIgw"
130+
}
131+
}
132+
},
133+
"EcsSecurityGroup": {
134+
"Type": "AWS::EC2::SecurityGroup",
135+
"Properties": {
136+
"GroupDescription": "ECS Allowed Ports",
137+
"VpcId": {
138+
"Ref": "Vpc"
139+
},
140+
"SecurityGroupIngress": [{
141+
"IpProtocol": "tcp",
142+
"FromPort": "80",
143+
"ToPort": "80",
144+
"CidrIp": "0.0.0.0/0"
145+
}]
146+
}
147+
},
148+
"EcsInstanceRole": {
149+
"Type": "AWS::IAM::Role",
150+
"Properties": {
151+
"AssumeRolePolicyDocument": {
152+
"Version": "2012-10-17",
153+
"Statement": [
154+
{
155+
"Effect": "Allow",
156+
"Principal": {
157+
"Service": [
158+
"Fn::If": [
159+
"IsCNRegion",
160+
"ec2.amazonaws.com.cn",
161+
"ec2.amazonaws.com"
162+
]
163+
]
164+
},
165+
"Action": [
166+
"sts:AssumeRole"
167+
]
168+
}
169+
]
170+
},
171+
"ManagedPolicyArns": [
172+
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
173+
]
174+
}
175+
},
176+
"EcsInstanceProfile": {
177+
"Type": "AWS::IAM::InstanceProfile",
178+
"Properties": {
179+
"Roles": [{ "Ref": "EcsInstanceRole" }]
180+
}
181+
},
182+
"EcsInstanceLc": {
183+
"Type": "AWS::AutoScaling::LaunchConfiguration",
184+
"Properties": {
185+
"AssociatePublicIpAddress": true,
186+
"ImageId": {
187+
"Ref": "EcsAmiId"
188+
},
189+
"InstanceType": "m5.large",
190+
"IamInstanceProfile": {
191+
"Ref": "EcsInstanceProfile"
192+
},
193+
"SecurityGroups": [{
194+
"Ref": "EcsSecurityGroup"
195+
}],
196+
"UserData": {
197+
"Fn::Base64": {
198+
"Fn::Join": ["", [
199+
"#!/bin/bash\n",
200+
"echo ECS_CLUSTER=ecs-cli-integ >> /etc/ecs/ecs.config"
201+
]]
202+
}
203+
}
204+
}
205+
},
206+
"EcsInstanceAsg": {
207+
"Type": "AWS::AutoScaling::AutoScalingGroup",
208+
"Properties": {
209+
"VPCZoneIdentifier": [{ "Ref": "PubSubnetAz1"}, {"Ref": "PubSubnetAz2"}],
210+
"LaunchConfigurationName": {
211+
"Ref": "EcsInstanceLc"
212+
},
213+
"MinSize": {
214+
"Ref": "AsgMinSize"
215+
},
216+
"MaxSize": {
217+
"Ref": "AsgMaxSize"
218+
},
219+
"DesiredCapacity": {
220+
"Ref": "AsgDesiredCapacity"
221+
},
222+
"Tags": [
223+
{
224+
"Key": "Name",
225+
"Value": "ECS CLI Integ cluster",
226+
"PropagateAtLaunch": true
227+
}
228+
]
229+
}
230+
},
231+
"EcsCluster": {
232+
"Type" : "AWS::ECS::Cluster",
233+
"Properties": {
234+
"ClusterName": "ecs-cli-integ"
235+
}
236+
},
237+
"EcsTaskDefinition": {
238+
"Type" : "AWS::ECS::TaskDefinition",
239+
"Properties": {
240+
"Family": "ecs-cli-integ",
241+
"Memory": "1GB",
242+
"NetworkMode": "awsvpc",
243+
"Cpu": "512",
244+
"RequiresCompatibilities": ["EC2", "FARGATE"],
245+
"ContainerDefinitions": [{
246+
"Name": "httpd",
247+
"Image": "httpd",
248+
"PortMappings": [{
249+
"ContainerPort": "80"
250+
}]
251+
}]
252+
}
253+
},
254+
"EcsService": {
255+
"Type": "AWS::ECS::Service",
256+
"Properties": {
257+
"Cluster": { "Ref": "EcsCluster" },
258+
"DesiredCount": { "Ref": "EcsServiceCount" },
259+
"ServiceName": "ecs-cli-integ-service",
260+
"TaskDefinition": { "Ref": "EcsTaskDefinition" },
261+
"NetworkConfiguration": {
262+
"AwsvpcConfiguration": {
263+
"AssignPublicIp": "DISABLED",
264+
"SecurityGroups": [{ "Ref": "EcsSecurityGroup" }],
265+
"Subnets": [{ "Ref": "PubSubnetAz1" }, { "Ref": "PubSubnetAz2" }]
266+
}
267+
}
268+
}
269+
}
270+
}
271+
}

0 commit comments

Comments
 (0)