launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/juju/testing/repo.go (about)

     1  package testing
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"path/filepath"
     7  	"sort"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"launchpad.net/juju-core/charm"
    12  	"launchpad.net/juju-core/state"
    13  	"launchpad.net/juju-core/utils"
    14  )
    15  
    16  // RepoSuite acts as a JujuConnSuite but also sets up
    17  // $JUJU_REPOSITORY to point to a local charm repository.
    18  type RepoSuite struct {
    19  	JujuConnSuite
    20  	SeriesPath string
    21  	RepoPath   string
    22  }
    23  
    24  func (s *RepoSuite) SetUpTest(c *gc.C) {
    25  	s.JujuConnSuite.SetUpTest(c)
    26  	// Change the environ's config to ensure we're using the one in state,
    27  	// not the one in the local environments.yaml
    28  	oldcfg, err := s.State.EnvironConfig()
    29  	c.Assert(err, gc.IsNil)
    30  	cfg, err := oldcfg.Apply(map[string]interface{}{"default-series": "precise"})
    31  	c.Assert(err, gc.IsNil)
    32  	err = s.State.SetEnvironConfig(cfg, oldcfg)
    33  	c.Assert(err, gc.IsNil)
    34  	s.RepoPath = os.Getenv("JUJU_REPOSITORY")
    35  	repoPath := c.MkDir()
    36  	os.Setenv("JUJU_REPOSITORY", repoPath)
    37  	s.SeriesPath = filepath.Join(repoPath, "precise")
    38  	err = os.Mkdir(s.SeriesPath, 0777)
    39  	c.Assert(err, gc.IsNil)
    40  	// Create a symlink "quantal" -> "precise", because most charms
    41  	// and machines are written with hard-coded "quantal" series,
    42  	// hence they interact badly with a local repository that assumes
    43  	// only "precise" charms are available.
    44  	err = os.Symlink(s.SeriesPath, filepath.Join(repoPath, "quantal"))
    45  	c.Assert(err, gc.IsNil)
    46  }
    47  
    48  func (s *RepoSuite) TearDownTest(c *gc.C) {
    49  	os.Setenv("JUJU_REPOSITORY", s.RepoPath)
    50  	s.JujuConnSuite.TearDownTest(c)
    51  }
    52  
    53  func (s *RepoSuite) AssertService(c *gc.C, name string, expectCurl *charm.URL, unitCount, relCount int) (*state.Service, []*state.Relation) {
    54  	svc, err := s.State.Service(name)
    55  	c.Assert(err, gc.IsNil)
    56  	ch, _, err := svc.Charm()
    57  	c.Assert(err, gc.IsNil)
    58  	c.Assert(ch.URL(), gc.DeepEquals, expectCurl)
    59  	s.AssertCharmUploaded(c, expectCurl)
    60  	units, err := svc.AllUnits()
    61  	c.Logf("Service units: %+v", units)
    62  	c.Assert(err, gc.IsNil)
    63  	c.Assert(units, gc.HasLen, unitCount)
    64  	s.AssertUnitMachines(c, units)
    65  	rels, err := svc.Relations()
    66  	c.Assert(err, gc.IsNil)
    67  	c.Assert(rels, gc.HasLen, relCount)
    68  	return svc, rels
    69  }
    70  
    71  func (s *RepoSuite) AssertCharmUploaded(c *gc.C, curl *charm.URL) {
    72  	ch, err := s.State.Charm(curl)
    73  	c.Assert(err, gc.IsNil)
    74  	url := ch.BundleURL()
    75  	resp, err := http.Get(url.String())
    76  	c.Assert(err, gc.IsNil)
    77  	defer resp.Body.Close()
    78  	digest, _, err := utils.ReadSHA256(resp.Body)
    79  	c.Assert(err, gc.IsNil)
    80  	c.Assert(ch.BundleSha256(), gc.Equals, digest)
    81  }
    82  
    83  func (s *RepoSuite) AssertUnitMachines(c *gc.C, units []*state.Unit) {
    84  	expectUnitNames := []string{}
    85  	for _, u := range units {
    86  		expectUnitNames = append(expectUnitNames, u.Name())
    87  	}
    88  	sort.Strings(expectUnitNames)
    89  
    90  	machines, err := s.State.AllMachines()
    91  	c.Assert(err, gc.IsNil)
    92  	c.Assert(machines, gc.HasLen, len(units))
    93  	unitNames := []string{}
    94  	for _, m := range machines {
    95  		mUnits, err := m.Units()
    96  		c.Assert(err, gc.IsNil)
    97  		c.Assert(mUnits, gc.HasLen, 1)
    98  		unitNames = append(unitNames, mUnits[0].Name())
    99  	}
   100  	sort.Strings(unitNames)
   101  	c.Assert(unitNames, gc.DeepEquals, expectUnitNames)
   102  }