Skip to content

Commit ef2531a

Browse files
committed
adds unit tests for sub modules
1 parent b9b20f2 commit ef2531a

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tests/unit_tests.tftest.hcl

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Attribute validations for s3 sub module
2+
run "s3_bucket_name_match" {
3+
command = plan
4+
5+
variables {
6+
name = "unique-s3-bucket-name"
7+
tags = {}
8+
9+
expected_s3_bucket_name = "unique-s3-bucket-name"
10+
}
11+
12+
module {
13+
source = "./modules/s3"
14+
}
15+
16+
assert {
17+
condition = aws_s3_bucket.this.bucket == var.expected_s3_bucket_name
18+
error_message = "Bucket name mismatch after creation"
19+
}
20+
}
21+
22+
run "s3_bucket_tags_match" {
23+
command = plan
24+
25+
variables {
26+
name = "unique-s3-bucket-name"
27+
tags = {
28+
"some_key_name" = "some_value"
29+
"another_key_name" = "another_value"
30+
}
31+
}
32+
33+
module {
34+
source = "./modules/s3"
35+
}
36+
37+
assert {
38+
condition = aws_s3_bucket.this.tags["some_key_name"] == "some_value"
39+
error_message = "Tag 'some_key_name' with value 'some_value' is missing or incorrect"
40+
}
41+
42+
assert {
43+
condition = aws_s3_bucket.this.tags["another_key_name"] == "another_value"
44+
error_message = "Tag 'another_key_name' with value 'another_value' is missing or incorrect"
45+
}
46+
}
47+
48+
# Attribute validations for dynamodb sub module
49+
run "dynamodb_table_name_match" {
50+
command = plan
51+
52+
variables {
53+
name = "dynamodb-table-name"
54+
55+
expected_table_name = "dynamodb-table-name"
56+
}
57+
58+
module {
59+
source = "./modules/dynamodb_table"
60+
}
61+
62+
assert {
63+
condition = aws_dynamodb_table.this.name == var.expected_table_name
64+
error_message = "Table name mismatch after creation"
65+
}
66+
}
67+
68+
run "dynamodb_table_write_capacity_match" {
69+
command = plan
70+
71+
variables {
72+
name = "dynamodb-table-name"
73+
write_capacity = 21
74+
75+
expected_write_capacity = 21
76+
}
77+
78+
module {
79+
source = "./modules/dynamodb_table"
80+
}
81+
82+
assert {
83+
condition = aws_dynamodb_table.this.write_capacity == var.expected_write_capacity
84+
error_message = "Write capacity mismatch after creation"
85+
}
86+
}
87+
88+
run "dynamodb_table_read_capacity_match" {
89+
command = plan
90+
91+
variables {
92+
name = "dynamodb-table-name"
93+
read_capacity = 21
94+
95+
expected_read_capacity = 21
96+
}
97+
98+
module {
99+
source = "./modules/dynamodb_table"
100+
}
101+
102+
assert {
103+
condition = aws_dynamodb_table.this.read_capacity == var.expected_read_capacity
104+
error_message = "Write capacity mismatch after creation"
105+
}
106+
}
107+
108+
run "dynamodb_table_tags_match" {
109+
command = plan
110+
111+
variables {
112+
name = "dynamodb-table-name"
113+
tags = {
114+
"some_key_name" = "some_value"
115+
"another_key_name" = "another_value"
116+
}
117+
}
118+
119+
module {
120+
source = "./modules/dynamodb_table"
121+
}
122+
123+
assert {
124+
condition = aws_dynamodb_table.this.tags["some_key_name"] == "some_value"
125+
error_message = "Tag 'some_key_name' with value 'some_value' is missing or incorrect"
126+
}
127+
128+
assert {
129+
condition = aws_dynamodb_table.this.tags["another_key_name"] == "another_value"
130+
error_message = "Tag 'another_key_name' with value 'another_value' is missing or incorrect"
131+
}
132+
}

0 commit comments

Comments
 (0)