statemgr: add a NewUnlockErrorFull state manager for tests (#25823)

* statemgr: add a NewUnlockErrorFull state manager for tests

I've frequently needed to coerce Unlock() errors for tests and it's been
awkward and fraught every time, so I decided to add a full state manger
that returns *mostly* errors. I intend to use this in conjunction with
the clistate.Locker interface, which first calls Lock() (to block if the
mutex is in use) at the start of Unlock(), so Lock() rather awkwardly needed to succeed.
This commit is contained in:
Kristin Laemmert 2020-08-14 14:14:51 -04:00 committed by GitHub
parent c9f710ac29
commit 95eca06782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 0 deletions

View File

@ -94,3 +94,39 @@ func (m *fakeFull) Unlock(id string) error {
m.locked = false
return nil
}
// NewUnlockErrorFull returns a state manager that is useful for testing errors
// (mostly Unlock errors) when used with the clistate.Locker interface. Lock()
// does not return an error because clistate.Locker Lock()s the state at the
// start of Unlock(), so Lock() must succeeded for Unlock() to get called.
func NewUnlockErrorFull(t Transient, initial *states.State) Full {
return &fakeErrorFull{}
}
type fakeErrorFull struct{}
var _ Full = (*fakeErrorFull)(nil)
func (m *fakeErrorFull) State() *states.State {
return nil
}
func (m *fakeErrorFull) WriteState(s *states.State) error {
return errors.New("fake state manager error")
}
func (m *fakeErrorFull) RefreshState() error {
return errors.New("fake state manager error")
}
func (m *fakeErrorFull) PersistState() error {
return errors.New("fake state manager error")
}
func (m *fakeErrorFull) Lock(info *LockInfo) (string, error) {
return "placeholder", nil
}
func (m *fakeErrorFull) Unlock(id string) error {
return errors.New("fake state manager error")
}