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
f-events-actions
MicahKimel 2 years ago committed by GitHub
parent 1ef2a9d44b
commit 3258744166
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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.
}

@ -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)

@ -0,0 +1,8 @@
# main.tf
variable "input" {}
resource "test_resource" "resource" {
value = var.input
}

@ -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!"
}
}
Loading…
Cancel
Save