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