github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/http/httptest" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/charm.v6-unstable" 13 "gopkg.in/juju/charmrepo.v1" 14 "gopkg.in/juju/charmrepo.v1/csclient" 15 "gopkg.in/juju/charmstore.v5-unstable" 16 17 "github.com/juju/juju/apiserver/charmrevisionupdater" 18 jujutesting "github.com/juju/juju/juju/testing" 19 "github.com/juju/juju/state" 20 "github.com/juju/juju/testcharms" 21 ) 22 23 // CharmSuite provides infrastructure to set up and perform tests associated 24 // with charm versioning. A testing charm store server is created and populated 25 // with some known charms used for testing. 26 type CharmSuite struct { 27 jcSuite *jujutesting.JujuConnSuite 28 29 Handler charmstore.HTTPCloseHandler 30 Server *httptest.Server 31 Client *csclient.Client 32 charms map[string]*state.Charm 33 } 34 35 func (s *CharmSuite) SetUpSuite(c *gc.C, jcSuite *jujutesting.JujuConnSuite) { 36 s.jcSuite = jcSuite 37 } 38 39 func (s *CharmSuite) TearDownSuite(c *gc.C) {} 40 41 func (s *CharmSuite) SetUpTest(c *gc.C) { 42 db := s.jcSuite.Session.DB("juju-testing") 43 params := charmstore.ServerParams{ 44 AuthUsername: "test-user", 45 AuthPassword: "test-password", 46 } 47 handler, err := charmstore.NewServer(db, nil, "", params, charmstore.V4) 48 c.Assert(err, jc.ErrorIsNil) 49 s.Handler = handler 50 s.Server = httptest.NewServer(handler) 51 s.Client = csclient.New(csclient.Params{ 52 URL: s.Server.URL, 53 User: params.AuthUsername, 54 Password: params.AuthPassword, 55 }) 56 urls := map[string]string{ 57 "mysql": "quantal/mysql-23", 58 "dummy": "quantal/dummy-24", 59 "riak": "quantal/riak-25", 60 "wordpress": "quantal/wordpress-26", 61 "logging": "quantal/logging-27", 62 } 63 for name, url := range urls { 64 testcharms.UploadCharm(c, s.Client, url, name) 65 } 66 s.jcSuite.PatchValue(&charmrepo.CacheDir, c.MkDir()) 67 // Patch the charm repo initializer function: it is replaced with a charm 68 // store repo pointing to the testing server. 69 s.jcSuite.PatchValue(&charmrevisionupdater.NewCharmStore, func(p charmrepo.NewCharmStoreParams) charmrepo.Interface { 70 p.URL = s.Server.URL 71 return charmrepo.NewCharmStore(p) 72 }) 73 s.charms = make(map[string]*state.Charm) 74 } 75 76 func (s *CharmSuite) TearDownTest(c *gc.C) { 77 s.Handler.Close() 78 s.Server.Close() 79 } 80 81 // AddMachine adds a new machine to state. 82 func (s *CharmSuite) AddMachine(c *gc.C, machineId string, job state.MachineJob) { 83 m, err := s.jcSuite.State.AddOneMachine(state.MachineTemplate{ 84 Series: "quantal", 85 Jobs: []state.MachineJob{job}, 86 }) 87 c.Assert(err, jc.ErrorIsNil) 88 c.Assert(m.Id(), gc.Equals, machineId) 89 cons, err := m.Constraints() 90 c.Assert(err, jc.ErrorIsNil) 91 inst, hc := jujutesting.AssertStartInstanceWithConstraints(c, s.jcSuite.Environ, m.Id(), cons) 92 err = m.SetProvisioned(inst.Id(), "fake_nonce", hc) 93 c.Assert(err, jc.ErrorIsNil) 94 } 95 96 // AddCharmWithRevision adds a charm with the specified revision to state. 97 func (s *CharmSuite) AddCharmWithRevision(c *gc.C, charmName string, rev int) *state.Charm { 98 ch := testcharms.Repo.CharmDir(charmName) 99 name := ch.Meta().Name 100 curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", name, rev)) 101 dummy, err := s.jcSuite.State.AddCharm(ch, curl, "dummy-path", fmt.Sprintf("%s-%d-sha256", name, rev)) 102 c.Assert(err, jc.ErrorIsNil) 103 s.charms[name] = dummy 104 return dummy 105 } 106 107 // AddService adds a service for the specified charm to state. 108 func (s *CharmSuite) AddService(c *gc.C, charmName, serviceName string, networks []string) { 109 ch, ok := s.charms[charmName] 110 c.Assert(ok, jc.IsTrue) 111 owner := s.jcSuite.AdminUserTag(c) 112 _, err := s.jcSuite.State.AddService(serviceName, owner.String(), ch, networks, nil) 113 c.Assert(err, jc.ErrorIsNil) 114 } 115 116 // AddUnit adds a new unit for service to the specified machine. 117 func (s *CharmSuite) AddUnit(c *gc.C, serviceName, machineId string) { 118 svc, err := s.jcSuite.State.Service(serviceName) 119 c.Assert(err, jc.ErrorIsNil) 120 u, err := svc.AddUnit() 121 c.Assert(err, jc.ErrorIsNil) 122 m, err := s.jcSuite.State.Machine(machineId) 123 c.Assert(err, jc.ErrorIsNil) 124 err = u.AssignToMachine(m) 125 c.Assert(err, jc.ErrorIsNil) 126 } 127 128 // SetUnitRevision sets the unit's charm to the specified revision. 129 func (s *CharmSuite) SetUnitRevision(c *gc.C, unitName string, rev int) { 130 u, err := s.jcSuite.State.Unit(unitName) 131 c.Assert(err, jc.ErrorIsNil) 132 svc, err := u.Service() 133 c.Assert(err, jc.ErrorIsNil) 134 curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", svc.Name(), rev)) 135 err = u.SetCharmURL(curl) 136 c.Assert(err, jc.ErrorIsNil) 137 } 138 139 // SetupScenario adds some machines and services to state. 140 // It assumes a state server machine has already been created. 141 func (s *CharmSuite) SetupScenario(c *gc.C) { 142 s.AddMachine(c, "1", state.JobHostUnits) 143 s.AddMachine(c, "2", state.JobHostUnits) 144 s.AddMachine(c, "3", state.JobHostUnits) 145 146 // mysql is out of date 147 s.AddCharmWithRevision(c, "mysql", 22) 148 s.AddService(c, "mysql", "mysql", nil) 149 s.AddUnit(c, "mysql", "1") 150 151 // wordpress is up to date 152 s.AddCharmWithRevision(c, "wordpress", 26) 153 s.AddService(c, "wordpress", "wordpress", nil) 154 s.AddUnit(c, "wordpress", "2") 155 s.AddUnit(c, "wordpress", "2") 156 // wordpress/0 has a version, wordpress/1 is unknown 157 s.SetUnitRevision(c, "wordpress/0", 26) 158 159 // varnish is a charm that does not have a version in the mock store. 160 s.AddCharmWithRevision(c, "varnish", 5) 161 s.AddService(c, "varnish", "varnish", nil) 162 s.AddUnit(c, "varnish", "3") 163 }