github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/juju/testing/repo.go (about)

     1  package testing
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/juju/names"
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/juju/utils"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/charm.v6-unstable"
    11  
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/state"
    14  	"github.com/juju/juju/state/storage"
    15  )
    16  
    17  type RepoSuite struct {
    18  	JujuConnSuite
    19  	CharmsPath string
    20  }
    21  
    22  func (s *RepoSuite) SetUpTest(c *gc.C) {
    23  	s.JujuConnSuite.SetUpTest(c)
    24  	s.CharmsPath = c.MkDir()
    25  	// Change the environ's config to ensure we're using the one in state.
    26  	updateAttrs := map[string]interface{}{"default-series": config.LatestLtsSeries()}
    27  	err := s.State.UpdateModelConfig(updateAttrs, nil, nil)
    28  	c.Assert(err, jc.ErrorIsNil)
    29  }
    30  
    31  func (s *RepoSuite) AssertService(c *gc.C, name string, expectCurl *charm.URL, unitCount, relCount int) (*state.Service, []*state.Relation) {
    32  	svc, err := s.State.Service(name)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	ch, _, err := svc.Charm()
    35  	c.Assert(err, jc.ErrorIsNil)
    36  	c.Assert(ch.URL(), gc.DeepEquals, expectCurl)
    37  	s.AssertCharmUploaded(c, expectCurl)
    38  
    39  	units, err := svc.AllUnits()
    40  	c.Logf("Service units: %+v", units)
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	c.Assert(units, gc.HasLen, unitCount)
    43  	s.AssertUnitMachines(c, units)
    44  	rels, err := svc.Relations()
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(rels, gc.HasLen, relCount)
    47  	return svc, rels
    48  }
    49  
    50  func (s *RepoSuite) AssertCharmUploaded(c *gc.C, curl *charm.URL) {
    51  	ch, err := s.State.Charm(curl)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  
    54  	storage := storage.NewStorage(s.State.ModelUUID(), s.State.MongoSession())
    55  	r, _, err := storage.Get(ch.StoragePath())
    56  	c.Assert(err, jc.ErrorIsNil)
    57  	defer r.Close()
    58  
    59  	digest, _, err := utils.ReadSHA256(r)
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	c.Assert(ch.BundleSha256(), gc.Equals, digest)
    62  }
    63  
    64  func (s *RepoSuite) AssertUnitMachines(c *gc.C, units []*state.Unit) {
    65  	tags := make([]names.UnitTag, len(units))
    66  	expectUnitNames := make([]string, len(units))
    67  	for i, u := range units {
    68  		expectUnitNames[i] = u.Name()
    69  		tags[i] = u.UnitTag()
    70  	}
    71  
    72  	// manually assign all units to machines.  This replaces work normally done
    73  	// by the unitassigner code.
    74  	errs, err := s.APIState.UnitAssigner().AssignUnits(tags)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(errs, gc.DeepEquals, make([]error, len(units)))
    77  
    78  	sort.Strings(expectUnitNames)
    79  
    80  	machines, err := s.State.AllMachines()
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(machines, gc.HasLen, len(units))
    83  
    84  	unitNames := []string{}
    85  	for _, m := range machines {
    86  		mUnits, err := m.Units()
    87  		c.Assert(err, jc.ErrorIsNil)
    88  		c.Assert(mUnits, gc.HasLen, 1)
    89  		unitNames = append(unitNames, mUnits[0].Name())
    90  	}
    91  	sort.Strings(unitNames)
    92  	c.Assert(unitNames, gc.DeepEquals, expectUnitNames)
    93  }