forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charmarchive_test.go
384 lines (319 loc) · 11.2 KB
/
charmarchive_test.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"github.com/juju/collections/set"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"gopkg.in/juju/charm.v6"
)
type CharmArchiveSuite struct {
archivePath string
}
var _ = gc.Suite(&CharmArchiveSuite{})
func (s *CharmArchiveSuite) SetUpSuite(c *gc.C) {
s.archivePath = archivePath(c, readCharmDir(c, "dummy"))
}
var dummyManifest = []string{
"actions.yaml",
"config.yaml",
"empty",
"empty/.gitkeep",
"hooks",
"hooks/install",
"metadata.yaml",
"revision",
"src",
"src/hello.c",
}
func (s *CharmArchiveSuite) TestReadCharmArchive(c *gc.C) {
archive, err := charm.ReadCharmArchive(s.archivePath)
c.Assert(err, gc.IsNil)
checkDummy(c, archive, s.archivePath)
}
func (s *CharmArchiveSuite) TestReadCharmArchiveWithoutConfig(c *gc.C) {
// Technically varnish has no config AND no actions.
// Perhaps we should make this more orthogonal?
path := archivePath(c, readCharmDir(c, "varnish"))
archive, err := charm.ReadCharmArchive(path)
c.Assert(err, gc.IsNil)
// A lacking config.yaml file still causes a proper
// Config value to be returned.
c.Assert(archive.Config().Options, gc.HasLen, 0)
}
func (s *CharmArchiveSuite) TestReadCharmArchiveWithoutMetrics(c *gc.C) {
path := archivePath(c, readCharmDir(c, "varnish"))
dir, err := charm.ReadCharmArchive(path)
c.Assert(err, gc.IsNil)
// A lacking metrics.yaml file indicates the unit will not
// be metered.
c.Assert(dir.Metrics(), gc.IsNil)
}
func (s *CharmArchiveSuite) TestReadCharmArchiveWithEmptyMetrics(c *gc.C) {
path := archivePath(c, readCharmDir(c, "metered-empty"))
dir, err := charm.ReadCharmArchive(path)
c.Assert(err, gc.IsNil)
c.Assert(Keys(dir.Metrics()), gc.HasLen, 0)
}
func (s *CharmArchiveSuite) TestReadCharmArchiveWithCustomMetrics(c *gc.C) {
path := archivePath(c, readCharmDir(c, "metered"))
dir, err := charm.ReadCharmArchive(path)
c.Assert(err, gc.IsNil)
c.Assert(dir.Metrics(), gc.NotNil)
c.Assert(Keys(dir.Metrics()), gc.DeepEquals, []string{"juju-unit-time", "pings"})
}
func (s *CharmArchiveSuite) TestReadCharmArchiveWithoutActions(c *gc.C) {
// Wordpress has config but no actions.
path := archivePath(c, readCharmDir(c, "wordpress"))
archive, err := charm.ReadCharmArchive(path)
c.Assert(err, gc.IsNil)
// A lacking actions.yaml file still causes a proper
// Actions value to be returned.
c.Assert(archive.Actions().ActionSpecs, gc.HasLen, 0)
}
func (s *CharmArchiveSuite) TestReadCharmArchiveBytes(c *gc.C) {
data, err := ioutil.ReadFile(s.archivePath)
c.Assert(err, gc.IsNil)
archive, err := charm.ReadCharmArchiveBytes(data)
c.Assert(err, gc.IsNil)
checkDummy(c, archive, "")
}
func (s *CharmArchiveSuite) TestReadCharmArchiveFromReader(c *gc.C) {
f, err := os.Open(s.archivePath)
c.Assert(err, gc.IsNil)
defer f.Close()
info, err := f.Stat()
c.Assert(err, gc.IsNil)
archive, err := charm.ReadCharmArchiveFromReader(f, info.Size())
c.Assert(err, gc.IsNil)
checkDummy(c, archive, "")
}
func (s *CharmArchiveSuite) TestManifest(c *gc.C) {
archive, err := charm.ReadCharmArchive(s.archivePath)
c.Assert(err, gc.IsNil)
manifest, err := archive.Manifest()
c.Assert(err, gc.IsNil)
c.Assert(manifest, jc.DeepEquals, set.NewStrings(dummyManifest...))
}
func (s *CharmArchiveSuite) TestManifestNoRevision(c *gc.C) {
archive, err := charm.ReadCharmArchive(s.archivePath)
c.Assert(err, gc.IsNil)
dirPath := c.MkDir()
err = archive.ExpandTo(dirPath)
c.Assert(err, gc.IsNil)
err = os.Remove(filepath.Join(dirPath, "revision"))
c.Assert(err, gc.IsNil)
archive = extCharmArchiveDir(c, dirPath)
manifest, err := archive.Manifest()
c.Assert(err, gc.IsNil)
c.Assert(manifest, gc.DeepEquals, set.NewStrings(dummyManifest...))
}
func (s *CharmArchiveSuite) TestManifestSymlink(c *gc.C) {
srcPath := cloneDir(c, charmDirPath(c, "dummy"))
if err := os.Symlink("../target", filepath.Join(srcPath, "hooks/symlink")); err != nil {
c.Skip("cannot symlink")
}
expected := append([]string{"hooks/symlink"}, dummyManifest...)
archive := archiveDir(c, srcPath)
manifest, err := archive.Manifest()
c.Assert(err, gc.IsNil)
c.Assert(manifest, gc.DeepEquals, set.NewStrings(expected...))
}
func (s *CharmArchiveSuite) TestExpandTo(c *gc.C) {
archive, err := charm.ReadCharmArchive(s.archivePath)
c.Assert(err, gc.IsNil)
path := filepath.Join(c.MkDir(), "charm")
err = archive.ExpandTo(path)
c.Assert(err, gc.IsNil)
dir, err := charm.ReadCharmDir(path)
c.Assert(err, gc.IsNil)
checkDummy(c, dir, path)
}
func (s *CharmArchiveSuite) prepareCharmArchive(c *gc.C, charmDir *charm.CharmDir, archivePath string) {
file, err := os.Create(archivePath)
c.Assert(err, gc.IsNil)
defer file.Close()
zipw := zip.NewWriter(file)
defer zipw.Close()
h := &zip.FileHeader{Name: "revision"}
h.SetMode(syscall.S_IFREG | 0644)
w, err := zipw.CreateHeader(h)
c.Assert(err, gc.IsNil)
_, err = w.Write([]byte(strconv.Itoa(charmDir.Revision())))
h = &zip.FileHeader{Name: "metadata.yaml", Method: zip.Deflate}
h.SetMode(0644)
w, err = zipw.CreateHeader(h)
c.Assert(err, gc.IsNil)
data, err := yaml.Marshal(charmDir.Meta())
c.Assert(err, gc.IsNil)
_, err = w.Write(data)
c.Assert(err, gc.IsNil)
for name := range charmDir.Meta().Hooks() {
hookName := filepath.Join("hooks", name)
h = &zip.FileHeader{
Name: hookName,
Method: zip.Deflate,
}
// Force it non-executable
h.SetMode(0644)
w, err := zipw.CreateHeader(h)
c.Assert(err, gc.IsNil)
_, err = w.Write([]byte("not important"))
c.Assert(err, gc.IsNil)
}
}
func (s *CharmArchiveSuite) TestExpandToSetsHooksExecutable(c *gc.C) {
charmDir, err := charm.ReadCharmDir(cloneDir(c, charmDirPath(c, "all-hooks")))
c.Assert(err, gc.IsNil)
// CharmArchive manually, so we can check ExpandTo(), unaffected
// by ArchiveTo()'s behavior
archivePath := filepath.Join(c.MkDir(), "archive.charm")
s.prepareCharmArchive(c, charmDir, archivePath)
archive, err := charm.ReadCharmArchive(archivePath)
c.Assert(err, gc.IsNil)
path := filepath.Join(c.MkDir(), "charm")
err = archive.ExpandTo(path)
c.Assert(err, gc.IsNil)
_, err = charm.ReadCharmDir(path)
c.Assert(err, gc.IsNil)
for name := range archive.Meta().Hooks() {
hookName := string(name)
info, err := os.Stat(filepath.Join(path, "hooks", hookName))
c.Assert(err, gc.IsNil)
perm := info.Mode() & 0777
c.Assert(perm&0100 != 0, gc.Equals, true, gc.Commentf("hook %q is not executable", hookName))
}
}
func (s *CharmArchiveSuite) TestCharmArchiveFileModes(c *gc.C) {
// Apply subtler mode differences than can be expressed in Bazaar.
srcPath := cloneDir(c, charmDirPath(c, "dummy"))
modes := []struct {
path string
mode os.FileMode
}{
{"hooks/install", 0751},
{"empty", 0750},
{"src/hello.c", 0614},
}
for _, m := range modes {
err := os.Chmod(filepath.Join(srcPath, m.path), m.mode)
c.Assert(err, gc.IsNil)
}
var haveSymlinks = true
if err := os.Symlink("../target", filepath.Join(srcPath, "hooks/symlink")); err != nil {
haveSymlinks = false
}
// CharmArchive and extract the charm to a new directory.
archive := archiveDir(c, srcPath)
path := c.MkDir()
err := archive.ExpandTo(path)
c.Assert(err, gc.IsNil)
// Check sensible file modes once round-tripped.
info, err := os.Stat(filepath.Join(path, "src", "hello.c"))
c.Assert(err, gc.IsNil)
c.Assert(info.Mode()&0777, gc.Equals, os.FileMode(0644))
c.Assert(info.Mode()&os.ModeType, gc.Equals, os.FileMode(0))
info, err = os.Stat(filepath.Join(path, "hooks", "install"))
c.Assert(err, gc.IsNil)
c.Assert(info.Mode()&0777, gc.Equals, os.FileMode(0755))
c.Assert(info.Mode()&os.ModeType, gc.Equals, os.FileMode(0))
info, err = os.Stat(filepath.Join(path, "empty"))
c.Assert(err, gc.IsNil)
c.Assert(info.Mode()&0777, gc.Equals, os.FileMode(0755))
if haveSymlinks {
target, err := os.Readlink(filepath.Join(path, "hooks", "symlink"))
c.Assert(err, gc.IsNil)
c.Assert(target, gc.Equals, "../target")
}
}
func (s *CharmArchiveSuite) TestCharmArchiveRevisionFile(c *gc.C) {
charmDir := cloneDir(c, charmDirPath(c, "dummy"))
revPath := filepath.Join(charmDir, "revision")
// Missing revision file
err := os.Remove(revPath)
c.Assert(err, gc.IsNil)
archive := extCharmArchiveDir(c, charmDir)
c.Assert(archive.Revision(), gc.Equals, 0)
// Missing revision file with obsolete old revision in metadata;
// the revision is ignored.
file, err := os.OpenFile(filepath.Join(charmDir, "metadata.yaml"), os.O_WRONLY|os.O_APPEND, 0)
c.Assert(err, gc.IsNil)
_, err = file.Write([]byte("\nrevision: 1234\n"))
c.Assert(err, gc.IsNil)
archive = extCharmArchiveDir(c, charmDir)
c.Assert(archive.Revision(), gc.Equals, 0)
// Revision file with bad content
err = ioutil.WriteFile(revPath, []byte("garbage"), 0666)
c.Assert(err, gc.IsNil)
path := extCharmArchiveDirPath(c, charmDir)
archive, err = charm.ReadCharmArchive(path)
c.Assert(err, gc.ErrorMatches, "invalid revision file")
c.Assert(archive, gc.IsNil)
}
func (s *CharmArchiveSuite) TestCharmArchiveSetRevision(c *gc.C) {
archive, err := charm.ReadCharmArchive(s.archivePath)
c.Assert(err, gc.IsNil)
c.Assert(archive.Revision(), gc.Equals, 1)
archive.SetRevision(42)
c.Assert(archive.Revision(), gc.Equals, 42)
path := filepath.Join(c.MkDir(), "charm")
err = archive.ExpandTo(path)
c.Assert(err, gc.IsNil)
dir, err := charm.ReadCharmDir(path)
c.Assert(err, gc.IsNil)
c.Assert(dir.Revision(), gc.Equals, 42)
}
func (s *CharmArchiveSuite) TestExpandToWithBadLink(c *gc.C) {
charmDir := cloneDir(c, charmDirPath(c, "dummy"))
badLink := filepath.Join(charmDir, "hooks", "badlink")
// Symlink targeting a path outside of the charm.
err := os.Symlink("../../target", badLink)
c.Assert(err, gc.IsNil)
archive := extCharmArchiveDir(c, charmDir)
c.Assert(err, gc.IsNil)
path := filepath.Join(c.MkDir(), "charm")
err = archive.ExpandTo(path)
c.Assert(err, gc.ErrorMatches, `cannot extract "hooks/badlink": symlink "../../target" leads out of scope`)
// Symlink targeting an absolute path.
os.Remove(badLink)
err = os.Symlink("/target", badLink)
c.Assert(err, gc.IsNil)
archive = extCharmArchiveDir(c, charmDir)
c.Assert(err, gc.IsNil)
path = filepath.Join(c.MkDir(), "charm")
err = archive.ExpandTo(path)
c.Assert(err, gc.ErrorMatches, `cannot extract "hooks/badlink": symlink "/target" is absolute`)
}
func extCharmArchiveDirPath(c *gc.C, dirpath string) string {
path := filepath.Join(c.MkDir(), "archive.charm")
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cd %s; zip --fifo --symlinks -r %s .", dirpath, path))
output, err := cmd.CombinedOutput()
c.Assert(err, gc.IsNil, gc.Commentf("Command output: %s", output))
return path
}
func extCharmArchiveDir(c *gc.C, dirpath string) *charm.CharmArchive {
path := extCharmArchiveDirPath(c, dirpath)
archive, err := charm.ReadCharmArchive(path)
c.Assert(err, gc.IsNil)
return archive
}
func archiveDir(c *gc.C, dirpath string) *charm.CharmArchive {
dir, err := charm.ReadCharmDir(dirpath)
c.Assert(err, gc.IsNil)
buf := new(bytes.Buffer)
err = dir.ArchiveTo(buf)
c.Assert(err, gc.IsNil)
archive, err := charm.ReadCharmArchiveBytes(buf.Bytes())
c.Assert(err, gc.IsNil)
return archive
}