Introducing overlord.Mock

Overlord has since long sported a Settle method to use in tests to wait in most common cases for a test-created Change to get to a ready state. But so far we could only construct a complete Overlord with New with all the managers, meaning Settle could only be used with all the managers together and consequently their Ensure side-effects. So lots of managers’ own test resorted to simply call Ensure and Wait manually/directly.

Now overlord.Mock allows to create an Overlord without initially any managers and with a mock backend that does not store state to disk. A subset of managers can then be added with calls to Overlord.AddManager.

This diff illustrates the change of testing style afforded by this:

--- a/overlord/devicestate/devicestate_test.go
+++ b/overlord/devicestate/devicestate_test.go
@@ -42,6 +42,7 @@ import (
 	"github.com/snapcore/snapd/boot/boottest"
 	"github.com/snapcore/snapd/dirs"
 	"github.com/snapcore/snapd/httputil"
+	"github.com/snapcore/snapd/overlord"
 	"github.com/snapcore/snapd/overlord/assertstate"
 	"github.com/snapcore/snapd/overlord/auth"
 	"github.com/snapcore/snapd/overlord/devicestate"
@@ -62,6 +63,7 @@ import (
 func TestDeviceManager(t *testing.T) { TestingT(t) }
 
 type deviceMgrSuite struct {
+	o       *overlord.Overlord
 	state   *state.State
 	hookMgr *hookstate.HookManager
 	mgr     *devicestate.DeviceManager
@@ -110,7 +112,8 @@ func (s *deviceMgrSuite) SetUpTest(c *C) {
 	s.restoreOnClassic = release.MockOnClassic(false)
 
 	s.storeSigning = assertstest.NewStoreStack("canonical", nil)
-	s.state = state.New(nil)
+	s.o = overlord.Mock()
+	s.state = s.o.State()
 
 	s.restoreGenericClassicMod = sysdb.MockGenericClassicModel(s.storeSigning.GenericClassicModel)
 
@@ -138,7 +141,9 @@ func (s *deviceMgrSuite) SetUpTest(c *C) {
 
 	s.db = db
 	s.hookMgr = hookMgr
+	s.o.AddManager(s.hookMgr)
 	s.mgr = mgr
+	s.o.AddManager(s.mgr)
 
 	s.state.Lock()
 	storestate.ReplaceStore(s.state, &fakeStore{
@@ -158,12 +163,7 @@ func (s *deviceMgrSuite) TearDownTest(c *C) {
 }
 
 func (s *deviceMgrSuite) settle() {
-	for i := 0; i < 50; i++ {
-		s.hookMgr.Ensure()
-		s.mgr.Ensure()
-		s.hookMgr.Wait()
-		s.mgr.Wait()
-	}
+	s.o.Settle(5 * time.Second)
 }