-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest-cfn.py
More file actions
executable file
·278 lines (244 loc) · 10.1 KB
/
test-cfn.py
File metadata and controls
executable file
·278 lines (244 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
"""Test all CloudFormation templates: validate, deploy, verify, delete.
Usage: python3 test-cfn.py [--parallel N] [--skip-deploy] [--region REGION]
"""
import argparse
import boto3
import json
import os
import sys
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from pathlib import Path
PREREQ_STACKS = {
"tutorial-prereqs-bucket": "cfn/prereq-bucket.yaml",
"tutorial-prereqs-vpc-public": "cfn/prereq-vpc-public.yaml",
"tutorial-prereqs-vpc-private": "cfn/prereq-vpc-private.yaml",
}
@dataclass
class TestResult:
template: str
validate: str = "SKIP"
deploy: str = "SKIP"
delete: str = "SKIP"
duration: float = 0
error: str = ""
stack_name: str = ""
def find_templates(repo_root):
"""Find all cfn-*.yaml templates in tutorial directories."""
templates = {}
for p in sorted(Path(repo_root, "tuts").glob("*/cfn-*.yaml")):
tut = p.parent.name
templates[tut] = str(p)
return templates
def detect_prereqs(template_path):
"""Check which prerequisite stacks a template needs."""
content = Path(template_path).read_text()
needed = []
if "prereqs-bucket" in content or "prereq-bucket" in content:
needed.append("tutorial-prereqs-bucket")
if "prereqs-vpc-public" in content or "prereq-vpc-public" in content:
needed.append("tutorial-prereqs-vpc-public")
if "prereqs-vpc-private" in content or "prereq-vpc-private" in content:
needed.append("tutorial-prereqs-vpc-private")
return needed
def needs_iam(template_path):
content = Path(template_path).read_text()
if "RoleName" in content or "PolicyName" in content:
return "CAPABILITY_NAMED_IAM"
if "AWS::IAM::" in content:
return "CAPABILITY_IAM"
return None
def validate_template(cfn, template_path):
body = Path(template_path).read_text()
cfn.validate_template(TemplateBody=body)
def deploy_stack(cfn, stack_name, template_path, timeout=600):
body = Path(template_path).read_text()
caps = []
cap = needs_iam(template_path)
if cap:
caps = [cap]
try:
cfn.create_stack(
StackName=stack_name,
TemplateBody=body,
Capabilities=caps,
Tags=[{"Key": "test-run", "Value": "cfn-test"}],
TimeoutInMinutes=10,
OnFailure="DELETE",
)
except cfn.exceptions.AlreadyExistsException:
cfn.delete_stack(StackName=stack_name)
waiter = cfn.get_waiter("stack_delete_complete")
waiter.wait(StackName=stack_name, WaiterConfig={"Delay": 10, "MaxAttempts": 60})
cfn.create_stack(
StackName=stack_name,
TemplateBody=body,
Capabilities=caps,
Tags=[{"Key": "test-run", "Value": "cfn-test"}],
TimeoutInMinutes=10,
OnFailure="DELETE",
)
waiter = cfn.get_waiter("stack_create_complete")
waiter.wait(StackName=stack_name, WaiterConfig={"Delay": 15, "MaxAttempts": int(timeout / 15)})
def delete_stack(cfn, stack_name, timeout=300):
cfn.delete_stack(StackName=stack_name)
waiter = cfn.get_waiter("stack_delete_complete")
waiter.wait(StackName=stack_name, WaiterConfig={"Delay": 10, "MaxAttempts": int(timeout / 10)})
def ensure_prereqs(cfn, repo_root, needed_stacks):
"""Deploy prerequisite stacks if they don't exist. Returns set of failed prereqs."""
failed = set()
for stack_name in needed_stacks:
try:
resp = cfn.describe_stacks(StackName=stack_name)
status = resp["Stacks"][0]["StackStatus"]
if status in ("CREATE_COMPLETE", "UPDATE_COMPLETE"):
print(f" Prereq {stack_name}: exists ({status})")
continue
elif "ROLLBACK" in status or "FAILED" in status:
print(f" Prereq {stack_name}: cleaning up failed stack...")
cfn.delete_stack(StackName=stack_name)
cfn.get_waiter("stack_delete_complete").wait(
StackName=stack_name, WaiterConfig={"Delay": 10, "MaxAttempts": 30})
except cfn.exceptions.ClientError:
pass
template_file = PREREQ_STACKS.get(stack_name)
if not template_file:
failed.add(stack_name)
continue
template_path = os.path.join(repo_root, template_file)
print(f" Prereq {stack_name}: deploying...")
try:
caps = []
if needs_iam(template_path):
caps = [needs_iam(template_path)]
body = Path(template_path).read_text()
cfn.create_stack(
StackName=stack_name, TemplateBody=body, Capabilities=caps,
Tags=[{"Key": "test-run", "Value": "cfn-test"}],
TimeoutInMinutes=10, OnFailure="DELETE",
)
waiter = cfn.get_waiter("stack_create_complete")
waiter.wait(StackName=stack_name, WaiterConfig={"Delay": 15, "MaxAttempts": 40})
print(f" Prereq {stack_name}: ready")
except Exception as e:
print(f" Prereq {stack_name}: CFN deploy failed ({e})")
print(f" Prereq {stack_name}: Run ./cfn/setup-bucket.sh first, then retry.")
failed.add(stack_name)
try:
cfn.delete_stack(StackName=stack_name)
except Exception:
pass
return failed
def test_template(cfn, tut_name, template_path, skip_deploy):
"""Test a single template: validate, deploy, delete."""
result = TestResult(template=tut_name)
stack_name = f"cfn-test-{tut_name[:40]}"
result.stack_name = stack_name
start = time.time()
# Validate
try:
validate_template(cfn, template_path)
result.validate = "PASS"
except Exception as e:
result.validate = "FAIL"
result.error = str(e)[:200]
result.duration = time.time() - start
return result
if skip_deploy:
result.duration = time.time() - start
return result
# Deploy
try:
deploy_stack(cfn, stack_name, template_path)
result.deploy = "PASS"
except Exception as e:
result.deploy = "FAIL"
result.error = str(e)[:200]
result.duration = time.time() - start
# Try cleanup
try:
delete_stack(cfn, stack_name)
except Exception:
pass
return result
# Delete
try:
delete_stack(cfn, stack_name)
result.delete = "PASS"
except Exception as e:
result.delete = "FAIL"
result.error = f"Delete failed: {str(e)[:150]}"
result.duration = time.time() - start
return result
def main():
parser = argparse.ArgumentParser(description="Test CloudFormation templates")
parser.add_argument("--parallel", type=int, default=3, help="Max parallel deployments")
parser.add_argument("--skip-deploy", action="store_true", help="Validate only, don't deploy")
parser.add_argument("--region", default="us-east-1")
parser.add_argument("--repo", default=".", help="Repo root directory")
args = parser.parse_args()
repo_root = os.path.abspath(args.repo)
cfn = boto3.client("cloudformation", region_name=args.region)
# Find templates
templates = find_templates(repo_root)
print(f"Found {len(templates)} templates")
if not args.skip_deploy:
# Collect all needed prereqs
all_prereqs = set()
template_prereqs = {}
for tut, path in templates.items():
prereqs = detect_prereqs(path)
template_prereqs[tut] = prereqs
all_prereqs.update(prereqs)
failed_prereqs = set()
if all_prereqs:
print(f"\nDeploying prerequisites: {', '.join(sorted(all_prereqs))}")
failed_prereqs = ensure_prereqs(cfn, repo_root, sorted(all_prereqs))
if failed_prereqs:
print(f"\nFailed prereqs: {', '.join(failed_prereqs)}")
# Test templates in parallel
print(f"\nTesting {len(templates)} templates (parallel={args.parallel})...\n")
results = []
with ThreadPoolExecutor(max_workers=args.parallel) as pool:
futures = {}
for tut, path in templates.items():
# Skip if prereqs failed
if not args.skip_deploy:
missing = set(template_prereqs.get(tut, [])) & failed_prereqs
if missing:
r = TestResult(template=tut, validate="PASS", deploy="SKIP", error=f"Prereq failed: {', '.join(missing)}")
results.append(r)
print(f" ⊘ {tut}: skipped (prereq failed)")
continue
# Each thread gets its own client
thread_cfn = boto3.client("cloudformation", region_name=args.region)
future = pool.submit(test_template, thread_cfn, tut, path, args.skip_deploy)
futures[future] = tut
for future in as_completed(futures):
tut = futures[future]
result = future.result()
results.append(result)
status = "✓" if result.deploy in ("PASS", "SKIP") and result.validate == "PASS" else "✗"
print(f" {status} {result.template}: validate={result.validate} deploy={result.deploy} delete={result.delete} ({result.duration:.0f}s)")
if result.error:
print(f" Error: {result.error}")
# Report
results.sort(key=lambda r: r.template)
passed = sum(1 for r in results if r.validate == "PASS" and r.deploy in ("PASS", "SKIP"))
failed = len(results) - passed
print(f"\n{'='*70}")
print(f"RESULTS: {passed} passed, {failed} failed, {len(results)} total")
print(f"{'='*70}")
print(f"{'Template':<45} {'Validate':<10} {'Deploy':<10} {'Delete':<10} {'Time':<8}")
print(f"{'-'*45} {'-'*10} {'-'*10} {'-'*10} {'-'*8}")
for r in results:
print(f"{r.template:<45} {r.validate:<10} {r.deploy:<10} {r.delete:<10} {r.duration:<8.0f}s")
# Cleanup prereqs if all tests passed
if not args.skip_deploy and failed == 0:
print(f"\nAll tests passed. Prerequisite stacks left running for reuse.")
print(f"To delete: python3 {sys.argv[0]} --cleanup-prereqs")
return 1 if failed > 0 else 0
if __name__ == "__main__":
sys.exit(main())