Skip to content
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

Fix flaky test in views package #33560

Merged
merged 1 commit into from
Jul 20, 2023
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
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
Loading