github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/names" 8 jc "github.com/juju/testing/checkers" 9 "github.com/juju/utils" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/mgo.v2" 12 13 "github.com/juju/juju/state" 14 statetesting "github.com/juju/juju/state/testing" 15 "github.com/juju/juju/testing" 16 ) 17 18 // ConnSuite provides the infrastructure for all other 19 // test suites (StateSuite, CharmSuite, MachineSuite, etc). 20 type ConnSuite struct { 21 statetesting.StateSuite 22 annotations *mgo.Collection 23 charms *mgo.Collection 24 machines *mgo.Collection 25 instanceData *mgo.Collection 26 relations *mgo.Collection 27 services *mgo.Collection 28 units *mgo.Collection 29 controllers *mgo.Collection 30 policy statetesting.MockPolicy 31 modelTag names.ModelTag 32 } 33 34 func (cs *ConnSuite) SetUpTest(c *gc.C) { 35 c.Log("SetUpTest") 36 37 cs.policy = statetesting.MockPolicy{} 38 cs.StateSuite.Policy = &cs.policy 39 40 cs.StateSuite.SetUpTest(c) 41 42 cs.modelTag = cs.State.ModelTag() 43 44 jujuDB := cs.MgoSuite.Session.DB("juju") 45 cs.annotations = jujuDB.C("annotations") 46 cs.charms = jujuDB.C("charms") 47 cs.machines = jujuDB.C("machines") 48 cs.instanceData = jujuDB.C("instanceData") 49 cs.relations = jujuDB.C("relations") 50 cs.services = jujuDB.C("services") 51 cs.units = jujuDB.C("units") 52 cs.controllers = jujuDB.C("controllers") 53 54 c.Log("SetUpTest done") 55 } 56 57 func (s *ConnSuite) AddTestingCharm(c *gc.C, name string) *state.Charm { 58 return state.AddTestingCharm(c, s.State, name) 59 } 60 61 func (s *ConnSuite) AddTestingService(c *gc.C, name string, ch *state.Charm) *state.Service { 62 return state.AddTestingService(c, s.State, name, ch, s.Owner) 63 } 64 65 func (s *ConnSuite) AddTestingServiceWithStorage(c *gc.C, name string, ch *state.Charm, storage map[string]state.StorageConstraints) *state.Service { 66 return state.AddTestingServiceWithStorage(c, s.State, name, ch, s.Owner, storage) 67 } 68 69 func (s *ConnSuite) AddTestingServiceWithBindings(c *gc.C, name string, ch *state.Charm, bindings map[string]string) *state.Service { 70 return state.AddTestingServiceWithBindings(c, s.State, name, ch, s.Owner, bindings) 71 } 72 73 func (s *ConnSuite) AddSeriesCharm(c *gc.C, name, series string) *state.Charm { 74 return state.AddCustomCharm(c, s.State, name, "", "", series, -1) 75 } 76 77 // AddConfigCharm clones a testing charm, replaces its config with 78 // the given YAML string and adds it to the state, using the given 79 // revision. 80 func (s *ConnSuite) AddConfigCharm(c *gc.C, name, configYaml string, revision int) *state.Charm { 81 return state.AddCustomCharm(c, s.State, name, "config.yaml", configYaml, "quantal", revision) 82 } 83 84 // AddActionsCharm clones a testing charm, replaces its actions schema with 85 // the given YAML, and adds it to the state, using the given revision. 86 func (s *ConnSuite) AddActionsCharm(c *gc.C, name, actionsYaml string, revision int) *state.Charm { 87 return state.AddCustomCharm(c, s.State, name, "actions.yaml", actionsYaml, "quantal", revision) 88 } 89 90 // AddMetaCharm clones a testing charm, replaces its metadata with the 91 // given YAML string and adds it to the state, using the given revision. 92 func (s *ConnSuite) AddMetaCharm(c *gc.C, name, metaYaml string, revision int) *state.Charm { 93 return state.AddCustomCharm(c, s.State, name, "metadata.yaml", metaYaml, "quantal", revision) 94 } 95 96 // AddMetricsCharm clones a testing charm, replaces its metrics declaration with the 97 // given YAML string and adds it to the state, using the given revision. 98 func (s *ConnSuite) AddMetricsCharm(c *gc.C, name, metricsYaml string, revision int) *state.Charm { 99 return state.AddCustomCharm(c, s.State, name, "metrics.yaml", metricsYaml, "quantal", revision) 100 } 101 102 // NewStateForModelNamed returns an new model with the given modelName, which 103 // has a unique UUID, and does not need to be closed when the test completes. 104 func (s *ConnSuite) NewStateForModelNamed(c *gc.C, modelName string) *state.State { 105 cfg := testing.CustomModelConfig(c, testing.Attrs{ 106 "name": modelName, 107 "uuid": utils.MustNewUUID().String(), 108 }) 109 otherOwner := names.NewLocalUserTag("test-admin") 110 _, otherState, err := s.State.NewModel(state.ModelArgs{Config: cfg, Owner: otherOwner}) 111 112 c.Assert(err, jc.ErrorIsNil) 113 s.AddCleanup(func(*gc.C) { otherState.Close() }) 114 return otherState 115 }