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

feat: add state_key for test run blocks #36185

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ENHANCEMENTS:

- `init`: Provider installation will utilise credentials configured in a `.netrc` file for the download and shasum URLs returned by provider registries. ([https://github.com/hashicorp/terraform/pull/35843](35843))
- The `terraform test` command now supports a `state_key` attribute in `run` blocks. This feature allows multiple Terraform test runs to target the same infrastructure.

EXPERIMENTS:

Expand Down
14 changes: 9 additions & 5 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,16 @@ func (runner *TestFileRunner) Test(file *moduletest.File) {
file.Status = moduletest.Error
continue // Abort!
}
}

if _, exists := runner.RelevantStates[key]; !exists {
runner.RelevantStates[key] = &TestFileState{
Run: nil,
State: states.NewState(),
}
if run.Config.StateKey != "" {
key = run.Config.StateKey
}

if _, exists := runner.RelevantStates[key]; !exists {
runner.RelevantStates[key] = &TestFileState{
Run: nil,
State: states.NewState(),
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ func TestTest_Runs(t *testing.T) {
expectedErr: []string{"Ephemeral resource instance has expired", "Ephemeral resources cannot be asserted"},
code: 1,
},
"with_state_key": {
expectedOut: []string{"3 passed, 1 failed."},
expectedErr: []string{"Test assertion failed", "resource renamed without moved block"},
code: 1,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
run "old_version" {
state_key = "test1"
}

run "new_code" {
state_key = "test1"
module {
source = "./breaking_change"
}
assert {
condition = test_resource.renamed_without_move.id == run.old_version.test_id
error_message = "resource renamed without moved block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "test_resource" "renamed_without_move" {
value = "test"
}

output "test_id" {
value = test_resource.renamed_without_move.id
}
11 changes: 11 additions & 0 deletions internal/command/testdata/test/with_state_key/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "test_resource" "test_id_moved" {
}

output "test_id" {
value = test_resource.test_id_moved.id
}

moved {
from = test_resource.test_id
to = test_resource.test_id_moved
}
14 changes: 14 additions & 0 deletions internal/command/testdata/test/with_state_key/moved.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
run "old_version" {
state_key = "test1"
module {
source = "./old_version"
}
}

run "new_code" {
state_key = "test1"
assert {
condition = test_resource.test_id_moved.id == run.old_version.test_id
error_message = "ressource_id differed"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "test_resource" "test_id" {
value = "test"
}

output "test_id" {
value = test_resource.test_id.id
}
8 changes: 8 additions & 0 deletions internal/configs/test_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type TestRun struct {
// run.
ExpectFailures []hcl.Traversal

StateKey string

NameDeclRange hcl.Range
VariablesDeclRange hcl.Range
DeclRange hcl.Range
Expand Down Expand Up @@ -606,6 +608,11 @@ func decodeTestRunBlock(block *hcl.Block) (*TestRun, hcl.Diagnostics) {
r.ExpectFailures = failures
}

if attr, exists := content.Attributes["state_key"]; exists {
rawDiags := gohcl.DecodeExpression(attr.Expr, nil, &r.StateKey)
diags = append(diags, rawDiags...)
}

return &r, diags
}

Expand Down Expand Up @@ -807,6 +814,7 @@ var testRunBlockSchema = &hcl.BodySchema{
{Name: "command"},
{Name: "providers"},
{Name: "expect_failures"},
{Name: "state_key"},
},
Blocks: []hcl.BlockHeaderSchema{
{
Expand Down
1 change: 1 addition & 0 deletions website/docs/language/tests/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Each `run` block has the following fields and blocks:
| [`providers`](#providers) | An optional `providers` attribute. | |
| [`assert`](#assertions) | Optional `assert` blocks. | |
| `expect_failures` | An optional attribute. | |
| `state_key` | An optional attribute, which can be used to test multiple run blocks on the same infrastructure | |

The `command` attribute and `plan_options` block tell Terraform which command and options to execute for each run block. The default operation, if you do not specify a `command` attribute or the `plan_options` block, is a normal Terraform apply operation.

Expand Down