Skip to content

Add validation to enforce skip_cleanup=false cannot be used with backends #36857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4013,6 +4013,78 @@ supplied_input_value = value-from-run-that-controls-backend`,

}

func TestTest_UseOfBackends_validatesUseOfSkipCleanup(t *testing.T) {

cases := map[string]struct {
testDir string
expectCode int
expectErr bool
}{
"cannot set skip_cleanup=false alongside a backend block": {
testDir: "backend-with-skip-cleanup/false",
expectCode: 1,
expectErr: true,
},
"can set skip_cleanup=true alongside a backend block": {
testDir: "backend-with-skip-cleanup/true",
expectCode: 0,
expectErr: false,
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
// SETUP
td := t.TempDir()
testCopyDir(t, testFixturePath(path.Join("test", tc.testDir)), 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
init := &InitCommand{
Meta: meta,
}

code := init.Run([]string{"-no-color"})
output := done(t)

// ASSERTIONS
if code != tc.expectCode {
t.Errorf("expected status code %d but got %d", tc.expectCode, code)
}
stdErr := output.Stderr()
if len(stdErr) == 0 && tc.expectErr {
t.Fatal("expected error output but got none")
}
if len(stdErr) != 0 && !tc.expectErr {
t.Fatalf("did not expect error output but got: %s", stdErr)
}

if provider.ResourceCount() > 0 {
t.Fatalf("should have deleted all resources on completion but left %v", provider.ResourceString())
}

})
}
}

func TestTest_UseOfBackends_unhappyPath(t *testing.T) {

testCases := map[string]struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "test_resource" "a" {
id = "12345"
value = "foobar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run "test" {
backend "local" {}
skip_cleanup = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "test_resource" "a" {
id = "12345"
value = "foobar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run "test" {
backend "local" {}
skip_cleanup = true
}
10 changes: 10 additions & 0 deletions internal/configs/test_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,16 @@ func decodeTestRunBlock(block *hcl.Block, file *TestFile) (*TestRun, hcl.Diagnos
r.SkipCleanupSet = true
}

if r.SkipCleanupSet && !r.SkipCleanup && r.Backend != nil {
// Stop user attempting to clean up long-lived resources
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Cannot use `skip_cleanup=false` in a run block that contains a backend block",
Detail: "Backend blocks are used in tests to allow reuse of long-lived resources. Due to this, cleanup behavior is implicitly skipped and backend blocks are incompatible with setting `skip_cleanup=false`",
Subject: backendRange.Ptr(),
})
}

return &r, diags
}

Expand Down