github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/charmrevisionupdater/testing/suite.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/charm.v4" 12 charmtesting "gopkg.in/juju/charm.v4/testing" 13 14 jujutesting "github.com/juju/juju/juju/testing" 15 "github.com/juju/juju/state" 16 "github.com/juju/juju/testcharms" 17 ) 18 19 // CharmSuite provides infrastructure to set up and perform tests associated 20 // with charm versioning. A mock charm store is created using some known charms 21 // used for testing. 22 type CharmSuite struct { 23 jcSuite *jujutesting.JujuConnSuite 24 25 Server *charmtesting.MockStore 26 charms map[string]*state.Charm 27 } 28 29 func (s *CharmSuite) SetUpSuite(c *gc.C, jcSuite *jujutesting.JujuConnSuite) { 30 s.jcSuite = jcSuite 31 s.Server = charmtesting.NewMockStore(c, testcharms.Repo, map[string]int{ 32 "cs:quantal/mysql": 23, 33 "cs:quantal/dummy": 24, 34 "cs:quantal/riak": 25, 35 "cs:quantal/wordpress": 26, 36 "cs:quantal/logging": 27, 37 "cs:quantal/borken": 28, 38 }) 39 } 40 41 func (s *CharmSuite) TearDownSuite(c *gc.C) { 42 s.Server.Close() 43 } 44 45 func (s *CharmSuite) SetUpTest(c *gc.C) { 46 s.jcSuite.PatchValue(&charm.CacheDir, c.MkDir()) 47 s.jcSuite.PatchValue(&charm.Store, &charm.CharmStore{BaseURL: s.Server.Address()}) 48 s.Server.Downloads = nil 49 s.Server.Authorizations = nil 50 s.Server.Metadata = nil 51 s.charms = make(map[string]*state.Charm) 52 } 53 54 func (s *CharmSuite) TearDownTest(c *gc.C) { 55 } 56 57 // UpdateStoreRevision sets the revision of the specified charm to rev. 58 func (s *CharmSuite) UpdateStoreRevision(ch string, rev int) { 59 s.Server.UpdateStoreRevision(ch, rev) 60 } 61 62 // AddMachine adds a new machine to state. 63 func (s *CharmSuite) AddMachine(c *gc.C, machineId string, job state.MachineJob) { 64 m, err := s.jcSuite.State.AddOneMachine(state.MachineTemplate{ 65 Series: "quantal", 66 Jobs: []state.MachineJob{job}, 67 }) 68 c.Assert(err, jc.ErrorIsNil) 69 c.Assert(m.Id(), gc.Equals, machineId) 70 cons, err := m.Constraints() 71 c.Assert(err, jc.ErrorIsNil) 72 inst, hc := jujutesting.AssertStartInstanceWithConstraints(c, s.jcSuite.Environ, m.Id(), cons) 73 err = m.SetProvisioned(inst.Id(), "fake_nonce", hc) 74 c.Assert(err, jc.ErrorIsNil) 75 76 } 77 78 // AddCharmWithRevision adds a charm with the specified revision to state. 79 func (s *CharmSuite) AddCharmWithRevision(c *gc.C, charmName string, rev int) *state.Charm { 80 ch := testcharms.Repo.CharmDir(charmName) 81 name := ch.Meta().Name 82 curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", name, rev)) 83 dummy, err := s.jcSuite.State.AddCharm(ch, curl, "dummy-path", fmt.Sprintf("%s-%d-sha256", name, rev)) 84 c.Assert(err, jc.ErrorIsNil) 85 s.charms[name] = dummy 86 return dummy 87 } 88 89 // AddService adds a service for the specified charm to state. 90 func (s *CharmSuite) AddService(c *gc.C, charmName, serviceName string, networks []string) { 91 ch, ok := s.charms[charmName] 92 c.Assert(ok, jc.IsTrue) 93 owner := s.jcSuite.AdminUserTag(c) 94 _, err := s.jcSuite.State.AddService(serviceName, owner.String(), ch, networks, nil) 95 c.Assert(err, jc.ErrorIsNil) 96 } 97 98 // AddUnit adds a new unit for service to the specified machine. 99 func (s *CharmSuite) AddUnit(c *gc.C, serviceName, machineId string) { 100 svc, err := s.jcSuite.State.Service(serviceName) 101 c.Assert(err, jc.ErrorIsNil) 102 u, err := svc.AddUnit() 103 c.Assert(err, jc.ErrorIsNil) 104 m, err := s.jcSuite.State.Machine(machineId) 105 c.Assert(err, jc.ErrorIsNil) 106 err = u.AssignToMachine(m) 107 c.Assert(err, jc.ErrorIsNil) 108 } 109 110 // SetUnitRevision sets the unit's charm to the specified revision. 111 func (s *CharmSuite) SetUnitRevision(c *gc.C, unitName string, rev int) { 112 u, err := s.jcSuite.State.Unit(unitName) 113 c.Assert(err, jc.ErrorIsNil) 114 svc, err := u.Service() 115 c.Assert(err, jc.ErrorIsNil) 116 curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", svc.Name(), rev)) 117 err = u.SetCharmURL(curl) 118 c.Assert(err, jc.ErrorIsNil) 119 } 120 121 // SetupScenario adds some machines and services to state. 122 // It assumes a state server machine has already been created. 123 func (s *CharmSuite) SetupScenario(c *gc.C) { 124 s.AddMachine(c, "1", state.JobHostUnits) 125 s.AddMachine(c, "2", state.JobHostUnits) 126 s.AddMachine(c, "3", state.JobHostUnits) 127 128 // mysql is out of date 129 s.AddCharmWithRevision(c, "mysql", 22) 130 s.AddService(c, "mysql", "mysql", nil) 131 s.AddUnit(c, "mysql", "1") 132 133 // wordpress is up to date 134 s.AddCharmWithRevision(c, "wordpress", 26) 135 s.AddService(c, "wordpress", "wordpress", nil) 136 s.AddUnit(c, "wordpress", "2") 137 s.AddUnit(c, "wordpress", "2") 138 // wordpress/0 has a version, wordpress/1 is unknown 139 s.SetUnitRevision(c, "wordpress/0", 26) 140 141 // varnish is a charm that does not have a version in the mock store. 142 s.AddCharmWithRevision(c, "varnish", 5) 143 s.AddService(c, "varnish", "varnish", nil) 144 s.AddUnit(c, "varnish", "3") 145 }