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