github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/conn_test.go (about) 1 // Copyright 2012, 2013 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/controller" 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/storage/provider" 18 dummystorage "github.com/juju/juju/storage/provider/dummy" 19 "github.com/juju/juju/testing" 20 ) 21 22 // ConnSuite provides the infrastructure for all other 23 // test suites (StateSuite, CharmSuite, MachineSuite, etc). 24 type ConnSuite struct { 25 statetesting.StateSuite 26 annotations *mgo.Collection 27 charms *mgo.Collection 28 machines *mgo.Collection 29 instanceData *mgo.Collection 30 relations *mgo.Collection 31 applications *mgo.Collection 32 units *mgo.Collection 33 controllers *mgo.Collection 34 policy statetesting.MockPolicy 35 modelTag names.ModelTag 36 } 37 38 func (s *ConnSuite) SetUpTest(c *gc.C) { 39 s.policy = statetesting.MockPolicy{ 40 GetStorageProviderRegistry: func() (storage.ProviderRegistry, error) { 41 return storage.ChainedProviderRegistry{ 42 dummystorage.StorageProviders(), 43 provider.CommonStorageProviders(), 44 }, nil 45 }, 46 } 47 s.StateSuite.NewPolicy = func(*state.State) state.Policy { 48 return &s.policy 49 } 50 51 s.StateSuite.SetUpTest(c) 52 53 s.modelTag = s.Model.ModelTag() 54 55 jujuDB := s.MgoSuite.Session.DB("juju") 56 s.annotations = jujuDB.C("annotations") 57 s.charms = jujuDB.C("charms") 58 s.machines = jujuDB.C("machines") 59 s.instanceData = jujuDB.C("instanceData") 60 s.relations = jujuDB.C("relations") 61 s.applications = jujuDB.C("applications") 62 s.units = jujuDB.C("units") 63 s.controllers = jujuDB.C("controllers") 64 } 65 66 func (s *ConnSuite) AddTestingCharm(c *gc.C, name string) *state.Charm { 67 return state.AddTestingCharm(c, s.State, name) 68 } 69 70 func (s *ConnSuite) AddTestingCharmWithSeries(c *gc.C, name string, series string) *state.Charm { 71 return state.AddTestingCharmWithSeries(c, s.State, name, series) 72 } 73 74 func (s *ConnSuite) AddTestingApplication(c *gc.C, name string, ch *state.Charm) *state.Application { 75 return state.AddTestingApplication(c, s.State, name, ch) 76 } 77 78 func (s *ConnSuite) AddTestingApplicationForBase(c *gc.C, base state.Base, name string, ch *state.Charm) *state.Application { 79 return state.AddTestingApplicationForBase(c, s.State, base, name, ch) 80 } 81 82 func (s *ConnSuite) AddTestingApplicationWithNumUnits(c *gc.C, numUnits int, name string, ch *state.Charm) *state.Application { 83 return state.AddTestingApplicationWithNumUnits(c, s.State, numUnits, name, ch) 84 } 85 86 func (s *ConnSuite) AddTestingApplicationWithStorage(c *gc.C, name string, ch *state.Charm, storage map[string]state.StorageConstraints) *state.Application { 87 return state.AddTestingApplicationWithStorage(c, s.State, name, ch, storage) 88 } 89 90 func (s *ConnSuite) AddTestingApplicationWithDevices(c *gc.C, name string, ch *state.Charm, devs map[string]state.DeviceConstraints) *state.Application { 91 return state.AddTestingApplicationWithDevices(c, s.State, name, ch, devs) 92 } 93 94 func (s *ConnSuite) AddTestingApplicationWithBindings(c *gc.C, name string, ch *state.Charm, bindings map[string]string) *state.Application { 95 return state.AddTestingApplicationWithBindings(c, s.State, name, ch, bindings) 96 } 97 98 func (s *ConnSuite) AddSeriesCharm(c *gc.C, name, series string) *state.Charm { 99 return state.AddCustomCharm(c, s.State, name, "", "", series, -1) 100 } 101 102 // AddConfigCharm clones a testing charm, replaces its config with 103 // the given YAML string and adds it to the state, using the given 104 // revision. 105 func (s *ConnSuite) AddConfigCharm(c *gc.C, name, configYaml string, revision int) *state.Charm { 106 return state.AddCustomCharm(c, s.State, name, "config.yaml", configYaml, "quantal", revision) 107 } 108 109 // AddActionsCharm clones a testing charm, replaces its actions schema with 110 // the given YAML, and adds it to the state, using the given revision. 111 func (s *ConnSuite) AddActionsCharm(c *gc.C, name, actionsYaml string, revision int) *state.Charm { 112 return state.AddCustomCharm(c, s.State, name, "actions.yaml", actionsYaml, "quantal", revision) 113 } 114 115 // AddManifestCharm clones a testing charm, replaces its manifest schema with 116 // the given YAML, and adds it to the state, using the given revision. 117 func (s *ConnSuite) AddManifestCharm(c *gc.C, name, manifestYaml string, revision int) *state.Charm { 118 return state.AddCustomCharm(c, s.State, name, "manifest.yaml", manifestYaml, "quantal", revision) 119 } 120 121 // AddLXDProfileCharm clones a testing charm, replaces its lxd profile config with 122 // the given YAML, and adds it to the state, using the given revision. 123 func (s *ConnSuite) AddLXDProfileCharm(c *gc.C, name, lxdProfileYaml string, revision int) *state.Charm { 124 return state.AddCustomCharm(c, s.State, name, "lxd-profile.yaml", lxdProfileYaml, "quantal", revision) 125 } 126 127 // AddMetaCharm clones a testing charm, replaces its metadata with the 128 // given YAML string and adds it to the state, using the given revision. 129 func (s *ConnSuite) AddMetaCharm(c *gc.C, name, metaYaml string, revision int) *state.Charm { 130 return state.AddCustomCharm(c, s.State, name, "metadata.yaml", metaYaml, "quantal", revision) 131 } 132 133 // AddMetricsCharm clones a testing charm, replaces its metrics declaration with the 134 // given YAML string and adds it to the state, using the given revision. 135 func (s *ConnSuite) AddMetricsCharm(c *gc.C, name, metricsYaml string, revision int) *state.Charm { 136 return state.AddCustomCharm(c, s.State, name, "metrics.yaml", metricsYaml, "quantal", revision) 137 } 138 139 // NewStateForModelNamed returns an new model with the given modelName, which 140 // has a unique UUID, and does not need to be closed when the test completes. 141 func (s *ConnSuite) NewStateForModelNamed(c *gc.C, modelName string) *state.State { 142 cfg := testing.CustomModelConfig(c, testing.Attrs{ 143 "name": modelName, 144 "uuid": utils.MustNewUUID().String(), 145 }) 146 otherOwner := names.NewLocalUserTag("test-admin") 147 _, otherState, err := s.Controller.NewModel(state.ModelArgs{ 148 Type: state.ModelTypeIAAS, 149 CloudName: "dummy", 150 CloudRegion: "dummy-region", 151 Config: cfg, 152 Owner: otherOwner, 153 StorageProviderRegistry: storage.StaticProviderRegistry{}, 154 }) 155 156 c.Assert(err, jc.ErrorIsNil) 157 s.AddCleanup(func(*gc.C) { otherState.Close() }) 158 return otherState 159 } 160 161 // SetJujuManagementSpace mimics a controller having been configured with a 162 // management space name. This is the space that constrains the set of 163 // addresses that agents should use for controller communication. 164 func (s *ConnSuite) SetJujuManagementSpace(c *gc.C, space string) { 165 controllerSettings, err := s.State.ReadSettings(state.ControllersC, "controllerSettings") 166 c.Assert(err, jc.ErrorIsNil) 167 controllerSettings.Set(controller.JujuManagementSpace, space) 168 _, err = controllerSettings.Write() 169 c.Assert(err, jc.ErrorIsNil) 170 }