Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update minimum TF version; add tests #6

Merged
merged 48 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
f52ceb6
Implement testing
KyleKotowick Mar 18, 2023
4b33701
Update CICD.yml
KyleKotowick Mar 18, 2023
4de4243
Update CICD.yml
KyleKotowick Mar 26, 2023
37b908d
Update CICD.yml
KyleKotowick Mar 26, 2023
19304ac
Update CICD.yml
KyleKotowick Mar 26, 2023
7acac4d
Update CICD.yml
KyleKotowick Mar 26, 2023
5d2c673
Testing
KyleKotowick Mar 26, 2023
24846b4
Update CICD.yml
KyleKotowick Mar 26, 2023
a8c60f9
Update CICD.yml
KyleKotowick Mar 26, 2023
e584f10
Update CICD.yml
KyleKotowick Mar 26, 2023
e984e92
Update CICD.yml
KyleKotowick Mar 26, 2023
fd465e2
Update main.tf
KyleKotowick Mar 26, 2023
f23f1a6
Update CICD.yml
KyleKotowick Mar 26, 2023
5a0a77f
Update CICD
KyleKotowick Mar 26, 2023
100de72
Update CICD.yml
KyleKotowick Mar 26, 2023
00a8319
Refactor to use new variable validation capabilities
KyleKotowick Jul 7, 2024
bdc74f4
Update CI/CD minimum Terraform version
KyleKotowick Jul 7, 2024
f37120b
Update CICD.yml
KyleKotowick Jul 11, 2024
4d01d62
Update CICD.yml
KyleKotowick Jul 11, 2024
4f2561d
Update CICD.yml
KyleKotowick Jul 11, 2024
47726b4
Update CICD.yml
KyleKotowick Jul 11, 2024
17bc1b7
Update CICD.yml
KyleKotowick Jul 11, 2024
32e3442
Update CICD.yml
KyleKotowick Jul 11, 2024
da3d24f
Update CICD.yml
KyleKotowick Jul 11, 2024
7991f18
Update CICD.yml
KyleKotowick Jul 11, 2024
0037be7
Update CICD.yml
KyleKotowick Jul 11, 2024
1ef9c6d
Update CICD.yml
KyleKotowick Jul 11, 2024
40a0160
Update CICD.yml
KyleKotowick Jul 11, 2024
25723ab
Merge branch 'main' into dev
KyleKotowick Jul 11, 2024
b95683e
Update version, add tests for forced output checking
KyleKotowick Dec 25, 2024
57698a8
Add tests for conditions not known until the apply step
KyleKotowick Dec 25, 2024
b3e0caa
Update CICD.yml
KyleKotowick Dec 25, 2024
70c959a
Update CICD.yml
KyleKotowick Dec 25, 2024
47c07cd
Update versions.tf
KyleKotowick Dec 25, 2024
ed70fa8
Update required version
KyleKotowick Dec 25, 2024
e3018e7
Update CICD.yml
KyleKotowick Dec 25, 2024
c0c4704
Remove lock files
KyleKotowick Dec 25, 2024
1b73731
Add delayed message tests
KyleKotowick Dec 25, 2024
f8b6ce9
Update variables.tf
KyleKotowick Dec 25, 2024
b3a28fa
Fix tests
KyleKotowick Dec 25, 2024
281d40a
Testing
KyleKotowick Dec 25, 2024
8cd53c2
Update CICD
KyleKotowick Dec 26, 2024
7f80898
Add stderr check
KyleKotowick Dec 26, 2024
31a69e9
Update CICD.yml
KyleKotowick Dec 26, 2024
23e6e96
Update CICD.yml
KyleKotowick Dec 26, 2024
6ba7bac
Update CICD.yml
KyleKotowick Dec 26, 2024
e4118f7
Delete commented code
KyleKotowick Dec 26, 2024
9e65577
Merge branch 'main' into dev
KyleKotowick Dec 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 101 additions & 6 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ jobs:
steps:
- name: Generate Matrix
id: matrix
uses: Invicton-Labs/terraform-module-testing/matrix@v0.2.0
uses: Invicton-Labs/terraform-module-testing/matrix@v0.3.0
with:
minimum_tf_version: '1.9.0'
minimum_tf_version: '1.10.3'
additional_runners: 'macos-13, windows-2019'

- name: Output Matrix
run: |
Expand All @@ -28,25 +29,119 @@ jobs:
steps:
- name: Initialize - Pass
id: init-pass
uses: Invicton-Labs/terraform-module-testing/initialize@v0.2.0
uses: Invicton-Labs/terraform-module-testing/initialize@v0.3.0
with:
tf_path: tests/pass
- name: Run Tests - Pass
id: tests-pass
uses: Invicton-Labs/terraform-module-testing/apply-destroy@v0.2.0
uses: Invicton-Labs/terraform-module-testing/apply-destroy@v0.3.0
with:
tf_path: tests/pass

- name: Initialize - Pass (With Output)
id: init-pass-output
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-output
- name: Run Tests - Pass (With Output)
id: tests-pass-output
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-output

- name: Initialize - Pass - Condition Delayed
id: init-pass-condition-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-condition-delayed
- name: Run Tests - Pass - Condition Delayed
id: tests-pass-condition-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-condition-delayed

- name: Initialize - Pass - Message Delayed
id: init-pass-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-message-delayed
- name: Run Tests - Pass - Message Delayed
id: tests-pass-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-message-delayed
stderr_contains: Unsuitable value for error message

- name: Initialize - Pass - Condition & Message Delayed
id: init-pass-condition-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-condition-message-delayed
- name: Run Tests - Pass - Condition & Message Delayed
id: tests-pass-condition-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/pass-condition-message-delayed

- name: Initialize - Fail
id: init-fail
uses: Invicton-Labs/terraform-module-testing/initialize@v0.2.0
uses: Invicton-Labs/terraform-module-testing/initialize@v0.3.0
with:
tf_path: tests/fail
- name: Run Tests - Fail
id: tests-fail
uses: Invicton-Labs/terraform-module-testing/apply-failure@v0.2.0
uses: Invicton-Labs/terraform-module-testing/apply-failure@v0.3.0
with:
tf_path: tests/fail
stderr_contains: sample error

- name: Initialize - Fail (Output)
id: init-fail-output
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-output
- name: Run Tests - Fail (Output)
id: tests-fail-output
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-output
stderr_contains: sample error

- name: Initialize - Fail - Condition Delayed
id: init-fail-condition-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-condition-delayed
- name: Run Tests - Fail - Condition Delayed
id: tests-fail-condition-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-condition-delayed
stderr_contains: sample error

- name: Initialize - Fail - Message Delayed
id: init-fail-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-message-delayed
- name: Run Tests - Fail - Message Delayed
id: tests-fail-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-message-delayed
stderr_contains: Unsuitable value for error message

- name: Initialize - Fail - Condition & Message Delayed
id: init-fail-condition-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-condition-message-delayed
- name: Run Tests - Fail - Condition & Message Delayed
id: tests-fail-condition-message-delayed
uses: Invicton-Labs/terraform-module-testing/[email protected]
with:
tf_path: tests/fail-condition-message-delayed
stderr_contains: sample error

# This job just waits for all other jobs to pass. We have it here
# so our branch protection rule can reference a single job, instead
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

/tests/**/.terraform.lock.hcl
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,25 @@ var.error_message is "This Terraform configuration can only be run on Unix-based
// Ignore this part
There is no function named "SEE_ABOVE_ERROR_MESSAGE".
```

## Plan, Apply, and Unknown Values

There are several edge cases regarding conditions that aren't known until apply and error messages that aren't known until apply.


### Conditions not known during planning

When the `condition` variable doesn't have a known value during the plan step, the assertion will not be checked during the plan step. The `error_message` variable will not be used or evaluated.

The `condition` _will_ still be checked during the apply step, and the `error_message` variable will be used if `condition` evaluates to `false`.


### Error messages not known during planning

When the `error_message` variable doesn't have a known value during the plan step, the behaviour will vary depending on whether the `condition` value is known during the plan step.

If `condition` is not known during planning, then the `error_message` will not be evaluated/used until the apply step, and only if `condition` evaluates to `false`.

If the `condition` _is_ known during planning and it evaluates to `false`, then the Terraform operation will fail during the plan step and the output error message will explain that the `error_message` variable value is invalid (not known).

If `condition` evaluates to `true` as soon as it's known, then `error_message` will never be used or evaluated, so it doesn't matter if it's known or not.
6 changes: 6 additions & 0 deletions tests/fail-condition-delayed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "fail" {
source = "../../"
// Use a condition that isn't known until the apply step
condition = uuid() == ""
error_message = "sample error"
}
7 changes: 7 additions & 0 deletions tests/fail-condition-message-delayed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "fail" {
source = "../../"
// Use a condition that isn't known until the apply step
condition = uuid() == ""
// Use a message that isn't known until the apply step
error_message = "sample error: ${uuid()}"
}
6 changes: 6 additions & 0 deletions tests/fail-message-delayed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "fail" {
source = "../../"
condition = false
// Use a message that isn't known until the apply step
error_message = "sample error: ${uuid()}"
}
22 changes: 22 additions & 0 deletions tests/fail-output/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/fail-output/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module "fail" {
source = "../../"
condition = false
error_message = "sample error"
}

// Force the module and conditions to be evaluated
// by using the module output in a config output.
output "result" {
value = module.fail.checked
}
6 changes: 6 additions & 0 deletions tests/pass-condition-delayed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "pass" {
source = "../../"
// Use a condition that isn't known until the apply step
condition = uuid() != ""
error_message = "sample error"
}
7 changes: 7 additions & 0 deletions tests/pass-condition-message-delayed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "pass" {
source = "../../"
// Use a condition that isn't known until the apply step
condition = uuid() != ""
// Use a message that isn't known until the apply step
error_message = "sample error: ${uuid()}"
}
6 changes: 6 additions & 0 deletions tests/pass-message-delayed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "pass" {
source = "../../"
condition = true
// Use a message that isn't known until the apply step
error_message = "sample error: ${uuid()}"
}
22 changes: 22 additions & 0 deletions tests/pass-output/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/pass-output/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module "pass" {
source = "../../"
condition = true
error_message = "sample error"
}

// Force the module and conditions to be evaluated
// by using the module output in a config output.
output "result" {
value = module.pass.checked
}
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ variable "condition" {
// until var.error_message is known. Otherwise, it can fail during the validation
// phase but won't output the proper error message.
// https://github.com/hashicorp/terraform/issues/35397
condition = var.error_message != null && var.condition == true
condition = var.error_message == "" ? var.condition : var.condition
error_message = var.error_message
}
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
terraform {
required_version = ">= 1.9.0"
required_version = ">= 1.10.3"
}
Loading