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 1 commit
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
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.Options.StateAlias != "" {
key = run.Config.Options.StateAlias
}

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_alias": {
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,18 @@
run "old_version" {
plan_options {
state_alias = "test1"
}
}

run "new_code" {
module {
source = "./breaking_change"
}
plan_options {
state_alias = "test1"
}
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_alias/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
}
18 changes: 18 additions & 0 deletions internal/command/testdata/test/with_state_alias/moved.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
run "old_version" {
module {
source = "./old_version"
}
plan_options {
state_alias = "test1"
}
}

run "new_code" {
plan_options {
state_alias = "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 @@ -275,6 +275,8 @@ type TestRunOptions struct {
// Refresh is analogous to the -refresh=false Terraform plan option.
Refresh bool

StateAlias string

// Replace is analogous to the -replace=ADDRESS Terraform plan option.
Replace []hcl.Traversal

Expand Down Expand Up @@ -760,6 +762,11 @@ func decodeTestRunOptionsBlock(block *hcl.Block) (*TestRunOptions, hcl.Diagnosti
opts.Target = tars
}

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

if !opts.Refresh && opts.Mode == RefreshOnlyTestMode {
// These options are incompatible.
diags = append(diags, &hcl.Diagnostic{
Expand Down Expand Up @@ -839,6 +846,7 @@ var testRunOptionsBlockSchema = &hcl.BodySchema{
{Name: "refresh"},
{Name: "replace"},
{Name: "target"},
{Name: "state_alias"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be state_key or even just state rather than state_alias. Alias implies it's another name for something rather than what it's doing which is just overriding the default state key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i'll wait for a definitive decision on your side and i'll apply the change

I agree that it may be confusing. I was also thinking about run_key as mentioning the state seems weird as is never appears with terraform test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I wouldn't put this within the run options block. The run options are a 1:1 mapping to actual run options a user can specify on the command line to the plan command, and overriding the state is not something that can be done this way. I think putting it here confuses things.

I think we can actually just have it at the root level of the run block. It's metadata about the run block rather than something that changes the behaviour of the plan/apply operation itself.

},
}

Expand Down
Loading