-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terraform test: allow computed/mocked values override during planning (…
- Loading branch information
Showing
20 changed files
with
472 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
kind: ENHANCEMENTS | ||
body: '`terraform test`: Test runs now support using mocked or overridden values during unit test runs (e.g., with command = "plan"). When override_during = "plan"' | ||
time: 2025-01-08T11:34:33.709443+01:00 | ||
custom: | ||
Issue: "36227" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
internal/command/testdata/test/mocking-error/child/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
terraform { | ||
required_providers { | ||
test = { | ||
source = "hashicorp/test" | ||
configuration_aliases = [test.primary, test.secondary] | ||
} | ||
} | ||
} | ||
|
||
variable "instances" { | ||
type = number | ||
} | ||
|
||
resource "test_resource" "primary" { | ||
provider = test.primary | ||
count = var.instances | ||
} | ||
|
||
resource "test_resource" "secondary" { | ||
provider = test.secondary | ||
count = var.instances | ||
} | ||
|
||
output "primary" { | ||
value = test_resource.primary | ||
} | ||
|
||
output "secondary" { | ||
value = test_resource.secondary | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
terraform { | ||
required_providers { | ||
test = { | ||
source = "hashicorp/test" | ||
} | ||
} | ||
} | ||
|
||
provider "test" { | ||
alias = "primary" | ||
} | ||
|
||
provider "test" { | ||
alias = "secondary" | ||
} | ||
|
||
variable "instances" { | ||
type = number | ||
} | ||
|
||
variable "child_instances" { | ||
type = number | ||
} | ||
|
||
resource "test_resource" "primary" { | ||
provider = test.primary | ||
count = var.instances | ||
} | ||
|
||
resource "test_resource" "secondary" { | ||
provider = test.secondary | ||
count = var.instances | ||
} | ||
|
||
module "child" { | ||
count = var.instances | ||
|
||
source = "./child" | ||
|
||
providers = { | ||
test.primary = test.primary | ||
test.secondary = test.secondary | ||
} | ||
|
||
instances = var.child_instances | ||
} |
34 changes: 34 additions & 0 deletions
34
internal/command/testdata/test/mocking-error/tests/plan_mocked_overridden.tftest.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
mock_provider "test" { | ||
alias = "primary" | ||
|
||
mock_resource "test_resource" { | ||
defaults = { | ||
id = "aaaa" | ||
} | ||
} | ||
|
||
override_resource { | ||
target = test_resource.primary | ||
values = { | ||
id = "bbbb" | ||
} | ||
} | ||
} | ||
|
||
variables { | ||
instances = 1 | ||
child_instances = 1 | ||
} | ||
|
||
// This test will fail because the plan command does not use the | ||
// overridden values for computed properties, | ||
// making the left-hand side of the condition unknown. | ||
run "test" { | ||
command = plan | ||
|
||
assert { | ||
condition = test_resource.primary[0].id == "bbbb" | ||
error_message = "plan should not have the overridden value" | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
internal/command/testdata/test/mocking-error/tests/plan_mocked_provider.tftest.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
mock_provider "test" { | ||
alias = "secondary" | ||
|
||
mock_resource "test_resource" { | ||
defaults = { | ||
id = "ffff" | ||
} | ||
} | ||
} | ||
|
||
|
||
variables { | ||
instances = 2 | ||
child_instances = 1 | ||
} | ||
|
||
run "test" { | ||
command = plan | ||
|
||
assert { | ||
condition = test_resource.secondary[0].id == "ffff" | ||
error_message = "plan should use the mocked provider value when override_during is plan" | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
.../command/testdata/test/mocking-invalid/tests/override_computed_invalid_boolean.tftest.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
mock_provider "test" { | ||
alias = "primary" | ||
override_during = baz // This should either be plan or apply, therefore this test should fail | ||
|
||
mock_resource "test_resource" { | ||
defaults = { | ||
id = "aaaa" | ||
} | ||
} | ||
|
||
override_resource { | ||
target = test_resource.primary | ||
values = { | ||
id = "bbbb" | ||
} | ||
} | ||
} | ||
|
||
variables { | ||
instances = 1 | ||
child_instances = 1 | ||
} | ||
|
||
run "test" { | ||
|
||
assert { | ||
condition = test_resource.primary[0].id == "bbbb" | ||
error_message = "mock not applied" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
internal/command/testdata/test/mocking/tests/plan_mocked_overridden.tftest.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
mock_provider "test" { | ||
alias = "primary" | ||
|
||
mock_resource "test_resource" { | ||
defaults = { | ||
id = "aaaa" | ||
} | ||
} | ||
|
||
override_resource { | ||
target = test_resource.primary | ||
override_during = plan | ||
values = { | ||
id = "bbbb" | ||
} | ||
} | ||
} | ||
|
||
variables { | ||
instances = 1 | ||
child_instances = 1 | ||
} | ||
|
||
run "test" { | ||
command = plan | ||
|
||
assert { | ||
condition = test_resource.primary[0].id == "bbbb" | ||
error_message = "plan should override the value when override_during is plan" | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
internal/command/testdata/test/mocking/tests/plan_mocked_provider.tftest.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
mock_provider "test" { | ||
alias = "secondary" | ||
override_during = plan | ||
|
||
mock_resource "test_resource" { | ||
defaults = { | ||
id = "ffff" | ||
} | ||
} | ||
} | ||
|
||
|
||
variables { | ||
instances = 2 | ||
child_instances = 1 | ||
} | ||
|
||
run "test" { | ||
command = plan | ||
|
||
assert { | ||
condition = test_resource.secondary[0].id == "ffff" | ||
error_message = "plan should use the mocked provider value when override_during is plan" | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
internal/command/testdata/test/mocking/tests/plan_mocked_provider_overridden.tftest.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
mock_provider "test" { | ||
alias = "primary" | ||
override_during = plan | ||
|
||
mock_resource "test_resource" { | ||
defaults = { | ||
id = "aaaa" | ||
} | ||
} | ||
|
||
override_resource { | ||
target = test_resource.primary | ||
values = { | ||
id = "bbbb" | ||
} | ||
} | ||
|
||
override_resource { | ||
target = test_resource.primary[1] | ||
override_during = apply // this should take precedence over the provider-level override_during | ||
values = { | ||
id = "bbbb" | ||
} | ||
} | ||
} | ||
|
||
|
||
override_resource { | ||
target = test_resource.secondary[0] | ||
override_during = plan | ||
values = { | ||
id = "ssss" | ||
} | ||
} | ||
|
||
|
||
variables { | ||
instances = 2 | ||
child_instances = 1 | ||
} | ||
|
||
run "test" { | ||
command = plan | ||
|
||
assert { | ||
condition = test_resource.primary[0].id == "bbbb" | ||
error_message = "plan should override the value when override_during is plan" | ||
} | ||
|
||
assert { | ||
condition = test_resource.secondary[0].id == "ssss" | ||
error_message = "plan should override the value when override_during is plan" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.