github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/juju/testing/repo.go (about)

     1  package testing
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sort"
     7  
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils"
    10  	"github.com/juju/utils/symlink"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/charm.v6-unstable"
    13  
    14  	"github.com/juju/juju/environs/config"
    15  	"github.com/juju/juju/state"
    16  	"github.com/juju/juju/state/storage"
    17  )
    18  
    19  // RepoSuite acts as a JujuConnSuite but also sets up
    20  // $JUJU_REPOSITORY to point to a local charm repository.
    21  type RepoSuite struct {
    22  	JujuConnSuite
    23  	SeriesPath string
    24  	RepoPath   string
    25  }
    26  
    27  func (s *RepoSuite) SetUpTest(c *gc.C) {
    28  	s.JujuConnSuite.SetUpTest(c)
    29  	// Change the environ's config to ensure we're using the one in state,
    30  	// not the one in the local environments.yaml
    31  	updateAttrs := map[string]interface{}{"default-series": config.LatestLtsSeries()}
    32  	err := s.State.UpdateEnvironConfig(updateAttrs, nil, nil)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	s.RepoPath = os.Getenv("JUJU_REPOSITORY")
    35  	repoPath := c.MkDir()
    36  	os.Setenv("JUJU_REPOSITORY", repoPath)
    37  	s.SeriesPath = filepath.Join(repoPath, config.LatestLtsSeries())
    38  	err = os.Mkdir(s.SeriesPath, 0777)
    39  	c.Assert(err, jc.ErrorIsNil)
    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 = symlink.New(s.SeriesPath, filepath.Join(repoPath, "quantal"))
    45  	c.Assert(err, jc.ErrorIsNil)
    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, jc.ErrorIsNil)
    56  	ch, _, err := svc.Charm()
    57  	c.Assert(err, jc.ErrorIsNil)
    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, jc.ErrorIsNil)
    63  	c.Assert(units, gc.HasLen, unitCount)
    64  	s.AssertUnitMachines(c, units)
    65  	rels, err := svc.Relations()
    66  	c.Assert(err, jc.ErrorIsNil)
    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, jc.ErrorIsNil)
    74  
    75  	storage := storage.NewStorage(s.State.EnvironUUID(), s.State.MongoSession())
    76  	r, _, err := storage.Get(ch.StoragePath())
    77  	c.Assert(err, jc.ErrorIsNil)
    78  	defer r.Close()
    79  
    80  	digest, _, err := utils.ReadSHA256(r)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(ch.BundleSha256(), gc.Equals, digest)
    83  }
    84  
    85  func (s *RepoSuite) AssertUnitMachines(c *gc.C, units []*state.Unit) {
    86  	expectUnitNames := []string{}
    87  	for _, u := range units {
    88  		expectUnitNames = append(expectUnitNames, u.Name())
    89  	}
    90  	sort.Strings(expectUnitNames)
    91  
    92  	machines, err := s.State.AllMachines()
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(machines, gc.HasLen, len(units))
    95  	unitNames := []string{}
    96  	for _, m := range machines {
    97  		mUnits, err := m.Units()
    98  		c.Assert(err, jc.ErrorIsNil)
    99  		c.Assert(mUnits, gc.HasLen, 1)
   100  		unitNames = append(unitNames, mUnits[0].Name())
   101  	}
   102  	sort.Strings(unitNames)
   103  	c.Assert(unitNames, gc.DeepEquals, expectUnitNames)
   104  }