Skip to content

Commit 8c5931f

Browse files
committed
chore: resolved the each reference and added unit tests
1 parent 61d935b commit 8c5931f

File tree

2 files changed

+105
-19
lines changed

2 files changed

+105
-19
lines changed

modules/budgets/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ resource "aws_budgets_budget" "this" {
5252
for_each = each.value.notifications
5353

5454
content {
55-
comparison_operator = notification.comparison_operator
56-
notification_type = notification.notification_type
55+
comparison_operator = notification.value.comparison_operator
56+
notification_type = notification.value.notification_type
5757
subscriber_email_addresses = var.notifications.email != null ? var.notifications.email.addresses : null
5858
subscriber_sns_topic_arns = var.notifications.sns != null ? [var.notifications.sns.topic_arn] : null
59-
threshold = notification.threshold
60-
threshold_type = notification.threshold_type
59+
threshold = notification.value.threshold
60+
threshold_type = notification.value.threshold_type
6161
}
6262
}
6363
}

tests/module.tftest.hcl

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mock_provider "aws" {
22
mock_data "aws_region" {
33
defaults = {
4-
name = "us-west-2"
5-
current_region = "us-west-2"
4+
name = "eu-west-2"
5+
current_region = "eu-west-2"
66
}
77
}
88

@@ -21,7 +21,7 @@ mock_provider "aws" {
2121
}
2222
}
2323

24-
run "basic_account_budget" {
24+
run "budgets" {
2525
command = plan
2626

2727
module {
@@ -33,8 +33,8 @@ run "basic_account_budget" {
3333
email = {
3434
addresses = ["[email protected]"]
3535
}
36-
slack = {
37-
webhook_url = "https://my-dev-alerts.slack.com"
36+
sns = {
37+
topic_arn = "arn:aws:sns:us-west-2:123456789012:my-topic"
3838
}
3939
}
4040
budgets = [
@@ -45,11 +45,19 @@ run "basic_account_budget" {
4545
limit_unit = "PERCENTAGE"
4646
time_unit = "MONTHLY"
4747

48-
notification = {
49-
comparison_operator = "LESS_THAN"
50-
threshold = "100"
51-
threshold_type = "PERCENTAGE"
52-
notification_type = "ACTUAL"
48+
notifications = {
49+
actual = {
50+
comparison_operator = "LESS_THAN"
51+
threshold = "100"
52+
threshold_type = "PERCENTAGE"
53+
notification_type = "ACTUAL"
54+
}
55+
forecasted = {
56+
comparison_operator = "LESS_THAN"
57+
threshold = "100"
58+
threshold_type = "PERCENTAGE"
59+
notification_type = "FORECASTED"
60+
}
5361
}
5462
},
5563
]
@@ -73,12 +81,90 @@ run "basic_account_budget" {
7381
condition = contains(["DAILY", "MONTHLY", "QUARTERLY", "ANNUALLY"], var.budgets.0.time_unit)
7482
error_message = "Not a valid budge time unit"
7583
}
76-
}
7784

78-
run "basic_team_budget" {
79-
command = plan
85+
## Should create a budget for each budget in the budgets array
86+
assert {
87+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"] != null
88+
error_message = "Budget should be created"
89+
}
8090

81-
module {
82-
source = "./modules/team-budgets"
91+
assert {
92+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].name == "AWS Savings Plan Coverage Budget"
93+
error_message = "Budget name should be AWS Savings Plan Coverage Budget"
94+
}
95+
96+
assert {
97+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].budget_type == "SAVINGS_PLANS_COVERAGE"
98+
error_message = "Budget type should be SAVINGS_PLANS_COVERAGE"
99+
}
100+
101+
assert {
102+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].limit_amount == "100.0"
103+
error_message = "Budget limit amount should be 100.0"
104+
}
105+
106+
assert {
107+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].limit_unit == "PERCENTAGE"
108+
error_message = "Budget limit unit should be PERCENTAGE"
109+
}
110+
111+
assert {
112+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].time_unit == "MONTHLY"
113+
error_message = "Budget time unit should be MONTHLY"
114+
}
115+
116+
assert {
117+
condition = length(aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].notification) == 2
118+
error_message = "Budget notifications should be correct"
119+
}
120+
121+
assert {
122+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_credit == false
123+
error_message = "Budget cost types should be correct"
124+
}
125+
126+
assert {
127+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_discount == false
128+
error_message = "Budget cost types should be correct"
129+
}
130+
131+
assert {
132+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_recurring == false
133+
error_message = "Budget cost types should be correct"
134+
}
135+
136+
assert {
137+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_refund == false
138+
error_message = "Budget cost types should be correct"
139+
}
140+
141+
assert {
142+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_subscription == true
143+
error_message = "Budget cost types should be correct"
144+
}
145+
146+
assert {
147+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_support == false
148+
error_message = "Budget cost types should be correct"
149+
}
150+
151+
assert {
152+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_tax == false
153+
error_message = "Budget cost types should be correct"
154+
}
155+
156+
assert {
157+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.include_upfront == false
158+
error_message = "Budget cost types should be correct"
159+
}
160+
161+
assert {
162+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.use_amortized == null
163+
error_message = "Budget cost types should be correct"
164+
}
165+
166+
assert {
167+
condition = aws_budgets_budget.this["AWS Savings Plan Coverage Budget"].cost_types.0.use_blended == false
168+
error_message = "Budget cost types should be correct"
83169
}
84170
}

0 commit comments

Comments
 (0)