Skip to content

Commit d316cdc

Browse files
Tests: add basics tests and circleci ci setup (#2)
1 parent 8121353 commit d316cdc

File tree

3 files changed

+192
-82
lines changed

3 files changed

+192
-82
lines changed

.circleci/config.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: 2
2+
jobs:
3+
test:
4+
docker:
5+
- image: alpine
6+
steps:
7+
- checkout
8+
# - run: apk add bats
9+
# - run: bats test.bats
10+
11+
deploy:
12+
docker:
13+
- image: alpine
14+
steps:
15+
- checkout
16+
- setup_remote_docker
17+
- run: docker build . -t mycs/ecs-cf-deploy
18+
- run: docker tag mycs/ecs-cf-deploy mycs/ecs-cf-deploy:${CIRCLE_BRANCH/master/dev}
19+
- run: docker push mycs/ecs-cf-deploy
20+
21+
workflows:
22+
version: 2
23+
test_and_deploy:
24+
jobs:
25+
- test
26+
- deploy:
27+
requires:
28+
- test
29+
filters:
30+
branches:
31+
only:
32+
- master

ecs-cf-deploy

+93-82
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ VERSION="0.1.0"
44
VERBOSE=false
55
AWS_CLI=$(which aws)
66
AWS_ECS="$AWS_CLI --output json ecs"
7-
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-eu-west-1}
7+
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:="eu-west-1"}
88
AWS_ASSUME_ROLE=true
99

10-
1110
function usage() {
1211
cat <<EOM
1312
-- ecs-cf-deploy --
@@ -55,7 +54,7 @@ function require() {
5554
function assumeRole() {
5655

5756
temp_role=$($AWS_CLI sts assume-role --role-arn "arn:aws:iam::${ACCOUNT_ID}:role/${CROSS_ACCOUNT_ROLE}" --role-session-name "$(date +"%s")")
58-
57+
5958
export AWS_ACCESS_KEY_ID=$(echo $temp_role | jq .Credentials.AccessKeyId | xargs)
6059
export AWS_SECRET_ACCESS_KEY=$(echo $temp_role | jq .Credentials.SecretAccessKey | xargs)
6160
export AWS_SESSION_TOKEN=$(echo $temp_role | jq .Credentials.SessionToken | xargs)
@@ -73,6 +72,10 @@ function getClusterName() {
7372
--query 'Stacks[0].Outputs[?OutputKey==`Cluster`].OutputValue' \
7473
--output text --region ${AWS_DEFAULT_REGION}
7574
)
75+
if [ -z $ECS_CLUSTER_NAME ]; then
76+
echo "Could not find the service under the '$ECS_CLUSTER_STACK' stack"
77+
exit 9
78+
fi
7679
}
7780

7881
function getServiceName() {
@@ -81,6 +84,10 @@ function getServiceName() {
8184
--query 'Stacks[0].Outputs[?OutputKey==`Service`].OutputValue' \
8285
--output text --region ${AWS_DEFAULT_REGION}
8386
)
87+
if [ -z $ECS_SERVICE_NAME ]; then
88+
echo "Could not find the service under the '$ECS_SERVICE_STACK' stack"
89+
exit 10
90+
fi
8491
}
8592

8693

@@ -121,99 +128,103 @@ function assertRequiredArgumentsSet() {
121128
fi
122129
if [ -z "${ACCOUNT_ID:-}" ]; then
123130
echo "ACCOUNT is required. You can pass the value using -a or --account"
124-
exit 6
131+
exit 7
132+
fi
133+
if [ -z "${CROSS_ACCOUNT_ROLE:-}" ]; then
134+
echo "ROLE_NAME is required. You can pass the value using -ar or --assume-role-name"
135+
exit 8
125136
fi
126137

127138
}
128139

129140
######################################################
130141
# When not being tested, run application as expected #
131142
######################################################
132-
set -o errexit
133-
set -o pipefail
134-
set -u
135-
set -e
136-
137-
# If no args are provided, display usage information
138-
if [ $# == 0 ]; then usage; fi
139-
140-
# Check for AWS, AWS Command Line Interface
141-
require aws
142-
# Check for jq, Command-line JSON processor
143-
require jq
144-
145-
# Loop through arguments, two at a time for key and value
146-
while [[ $# -gt 0 ]]
147-
do
148-
key="$1"
149-
150-
case $key in
151-
-c|--cluster-name)
152-
ECS_CLUSTER_STACK="$2"
153-
shift # past argument
154-
;;
155-
-s|--service-name)
156-
ECS_SERVICE_STACK="$2"
157-
shift # past argument
158-
;;
159-
-ar|--assume-role-name)
160-
CROSS_ACCOUNT_ROLE="$2"
161-
shift # past argument
162-
;;
163-
-a|--account)
164-
ACCOUNT_ID="$2"
165-
shift # past argument
166-
;;
167-
-k|--aws-access-key)
168-
AWS_ACCESS_KEY_ID="$2"
169-
shift # past argument
170-
;;
171-
-s|--aws-secret-key)
172-
AWS_SECRET_ACCESS_KEY="$2"
173-
shift # past argument
174-
;;
175-
-r|--region)
176-
AWS_DEFAULT_REGION="$2"
177-
shift # past argument
178-
;;
179-
-v|--verbose)
180-
VERBOSE=true
181-
;;
182-
--version)
183-
echo ${VERSION}
184-
exit 0
185-
;;
186-
*)
187-
echo "Found nothing, will display usage and exit"
188-
usage
189-
exit 2
190-
;;
191-
esac
192-
shift # past argument or value
193-
done
194-
195-
if [ $VERBOSE == true ]; then
196-
set -x
197-
fi
143+
if [ -z "$TEST_ENV" ]; then
144+
set -o errexit
145+
set -o pipefail
146+
set -u
147+
set -e
148+
# If no args are provided, display usage information
149+
if [ $# == 0 ]; then usage; fi
150+
151+
# Check for AWS, AWS Command Line Interface
152+
require aws
153+
# Check for jq, Command-line JSON processor
154+
require jq
155+
156+
# Loop through arguments, two at a time for key and value
157+
while [[ $# -gt 0 ]]
158+
do
159+
key="$1"
160+
161+
case $key in
162+
-c|--cluster-name)
163+
ECS_CLUSTER_STACK="$2"
164+
shift # past argument
165+
;;
166+
-s|--service-name)
167+
ECS_SERVICE_STACK="$2"
168+
shift # past argument
169+
;;
170+
-ar|--assume-role-name)
171+
CROSS_ACCOUNT_ROLE="$2"
172+
shift # past argument
173+
;;
174+
-a|--account)
175+
ACCOUNT_ID="$2"
176+
shift # past argument
177+
;;
178+
-k|--aws-access-key)
179+
AWS_ACCESS_KEY_ID="$2"
180+
shift # past argument
181+
;;
182+
-s|--aws-secret-key)
183+
AWS_SECRET_ACCESS_KEY="$2"
184+
shift # past argument
185+
;;
186+
-r|--region)
187+
AWS_DEFAULT_REGION="$2"
188+
shift # past argument
189+
;;
190+
-v|--verbose)
191+
VERBOSE=true
192+
;;
193+
--version)
194+
echo ${VERSION}
195+
exit 0
196+
;;
197+
*)
198+
echo "Found nothing, will display usage and exit"
199+
usage
200+
exit 2
201+
;;
202+
esac
203+
shift # past argument or value
204+
done
198205

199-
# Check that required arguments are provided
200-
assertRequiredArgumentsSet
206+
if [ $VERBOSE == true ]; then
207+
set -x
208+
fi
201209

202-
if [[ "$AWS_ASSUME_ROLE" != false ]]; then
203-
assumeRole
204-
fi
210+
# Check that required arguments are provided
211+
assertRequiredArgumentsSet
205212

206-
getClusterName
207-
getServiceName
208-
updateService
213+
if [[ "$AWS_ASSUME_ROLE" != false ]]; then
214+
assumeRole
215+
fi
209216

217+
getClusterName
218+
getServiceName
219+
updateService
210220

211-
if [[ "$AWS_ASSUME_ROLE" != false ]]; then
212-
assumeRoleClean
213-
fi
214221

215-
exit 0
222+
if [[ "$AWS_ASSUME_ROLE" != false ]]; then
223+
assumeRoleClean
224+
fi
216225

226+
exit 0
227+
fi
217228
#############################
218229
# End application run logic #
219230
#############################

test.bats

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bats
2+
3+
#BATS_ERROR_STACK_TRACE=()
4+
5+
if [ -z $CIRCLE_SHA1 ]; then
6+
setup() {
7+
# Source ecs-cf-deploy
8+
TEST_ENV=true
9+
. ecs-cf-deploy
10+
}
11+
@test "check that usage() returns string and exits with status code 3" {
12+
run usage
13+
echo $status
14+
[ $status -eq 3 ]
15+
}
16+
@test "test assertRequiredArgumentsSet success" {
17+
ECS_SERVICE_STACK=true
18+
ECS_CLUSTER_STACK=false
19+
ACCOUNT_ID=false
20+
CROSS_ACCOUNT_ROLE=false
21+
run assertRequiredArgumentsSet
22+
[ ! -z $status ]
23+
}
24+
@test "test assertRequiredArgumentsSet missing ECS_SERVICE_STACK (status=5)" {
25+
run assertRequiredArgumentsSet
26+
[ $status -eq 5 ]
27+
}
28+
@test "test assertRequiredArgumentsSet missing ECS_CLUSTER_STACK (status=6)" {
29+
ECS_SERVICE_STACK=example
30+
run assertRequiredArgumentsSet
31+
[ $status -eq 6 ]
32+
}
33+
@test "test assertRequiredArgumentsSet missing ACCOUNT_ID (status=7)" {
34+
ECS_SERVICE_STACK=example
35+
ECS_CLUSTER_STACK=example
36+
run assertRequiredArgumentsSet
37+
[ $status -eq 7 ]
38+
}
39+
@test "test assertRequiredArgumentsSet missing CROSS_ACCOUNT_ROLE (status=8)" {
40+
ECS_SERVICE_STACK=true
41+
ECS_CLUSTER_STACK=false
42+
ACCOUNT_ID=false
43+
run assertRequiredArgumentsSet
44+
[ $status -eq 8 ]
45+
}
46+
@test "test require missing command (status=4)" {
47+
run require notexistingcommand
48+
[ $status -eq 4 ]
49+
}
50+
@test "test require sh (status=0)" {
51+
run require sh
52+
[ $status -eq 0 ]
53+
}
54+
fi
55+
@test "test call ecs-cf-deploy without args (status=255)" {
56+
run ./ecs-cf-deploy
57+
echo $output
58+
[ $status -eq 4 ]
59+
}
60+
@test "test call ecs-cf-deploy without credentials (status=255)" {
61+
run ./ecs-cf-deploy -s example-api -c example-cluster -a 12345677 -ar assumeRole
62+
[ $status -eq 255 ]
63+
}
64+
@test "test call ecs-cf-deploy without cluster (status=255)" {
65+
run ./ecs-cf-deploy -s example-api -a 12345677 -ar assumeRole
66+
[ $status -eq 6 ] && [ $output == "CLUSTER is required. You can pass the value using -c or --cluster" ]
67+
}

0 commit comments

Comments
 (0)