From 408a283de76979451b1e2a50d38d8f5a528d6506 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Thu, 20 Jul 2023 17:24:22 +0200 Subject: [PATCH] Fix flaky test in views package --- internal/command/views/test.go | 44 +++++++++++++++-------------- internal/command/views/test_test.go | 23 +++++++++++---- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/internal/command/views/test.go b/internal/command/views/test.go index 56466aeaee25..87c0317cbbf5 100644 --- a/internal/command/views/test.go +++ b/internal/command/views/test.go @@ -243,31 +243,33 @@ func (t *TestHuman) FatalInterrupt() { func (t *TestHuman) FatalInterruptSummary(run *moduletest.Run, file *moduletest.File, existingStates map[*moduletest.Run]*states.State, created []*plans.ResourceInstanceChangeSrc) { t.view.streams.Eprint(format.WordWrap(fmt.Sprintf("\nTerraform was interrupted while executing %s, and may not have performed the expected cleanup operations.\n", file.Name), t.view.errorColumns())) - for run, state := range existingStates { - if state.Empty() { - // Then it's fine, don't worry about it. + // Print out the main state first, this is the state that isn't associated + // with a run block. + if state, exists := existingStates[nil]; exists && !state.Empty() { + t.view.streams.Eprint(format.WordWrap("\nTerraform has already created the following resources from the module under test:\n", t.view.errorColumns())) + for _, resource := range state.AllResourceInstanceObjectAddrs() { + if resource.DeposedKey != states.NotDeposed { + t.view.streams.Eprintf(" - %s (%s)\n", resource.Instance, resource.DeposedKey) + continue + } + t.view.streams.Eprintf(" - %s\n", resource.Instance) + } + } + + // Then print out the other states in order. + for _, run := range file.Runs { + state, exists := existingStates[run] + if !exists || state.Empty() { continue } - if run == nil { - // Then this is just the main state for the whole file. - t.view.streams.Eprint(format.WordWrap("\nTerraform has already created the following resources from the module under test:\n", t.view.errorColumns())) - for _, resource := range state.AllResourceInstanceObjectAddrs() { - if resource.DeposedKey != states.NotDeposed { - t.view.streams.Eprintf(" - %s (%s)\n", resource.Instance, resource.DeposedKey) - continue - } - t.view.streams.Eprintf(" - %s\n", resource.Instance) - } - } else { - t.view.streams.Eprint(format.WordWrap(fmt.Sprintf("\nTerraform has already created the following resources for %q from %q:\n", run.Name, run.Config.Module.Source), t.view.errorColumns())) - for _, resource := range state.AllResourceInstanceObjectAddrs() { - if resource.DeposedKey != states.NotDeposed { - t.view.streams.Eprintf(" - %s (%s)\n", resource.Instance, resource.DeposedKey) - continue - } - t.view.streams.Eprintf(" - %s\n", resource.Instance) + t.view.streams.Eprint(format.WordWrap(fmt.Sprintf("\nTerraform has already created the following resources for %q from %q:\n", run.Name, run.Config.Module.Source), t.view.errorColumns())) + for _, resource := range state.AllResourceInstanceObjectAddrs() { + if resource.DeposedKey != states.NotDeposed { + t.view.streams.Eprintf(" - %s (%s)\n", resource.Instance, resource.DeposedKey) + continue } + t.view.streams.Eprintf(" - %s\n", resource.Instance) } } diff --git a/internal/command/views/test_test.go b/internal/command/views/test_test.go index f25d2b37d692..bd49194b7d06 100644 --- a/internal/command/views/test_test.go +++ b/internal/command/views/test_test.go @@ -1200,16 +1200,16 @@ Terraform has already created the following resources for "setup_block" from Terraform was interrupted while executing main.tftest.hcl, and may not have performed the expected cleanup operations. -Terraform has already created the following resources for "setup_block" from -"../setup": - - test_instance.setup_one - - test_instance.setup_two - Terraform has already created the following resources from the module under test: - test_instance.one - test_instance.two +Terraform has already created the following resources for "setup_block" from +"../setup": + - test_instance.setup_one + - test_instance.setup_two + Terraform was in the process of creating the following resources for "run_block" from the module under test, and they may not have been destroyed: - test_instance.new_one @@ -1222,7 +1222,18 @@ Terraform was in the process of creating the following resources for streams, done := terminal.StreamsForTesting(t) view := NewTest(arguments.ViewHuman, NewView(streams)) - file := &moduletest.File{Name: "main.tftest.hcl"} + file := &moduletest.File{ + Name: "main.tftest.hcl", + Runs: func() []*moduletest.Run { + var runs []*moduletest.Run + for run := range tc.states { + if run != nil { + runs = append(runs, run) + } + } + return runs + }(), + } view.FatalInterruptSummary(tc.run, file, tc.states, tc.created) actual, expected := done(t).Stderr(), tc.want