From ecd9ef0f7732f1fe7019ccad03f8a9e53bc00a00 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 9 Feb 2018 19:13:34 -0500 Subject: [PATCH] ignore error in plan shutdown test The error was being silently dropped before. There is an interpolation error, because the plan is canceled before some of the resources can be evaluated. There might be a better way to handle this in the walk cancellation, but the behavior has not changed. Make the plan and apply shutdown match implementation-wise --- command/apply_test.go | 29 ++++++++++++++++++++--------- command/plan_test.go | 8 +++++--- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/command/apply_test.go b/command/apply_test.go index 968f8595f5..6eda227f09 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -824,12 +824,11 @@ func TestApply_refresh(t *testing.T) { } func TestApply_shutdown(t *testing.T) { - cancelled := false - stopped := make(chan struct{}) + cancelled := make(chan struct{}) + shutdownCh := make(chan struct{}) statePath := testTempFile(t) p := testProvider() - shutdownCh := make(chan struct{}) ui := new(cli.MockUi) c := &ApplyCommand{ @@ -841,8 +840,7 @@ func TestApply_shutdown(t *testing.T) { } p.StopFn = func() error { - close(stopped) - cancelled = true + close(cancelled) return nil } @@ -858,15 +856,26 @@ func TestApply_shutdown(t *testing.T) { }, }, nil } + + var once sync.Once p.ApplyFn = func( *terraform.InstanceInfo, *terraform.InstanceState, *terraform.InstanceDiff) (*terraform.InstanceState, error) { + // only cancel once - if !cancelled { + once.Do(func() { shutdownCh <- struct{}{} - <-stopped - } + }) + + // Because of the internal lock in the MockProvider, we can't + // coordiante directly with the calling of Stop, and making the + // MockProvider concurrent is disruptive to a lot of existing tests. + // Wait here a moment to help make sure the main goroutine gets to the + // Stop call before we exit, or the plan may finish before it can be + // canceled. + time.Sleep(200 * time.Millisecond) + return &terraform.InstanceState{ ID: "foo", Attributes: map[string]string{ @@ -888,7 +897,9 @@ func TestApply_shutdown(t *testing.T) { t.Fatalf("err: %s", err) } - if !cancelled { + select { + case <-cancelled: + default: t.Fatal("command not cancelled") } diff --git a/command/plan_test.go b/command/plan_test.go index e9cfd6dbb2..b78de9b8e7 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -835,8 +835,8 @@ func TestPlan_detailedExitcode_emptyDiff(t *testing.T) { func TestPlan_shutdown(t *testing.T) { cancelled := make(chan struct{}) - shutdownCh := make(chan struct{}) + p := testProvider() ui := new(cli.MockUi) c := &PlanCommand{ @@ -880,8 +880,10 @@ func TestPlan_shutdown(t *testing.T) { }, nil } - if code := c.Run([]string{testFixturePath("apply-shutdown")}); code != 0 { - t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + if code := c.Run([]string{testFixturePath("apply-shutdown")}); code != 1 { + // FIXME: we should be able to avoid the error during evaluation + // the early exit isn't caught before the interpolation is evaluated + t.Fatal(ui.OutputWriter.String()) } select {