Skip to content

Commit

Permalink
Fix flaky test in views package (#33560)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcervante authored Jul 20, 2023
1 parent c91f91c commit ad26644
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
44 changes: 23 additions & 21 deletions internal/command/views/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
23 changes: 17 additions & 6 deletions internal/command/views/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ad26644

Please sign in to comment.