github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/conn_wallclock_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 "github.com/juju/utils" 9 gc "gopkg.in/check.v1" 10 "gopkg.in/juju/names.v2" 11 "gopkg.in/mgo.v2" 12 13 "github.com/juju/juju/provider/dummy" 14 "github.com/juju/juju/state" 15 statetesting "github.com/juju/juju/state/testing" 16 "github.com/juju/juju/storage" 17 "github.com/juju/juju/testing" 18 ) 19 20 // ConnWithWallclockSuite provides the infrastructure for all other test suites 21 // (StateSuite, CharmSuite, MachineSuite, etc). This should be deprecated in 22 // favour of ConnSuite, and tests updated to use the testing clock provided in 23 // ConnSuite via StateSuite. 24 type ConnWithWallclockSuite struct { 25 statetesting.StateWithWallclockSuite 26 annotations *mgo.Collection 27 charms *mgo.Collection 28 machines *mgo.Collection 29 instanceData *mgo.Collection 30 relations *mgo.Collection 31 services *mgo.Collection 32 units *mgo.Collection 33 controllers *mgo.Collection 34 policy statetesting.MockPolicy 35 modelTag names.ModelTag 36 } 37 38 func (cs *ConnWithWallclockSuite) SetUpTest(c *gc.C) { 39 cs.policy = statetesting.MockPolicy{ 40 GetStorageProviderRegistry: func() (storage.ProviderRegistry, error) { 41 return dummy.StorageProviders(), nil 42 }, 43 } 44 cs.StateWithWallclockSuite.NewPolicy = func(*state.State) state.Policy { 45 return &cs.policy 46 } 47 48 cs.StateWithWallclockSuite.SetUpTest(c) 49 50 cs.modelTag = cs.State.ModelTag() 51 52 jujuDB := cs.MgoSuite.Session.DB("juju") 53 cs.annotations = jujuDB.C("annotations") 54 cs.charms = jujuDB.C("charms") 55 cs.machines = jujuDB.C("machines") 56 cs.instanceData = jujuDB.C("instanceData") 57 cs.relations = jujuDB.C("relations") 58 cs.services = jujuDB.C("applications") 59 cs.units = jujuDB.C("units") 60 cs.controllers = jujuDB.C("controllers") 61 } 62 63 func (s *ConnWithWallclockSuite) AddTestingCharm(c *gc.C, name string) *state.Charm { 64 return state.AddTestingCharm(c, s.State, name) 65 } 66 67 func (s *ConnWithWallclockSuite) AddTestingService(c *gc.C, name string, ch *state.Charm) *state.Application { 68 return state.AddTestingService(c, s.State, name, ch) 69 } 70 71 func (s *ConnWithWallclockSuite) AddTestingServiceWithStorage(c *gc.C, name string, ch *state.Charm, storage map[string]state.StorageConstraints) *state.Application { 72 return state.AddTestingServiceWithStorage(c, s.State, name, ch, storage) 73 } 74 75 func (s *ConnWithWallclockSuite) AddTestingServiceWithBindings(c *gc.C, name string, ch *state.Charm, bindings map[string]string) *state.Application { 76 return state.AddTestingServiceWithBindings(c, s.State, name, ch, bindings) 77 } 78 79 func (s *ConnWithWallclockSuite) AddSeriesCharm(c *gc.C, name, series string) *state.Charm { 80 return state.AddCustomCharm(c, s.State, name, "", "", series, -1) 81 } 82 83 // AddConfigCharm clones a testing charm, replaces its config with 84 // the given YAML string and adds it to the state, using the given 85 // revision. 86 func (s *ConnWithWallclockSuite) AddConfigCharm(c *gc.C, name, configYaml string, revision int) *state.Charm { 87 return state.AddCustomCharm(c, s.State, name, "config.yaml", configYaml, "quantal", revision) 88 } 89 90 // AddActionsCharm clones a testing charm, replaces its actions schema with 91 // the given YAML, and adds it to the state, using the given revision. 92 func (s *ConnWithWallclockSuite) AddActionsCharm(c *gc.C, name, actionsYaml string, revision int) *state.Charm { 93 return state.AddCustomCharm(c, s.State, name, "actions.yaml", actionsYaml, "quantal", revision) 94 } 95 96 // AddMetaCharm clones a testing charm, replaces its metadata with the 97 // given YAML string and adds it to the state, using the given revision. 98 func (s *ConnWithWallclockSuite) AddMetaCharm(c *gc.C, name, metaYaml string, revision int) *state.Charm { 99 return state.AddCustomCharm(c, s.State, name, "metadata.yaml", metaYaml, "quantal", revision) 100 } 101 102 // AddMetricsCharm clones a testing charm, replaces its metrics declaration with the 103 // given YAML string and adds it to the state, using the given revision. 104 func (s *ConnWithWallclockSuite) AddMetricsCharm(c *gc.C, name, metricsYaml string, revision int) *state.Charm { 105 return state.AddCustomCharm(c, s.State, name, "metrics.yaml", metricsYaml, "quantal", revision) 106 } 107 108 // NewStateForModelNamed returns an new model with the given modelName, which 109 // has a unique UUID, and does not need to be closed when the test completes. 110 func (s *ConnWithWallclockSuite) NewStateForModelNamed(c *gc.C, modelName string) *state.State { 111 cfg := testing.CustomModelConfig(c, testing.Attrs{ 112 "name": modelName, 113 "uuid": utils.MustNewUUID().String(), 114 }) 115 otherOwner := names.NewLocalUserTag("test-admin") 116 _, otherState, err := s.State.NewModel(state.ModelArgs{ 117 CloudName: "dummy", CloudRegion: "dummy-region", Config: cfg, Owner: otherOwner, 118 StorageProviderRegistry: storage.StaticProviderRegistry{}, 119 }) 120 121 c.Assert(err, jc.ErrorIsNil) 122 s.AddCleanup(func(*gc.C) { otherState.Close() }) 123 return otherState 124 }