github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/juju/testing/repo.go (about) 1 package testing 2 3 import ( 4 "net/http" 5 "os" 6 "path/filepath" 7 "sort" 8 9 "github.com/juju/charm" 10 "github.com/juju/utils" 11 gc "launchpad.net/gocheck" 12 13 "github.com/juju/juju/state" 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 updateAttrs := map[string]interface{}{"default-series": "precise"} 29 err := s.State.UpdateEnvironConfig(updateAttrs, nil, nil) 30 c.Assert(err, gc.IsNil) 31 s.RepoPath = os.Getenv("JUJU_REPOSITORY") 32 repoPath := c.MkDir() 33 os.Setenv("JUJU_REPOSITORY", repoPath) 34 s.SeriesPath = filepath.Join(repoPath, "precise") 35 err = os.Mkdir(s.SeriesPath, 0777) 36 c.Assert(err, gc.IsNil) 37 // Create a symlink "quantal" -> "precise", because most charms 38 // and machines are written with hard-coded "quantal" series, 39 // hence they interact badly with a local repository that assumes 40 // only "precise" charms are available. 41 err = os.Symlink(s.SeriesPath, filepath.Join(repoPath, "quantal")) 42 c.Assert(err, gc.IsNil) 43 } 44 45 func (s *RepoSuite) TearDownTest(c *gc.C) { 46 os.Setenv("JUJU_REPOSITORY", s.RepoPath) 47 s.JujuConnSuite.TearDownTest(c) 48 } 49 50 func (s *RepoSuite) AssertService(c *gc.C, name string, expectCurl *charm.URL, unitCount, relCount int) (*state.Service, []*state.Relation) { 51 svc, err := s.State.Service(name) 52 c.Assert(err, gc.IsNil) 53 ch, _, err := svc.Charm() 54 c.Assert(err, gc.IsNil) 55 c.Assert(ch.URL(), gc.DeepEquals, expectCurl) 56 s.AssertCharmUploaded(c, expectCurl) 57 units, err := svc.AllUnits() 58 c.Logf("Service units: %+v", units) 59 c.Assert(err, gc.IsNil) 60 c.Assert(units, gc.HasLen, unitCount) 61 s.AssertUnitMachines(c, units) 62 rels, err := svc.Relations() 63 c.Assert(err, gc.IsNil) 64 c.Assert(rels, gc.HasLen, relCount) 65 return svc, rels 66 } 67 68 func (s *RepoSuite) AssertCharmUploaded(c *gc.C, curl *charm.URL) { 69 ch, err := s.State.Charm(curl) 70 c.Assert(err, gc.IsNil) 71 url := ch.BundleURL() 72 resp, err := http.Get(url.String()) 73 c.Assert(err, gc.IsNil) 74 defer resp.Body.Close() 75 digest, _, err := utils.ReadSHA256(resp.Body) 76 c.Assert(err, gc.IsNil) 77 c.Assert(ch.BundleSha256(), gc.Equals, digest) 78 } 79 80 func (s *RepoSuite) AssertUnitMachines(c *gc.C, units []*state.Unit) { 81 expectUnitNames := []string{} 82 for _, u := range units { 83 expectUnitNames = append(expectUnitNames, u.Name()) 84 } 85 sort.Strings(expectUnitNames) 86 87 machines, err := s.State.AllMachines() 88 c.Assert(err, gc.IsNil) 89 c.Assert(machines, gc.HasLen, len(units)) 90 unitNames := []string{} 91 for _, m := range machines { 92 mUnits, err := m.Units() 93 c.Assert(err, gc.IsNil) 94 c.Assert(mUnits, gc.HasLen, 1) 95 unitNames = append(unitNames, mUnits[0].Name()) 96 } 97 sort.Strings(unitNames) 98 c.Assert(unitNames, gc.DeepEquals, expectUnitNames) 99 }