backend/azure: azure state refreshes outside of grabbing the lock #26561

This commit is contained in:
Matthew Frahry 2021-02-17 15:14:47 -08:00 committed by GitHub
commit 85b9bdea96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 8 deletions

View File

@ -95,9 +95,13 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
stateMgr := &remote.State{Client: client}
// Grab the value
if err := stateMgr.RefreshState(); err != nil {
return nil, err
}
//if this isn't the default state name, we need to create the object so
//it's listed by States.
if name != backend.DefaultStateName {
if v := stateMgr.State(); v == nil {
// take a lock on this state while we write it
lockInfo := statemgr.NewLockInfo()
lockInfo.Operation = "init"
@ -119,9 +123,10 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
err = lockUnlock(err)
return nil, err
}
// If we have no state, we have to create an empty state
//if this isn't the default state name, we need to create the object so
//it's listed by States.
if v := stateMgr.State(); v == nil {
// If we have no state, we have to create an empty state
if err := stateMgr.WriteState(states.NewState()); err != nil {
err = lockUnlock(err)
return nil, err
@ -130,13 +135,12 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
err = lockUnlock(err)
return nil, err
}
}
// Unlock, the state should now be initialized
if err := lockUnlock(nil); err != nil {
return nil, err
// Unlock, the state should now be initialized
if err := lockUnlock(nil); err != nil {
return nil, err
}
}
}
return stateMgr, nil

View File

@ -258,6 +258,9 @@ func TestBackendAccessKeyLocked(t *testing.T) {
backend.TestBackendStateLocks(t, b1, b2)
backend.TestBackendStateForceUnlock(t, b1, b2)
backend.TestBackendStateLocksInWS(t, b1, b2, "foo")
backend.TestBackendStateForceUnlockInWS(t, b1, b2, "foo")
}
func TestBackendServicePrincipalLocked(t *testing.T) {
@ -301,4 +304,7 @@ func TestBackendServicePrincipalLocked(t *testing.T) {
backend.TestBackendStateLocks(t, b1, b2)
backend.TestBackendStateForceUnlock(t, b1, b2)
backend.TestBackendStateLocksInWS(t, b1, b2, "foo")
backend.TestBackendStateForceUnlockInWS(t, b1, b2, "foo")
}

View File

@ -279,7 +279,27 @@ func TestBackendStateForceUnlock(t *testing.T, b1, b2 Backend) {
testLocks(t, b1, b2, true)
}
// TestBackendStateLocksInWS will test the locking functionality of the remote
// state backend.
func TestBackendStateLocksInWS(t *testing.T, b1, b2 Backend, ws string) {
t.Helper()
testLocksInWorkspace(t, b1, b2, false, ws)
}
// TestBackendStateForceUnlockInWS verifies that the lock error is the expected
// type, and the lock can be unlocked using the ID reported in the error.
// Remote state backends that support -force-unlock should call this in at
// least one of the acceptance tests.
func TestBackendStateForceUnlockInWS(t *testing.T, b1, b2 Backend, ws string) {
t.Helper()
testLocksInWorkspace(t, b1, b2, true, ws)
}
func testLocks(t *testing.T, b1, b2 Backend, testForceUnlock bool) {
testLocksInWorkspace(t, b1, b2, testForceUnlock, DefaultStateName)
}
func testLocksInWorkspace(t *testing.T, b1, b2 Backend, testForceUnlock bool, workspace string) {
t.Helper()
// Get the default state for each