From 95eca0678201cfae9845e6222e74ea4ed5fd0f58 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 14 Aug 2020 14:14:51 -0400 Subject: [PATCH] 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. --- states/statemgr/statemgr_fake.go | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/states/statemgr/statemgr_fake.go b/states/statemgr/statemgr_fake.go index 42d2b9bb3..36de10865 100644 --- a/states/statemgr/statemgr_fake.go +++ b/states/statemgr/statemgr_fake.go @@ -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") +}