1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
12
"gopkg.in/juju/charm.v6-unstable"
14
"gopkg.in/juju/charmrepo.v2-unstable"
17
type charmPathSuite struct {
21
var _ = gc.Suite(&charmPathSuite{})
23
func (s *charmPathSuite) SetUpTest(c *gc.C) {
24
s.repoPath = c.MkDir()
27
func (s *charmPathSuite) cloneCharmDir(path, name string) string {
28
return TestCharms.ClonedDirPath(path, name)
31
func (s *charmPathSuite) TestNoPath(c *gc.C) {
32
_, _, err := charmrepo.NewCharmAtPath("", "trusty")
33
c.Assert(err, gc.ErrorMatches, "empty charm path")
36
func (s *charmPathSuite) TestInvalidPath(c *gc.C) {
37
_, _, err := charmrepo.NewCharmAtPath("/foo", "trusty")
38
c.Assert(err, gc.Equals, os.ErrNotExist)
41
func (s *charmPathSuite) TestRepoURL(c *gc.C) {
42
_, _, err := charmrepo.NewCharmAtPath("cs:foo", "trusty")
43
c.Assert(err, gc.Equals, os.ErrNotExist)
46
func (s *charmPathSuite) TestInvalidRelativePath(c *gc.C) {
47
_, _, err := charmrepo.NewCharmAtPath("./foo", "trusty")
48
c.Assert(err, gc.Equals, os.ErrNotExist)
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)
56
c.Assert(os.Chdir(s.repoPath), jc.ErrorIsNil)
57
_, _, err = charmrepo.NewCharmAtPath("mysql", "trusty")
58
c.Assert(charmrepo.IsInvalidPathError(err), jc.IsTrue)
61
func (s *charmPathSuite) TestNoCharmAtPath(c *gc.C) {
62
_, _, err := charmrepo.NewCharmAtPath(c.MkDir(), "trusty")
63
c.Assert(err, gc.ErrorMatches, "charm not found.*")
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"))
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")
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")
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"))
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"))
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.*`)
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.*`)
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"))
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)
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"))