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