github.com/juju/charmrepo/v7@v7.0.1/charmpath_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENCE file for details. 3 4 package charmrepo_test // import "github.com/juju/charmrepo/v7" 5 6 import ( 7 "os" 8 "path/filepath" 9 10 "github.com/juju/charm/v9" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/charmrepo/v7" 15 ) 16 17 type charmPathSuite struct { 18 repoPath string 19 } 20 21 var _ = gc.Suite(&charmPathSuite{}) 22 23 func (s *charmPathSuite) SetUpTest(c *gc.C) { 24 s.repoPath = c.MkDir() 25 } 26 27 func (s *charmPathSuite) cloneCharmDir(path, name string) string { 28 return TestCharms.ClonedDirPath(path, name) 29 } 30 31 func (s *charmPathSuite) TestNoPath(c *gc.C) { 32 _, _, err := charmrepo.NewCharmAtPath("", "trusty") 33 c.Assert(err, gc.ErrorMatches, "empty charm path") 34 } 35 36 func (s *charmPathSuite) TestInvalidPath(c *gc.C) { 37 _, _, err := charmrepo.NewCharmAtPath("/foo", "trusty") 38 c.Assert(err, gc.Equals, os.ErrNotExist) 39 } 40 41 func (s *charmPathSuite) TestRepoURL(c *gc.C) { 42 _, _, err := charmrepo.NewCharmAtPath("cs:foo", "trusty") 43 c.Assert(err, gc.Equals, os.ErrNotExist) 44 } 45 46 func (s *charmPathSuite) TestInvalidRelativePath(c *gc.C) { 47 _, _, err := charmrepo.NewCharmAtPath("./foo", "trusty") 48 c.Assert(err, gc.Equals, os.ErrNotExist) 49 } 50 51 func (s *charmPathSuite) TestRelativePath(c *gc.C) { 52 s.cloneCharmDir(s.repoPath, "mysql") 53 cwd, err := os.Getwd() 54 c.Assert(err, jc.ErrorIsNil) 55 defer os.Chdir(cwd) 56 c.Assert(os.Chdir(s.repoPath), jc.ErrorIsNil) 57 _, _, err = charmrepo.NewCharmAtPath("mysql", "trusty") 58 c.Assert(charmrepo.IsInvalidPathError(err), jc.IsTrue) 59 } 60 61 func (s *charmPathSuite) TestNoCharmAtPath(c *gc.C) { 62 _, _, err := charmrepo.NewCharmAtPath(c.MkDir(), "trusty") 63 c.Assert(err, gc.ErrorMatches, "charm not found.*") 64 } 65 66 func (s *charmPathSuite) TestCharm(c *gc.C) { 67 charmDir := filepath.Join(s.repoPath, "mysql") 68 s.cloneCharmDir(s.repoPath, "mysql") 69 ch, url, err := charmrepo.NewCharmAtPath(charmDir, "quantal") 70 c.Assert(err, jc.ErrorIsNil) 71 c.Assert(ch.Meta().Name, gc.Equals, "mysql") 72 c.Assert(ch.Revision(), gc.Equals, 1) 73 c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:quantal/mysql-1")) 74 } 75 76 func (s *charmPathSuite) TestNoSeriesSpecified(c *gc.C) { 77 charmDir := filepath.Join(s.repoPath, "mysql") 78 s.cloneCharmDir(s.repoPath, "mysql") 79 _, _, err := charmrepo.NewCharmAtPath(charmDir, "") 80 c.Assert(err, gc.ErrorMatches, "series not specified and charm does not define any") 81 } 82 83 func (s *charmPathSuite) TestNoSeriesSpecifiedForceStillFails(c *gc.C) { 84 charmDir := filepath.Join(s.repoPath, "mysql") 85 s.cloneCharmDir(s.repoPath, "mysql") 86 _, _, err := charmrepo.NewCharmAtPathForceSeries(charmDir, "", true) 87 c.Assert(err, gc.ErrorMatches, "series not specified and charm does not define any") 88 } 89 90 func (s *charmPathSuite) TestMuliSeriesDefault(c *gc.C) { 91 charmDir := filepath.Join(s.repoPath, "multi-series") 92 s.cloneCharmDir(s.repoPath, "multi-series") 93 ch, url, err := charmrepo.NewCharmAtPath(charmDir, "") 94 c.Assert(err, gc.IsNil) 95 c.Assert(ch.Meta().Name, gc.Equals, "new-charm-with-multi-series") 96 c.Assert(ch.Revision(), gc.Equals, 7) 97 c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:precise/multi-series-7")) 98 } 99 100 func (s *charmPathSuite) TestMuliSeries(c *gc.C) { 101 charmDir := filepath.Join(s.repoPath, "multi-series") 102 s.cloneCharmDir(s.repoPath, "multi-series") 103 ch, url, err := charmrepo.NewCharmAtPath(charmDir, "trusty") 104 c.Assert(err, gc.IsNil) 105 c.Assert(ch.Meta().Name, gc.Equals, "new-charm-with-multi-series") 106 c.Assert(ch.Revision(), gc.Equals, 7) 107 c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:trusty/multi-series-7")) 108 } 109 110 func (s *charmPathSuite) TestUnsupportedSeries(c *gc.C) { 111 charmDir := filepath.Join(s.repoPath, "multi-series") 112 s.cloneCharmDir(s.repoPath, "multi-series") 113 _, _, err := charmrepo.NewCharmAtPath(charmDir, "wily") 114 c.Assert(err, gc.ErrorMatches, `series "wily" not supported by charm, supported series are.*`) 115 } 116 117 func (s *charmPathSuite) TestUnsupportedSeriesNoForce(c *gc.C) { 118 charmDir := filepath.Join(s.repoPath, "multi-series") 119 s.cloneCharmDir(s.repoPath, "multi-series") 120 _, _, err := charmrepo.NewCharmAtPathForceSeries(charmDir, "wily", false) 121 c.Assert(err, gc.ErrorMatches, `series "wily" not supported by charm, supported series are.*`) 122 } 123 124 func (s *charmPathSuite) TestUnsupportedSeriesForce(c *gc.C) { 125 charmDir := filepath.Join(s.repoPath, "multi-series") 126 s.cloneCharmDir(s.repoPath, "multi-series") 127 ch, url, err := charmrepo.NewCharmAtPathForceSeries(charmDir, "wily", true) 128 c.Assert(err, jc.ErrorIsNil) 129 c.Assert(ch.Meta().Name, gc.Equals, "new-charm-with-multi-series") 130 c.Assert(ch.Revision(), gc.Equals, 7) 131 c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:wily/multi-series-7")) 132 } 133 134 func (s *charmPathSuite) TestFindsSymlinks(c *gc.C) { 135 realPath := TestCharms.ClonedDirPath(c.MkDir(), "dummy") 136 charmsPath := c.MkDir() 137 linkPath := filepath.Join(charmsPath, "dummy") 138 err := os.Symlink(realPath, linkPath) 139 c.Assert(err, gc.IsNil) 140 141 ch, url, err := charmrepo.NewCharmAtPath(filepath.Join(charmsPath, "dummy"), "quantal") 142 c.Assert(err, gc.IsNil) 143 c.Assert(ch.Revision(), gc.Equals, 1) 144 c.Assert(ch.Meta().Name, gc.Equals, "dummy") 145 c.Assert(ch.Config().Options["title"].Default, gc.Equals, "My Title") 146 c.Assert(ch.(*charm.CharmDir).Path, gc.Equals, linkPath) 147 c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:quantal/dummy-1")) 148 }