Skip to content

Commit d78eefd

Browse files
authored
Terraform Test: Allow skipping cleanup of entire test file or individual run blocks (#36729)
1 parent b6b38f4 commit d78eefd

File tree

12 files changed

+553
-85
lines changed

12 files changed

+553
-85
lines changed

internal/command/test_test.go

Lines changed: 353 additions & 28 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "id" {
2+
type = string
3+
}
4+
5+
resource "test_resource" "resource" {
6+
value = var.id
7+
}
8+
9+
output "id" {
10+
value = test_resource.resource.id
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
run "test" {
2+
variables {
3+
id = "test"
4+
}
5+
}
6+
7+
run "test_two" {
8+
skip_cleanup = true
9+
variables {
10+
id = "test_two"
11+
}
12+
}
13+
14+
run "test_three" {
15+
skip_cleanup = true
16+
variables {
17+
id = "test_three"
18+
}
19+
}
20+
21+
run "test_four" {
22+
variables {
23+
id = "test_four"
24+
}
25+
}
26+
27+
run "test_five" {
28+
variables {
29+
id = "test_five"
30+
}
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "id" {
2+
type = string
3+
}
4+
5+
resource "test_resource" "resource" {
6+
value = var.id
7+
}
8+
9+
output "id" {
10+
value = test_resource.resource.id
11+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
test {
2+
skip_cleanup = true
3+
}
4+
5+
run "test" {
6+
variables {
7+
id = "test"
8+
}
9+
}
10+
11+
run "test_two" {
12+
variables {
13+
id = "test_two"
14+
}
15+
}
16+
17+
run "test_three" {
18+
variables {
19+
id = "test_three"
20+
}
21+
}
22+
23+
run "test_four" {
24+
variables {
25+
id = "test_four"
26+
}
27+
}
28+
29+
run "test_five" {
30+
variables {
31+
id = "test_five"
32+
}
33+
}

internal/command/views/test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ func (t *TestHuman) DestroySummary(diags tfdiags.Diagnostics, run *moduletest.Ru
275275
}
276276
t.Diagnostics(run, file, diags)
277277

278-
if state.HasManagedResourceInstanceObjects() {
278+
skipCleanup := run != nil && run.Config.SkipCleanup
279+
if state.HasManagedResourceInstanceObjects() && !skipCleanup {
279280
// FIXME: This message says "resources" but this is actually a list
280281
// of resource instance objects.
281282
t.view.streams.Eprint(format.WordWrap(fmt.Sprintf("\nTerraform left the following resources in state after executing %s, and they need to be cleaned up manually:\n", identifier), t.view.errorColumns()))
@@ -602,7 +603,8 @@ func (t *TestJSON) Run(run *moduletest.Run, file *moduletest.File, progress modu
602603
}
603604

604605
func (t *TestJSON) DestroySummary(diags tfdiags.Diagnostics, run *moduletest.Run, file *moduletest.File, state *states.State) {
605-
if state.HasManagedResourceInstanceObjects() {
606+
skipCleanup := run != nil && run.Config.SkipCleanup
607+
if state.HasManagedResourceInstanceObjects() && !skipCleanup {
606608
cleanup := json.TestFileCleanup{}
607609
for _, resource := range addrs.SetSortedNatural(state.AllManagedResourceInstanceObjectAddrs()) {
608610
cleanup.FailedResources = append(cleanup.FailedResources, json.TestFailedResource{

internal/command/views/test_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func TestTestHuman_Run(t *testing.T) {
480480
StdErr string
481481
}{
482482
"pass": {
483-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
483+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
484484
Progress: moduletest.Complete,
485485
StdOut: " run \"run_block\"... pass\n",
486486
},
@@ -502,19 +502,19 @@ some warning happened during this test
502502
},
503503

504504
"pending": {
505-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Pending},
505+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pending},
506506
Progress: moduletest.Complete,
507507
StdOut: " run \"run_block\"... pending\n",
508508
},
509509

510510
"skip": {
511-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Skip},
511+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Skip},
512512
Progress: moduletest.Complete,
513513
StdOut: " run \"run_block\"... skip\n",
514514
},
515515

516516
"fail": {
517-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Fail},
517+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Fail},
518518
Progress: moduletest.Complete,
519519
StdOut: " run \"run_block\"... fail\n",
520520
},
@@ -542,7 +542,7 @@ other details
542542
},
543543

544544
"error": {
545-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Error},
545+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Error},
546546
Progress: moduletest.Complete,
547547
StdOut: " run \"run_block\"... fail\n",
548548
},
@@ -725,15 +725,15 @@ resource "test_resource" "creating" {
725725
// These next three tests should print nothing, as we only report on
726726
// progress complete.
727727
"progress_starting": {
728-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
728+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
729729
Progress: moduletest.Starting,
730730
},
731731
"progress_running": {
732-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
732+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
733733
Progress: moduletest.Running,
734734
},
735735
"progress_teardown": {
736-
Run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
736+
Run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
737737
Progress: moduletest.TearDown,
738738
},
739739
}
@@ -822,7 +822,7 @@ this time it is very bad
822822
diags: tfdiags.Diagnostics{
823823
tfdiags.Sourceless(tfdiags.Error, "first error", "this time it is very bad"),
824824
},
825-
run: &moduletest.Run{Name: "run_block"},
825+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}},
826826
file: &moduletest.File{Name: "main.tftest.hcl"},
827827
state: states.NewState(),
828828
stderr: `Terraform encountered an error destroying resources created while executing
@@ -1973,7 +1973,7 @@ func TestTestJSON_DestroySummary(t *testing.T) {
19731973
},
19741974
"state_from_run": {
19751975
file: &moduletest.File{Name: "main.tftest.hcl"},
1976-
run: &moduletest.Run{Name: "run_block"},
1976+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}},
19771977
state: states.BuildState(func(state *states.SyncState) {
19781978
state.SetResourceInstanceCurrent(
19791979
addrs.Resource{
@@ -2380,7 +2380,7 @@ func TestTestJSON_Run(t *testing.T) {
23802380
want []map[string]interface{}
23812381
}{
23822382
"starting": {
2383-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
2383+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
23842384
progress: moduletest.Starting,
23852385
want: []map[string]interface{}{
23862386
{
@@ -2401,7 +2401,7 @@ func TestTestJSON_Run(t *testing.T) {
24012401
},
24022402

24032403
"running": {
2404-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
2404+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
24052405
progress: moduletest.Running,
24062406
elapsed: 2024,
24072407
want: []map[string]interface{}{
@@ -2423,7 +2423,7 @@ func TestTestJSON_Run(t *testing.T) {
24232423
},
24242424

24252425
"teardown": {
2426-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
2426+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
24272427
progress: moduletest.TearDown,
24282428
want: []map[string]interface{}{
24292429
{
@@ -2444,7 +2444,7 @@ func TestTestJSON_Run(t *testing.T) {
24442444
},
24452445

24462446
"pass": {
2447-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Pass},
2447+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pass},
24482448
progress: moduletest.Complete,
24492449
want: []map[string]interface{}{
24502450
{
@@ -2503,7 +2503,7 @@ func TestTestJSON_Run(t *testing.T) {
25032503
},
25042504

25052505
"pending": {
2506-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Pending},
2506+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Pending},
25072507
progress: moduletest.Complete,
25082508
want: []map[string]interface{}{
25092509
{
@@ -2524,7 +2524,7 @@ func TestTestJSON_Run(t *testing.T) {
25242524
},
25252525

25262526
"skip": {
2527-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Skip},
2527+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Skip},
25282528
progress: moduletest.Complete,
25292529
want: []map[string]interface{}{
25302530
{
@@ -2545,7 +2545,7 @@ func TestTestJSON_Run(t *testing.T) {
25452545
},
25462546

25472547
"fail": {
2548-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Fail},
2548+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Fail},
25492549
progress: moduletest.Complete,
25502550
want: []map[string]interface{}{
25512551
{
@@ -2620,7 +2620,7 @@ func TestTestJSON_Run(t *testing.T) {
26202620
},
26212621

26222622
"error": {
2623-
run: &moduletest.Run{Name: "run_block", Status: moduletest.Error},
2623+
run: &moduletest.Run{Name: "run_block", Config: &configs.TestRun{}, Status: moduletest.Error},
26242624
progress: moduletest.Complete,
26252625
want: []map[string]interface{}{
26262626
{

internal/configs/test_file.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ type TestFileConfig struct {
8282
// Parallel: Indicates if test runs should be executed in parallel.
8383
Parallel bool
8484

85+
// SkipCleanup: Indicates if the test runs should skip the cleanup phase.
86+
SkipCleanup bool
87+
8588
DeclRange hcl.Range
8689
}
8790

@@ -164,6 +167,10 @@ type TestRun struct {
164167

165168
Backend *Backend
166169

170+
// SkipCleanup: Indicates if the test run should skip the cleanup phase.
171+
SkipCleanup bool
172+
SkipCleanupSet bool
173+
167174
NameDeclRange hcl.Range
168175
VariablesDeclRange hcl.Range
169176
DeclRange hcl.Range
@@ -575,6 +582,11 @@ func decodeFileConfigBlock(fileContent *hcl.BodyContent) (*TestFileConfig, hcl.D
575582
diags = append(diags, rawDiags...)
576583
}
577584

585+
if attr, exists := content.Attributes["skip_cleanup"]; exists {
586+
rawDiags := gohcl.DecodeExpression(attr.Expr, nil, &ret.SkipCleanup)
587+
diags = append(diags, rawDiags...)
588+
}
589+
578590
return ret, diags
579591
}
580592

@@ -591,6 +603,7 @@ func decodeTestRunBlock(block *hcl.Block, file *TestFile) (*TestRun, hcl.Diagnos
591603
NameDeclRange: block.LabelRanges[0],
592604
DeclRange: block.DefRange,
593605
Parallel: file.Config != nil && file.Config.Parallel,
606+
SkipCleanup: file.Config != nil && file.Config.SkipCleanup,
594607
}
595608

596609
if !hclsyntax.ValidIdentifier(r.Name) {
@@ -822,6 +835,12 @@ func decodeTestRunBlock(block *hcl.Block, file *TestFile) (*TestRun, hcl.Diagnos
822835
})
823836
}
824837

838+
if attr, exists := content.Attributes["skip_cleanup"]; exists {
839+
rawDiags := gohcl.DecodeExpression(attr.Expr, nil, &r.SkipCleanup)
840+
diags = append(diags, rawDiags...)
841+
r.SkipCleanupSet = true
842+
}
843+
825844
return &r, diags
826845
}
827846

@@ -1021,6 +1040,7 @@ var testFileSchema = &hcl.BodySchema{
10211040
var testFileConfigBlockSchema = &hcl.BodySchema{
10221041
Attributes: []hcl.AttributeSchema{
10231042
{Name: "parallel"},
1043+
{Name: "skip_cleanup"},
10241044
},
10251045
}
10261046

@@ -1031,6 +1051,7 @@ var testRunBlockSchema = &hcl.BodySchema{
10311051
{Name: "expect_failures"},
10321052
{Name: "state_key"},
10331053
{Name: "parallel"},
1054+
{Name: "skip_cleanup"},
10341055
},
10351056
Blocks: []hcl.BlockHeaderSchema{
10361057
{

0 commit comments

Comments
 (0)