From 8b203036a7c11003ec95292decce4fe8910fb68d Mon Sep 17 00:00:00 2001 From: Graham Hargreaves Date: Mon, 3 Aug 2020 09:38:27 +0100 Subject: [PATCH 1/3] 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. --- command/clistate/state.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/command/clistate/state.go b/command/clistate/state.go index 7e5d32e607ab..c02531d5c3a4 100644 --- a/command/clistate/state.go +++ b/command/clistate/state.go @@ -151,6 +151,8 @@ func (l *locker) Unlock(parentErr error) error { if parentErr != nil { parentErr = multierror.Append(parentErr, err) + } else { + return err } } From c1c024cb091a3bfdd7ae72a147a5fdcd57f04eb4 Mon Sep 17 00:00:00 2001 From: Graham Hargreaves Date: Wed, 12 Aug 2020 15:03:19 +0100 Subject: [PATCH 2/3] Update command/clistate/state.go Accept review suggestion Co-authored-by: ZymoticB --- command/clistate/state.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/command/clistate/state.go b/command/clistate/state.go index c02531d5c3a4..2620c62f25df 100644 --- a/command/clistate/state.go +++ b/command/clistate/state.go @@ -149,11 +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) - } else { - return err - } + parentErr = multierror.Append(parentErr, err) } return parentErr From 7a8af65a14896ab48492e1b7ab9c3b640cf27005 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 14 Aug 2020 14:18:42 -0400 Subject: [PATCH 3/3] add test --- command/clistate/state_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 command/clistate/state_test.go diff --git a/command/clistate/state_test.go b/command/clistate/state_test.go new file mode 100644 index 000000000000..f1ba88ab86cf --- /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") + } +}