-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest-aws.ngs
executable file
·121 lines (115 loc) · 3.26 KB
/
test-aws.ngs
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
#!/usr/bin/env ngs
ns(Igw=AWS::Igw, SecGroup=AWS::SecGroup, Subnet=AWS::Subnet, Vpc=AWS::Vpc, zones=AWS::zones, Elb=AWS::Elb) {
# TODO: test for all resources and all operations
# TODO: test tags with spaces
# --- settings ---
cidr = ENV.get('NGS_AWS_TEST_CIDR', "192.168.11.0/24")
subnet_cidrs = [
ENV.get('NGS_AWS_TEST_CIDR1', "192.168.11.0/25")
ENV.get('NGS_AWS_TEST_CIDR2', "192.168.11.128/25")
]
name_tag = {"Name": "ngs-test"}
# --- vpc ---
test("Create VPC with Name tag 'ngs-test' and CIDR $cidr") with {
Vpc(name_tag).converge(CidrBlock=cidr)
}
test("Update tags on VPCs with Name tag 'ngs-test'") with {
Vpc(name_tag).converge(Tags = name_tag + {'k1': 'v1'})
}
test("Find exactly one VPC with 'Name' tag 'ngs-test' and 'k1' tag 'v1'") with {
Vpc({"Name": "ngs-test", "k1": "v1"}).expect(1)
}
test("Find exactly one VPC with CIDR $cidr") with {
Vpc(CidrBlock=cidr).expect(1)
}
# --- subnets ---
for(i;subnet_cidrs.len()) {
test("Create Subnet ${i+1}") with {
vpc = Vpc(CidrBlock=cidr).expect(1)
Subnet(VpcId=vpc, CidrBlock=subnet_cidrs[i], AvailabilityZone=zones()[i]).converge()
}
}
test("Add Name tag to Subnets") with {
vpc = Vpc({"Name": "ngs-test"}).expect(1)
Subnet(VpcId=vpc).converge(Tags={'Name': 'ngs-test'})
}
# --- security groups ---
vpc = Vpc({"Name": "ngs-test"}).expect(1)
test("Create security group") with {
SecGroup("ngs-test-elb", vpc).converge(
Description="NGS test ELB",
IpPermissions= [
{
'IpProtocol': 'tcp'
'FromPort': 443
'ToPort': 443
'IpRanges': [
{
'CidrIp': '0.0.0.0/0'
}
]
}
]
)
}
test("Add Name tag to ELB SG") with {
SecGroup("ngs-test-elb", vpc).converge(Tags=name_tag)
}
# --- Internet Gateway ---
test("Add Internet Gateway") with {
# Not tagging it, it's uncommon practice
Igw(Attachments=[{'VpcId': vpc}]).converge()
}
test("Add Name tag to Gateway, remove attachment") with {
Igw(Attachments=[{'VpcId': vpc}]).converge(Tags=name_tag, Attachments=[])
}
test("Re-attach gateway") with {
Igw(name_tag).converge(Attachments=[{'VpcId': vpc}])
}
# --- ELB ---
test("Add ELB") with {
elb = Elb("ngs-test-elb").converge(
Tags = name_tag,
ListenerDescriptions = [
%{
Protocol TCP
LoadBalancerPort 443
InstanceProtocol TCP
InstancePort 443
}
],
Subnets = AWS::Subnet(name_tag).expect(2),
SecurityGroups = SecGroup("ngs-test-elb", vpc),
HealthCheck = %{
UnhealthyThreshold 5
Timeout 5
HealthyThreshold 3
Interval 10
Target 'SSL:443'
}
)
}
# 'Instances': AWS::Instance({'env': env, 'role': role}).expect()
# --- cleanup ---
if not(ENV.get('NGS_AWS_TEST_SKIP_DESTROY', false)) {
test("Delete ELB") with {
Elb("ngs-test-elb").delete()
}
test("Delete all security groups with tag $name_tag") with {
SecGroup(name_tag).delete()
}
test("Delete all subnets with tag $name_tag") with {
Subnet(name_tag).delete()
}
test("Detach from all VPCs all internet-gateways with tag $name_tag") with {
Igw(name_tag).converge(Attachments=[])
}
test("Delete all internet-gateways with tag $name_tag") with {
Igw(name_tag).delete()
}
test("Delete all VPCs with tag $name_tag") with {
Vpc(name_tag).delete()
}
}
echo("All tests passed.")
}