OLD | NEW |
1 package charm_test | 1 package charm_test |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "fmt" | 5 "fmt" |
6 "io/ioutil" | 6 "io/ioutil" |
7 . "launchpad.net/gocheck" | 7 . "launchpad.net/gocheck" |
8 "launchpad.net/juju-core/charm" | 8 "launchpad.net/juju-core/charm" |
9 "launchpad.net/juju-core/testing" | 9 "launchpad.net/juju-core/testing" |
10 "os" | 10 "os" |
11 "os/exec" | 11 "os/exec" |
12 "path/filepath" | 12 "path/filepath" |
13 ) | 13 ) |
14 | 14 |
15 type BundleSuite struct { | 15 type BundleSuite struct { |
16 » repo *testing.Repo | 16 » repo testing.Repo |
17 bundlePath string | 17 bundlePath string |
18 } | 18 } |
19 | 19 |
20 var _ = Suite(&BundleSuite{}) | 20 var _ = Suite(&BundleSuite{}) |
21 | 21 |
22 func (s *BundleSuite) SetUpSuite(c *C) { | 22 func (s *BundleSuite) SetUpTest(c *C) { |
23 » s.bundlePath = testing.Charms.BundlePath(c.MkDir(), "dummy") | 23 » s.repo.Path = c.MkDir() |
| 24 » s.bundlePath = s.repo.Bundle("dummy") |
24 } | 25 } |
25 | 26 |
26 func (s *BundleSuite) TestReadBundle(c *C) { | 27 func (s *BundleSuite) TestReadBundle(c *C) { |
27 bundle, err := charm.ReadBundle(s.bundlePath) | 28 bundle, err := charm.ReadBundle(s.bundlePath) |
28 c.Assert(err, IsNil) | 29 c.Assert(err, IsNil) |
29 checkDummy(c, bundle, s.bundlePath) | 30 checkDummy(c, bundle, s.bundlePath) |
30 } | 31 } |
31 | 32 |
32 func (s *BundleSuite) TestReadBundleWithoutConfig(c *C) { | 33 func (s *BundleSuite) TestReadBundleWithoutConfig(c *C) { |
33 » path := testing.Charms.BundlePath(c.MkDir(), "varnish") | 34 » path := s.repo.Bundle("varnish") |
34 bundle, err := charm.ReadBundle(path) | 35 bundle, err := charm.ReadBundle(path) |
35 c.Assert(err, IsNil) | 36 c.Assert(err, IsNil) |
36 | 37 |
37 // A lacking config.yaml file still causes a proper | 38 // A lacking config.yaml file still causes a proper |
38 // Config value to be returned. | 39 // Config value to be returned. |
39 c.Assert(bundle.Config().Options, HasLen, 0) | 40 c.Assert(bundle.Config().Options, HasLen, 0) |
40 } | 41 } |
41 | 42 |
42 func (s *BundleSuite) TestReadBundleBytes(c *C) { | 43 func (s *BundleSuite) TestReadBundleBytes(c *C) { |
43 data, err := ioutil.ReadFile(s.bundlePath) | 44 data, err := ioutil.ReadFile(s.bundlePath) |
(...skipping 12 matching lines...) Expand all Loading... |
56 err = bundle.ExpandTo(path) | 57 err = bundle.ExpandTo(path) |
57 c.Assert(err, IsNil) | 58 c.Assert(err, IsNil) |
58 | 59 |
59 dir, err := charm.ReadDir(path) | 60 dir, err := charm.ReadDir(path) |
60 c.Assert(err, IsNil) | 61 c.Assert(err, IsNil) |
61 checkDummy(c, dir, path) | 62 checkDummy(c, dir, path) |
62 } | 63 } |
63 | 64 |
64 func (s *BundleSuite) TestBundleFileModes(c *C) { | 65 func (s *BundleSuite) TestBundleFileModes(c *C) { |
65 // Apply subtler mode differences than can be expressed in Bazaar. | 66 // Apply subtler mode differences than can be expressed in Bazaar. |
66 » srcPath := testing.Charms.ClonedDirPath(c.MkDir(), "dummy") | 67 » srcPath := s.repo.Dir("dummy").Path |
67 modes := []struct { | 68 modes := []struct { |
68 path string | 69 path string |
69 mode os.FileMode | 70 mode os.FileMode |
70 }{ | 71 }{ |
71 {"hooks/install", 0751}, | 72 {"hooks/install", 0751}, |
72 {"empty", 0750}, | 73 {"empty", 0750}, |
73 {"src/hello.c", 0614}, | 74 {"src/hello.c", 0614}, |
74 } | 75 } |
75 for _, m := range modes { | 76 for _, m := range modes { |
76 err := os.Chmod(filepath.Join(srcPath, m.path), m.mode) | 77 err := os.Chmod(filepath.Join(srcPath, m.path), m.mode) |
(...skipping 28 matching lines...) Expand all Loading... |
105 info, err = os.Stat(filepath.Join(path, "empty")) | 106 info, err = os.Stat(filepath.Join(path, "empty")) |
106 c.Assert(err, IsNil) | 107 c.Assert(err, IsNil) |
107 c.Assert(info.Mode()&0777, Equals, os.FileMode(0755)) | 108 c.Assert(info.Mode()&0777, Equals, os.FileMode(0755)) |
108 | 109 |
109 target, err := os.Readlink(filepath.Join(path, "hooks", "symlink")) | 110 target, err := os.Readlink(filepath.Join(path, "hooks", "symlink")) |
110 c.Assert(err, IsNil) | 111 c.Assert(err, IsNil) |
111 c.Assert(target, Equals, "../target") | 112 c.Assert(target, Equals, "../target") |
112 } | 113 } |
113 | 114 |
114 func (s *BundleSuite) TestBundleRevisionFile(c *C) { | 115 func (s *BundleSuite) TestBundleRevisionFile(c *C) { |
115 » charmDir := testing.Charms.ClonedDirPath(c.MkDir(), "dummy") | 116 » charmDir := s.repo.Dir("dummy").Path |
116 revPath := filepath.Join(charmDir, "revision") | 117 revPath := filepath.Join(charmDir, "revision") |
117 | 118 |
118 // Missing revision file | 119 // Missing revision file |
119 err := os.Remove(revPath) | 120 err := os.Remove(revPath) |
120 c.Assert(err, IsNil) | 121 c.Assert(err, IsNil) |
121 | 122 |
122 bundle, err := charm.ReadBundle(extBundleDir(c, charmDir)) | 123 bundle, err := charm.ReadBundle(extBundleDir(c, charmDir)) |
123 c.Assert(err, IsNil) | 124 c.Assert(err, IsNil) |
124 c.Assert(bundle.Revision(), Equals, 0) | 125 c.Assert(bundle.Revision(), Equals, 0) |
125 | 126 |
(...skipping 27 matching lines...) Expand all Loading... |
153 path := filepath.Join(c.MkDir(), "charm") | 154 path := filepath.Join(c.MkDir(), "charm") |
154 err = bundle.ExpandTo(path) | 155 err = bundle.ExpandTo(path) |
155 c.Assert(err, IsNil) | 156 c.Assert(err, IsNil) |
156 | 157 |
157 dir, err := charm.ReadDir(path) | 158 dir, err := charm.ReadDir(path) |
158 c.Assert(err, IsNil) | 159 c.Assert(err, IsNil) |
159 c.Assert(dir.Revision(), Equals, 42) | 160 c.Assert(dir.Revision(), Equals, 42) |
160 } | 161 } |
161 | 162 |
162 func (s *BundleSuite) TestExpandToWithBadLink(c *C) { | 163 func (s *BundleSuite) TestExpandToWithBadLink(c *C) { |
163 » charmDir := testing.Charms.ClonedDirPath(c.MkDir(), "dummy") | 164 » charmDir := s.repo.Dir("dummy").Path |
164 badLink := filepath.Join(charmDir, "hooks", "badlink") | 165 badLink := filepath.Join(charmDir, "hooks", "badlink") |
165 | 166 |
166 // Symlink targeting a path outside of the charm. | 167 // Symlink targeting a path outside of the charm. |
167 err := os.Symlink("../../target", badLink) | 168 err := os.Symlink("../../target", badLink) |
168 c.Assert(err, IsNil) | 169 c.Assert(err, IsNil) |
169 | 170 |
170 bundle, err := charm.ReadBundle(extBundleDir(c, charmDir)) | 171 bundle, err := charm.ReadBundle(extBundleDir(c, charmDir)) |
171 c.Assert(err, IsNil) | 172 c.Assert(err, IsNil) |
172 | 173 |
173 path := filepath.Join(c.MkDir(), "charm") | 174 path := filepath.Join(c.MkDir(), "charm") |
(...skipping 13 matching lines...) Expand all Loading... |
187 c.Assert(err, ErrorMatches, `symlink "hooks/badlink" is absolute: "/targ
et"`) | 188 c.Assert(err, ErrorMatches, `symlink "hooks/badlink" is absolute: "/targ
et"`) |
188 } | 189 } |
189 | 190 |
190 func extBundleDir(c *C, dirpath string) (path string) { | 191 func extBundleDir(c *C, dirpath string) (path string) { |
191 path = filepath.Join(c.MkDir(), "bundle.charm") | 192 path = filepath.Join(c.MkDir(), "bundle.charm") |
192 cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cd %s; zip --fifo --sy
mlinks -r %s .", dirpath, path)) | 193 cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cd %s; zip --fifo --sy
mlinks -r %s .", dirpath, path)) |
193 output, err := cmd.CombinedOutput() | 194 output, err := cmd.CombinedOutput() |
194 c.Assert(err, IsNil, Commentf("Command output: %s", output)) | 195 c.Assert(err, IsNil, Commentf("Command output: %s", output)) |
195 return path | 196 return path |
196 } | 197 } |
OLD | NEW |