launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	gc "launchpad.net/gocheck"
     9  	"net/url"
    10  
    11  	"launchpad.net/juju-core/charm"
    12  	charmtesting "launchpad.net/juju-core/charm/testing"
    13  	jujutesting "launchpad.net/juju-core/juju/testing"
    14  	"launchpad.net/juju-core/state"
    15  	coretesting "launchpad.net/juju-core/testing"
    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  	jujutesting.JujuConnSuite
    23  
    24  	Server *charmtesting.MockStore
    25  	charms map[string]*state.Charm
    26  }
    27  
    28  func (s *CharmSuite) SetUpSuite(c *gc.C) {
    29  	s.JujuConnSuite.SetUpSuite(c)
    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) SetUpTest(c *gc.C) {
    41  	s.JujuConnSuite.SetUpTest(c)
    42  	s.PatchValue(&charm.CacheDir, c.MkDir())
    43  	s.PatchValue(&charm.Store, &charm.CharmStore{BaseURL: s.Server.Address()})
    44  	s.Server.Downloads = nil
    45  	s.Server.Authorizations = nil
    46  	s.Server.Metadata = nil
    47  	s.charms = make(map[string]*state.Charm)
    48  }
    49  
    50  func (s *CharmSuite) TearDownSuite(c *gc.C) {
    51  	s.Server.Close()
    52  	s.JujuConnSuite.TearDownSuite(c)
    53  }
    54  
    55  // UpdateStoreRevision sets the revision of the specified charm to rev.
    56  func (s *CharmSuite) UpdateStoreRevision(ch string, rev int) {
    57  	s.Server.UpdateStoreRevision(ch, rev)
    58  }
    59  
    60  // AddMachine adds a new machine to state.
    61  func (s *CharmSuite) AddMachine(c *gc.C, machineId string, job state.MachineJob) {
    62  	m, err := s.State.AddOneMachine(state.MachineTemplate{
    63  		Series: "quantal",
    64  		Jobs:   []state.MachineJob{job},
    65  	})
    66  	c.Assert(err, gc.IsNil)
    67  	c.Assert(m.Id(), gc.Equals, machineId)
    68  	cons, err := m.Constraints()
    69  	c.Assert(err, gc.IsNil)
    70  	inst, hc := jujutesting.AssertStartInstanceWithConstraints(c, s.Conn.Environ, m.Id(), cons)
    71  	err = m.SetProvisioned(inst.Id(), "fake_nonce", hc)
    72  	c.Assert(err, gc.IsNil)
    73  
    74  }
    75  
    76  // AddCharmWithRevision adds a charm with the specified revision to state.
    77  func (s *CharmSuite) AddCharmWithRevision(c *gc.C, charmName string, rev int) *state.Charm {
    78  	ch := coretesting.Charms.Dir(charmName)
    79  	name := ch.Meta().Name
    80  	curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", name, rev))
    81  	bundleURL, err := url.Parse(fmt.Sprintf("http://bundles.testing.invalid/%s-%d", name, rev))
    82  	c.Assert(err, gc.IsNil)
    83  	dummy, err := s.State.AddCharm(ch, curl, bundleURL, fmt.Sprintf("%s-%d-sha256", name, rev))
    84  	c.Assert(err, gc.IsNil)
    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) {
    91  	ch, ok := s.charms[charmName]
    92  	c.Assert(ok, gc.Equals, true)
    93  	_, err := s.State.AddService(serviceName, "user-admin", ch)
    94  	c.Assert(err, gc.IsNil)
    95  }
    96  
    97  // AddUnit adds a new unit for service to the specified machine.
    98  func (s *CharmSuite) AddUnit(c *gc.C, serviceName, machineId string) {
    99  	svc, err := s.State.Service(serviceName)
   100  	c.Assert(err, gc.IsNil)
   101  	u, err := svc.AddUnit()
   102  	c.Assert(err, gc.IsNil)
   103  	m, err := s.State.Machine(machineId)
   104  	c.Assert(err, gc.IsNil)
   105  	err = u.AssignToMachine(m)
   106  	c.Assert(err, gc.IsNil)
   107  }
   108  
   109  // SetUnitRevision sets the unit's charm to the specified revision.
   110  func (s *CharmSuite) SetUnitRevision(c *gc.C, unitName string, rev int) {
   111  	u, err := s.State.Unit(unitName)
   112  	c.Assert(err, gc.IsNil)
   113  	svc, err := u.Service()
   114  	c.Assert(err, gc.IsNil)
   115  	curl := charm.MustParseURL(fmt.Sprintf("cs:quantal/%s-%d", svc.Name(), rev))
   116  	err = u.SetCharmURL(curl)
   117  	c.Assert(err, gc.IsNil)
   118  }
   119  
   120  // SetupScenario adds some machines and services to state.
   121  // It assumes a state server machine has already been created.
   122  func (s *CharmSuite) SetupScenario(c *gc.C) {
   123  	s.AddMachine(c, "1", state.JobHostUnits)
   124  	s.AddMachine(c, "2", state.JobHostUnits)
   125  	s.AddMachine(c, "3", state.JobHostUnits)
   126  
   127  	// mysql is out of date
   128  	s.AddCharmWithRevision(c, "mysql", 22)
   129  	s.AddService(c, "mysql", "mysql")
   130  	s.AddUnit(c, "mysql", "1")
   131  
   132  	// wordpress is up to date
   133  	s.AddCharmWithRevision(c, "wordpress", 26)
   134  	s.AddService(c, "wordpress", "wordpress")
   135  	s.AddUnit(c, "wordpress", "2")
   136  	s.AddUnit(c, "wordpress", "2")
   137  	// wordpress/0 has a version, wordpress/1 is unknown
   138  	s.SetUnitRevision(c, "wordpress/0", 26)
   139  
   140  	// varnish is a charm that does not have a version in the mock store.
   141  	s.AddCharmWithRevision(c, "varnish", 5)
   142  	s.AddService(c, "varnish", "varnish")
   143  	s.AddUnit(c, "varnish", "3")
   144  }