Skip to content

Commit

Permalink
command/clistate: Return an error on unlock failure (#25729)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* add test

Co-authored-by: ZymoticB <[email protected]>
Co-authored-by: Kristin Laemmert <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2020
1 parent 9fba422 commit e9394df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 1 addition & 3 deletions command/clistate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions command/clistate/state_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit e9394df

Please sign in to comment.