From 3258744166e8ae3d78a56b934ec7c64c8a034264 Mon Sep 17 00:00:00 2001 From: MicahKimel Date: Thu, 23 May 2024 04:32:39 -0400 Subject: [PATCH] Remove invalid warning during cleanup phase (#35172) * stop invalid warning during cleanup phase * stop invalid warning during cleanup phase * invalid warning during cleanup phase * remove unwanted comment * update GetVariables parameter for including warnings * invalid warning during cleanup phase test case * update invalid warnings in cleanup test case to throw a warning for the validation variable before cleanup phase * remove unwanted warnings --- internal/backend/local/test.go | 28 +++---- internal/command/test_test.go | 78 +++++++++++++++++++ .../test/invalid-cleanup-warnings/main.tf | 8 ++ .../invalid-cleanup-warnings/main.tftest.hcl | 13 ++++ 4 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 internal/command/testdata/test/invalid-cleanup-warnings/main.tf create mode 100644 internal/command/testdata/test/invalid-cleanup-warnings/main.tftest.hcl diff --git a/internal/backend/local/test.go b/internal/backend/local/test.go index aff0f90229..4027c9fec3 100644 --- a/internal/backend/local/test.go +++ b/internal/backend/local/test.go @@ -431,7 +431,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st return state, false } - variables, variableDiags := runner.GetVariables(config, run, references) + variables, variableDiags := runner.GetVariables(config, run, references, true) run.Diagnostics = run.Diagnostics.Append(variableDiags) if variableDiags.HasErrors() { run.Status = moduletest.Error @@ -544,12 +544,6 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st resetVariables := runner.AddVariablesToConfig(config, variables) defer resetVariables() - run.Diagnostics = run.Diagnostics.Append(variableDiags) - if variableDiags.HasErrors() { - run.Status = moduletest.Error - return updated, true - } - if runner.Suite.Verbose { schemas, diags := tfCtx.Schemas(config, updated) @@ -637,7 +631,7 @@ func (runner *TestFileRunner) destroy(config *configs.Config, state *states.Stat var diags tfdiags.Diagnostics - variables, variableDiags := runner.GetVariables(config, run, nil) + variables, variableDiags := runner.GetVariables(config, run, nil, false) diags = diags.Append(variableDiags) if diags.HasErrors() { @@ -1004,7 +998,7 @@ func (runner *TestFileRunner) cleanup(file *moduletest.File) { // more variables than are required by the config. FilterVariablesToConfig // should be called before trying to use these variables within a Terraform // plan, apply, or destroy operation. -func (runner *TestFileRunner) GetVariables(config *configs.Config, run *moduletest.Run, references []*addrs.Reference) (terraform.InputValues, tfdiags.Diagnostics) { +func (runner *TestFileRunner) GetVariables(config *configs.Config, run *moduletest.Run, references []*addrs.Reference, includeWarnings bool) (terraform.InputValues, tfdiags.Diagnostics) { var diags tfdiags.Diagnostics // relevantVariables contains the variables that are of interest to this @@ -1071,13 +1065,15 @@ func (runner *TestFileRunner) GetVariables(config *configs.Config, run *modulete // wrote in the variable expression. But, we don't want to actually use // it if it's not actually relevant. if _, exists := relevantVariables[name]; !exists { - diags = diags.Append(&hcl.Diagnostic{ - Severity: hcl.DiagWarning, - Summary: "Value for undeclared variable", - Detail: fmt.Sprintf("The module under test does not declare a variable named %q, but it is declared in run block %q.", name, run.Name), - Subject: expr.Range().Ptr(), - }) - + // Do not display warnings during cleanup phase + if includeWarnings { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagWarning, + Summary: "Value for undeclared variable", + Detail: fmt.Sprintf("The module under test does not declare a variable named %q, but it is declared in run block %q.", name, run.Name), + Subject: expr.Range().Ptr(), + }) + } continue // Don't add it to our final set of variables. } diff --git a/internal/command/test_test.go b/internal/command/test_test.go index 7ac4f9655e..a81f9554bc 100644 --- a/internal/command/test_test.go +++ b/internal/command/test_test.go @@ -1270,6 +1270,84 @@ Success! 2 passed, 0 failed. } } +// There should not be warnings in clean-up +func TestTest_InvalidWarningsInCleanup(t *testing.T) { + td := t.TempDir() + testCopyDir(t, testFixturePath(path.Join("test", "invalid-cleanup-warnings")), td) + defer testChdir(t, td)() + + provider := testing_command.NewProvider(nil) + providerSource, close := newMockProviderSource(t, map[string][]string{ + "test": {"1.0.0"}, + }) + defer close() + + streams, done := terminal.StreamsForTesting(t) + view := views.NewView(streams) + ui := new(cli.MockUi) + + meta := Meta{ + testingOverrides: metaOverridesForProvider(provider.Provider), + Ui: ui, + View: view, + Streams: streams, + ProviderSource: providerSource, + } + + init := &InitCommand{ + Meta: meta, + } + + output := done(t) + + if code := init.Run(nil); code != 0 { + t.Fatalf("expected status code 0 but got %d: %s", code, output.All()) + } + + // Reset the streams for the next command. + streams, done = terminal.StreamsForTesting(t) + meta.Streams = streams + meta.View = views.NewView(streams) + + c := &TestCommand{ + Meta: meta, + } + + code := c.Run([]string{"-no-color"}) + output = done(t) + + if code != 0 { + t.Errorf("expected status code 0 but got %d", code) + } + + expected := `main.tftest.hcl... in progress + run "test"... pass + +Warning: Value for undeclared variable + + on main.tftest.hcl line 6, in run "test": + 6: validation = "Hello, world!" + +The module under test does not declare a variable named "validation", but it +is declared in run block "test". + +main.tftest.hcl... tearing down +main.tftest.hcl... pass + +Success! 1 passed, 0 failed. +` + + actual := output.All() + + if diff := cmp.Diff(actual, expected); len(diff) > 0 { + t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expected, actual, diff) + } + + if provider.ResourceCount() > 0 { + t.Errorf("should have deleted all resources on completion but left %v", provider.ResourceString()) + } +} + func TestTest_BadReferences(t *testing.T) { td := t.TempDir() testCopyDir(t, testFixturePath(path.Join("test", "bad-references")), td) diff --git a/internal/command/testdata/test/invalid-cleanup-warnings/main.tf b/internal/command/testdata/test/invalid-cleanup-warnings/main.tf new file mode 100644 index 0000000000..25878f33d4 --- /dev/null +++ b/internal/command/testdata/test/invalid-cleanup-warnings/main.tf @@ -0,0 +1,8 @@ +# main.tf + +variable "input" {} + +resource "test_resource" "resource" { + value = var.input +} + diff --git a/internal/command/testdata/test/invalid-cleanup-warnings/main.tftest.hcl b/internal/command/testdata/test/invalid-cleanup-warnings/main.tftest.hcl new file mode 100644 index 0000000000..2f778e2704 --- /dev/null +++ b/internal/command/testdata/test/invalid-cleanup-warnings/main.tftest.hcl @@ -0,0 +1,13 @@ +# main.tftest.hcl + +run "test" { + variables { + input = "Hello, world!" + validation = "Hello, world!" + } + assert { + condition = test_resource.resource.value == "Hello, world!" + error_message = "bad!" + } +} +