forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundlearchive_test.go
98 lines (78 loc) · 2.69 KB
/
bundlearchive_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
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v6"
)
var _ = gc.Suite(&BundleArchiveSuite{})
type BundleArchiveSuite struct {
archivePath string
}
func (s *BundleArchiveSuite) SetUpSuite(c *gc.C) {
s.archivePath = archivePath(c, readBundleDir(c, "wordpress-simple"))
}
func (s *BundleArchiveSuite) TestReadBundleArchive(c *gc.C) {
archive, err := charm.ReadBundleArchive(s.archivePath)
c.Assert(err, gc.IsNil)
checkWordpressBundle(c, archive, s.archivePath)
}
func (s *BundleArchiveSuite) TestReadBundleArchiveBytes(c *gc.C) {
data, err := ioutil.ReadFile(s.archivePath)
c.Assert(err, gc.IsNil)
archive, err := charm.ReadBundleArchiveBytes(data)
c.Assert(err, gc.IsNil)
checkWordpressBundle(c, archive, "")
}
func (s *BundleArchiveSuite) TestReadBundleArchiveFromReader(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.ReadBundleArchiveFromReader(f, info.Size())
c.Assert(err, gc.IsNil)
checkWordpressBundle(c, archive, "")
}
func (s *BundleArchiveSuite) TestReadBundleArchiveWithoutBundleYAML(c *gc.C) {
testReadBundleArchiveWithoutFile(c, "bundle.yaml")
}
func (s *BundleArchiveSuite) TestReadBundleArchiveWithoutREADME(c *gc.C) {
testReadBundleArchiveWithoutFile(c, "README.md")
}
func testReadBundleArchiveWithoutFile(c *gc.C, fileToRemove string) {
path := cloneDir(c, bundleDirPath(c, "wordpress-simple"))
dir, err := charm.ReadBundleDir(path)
c.Assert(err, gc.IsNil)
// Remove the file from the bundle directory.
// ArchiveTo just zips the contents of the directory as-is,
// so the resulting bundle archive not contain the
// file.
err = os.Remove(filepath.Join(dir.Path, fileToRemove))
c.Assert(err, gc.IsNil)
archivePath := filepath.Join(c.MkDir(), "out.bundle")
dstf, err := os.Create(archivePath)
c.Assert(err, gc.IsNil)
err = dir.ArchiveTo(dstf)
dstf.Close()
archive, err := charm.ReadBundleArchive(archivePath)
// Slightly dubious assumption: the quoted file name has no
// regexp metacharacters worth worrying about.
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("archive file %q not found", fileToRemove))
c.Assert(archive, gc.IsNil)
}
func (s *BundleArchiveSuite) TestExpandTo(c *gc.C) {
dir := c.MkDir()
archive, err := charm.ReadBundleArchive(s.archivePath)
c.Assert(err, gc.IsNil)
err = archive.ExpandTo(dir)
c.Assert(err, gc.IsNil)
bdir, err := charm.ReadBundleDir(dir)
c.Assert(err, gc.IsNil)
c.Assert(bdir.ReadMe(), gc.Equals, archive.ReadMe())
c.Assert(bdir.Data(), gc.DeepEquals, archive.Data())
}