From e9394dfb38c5a4fdd46a20741e4334210df1a339 Mon Sep 17 00:00:00 2001 From: Graham Hargreaves Date: Thu, 3 Sep 2020 21:05:16 +0100 Subject: [PATCH] command/clistate: Return an error on unlock failure (#25729) * Return an error on unlock failure When the lock can't be released return the err even if there is no previous error with the current action. This allows faster failure in CI/CD systems. Without this failure to remove the lock would result in the failure happening on a subsequent plan or apply which slows down the feedback loop in automated systems. * Update command/clistate/state.go Accept review suggestion Co-authored-by: ZymoticB * add test Co-authored-by: ZymoticB Co-authored-by: Kristin Laemmert --- command/clistate/state.go | 4 +--- command/clistate/state_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 command/clistate/state_test.go diff --git a/command/clistate/state.go b/command/clistate/state.go index 7e5d32e60..2620c62f2 100644 --- a/command/clistate/state.go +++ b/command/clistate/state.go @@ -149,9 +149,7 @@ func (l *locker) Unlock(parentErr error) error { l.ui.Output(l.color.Color(fmt.Sprintf( "\n"+strings.TrimSpace(UnlockErrorMessage)+"\n", err))) - if parentErr != nil { - parentErr = multierror.Append(parentErr, err) - } + parentErr = multierror.Append(parentErr, err) } return parentErr diff --git a/command/clistate/state_test.go b/command/clistate/state_test.go new file mode 100644 index 000000000..f1ba88ab8 --- /dev/null +++ b/command/clistate/state_test.go @@ -0,0 +1,25 @@ +package clistate + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/terraform/states/statemgr" + "github.com/mitchellh/cli" + "github.com/mitchellh/colorstring" +) + +func TestUnlock(t *testing.T) { + ui := new(cli.MockUi) + + l := NewLocker(context.Background(), 0, ui, &colorstring.Colorize{Disable: true}) + l.Lock(statemgr.NewUnlockErrorFull(nil, nil), "test-lock") + + err := l.Unlock(nil) + if err != nil { + fmt.Printf(err.Error()) + } else { + t.Error("expected error") + } +}