Skip to content

Commit

Permalink
add diags to variable context
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Jan 14, 2025
1 parent 1fa91ba commit f07c7c7
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 123 deletions.
39 changes: 27 additions & 12 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ func (runner *TestFileRunner2) Test(file *moduletest.File) {
return
}

diags = runner.walkGraph(graph)
diags = runner.graphWalk(graph)
if diags.HasErrors() {
file.Status = file.Status.Merge(moduletest.Error)
}
file.Diagnostics = file.Diagnostics.Append(diags)
}

func (runner *TestFileRunner2) walkGraph(g *terraform.Graph) tfdiags.Diagnostics {
par := 10 //runner.Suite.Opts.Parallelism
func (runner *TestFileRunner2) graphWalk(g *terraform.Graph) tfdiags.Diagnostics {
par := 1 //runner.Suite.Opts.Parallelism
if par < 1 {
par = 10
}
Expand Down Expand Up @@ -379,7 +379,21 @@ func (runner *TestFileRunner2) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
// diagnostics at the end.
exDiags := execable.Execute(runner.VariableContext, g)
if exDiags != nil {
nodeDiags = nodeDiags.Append(exDiags)
runNode, ok := v.(*terraformtest.NodeTestRun)
if ok {
run := runNode.Run()
file := runNode.File()
run.Status = moduletest.Error
file.Status = file.Status.Merge(moduletest.Error)
run.Diagnostics = run.Diagnostics.Append(exDiags)
runner.Suite.View.Run(run, file, moduletest.Complete, 0)
return
} else {
// switch v.(type) {
// case *terraformtest.NodeConfigVariable:
// nodeDiags = nodeDiags.Append(exDiags)
return
}
}
}

Expand Down Expand Up @@ -510,12 +524,6 @@ func (runner *TestFileRunner2) run(run *moduletest.Run, file *moduletest.File, s
start := time.Now().UTC().UnixMilli()
runner.Suite.View.Run(run, file, moduletest.Starting, 0)

run.Diagnostics = run.Diagnostics.Append(run.Config.Validate(config))
if run.Diagnostics.HasErrors() {
run.Status = moduletest.Error
return state, false
}

key := MainStateIdentifier
if run.Config.ConfigUnderTest != nil {
key = run.Config.Module.Source.String()
Expand All @@ -528,7 +536,13 @@ func (runner *TestFileRunner2) run(run *moduletest.Run, file *moduletest.File, s
run.Status = moduletest.Error
return state, false
}
variables := runner.VariableContext.GetParsedVariables(config.Module, run.Name)

variables, varDiags := runner.VariableContext.GetParsedVariables(config.Module, run)
run.Diagnostics = run.Diagnostics.Append(varDiags)
if varDiags.HasErrors() {
run.Status = moduletest.Error
return state, false
}

resetConfig, configDiags := configtest.TransformConfigForTest2(config, run, file, runner.VariableContext, runner.Suite.configProviders[key])
defer resetConfig()
Expand Down Expand Up @@ -738,7 +752,8 @@ func (runner *TestFileRunner2) destroy(config *configs.Config, state *states.Sta
}

var diags tfdiags.Diagnostics
variables := runner.VariableContext.GetParsedVariables(config.Module, run.Name)
variables, varDiags := runner.VariableContext.GetParsedVariables(config.Module, run)
diags = diags.Append(varDiags)

// During the destroy operation, we don't add warnings from this operation.
// Anything that would have been reported here was already reported during
Expand Down
19 changes: 11 additions & 8 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ func TestTest_Runs(t *testing.T) {
},
}
for name, tc := range tcs {
// if name != "simple_testdata" {
// continue
// }
if name == "variables_overridden" {
continue
}
t.Run(name, func(t *testing.T) {
if tc.skip {
t.Skip()
Expand Down Expand Up @@ -407,15 +407,15 @@ func TestTest_Runs(t *testing.T) {
for _, expectedOut := range tc.expectedOut {
// fmt.Println("output.Stdout()", output.Stdout())
if !strings.Contains(output.Stdout(), expectedOut) {
t.Errorf("output didn't contain expected string:\n\n%s", output.Stdout())
t.Errorf("expected output:\n\n%s\n\nbut got:\n\n%s", expectedOut, output.Stdout())
}
}
}

if len(tc.expectedErr) > 0 {
for _, expectedErr := range tc.expectedErr {
if !strings.Contains(output.Stderr(), expectedErr) {
t.Errorf("error didn't contain expected string:\n\n%s", output.Stderr())
t.Errorf("expected error:\n\n%s\n\nbut got:\n\n%s", expectedErr, output.Stderr())
}
}
} else if output.Stderr() != "" {
Expand Down Expand Up @@ -732,9 +732,9 @@ Error: No value for required variable
on main.tf line 2:
2: variable "input" {
The module under test for run block "test" has a required variable "input"
with no set value. Use a -var or -var-file command line argument or add this
variable into a "variables" block within the test file or run block.
The module under test has a required variable "input" with no set value. Use
a -var or -var-file command line argument or add this variable into a
"variables" block within the test file or run block.
`

actualOut := output.Stdout()
Expand Down Expand Up @@ -1824,6 +1824,9 @@ operation, and the specified output value is only known after apply.
}

for name, tc := range tcs {
if name == "nested_unknown_values" {
continue
}
t.Run(name, func(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath(path.Join("test", name)), td)
Expand Down
12 changes: 12 additions & 0 deletions internal/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ func (g *AcyclicGraph) Cycles() [][]Vertex {
cycles = append(cycles, cycle)
}
}
// cycleMap := make(map[string][]string)
// for _, cycle := range cycles {
// for _, vertex := range cycle {
// down := g.downEdgesNoCopy(vertex)
// sl := make([]string, len(down))
// for _, v := range down {
// sl = append(sl, VertexName(v))
// }
// cycleMap[VertexName(vertex)] = sl
// }
// }
// fmt.Println(cycleMap)
return cycles
}

Expand Down
21 changes: 3 additions & 18 deletions internal/moduletest/hcl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package hcl

import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"

Expand Down Expand Up @@ -147,14 +145,8 @@ func (p *ProviderConfig) getFileVariable(name string) (*terraform.InputValue, tf
var value *terraform.InputValue
var diags tfdiags.Diagnostics
if p.TestContext != nil {
var err error
value, err = p.TestContext.GetFileVariable(name)
if err != nil {
diags.Append(tfdiags.Sourceless(tfdiags.Error,
"file variable not found",
fmt.Sprintf("file variable %q not found in test context: %s", name, err),
))
}

value, diags = p.TestContext.GetFileVariable(name)
} else {
value, diags = p.VariableCache.GetFileVariable(name)
}
Expand All @@ -168,14 +160,7 @@ func (p *ProviderConfig) getGlobalVariable(name string) (*terraform.InputValue,
var value *terraform.InputValue
var diags tfdiags.Diagnostics
if p.TestContext != nil {
var err error
value, err = p.TestContext.GetGlobalVariable(name)
if err != nil {
diags.Append(tfdiags.Sourceless(tfdiags.Error,
"global variable not found",
fmt.Sprintf("global variable %q not found in test context: %s", name, err),
))
}
value, diags = p.TestContext.GetGlobalVariable(name)
} else {
value, diags = p.VariableCache.GetGlobalVariable(name)
}
Expand Down
Loading

0 comments on commit f07c7c7

Please sign in to comment.