github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/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 "net/url" 9 10 gc "launchpad.net/gocheck" 11 12 "github.com/juju/juju/charm" 13 charmtesting "github.com/juju/juju/charm/testing" 14 jujutesting "github.com/juju/juju/juju/testing" 15 "github.com/juju/juju/state" 16 ) 17 18 // CharmSuite provides infrastructure to set up and perform tests associated 19 // with charm versioning. A mock charm store is created using some known charms 20 // used for testing. 21 type CharmSuite struct { 22 jcSuite *jujutesting.JujuConnSuite 23 24 Server *charmtesting.MockStore 25 charms map[string]*state.Charm 26 } 27 28 func (s *CharmSuite) SetUpSuite(c *gc.C, jcSuite *jujutesting.JujuConnSuite) { 29 s.jcSuite = jcSuite 30 s.Server = charmtesting.NewMockStore(c, map[string]int{ 31 "cs:quantal/mysql": 23, 32 "cs:quantal/dummy": 24, 33 "cs:quantal/riak": 25, 34 "cs:quantal/wordpress": 26, 35 "cs:quantal/logging": 27, 36 "cs:quantal/borken": 28, 37 }) 38 } 39 40 func (s *CharmSuite) TearDownSuite(c *gc.C) { 41 s.Server.Close() 42 } 43 44 func (s *CharmSuite) SetUpTest(c *gc.C) { 45 s.jcSuite.PatchValue(&charm.CacheDir, c.MkDir()) 46 s.jcSuite.PatchValue(&charm.Store, &charm.CharmStore{BaseURL: s.Server.Address()}) 47 s.Server.Downloads = nil 48 s.Server.Authorizations = nil 49 s.Server.Metadata = nil 50 s.charms = make(map[string]*state.Charm) 51 } 52 53 func (s *CharmSuite) TearDownTest(c *gc.C) { 54 } 55 56 // UpdateStoreRevision sets the revision of the specified charm to rev. 57 func (s *CharmSuite) UpdateStoreRevision(ch string, rev int) { 58 s.Server.UpdateStoreRevision(ch, rev) 59 } 60 61 // AddMachine adds a new machine to state. 62 func (s *CharmSuite) AddMachine(c *gc.C, machineId string, job state.MachineJob) { 63 m, err := s.jcSuite.State.AddOneMachine(state.MachineTemplate{ 64 Series: "quantal", 65 Jobs: []state.MachineJob{job}, 66 }) 67 c.Assert(err, gc.IsNil) 68 c.Assert(m.Id(), gc.Equals, machineId) 69 cons, err := m.Constraints() 70 c.Assert(err, gc.IsNil) 71 inst, hc := jujutesting.AssertStartInstanceWithConstraints(c, s.jcSuite.Conn.Environ, m.Id(), cons) 72 err = m.SetProvisioned(inst.Id(), "fake_nonce", hc) 73 c.Assert(err, gc.IsNil) 74 75 } 76 77 // AddCharmWithRevision adds a charm with the specified revision to state. 78 func (s *CharmSuite) AddCharmWithRevision(c *gc.C, charmName string, rev int) *state.Charm { 79 ch := charmtesting.Charms.Dir(charmName) 80 name := ch.Meta().Name 81 curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", name, rev)) 82 bundleURL, err := url.Parse(fmt.Sprintf("http://bundles.testing.invalid/%s-%d", name, rev)) 83 c.Assert(err, gc.IsNil) 84 dummy, err := s.jcSuite.State.AddCharm(ch, curl, bundleURL, fmt.Sprintf("%s-%d-sha256", name, rev)) 85 c.Assert(err, gc.IsNil) 86 s.charms[name] = dummy 87 return dummy 88 } 89 90 // AddService adds a service for the specified charm to state. 91 func (s *CharmSuite) AddService(c *gc.C, charmName, serviceName string, networks []string) { 92 ch, ok := s.charms[charmName] 93 c.Assert(ok, gc.Equals, true) 94 _, err := s.jcSuite.State.AddService(serviceName, "user-admin", ch, networks) 95 c.Assert(err, gc.IsNil) 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, gc.IsNil) 102 u, err := svc.AddUnit() 103 c.Assert(err, gc.IsNil) 104 m, err := s.jcSuite.State.Machine(machineId) 105 c.Assert(err, gc.IsNil) 106 err = u.AssignToMachine(m) 107 c.Assert(err, gc.IsNil) 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, gc.IsNil) 114 svc, err := u.Service() 115 c.Assert(err, gc.IsNil) 116 curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", svc.Name(), rev)) 117 err = u.SetCharmURL(curl) 118 c.Assert(err, gc.IsNil) 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 }