Skip to content

Commit 977fd8b

Browse files
committed
tests: add functional tests for integrated hooks
1 parent 313aba7 commit 977fd8b

4 files changed

Lines changed: 138 additions & 2 deletions

File tree

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
"moto~=1.3.7",
2929
"testfixtures~=4.10.0",
3030
"flake8-future-import",
31+
"stacker_blueprints",
32+
"awscli"
3133
]
3234

3335
scripts = [

stacker/tests/factories.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def __init__(self, outputs=None, region=None, profile=None):
3939
"Outputs": stack_outputs,
4040
"StackStatus": "CREATED"
4141
}
42-
self._sessions = {}
4342

4443
def get_stack(self, stack_name, **kwargs):
4544
try:

tests/test_helper.bash

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ assert() {
3636

3737
# Checks that the given line is in $output.
3838
assert_has_line() {
39-
echo "$output" | grep "$@" 1>/dev/null
39+
echo "$output" | grep -q "$@"
40+
}
41+
42+
assert_has_lines_in_order() {
43+
local search_line
44+
read -r search_line || return $?
45+
46+
for line in "${lines[@]}"; do
47+
if grep -q "$@" "$search_line" <<< "$line"; then
48+
if ! read -r search_line && [ -z "$search_line" ]; then
49+
return 0
50+
fi
51+
fi
52+
done
53+
54+
echo "Error: did not match line in correct order: '$search_line'" >&2
55+
return 1
4056
}
4157

4258
# This helper wraps "stacker" with bats' "run" and also outputs debug
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env bats
2+
3+
# This test will exercise the integration of hooks among the execution of stacks
4+
# making use of the fact that S3 buckets cannot be deleted when not empty.
5+
# The test will create the bucket and populate it during build, and erase the
6+
# objects before destruction. If the hooks are not executed in the proper order,
7+
# the destruction will fail, and so will the tst.
8+
9+
load ../test_helper
10+
11+
@test "stacker build - integrated hooks" {
12+
needs_aws
13+
14+
config() {
15+
echo "namespace: ${STACKER_NAMESPACE}"
16+
cat <<'EOF'
17+
stacks:
18+
- name: buckets
19+
profile: stacker
20+
class_path: stacker_blueprints.s3.Buckets
21+
variables:
22+
Buckets:
23+
StackerTestBucket:
24+
BucketName: "stacker-${envvar STACKER_NAMESPACE}-integrated-hooks-${awsparam AccountId}"
25+
26+
build_hooks:
27+
- name: write-hello
28+
path: stacker.hooks.command.run_command
29+
args:
30+
command: 'echo "Hello from Stacker!" > /tmp/hello.txt'
31+
shell: true
32+
33+
- name: send-hello
34+
path: stacker.hooks.command.run_command
35+
requires:
36+
- write-hello
37+
args:
38+
command: 'aws s3 cp /tmp/hello.txt "s3://$BUCKET/hello.txt"'
39+
shell: true
40+
env:
41+
BUCKET: "${output buckets::StackerTestBucketBucketId}"
42+
AWS_PROFILE: stacker
43+
44+
- name: send-world
45+
path: stacker.hooks.command.run_command
46+
requires:
47+
- send-hello
48+
args:
49+
command: 'aws s3 cp "s3://$BUCKET/hello.txt" "s3://$BUCKET/world.txt"'
50+
shell: true
51+
env:
52+
BUCKET: "${output buckets::StackerTestBucketBucketId}"
53+
AWS_PROFILE: stacker
54+
55+
destroy_hooks:
56+
- name: remove-world
57+
path: stacker.hooks.command.run_command
58+
args:
59+
command: 'aws s3 rm "s3://$BUCKET/world.txt"'
60+
shell: true
61+
env:
62+
BUCKET: "${output buckets::StackerTestBucketBucketId}"
63+
AWS_PROFILE: stacker
64+
65+
- name: remove-hello
66+
path: stacker.hooks.command.run_command
67+
required_by:
68+
- remove-world
69+
args:
70+
command: 'aws s3 rm "s3://$BUCKET/hello.txt"'
71+
shell: true
72+
env:
73+
BUCKET: "${output buckets::StackerTestBucketBucketId}"
74+
AWS_PROFILE: stacker
75+
76+
- name: clean-hello
77+
path: stacker.hooks.command.run_command
78+
required_by:
79+
- buckets
80+
args:
81+
command: [rm, /tmp/hello.txt]
82+
EOF
83+
}
84+
85+
teardown() {
86+
stacker destroy --force <(config)
87+
}
88+
89+
stacker build <(config)
90+
assert "$status" -eq 0
91+
assert_has_line "Using default AWS provider mode"
92+
assert_has_lines_in_order -E <<'EOF'
93+
pre_build_hooks: complete
94+
write-hello: complete
95+
buckets: submitted \(creating new stack\)
96+
buckets: complete \(creating new stack\)
97+
upload: [^ ]*/hello.txt to s3://[^ ]*/hello.txt
98+
send-hello: complete
99+
copy: s3://[^ ]*/hello.txt to s3://[^ ]*/world.txt
100+
send-world: complete
101+
post_build_hooks: complete
102+
EOF
103+
104+
stacker destroy --force <(config)
105+
assert "$status" -eq 0
106+
assert_has_line "Using default AWS provider mode"
107+
assert_has_lines_in_order -E <<'EOF'
108+
pre_destroy_hooks: complete
109+
delete: s3://[^ ]*/world.txt
110+
remove-world: complete
111+
delete: s3://[^ ]*/hello.txt
112+
remove-hello: complete
113+
buckets: submitted \(submitted for destruction\)
114+
buckets: complete \(stack destroyed\)
115+
clean-hello: complete
116+
post_destroy_hooks: complete
117+
EOF
118+
assert ! -e /tmp/hello.txt
119+
}

0 commit comments

Comments
 (0)