Skip to content

Commit 7ac2478

Browse files
committed
feat(tests): alb tests
1 parent 487381a commit 7ac2478

File tree

1 file changed

+371
-0
lines changed

1 file changed

+371
-0
lines changed

tests/alb_unit_tests.tftest.hcl

Lines changed: 371 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,371 @@
1+
################################################################################
2+
# Load Balancer
3+
################################################################################
4+
5+
run "lb_attributes_match" {
6+
command = plan
7+
8+
module {
9+
source = "./modules/alb"
10+
}
11+
12+
variables {
13+
name = "example-name"
14+
internal = true
15+
subnets_ids = ["subnet-1234567890123", "subnet-1234567890124"]
16+
security_groups_ids = ["sg-123456789012345", "sg-123456789012346"]
17+
preserve_host_header = true
18+
enable_deletion_protection = true
19+
20+
listeners = {}
21+
22+
tags = {
23+
Example = "Tag"
24+
}
25+
}
26+
27+
assert {
28+
condition = aws_lb.this.name == var.name
29+
error_message = "Names mismatch"
30+
}
31+
32+
assert {
33+
condition = aws_lb.this.internal == var.internal
34+
error_message = "Internal mismatch"
35+
}
36+
37+
assert {
38+
condition = tolist(aws_lb.this.subnets) == var.subnets_ids
39+
error_message = "Subnets mismatch"
40+
}
41+
assert {
42+
condition = tolist(aws_lb.this.security_groups) == var.security_groups_ids
43+
error_message = "Security groups ids mismatch"
44+
}
45+
46+
assert {
47+
condition = aws_lb.this.preserve_host_header == var.preserve_host_header
48+
error_message = "Preserve host header mismatch"
49+
}
50+
51+
assert {
52+
condition = aws_lb.this.enable_deletion_protection == var.enable_deletion_protection
53+
error_message = "Enable deletion protection mismatch"
54+
}
55+
56+
assert {
57+
condition = aws_lb.this.tags == var.tags
58+
error_message = "Tags mismatch"
59+
}
60+
}
61+
62+
################################################################################
63+
# Load Balancer Target Group
64+
################################################################################
65+
66+
run "lb_target_group_attributes_match" {
67+
command = plan
68+
69+
module {
70+
source = "./modules/alb"
71+
}
72+
73+
variables {
74+
name = "example-name"
75+
internal = true
76+
subnets_ids = ["subnet-1234567890123", "subnet-1234567890124"]
77+
security_groups_ids = ["sg-123456789012345", "sg-123456789012346"]
78+
preserve_host_header = true
79+
enable_deletion_protection = true
80+
81+
target_groups = {
82+
this = {
83+
name = "example-name"
84+
vpc_id = "vpc-0123456789abcdef0"
85+
port = 1234
86+
protocol = "HTTPS"
87+
target_type = "ip"
88+
89+
health_check = {
90+
enabled = true
91+
healthy_threshold = 7
92+
interval = 180
93+
matcher = "200,299"
94+
path = "/example/path"
95+
port = 1234
96+
protocol = "HTTPS"
97+
timeout = 70
98+
unhealthy_threshold = 6
99+
}
100+
}
101+
}
102+
103+
listeners = {}
104+
105+
tags = {
106+
Example = "Tag"
107+
}
108+
}
109+
110+
assert {
111+
condition = aws_lb_target_group.this["this"].name == var.target_groups["this"].name
112+
error_message = "Name mismatch"
113+
}
114+
115+
assert {
116+
condition = aws_lb_target_group.this["this"].vpc_id == var.target_groups["this"].vpc_id
117+
error_message = "VPC ID mismatch"
118+
}
119+
120+
assert {
121+
condition = aws_lb_target_group.this["this"].port == var.target_groups["this"].port
122+
error_message = "Port mismatch"
123+
}
124+
125+
assert {
126+
condition = aws_lb_target_group.this["this"].protocol == var.target_groups["this"].protocol
127+
error_message = "Protocol mismatch"
128+
}
129+
130+
assert {
131+
condition = aws_lb_target_group.this["this"].target_type == var.target_groups["this"].target_type
132+
error_message = "Target type mismatch"
133+
}
134+
135+
assert {
136+
condition = aws_lb_target_group.this["this"].health_check[0].enabled == var.target_groups["this"].health_check.enabled
137+
error_message = "Health check enabled mismatch"
138+
}
139+
140+
assert {
141+
condition = aws_lb_target_group.this["this"].health_check[0].healthy_threshold == var.target_groups["this"].health_check.healthy_threshold
142+
error_message = "Healthy threshold mismatch"
143+
}
144+
145+
assert {
146+
condition = aws_lb_target_group.this["this"].health_check[0].interval == var.target_groups["this"].health_check.interval
147+
error_message = "Interval mismatch"
148+
}
149+
150+
assert {
151+
condition = aws_lb_target_group.this["this"].health_check[0].matcher == var.target_groups["this"].health_check.matcher
152+
error_message = "Matcher mismatch"
153+
}
154+
155+
assert {
156+
condition = aws_lb_target_group.this["this"].health_check[0].path == var.target_groups["this"].health_check.path
157+
error_message = "Path mismatch"
158+
}
159+
160+
assert {
161+
condition = tonumber(aws_lb_target_group.this["this"].health_check[0].port) == var.target_groups["this"].health_check.port
162+
error_message = "Port mismatch"
163+
}
164+
165+
assert {
166+
condition = aws_lb_target_group.this["this"].health_check[0].protocol == var.target_groups["this"].health_check.protocol
167+
error_message = "Protocol mismatch"
168+
}
169+
170+
assert {
171+
condition = aws_lb_target_group.this["this"].health_check[0].timeout == var.target_groups["this"].health_check.timeout
172+
error_message = "Timeout mismatch"
173+
}
174+
175+
assert {
176+
condition = aws_lb_target_group.this["this"].health_check[0].unhealthy_threshold == var.target_groups["this"].health_check.unhealthy_threshold
177+
error_message = "Unhealthy threshold mismatch"
178+
}
179+
}
180+
181+
################################################################################
182+
# Load Balancer Listener
183+
################################################################################
184+
185+
run "lb_listener_attributes_match" {
186+
command = plan
187+
188+
module {
189+
source = "./modules/alb"
190+
}
191+
192+
variables {
193+
name = "example-name"
194+
internal = true
195+
subnets_ids = ["subnet-1234567890123", "subnet-1234567890124"]
196+
security_groups_ids = ["sg-123456789012345", "sg-123456789012346"]
197+
preserve_host_header = true
198+
enable_deletion_protection = true
199+
200+
target_groups = {
201+
this = {
202+
port = 1234
203+
protocol = "HTTPS"
204+
target_type = "ip"
205+
vpc_id = "vpc-0123456789abcdef0"
206+
}
207+
}
208+
209+
listeners = {
210+
this = {
211+
alpn_policy = "HTTP2Preferred"
212+
certificate_arn = "arn:aws:acm:us-west-2:123456789012:certificate/12345678-1234-1234-1234-123456789012"
213+
port = 1234
214+
protocol = "HTTPS"
215+
ssl_policy = "ExampleSSLPolicy"
216+
217+
default_action = [
218+
{
219+
type = "forward"
220+
target_group = "this"
221+
}
222+
]
223+
}
224+
}
225+
226+
tags = {
227+
Example = "Tag"
228+
}
229+
}
230+
231+
assert {
232+
condition = aws_lb_listener.this["this"].alpn_policy == var.listeners["this"].alpn_policy
233+
error_message = "ALPN policy mismatch"
234+
}
235+
236+
assert {
237+
condition = aws_lb_listener.this["this"].certificate_arn == var.listeners["this"].certificate_arn
238+
error_message = "Certificate ARN mismatch"
239+
}
240+
241+
assert {
242+
condition = aws_lb_listener.this["this"].port == var.listeners["this"].port
243+
error_message = "Port mismatch"
244+
}
245+
246+
assert {
247+
condition = aws_lb_listener.this["this"].protocol == var.listeners["this"].protocol
248+
error_message = "Protocol mismatch"
249+
}
250+
251+
assert {
252+
condition = aws_lb_listener.this["this"].ssl_policy == var.listeners["this"].ssl_policy
253+
error_message = "SSL Policy mismatch"
254+
}
255+
}
256+
257+
run "lb_listener_default_action_attributes_match" {
258+
command = plan
259+
260+
module {
261+
source = "./modules/alb"
262+
}
263+
264+
variables {
265+
name = "example-name"
266+
internal = true
267+
subnets_ids = ["subnet-1234567890123", "subnet-1234567890124"]
268+
security_groups_ids = ["sg-123456789012345", "sg-123456789012346"]
269+
preserve_host_header = true
270+
enable_deletion_protection = true
271+
272+
target_groups = {
273+
this = {
274+
port = 1234
275+
protocol = "HTTPS"
276+
target_type = "ip"
277+
vpc_id = "vpc-0123456789abcdef0"
278+
}
279+
}
280+
281+
listeners = {
282+
this = {
283+
alpn_policy = "HTTP2Preferred"
284+
certificate_arn = "arn:aws:acm:us-west-2:123456789012:certificate/12345678-1234-1234-1234-123456789012"
285+
port = 1234
286+
protocol = "HTTPS"
287+
ssl_policy = "ExampleSSLPolicy"
288+
289+
default_action = [
290+
{
291+
type = "forward"
292+
target_group = "this"
293+
order = 1
294+
295+
authenticate_cognito = {
296+
user_pool_arn = "arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_ABCDEFG"
297+
user_pool_client_id = "1234567890abcdefg"
298+
user_pool_domain = "example.auth.us-east-1.amazoncognito.com"
299+
authentication_request_extra_params = {
300+
key = "example_key"
301+
value = "example_value"
302+
}
303+
on_unauthenticated_request = "deny"
304+
scope = "openid profile email"
305+
session_cookie_name = "AWSELBAuthSessionCookie"
306+
session_timeout = 604800
307+
}
308+
authenticate_oidc = {
309+
authorization_endpoint = "https://example.com/authz"
310+
client_id = "dummy_client_id"
311+
client_secret = "dummy_client_secret"
312+
issuer = "https://example.com/issuer"
313+
token_endpoint = "https://example.com/token"
314+
user_info_endpoint = "https://example.com/userinfo"
315+
authentication_request_extra_params = {
316+
example = "param"
317+
}
318+
on_unauthenticated_request = "deny"
319+
scope = "openid profile email"
320+
session_cookie_name = "AWSELBAuthSessionCookie"
321+
session_timeout = 604800
322+
}
323+
fixed_response = {
324+
content_type : "application/json",
325+
message_body : "{\"message\": \"Hello from Load Balancer\"}",
326+
status_code : 200
327+
}
328+
forward = {
329+
target_group = [
330+
{
331+
arn = "tg-123456789012345",
332+
weight = 1
333+
},
334+
{
335+
arn = "tg-123456789012346",
336+
weight = 2
337+
}
338+
]
339+
stickiness = {
340+
duration = 100
341+
enabled = true
342+
}
343+
}
344+
redirect = {
345+
status_code = 302
346+
host = "example.com"
347+
path = "/new-path"
348+
port = 443
349+
protocol = "https"
350+
query = "param1=value1&param2=value2"
351+
}
352+
}
353+
]
354+
}
355+
}
356+
357+
tags = {
358+
Example = "Tag"
359+
}
360+
}
361+
362+
assert {
363+
condition = aws_lb_listener.this["this"].default_action[0].type == var.listeners["this"].default_action[0].type
364+
error_message = "Type mismatch"
365+
}
366+
367+
assert {
368+
condition = aws_lb_listener.this["this"].default_action[0].order == var.listeners["this"].default_action[0].order
369+
error_message = "Order mismatch"
370+
}
371+
}

0 commit comments

Comments
 (0)