github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	"launchpad.net/juju-core/charm"
    13  	charmtesting "launchpad.net/juju-core/charm/testing"
    14  	jujutesting "launchpad.net/juju-core/juju/testing"
    15  	"launchpad.net/juju-core/state"
    16  	coretesting "launchpad.net/juju-core/testing"
    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  	jujutesting.JujuConnSuite
    24  
    25  	Server *charmtesting.MockStore
    26  	charms map[string]*state.Charm
    27  }
    28  
    29  func (s *CharmSuite) SetUpSuite(c *gc.C) {
    30  	s.JujuConnSuite.SetUpSuite(c)
    31  	s.Server = charmtesting.NewMockStore(c, 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) SetUpTest(c *gc.C) {
    42  	s.JujuConnSuite.SetUpTest(c)
    43  	s.PatchValue(&charm.CacheDir, c.MkDir())
    44  	s.PatchValue(&charm.Store, &charm.CharmStore{BaseURL: s.Server.Address()})
    45  	s.Server.Downloads = nil
    46  	s.Server.Authorizations = nil
    47  	s.Server.Metadata = nil
    48  	s.charms = make(map[string]*state.Charm)
    49  }
    50  
    51  func (s *CharmSuite) TearDownSuite(c *gc.C) {
    52  	s.Server.Close()
    53  	s.JujuConnSuite.TearDownSuite(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.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.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 := coretesting.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.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) {
    92  	ch, ok := s.charms[charmName]
    93  	c.Assert(ok, gc.Equals, true)
    94  	_, err := s.State.AddService(serviceName, "user-admin", ch)
    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.State.Service(serviceName)
   101  	c.Assert(err, gc.IsNil)
   102  	u, err := svc.AddUnit()
   103  	c.Assert(err, gc.IsNil)
   104  	m, err := s.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.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")
   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")
   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")
   144  	s.AddUnit(c, "varnish", "3")
   145  }