Skip to content

Commit

Permalink
update the test for badreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Jan 16, 2025
1 parent 45efd11 commit 614c682
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
8 changes: 3 additions & 5 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func (runner *TestFileRunner) Test(file *moduletest.File) {
graph, diags := b.Build(addrs.RootModuleInstance)
file.Diagnostics = file.Diagnostics.Append(diags)
if diags.HasErrors() {
file.Status = file.Status.Merge(moduletest.Error)
return
}

Expand Down Expand Up @@ -421,11 +422,8 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
}

key := run.GetStateKey()
config := runner.Suite.Config
config := run.ModuleConfig
if run.Config.ConfigUnderTest != nil {
config = run.Config.ConfigUnderTest
// Then we need to load an alternate state and not the main one.

if key == MainStateIdentifier {
// This is bad. It means somehow the module we're loading has
// the same key as main state and we're about to corrupt things.
Expand Down Expand Up @@ -1069,7 +1067,7 @@ func (runner *TestFileRunner) cleanup(file *moduletest.File) {

var diags tfdiags.Diagnostics

config := runner.Suite.Config
config := state.Run.ModuleConfig
key := state.Run.GetStateKey()

reset, configDiags := configtest.TransformConfigForTest(config, state.Run, file, runner.VariableCaches, runner.PriorOutputs, runner.Suite.configProviders[key])
Expand Down
22 changes: 1 addition & 21 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1560,41 +1560,21 @@ func TestTest_BadReferences(t *testing.T) {
}

expectedOut := `main.tftest.hcl... in progress
run "setup"... pass
run "test"... fail
Warning: Value for undeclared variable
on main.tftest.hcl line 17, in run "test":
17: input_three = run.madeup.response
The module under test does not declare a variable named "input_three", but it
is declared in run block "test".
run "finalise"... skip
main.tftest.hcl... tearing down
main.tftest.hcl... fail
providers.tftest.hcl... in progress
run "test"... fail
providers.tftest.hcl... tearing down
providers.tftest.hcl... fail
Failure! 1 passed, 2 failed, 1 skipped.
Failure! 0 passed, 1 failed.
`
actualOut := output.Stdout()
if diff := cmp.Diff(actualOut, expectedOut); len(diff) > 0 {
t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expectedOut, actualOut, diff)
}

expectedErr := `
Error: Reference to unavailable variable
on main.tftest.hcl line 15, in run "test":
15: input_one = var.notreal
The input variable "notreal" is not available to the current run block. You
can only reference variables defined at the file or global levels.
Error: Reference to unavailable run block
on main.tftest.hcl line 16, in run "test":
Expand Down
30 changes: 28 additions & 2 deletions internal/terraformtest/transform_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"fmt"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/backend/backendrun"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/dag"
"github.com/hashicorp/terraform/internal/lang/langrefs"
"github.com/hashicorp/terraform/internal/moduletest"
"github.com/hashicorp/terraform/internal/terraform"
"github.com/hashicorp/terraform/internal/tfdiags"
)

// TestRunTransformer is a GraphTransformer that adds all the test runs,
Expand Down Expand Up @@ -73,10 +75,13 @@ func (t *TestRunTransformer) createNodes(g *terraform.Graph) ([]*NodeTestRun, er
func (t *TestRunTransformer) connectDependencies(g *terraform.Graph, nodes []*NodeTestRun) error {
var errs []error
nodeMap := make(map[string]*NodeTestRun)
// add all nodes to the map. They are initialized to nil,
// and we will update them as we iterate through the nodes in the next loop.
for _, node := range nodes {
nodeMap[node.run.Name] = node
nodeMap[node.run.Name] = nil
}
for _, node := range nodes {
nodeMap[node.run.Name] = node // node encountered, so update the map
refs, err := getRefs(node.run)
if err != nil {
return err
Expand All @@ -91,10 +96,31 @@ func (t *TestRunTransformer) connectDependencies(g *terraform.Graph, nodes []*No
continue
}
dependency, ok := nodeMap[runName]
diagPrefix := "You can only reference run blocks that are in the same test file and will execute before the current run block."
// Then this is a made up run block, and it doesn't exist at all.
if !ok {
errs = append(errs, fmt.Errorf("dependency `run.%s` not found for run %q", runName, node.run.Name))
diags := tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Reference to unknown run block",
Detail: fmt.Sprintf("The run block %q does not exist within this test file. %s", runName, diagPrefix),
Subject: ref.SourceRange.ToHCL().Ptr(),
})
errs = append(errs, tfdiags.NonFatalError{Diagnostics: diags})
continue
}

// This run block exists, but it is after the current run block.
if dependency == nil {
diags := tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Reference to unavailable run block",
Detail: fmt.Sprintf("The run block %q has not executed yet. %s", runName, diagPrefix),
Subject: ref.SourceRange.ToHCL().Ptr(),
})
errs = append(errs, tfdiags.NonFatalError{Diagnostics: diags})
continue
}

g.Connect(dag.BasicEdge(node, dependency))
}
}
Expand Down

0 comments on commit 614c682

Please sign in to comment.