Skip to content

Commit

Permalink
feat: add state_alias for test run blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kevineor committed Dec 10, 2024
1 parent ce80353 commit 3a17ab1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
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"},
},
}

Expand Down

0 comments on commit 3a17ab1

Please sign in to comment.