Skip to content

Commit 4abefbd

Browse files
committed
Add validation to enforce skip_cleanup=false cannot be used with backend blocks
1 parent abed9e5 commit 4abefbd

File tree

6 files changed

+103
-1
lines changed

6 files changed

+103
-1
lines changed

internal/command/test_test.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4013,7 +4013,83 @@ supplied_input_value = value-from-run-that-controls-backend`,
40134013

40144014
}
40154015

4016-
func TestTest_UseOfBackends_unhappyPath(t *testing.T) {
4016+
func TestTest_UseOfBackends_validatesUseOfSkipCleanup(t *testing.T) {
4017+
4018+
cases := map[string]struct {
4019+
testDir string
4020+
expectCode int
4021+
expectErr bool
4022+
}{
4023+
"cannot set skip_cleanup=false alongside a backend block": {
4024+
testDir: "backend-with-skip-cleanup/false",
4025+
expectCode: 1,
4026+
expectErr: true,
4027+
},
4028+
"can set skip_cleanup=true alongside a backend block": {
4029+
testDir: "backend-with-skip-cleanup/true",
4030+
expectCode: 0,
4031+
expectErr: false,
4032+
},
4033+
}
4034+
4035+
for tn, tc := range cases {
4036+
t.Run(tn, func(t *testing.T) {
4037+
// SETUP
4038+
td := t.TempDir()
4039+
testCopyDir(t, testFixturePath(path.Join("test", tc.testDir)), td)
4040+
defer testChdir(t, td)()
4041+
4042+
provider := testing_command.NewProvider(nil)
4043+
providerSource, close := newMockProviderSource(t, map[string][]string{
4044+
"test": {"1.0.0"},
4045+
})
4046+
defer close()
4047+
4048+
streams, done := terminal.StreamsForTesting(t)
4049+
view := views.NewView(streams)
4050+
ui := new(cli.MockUi)
4051+
4052+
meta := Meta{
4053+
testingOverrides: metaOverridesForProvider(provider.Provider),
4054+
Ui: ui,
4055+
View: view,
4056+
Streams: streams,
4057+
ProviderSource: providerSource,
4058+
}
4059+
4060+
// INIT
4061+
init := &InitCommand{
4062+
Meta: meta,
4063+
}
4064+
4065+
code := init.Run([]string{"-no-color"})
4066+
output := done(t)
4067+
4068+
// ASSERTIONS
4069+
if code != tc.expectCode {
4070+
t.Errorf("expected status code %d but got %d", tc.expectCode, code)
4071+
}
4072+
stdErr := output.Stderr()
4073+
if len(stdErr) == 0 && tc.expectErr {
4074+
t.Fatal("expected error output but got none")
4075+
}
4076+
if len(stdErr) != 0 && !tc.expectErr {
4077+
t.Fatalf("did not expect error output but got: %s", stdErr)
4078+
}
4079+
4080+
if provider.ResourceCount() > 0 {
4081+
t.Fatalf("should have deleted all resources on completion but left %v", provider.ResourceString())
4082+
}
4083+
4084+
})
4085+
}
4086+
}
4087+
4088+
func TestTest_UseOfBackends_failureDuringApply(t *testing.T) {
4089+
// SETUP
4090+
td := t.TempDir()
4091+
testCopyDir(t, testFixturePath(path.Join("test", "valid-use-local-backend")), td)
4092+
defer testChdir(t, td)()
40174093

40184094
testCases := map[string]struct {
40194095
dirName string
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "test_resource" "a" {
2+
id = "12345"
3+
value = "foobar"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
run "test" {
2+
backend "local" {}
3+
skip_cleanup = false
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "test_resource" "a" {
2+
id = "12345"
3+
value = "foobar"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
run "test" {
2+
backend "local" {}
3+
skip_cleanup = true
4+
}

internal/configs/test_file.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,16 @@ func decodeTestRunBlock(block *hcl.Block, file *TestFile) (*TestRun, hcl.Diagnos
841841
r.SkipCleanupSet = true
842842
}
843843

844+
if r.SkipCleanupSet && !r.SkipCleanup && r.Backend != nil {
845+
// Stop user attempting to clean up long-lived resources
846+
diags = append(diags, &hcl.Diagnostic{
847+
Severity: hcl.DiagError,
848+
Summary: "Cannot use `skip_cleanup=false` in a run block that contains a backend block",
849+
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`",
850+
Subject: backendRange.Ptr(),
851+
})
852+
}
853+
844854
return &r, diags
845855
}
846856

0 commit comments

Comments
 (0)