forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charm_test.go
223 lines (198 loc) · 5.73 KB
/
charm_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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
stdtesting "testing"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/fs"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v6"
"gopkg.in/yaml.v2"
)
func Test(t *stdtesting.T) {
gc.TestingT(t)
}
type CharmSuite struct {
testing.CleanupSuite
}
var _ = gc.Suite(&CharmSuite{})
func (s *CharmSuite) TestReadCharm(c *gc.C) {
ch, err := charm.ReadCharm(charmDirPath(c, "dummy"))
c.Assert(err, gc.IsNil)
c.Assert(ch.Meta().Name, gc.Equals, "dummy")
bPath := archivePath(c, readCharmDir(c, "dummy"))
ch, err = charm.ReadCharm(bPath)
c.Assert(err, gc.IsNil)
c.Assert(ch.Meta().Name, gc.Equals, "dummy")
}
func (s *CharmSuite) TestReadCharmDirError(c *gc.C) {
ch, err := charm.ReadCharm(c.MkDir())
c.Assert(err, gc.NotNil)
c.Assert(ch, gc.Equals, nil)
}
func (s *CharmSuite) TestReadCharmArchiveError(c *gc.C) {
path := filepath.Join(c.MkDir(), "path")
err := ioutil.WriteFile(path, []byte("foo"), 0644)
c.Assert(err, gc.IsNil)
ch, err := charm.ReadCharm(path)
c.Assert(err, gc.NotNil)
c.Assert(ch, gc.Equals, nil)
}
func (s *CharmSuite) TestSeriesToUse(c *gc.C) {
tests := []struct {
series string
supportedSeries []string
seriesToUse string
err string
}{{
series: "",
err: "series not specified and charm does not define any",
}, {
series: "trusty",
seriesToUse: "trusty",
}, {
series: "trusty",
supportedSeries: []string{"precise", "trusty"},
seriesToUse: "trusty",
}, {
series: "",
supportedSeries: []string{"precise", "trusty"},
seriesToUse: "precise",
}, {
series: "wily",
supportedSeries: []string{"precise", "trusty"},
err: `series "wily" not supported by charm.*`,
}}
for _, test := range tests {
series, err := charm.SeriesForCharm(test.series, test.supportedSeries)
if test.err != "" {
c.Assert(err, gc.ErrorMatches, test.err)
continue
}
c.Assert(err, jc.ErrorIsNil)
c.Assert(series, jc.DeepEquals, test.seriesToUse)
}
}
func (s *CharmSuite) IsUnsupportedSeriesError(c *gc.C) {
err := charm.NewUnsupportedSeriesError("series", []string{"supported"})
c.Assert(charm.IsUnsupportedSeriesError(err), jc.IsTrue)
c.Assert(charm.IsUnsupportedSeriesError(fmt.Errorf("foo")), jc.IsFalse)
}
func (s *CharmSuite) IsMissingSeriesError(c *gc.C) {
err := charm.MissingSeriesError()
c.Assert(charm.IsMissingSeriesError(err), jc.IsTrue)
c.Assert(charm.IsMissingSeriesError(fmt.Errorf("foo")), jc.IsFalse)
}
func checkDummy(c *gc.C, f charm.Charm, path string) {
c.Assert(f.Revision(), gc.Equals, 1)
c.Assert(f.Meta().Name, gc.Equals, "dummy")
c.Assert(f.Config().Options["title"].Default, gc.Equals, "My Title")
c.Assert(f.Actions(), jc.DeepEquals,
&charm.Actions{
map[string]charm.ActionSpec{
"snapshot": {
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"type": "object",
"description": "Take a snapshot of the database.",
"title": "snapshot",
"properties": map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string",
"default": "foo.bz2",
}}}}}})
switch f := f.(type) {
case *charm.CharmArchive:
c.Assert(f.Path, gc.Equals, path)
case *charm.CharmDir:
c.Assert(f.Path, gc.Equals, path)
}
}
type YamlHacker map[interface{}]interface{}
func ReadYaml(r io.Reader) YamlHacker {
data, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
m := make(map[interface{}]interface{})
err = yaml.Unmarshal(data, m)
if err != nil {
panic(err)
}
return YamlHacker(m)
}
func (yh YamlHacker) Reader() io.Reader {
data, err := yaml.Marshal(yh)
if err != nil {
panic(err)
}
return bytes.NewBuffer(data)
}
// charmDirPath returns the path to the charm with the
// given name in the testing repository.
func charmDirPath(c *gc.C, name string) string {
path := filepath.Join("internal/test-charm-repo/quantal", name)
assertIsDir(c, path)
return path
}
// bundleDirPath returns the path to the bundle with the
// given name in the testing repository.
func bundleDirPath(c *gc.C, name string) string {
path := filepath.Join("internal/test-charm-repo/bundle", name)
assertIsDir(c, path)
return path
}
func assertIsDir(c *gc.C, path string) {
info, err := os.Stat(path)
c.Assert(err, gc.IsNil)
c.Assert(info.IsDir(), gc.Equals, true)
}
// readCharmDir returns the charm with the given
// name from the testing repository.
func readCharmDir(c *gc.C, name string) *charm.CharmDir {
path := charmDirPath(c, name)
ch, err := charm.ReadCharmDir(path)
c.Assert(err, gc.IsNil)
return ch
}
// readBundleDir returns the bundle with the
// given name from the testing repository.
func readBundleDir(c *gc.C, name string) *charm.BundleDir {
path := bundleDirPath(c, name)
ch, err := charm.ReadBundleDir(path)
c.Assert(err, gc.IsNil)
return ch
}
type ArchiverTo interface {
ArchiveTo(w io.Writer) error
}
// archivePath archives the given charm or bundle
// to a newly created file and returns the path to the
// file.
func archivePath(c *gc.C, a ArchiverTo) string {
dir := c.MkDir()
path := filepath.Join(dir, "archive")
file, err := os.Create(path)
c.Assert(err, gc.IsNil)
defer file.Close()
err = a.ArchiveTo(file)
c.Assert(err, gc.IsNil)
return path
}
// cloneDir recursively copies the path directory
// into a new directory and returns the path
// to it.
func cloneDir(c *gc.C, path string) string {
newPath := filepath.Join(c.MkDir(), filepath.Base(path))
err := fs.Copy(path, newPath)
c.Assert(err, gc.IsNil)
return newPath
}