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