-
Notifications
You must be signed in to change notification settings - Fork 59
/
bundledatasrc_test.go
245 lines (211 loc) · 6.28 KB
/
bundledatasrc_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
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"archive/zip"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)
type BundleDataSourceSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&BundleDataSourceSuite{})
func (s *BundleDataSourceSuite) TestReadBundleFromLocalFile(c *gc.C) {
path := bundleDirPath(c, "wordpress-multidoc")
src, err := LocalBundleDataSource(filepath.Join(path, "bundle.yaml"))
c.Assert(err, gc.IsNil)
assertBundleSourceProcessed(c, src)
}
func (s *BundleDataSourceSuite) TestReadBundleFromExplodedArchiveFolder(c *gc.C) {
path := bundleDirPath(c, "wordpress-multidoc")
src, err := LocalBundleDataSource(path)
c.Assert(err, gc.IsNil)
assertBundleSourceProcessed(c, src)
}
func (s *BundleDataSourceSuite) TestReadBundleFromArchive(c *gc.C) {
path := archiveBundleDirPath(c, "wordpress-multidoc")
src, err := LocalBundleDataSource(path)
c.Assert(err, gc.IsNil)
assertBundleSourceProcessed(c, src)
}
func (s *BundleDataSourceSuite) TestReadBundleFromStream(c *gc.C) {
r := strings.NewReader(`
applications:
wordpress:
charm: wordpress
mysql:
charm: mysql
num_units: 1
relations:
- ["wordpress:db", "mysql:server"]
--- # overlay.yaml
applications:
wordpress:
offers:
offer1:
endpoints:
- "some-endpoint"
--- # overlay2.yaml
applications:
wordpress:
offers:
offer1:
acl:
admin: "admin"
foo: "consume"
`)
src, err := StreamBundleDataSource(r, "https://example.com")
c.Assert(err, gc.IsNil)
assertBundleSourceProcessed(c, src)
}
func assertBundleSourceProcessed(c *gc.C, src BundleDataSource) {
parts := src.Parts()
c.Assert(parts, gc.HasLen, 3)
assertFieldPresent(c, parts[1], "applications.wordpress.offers.offer1.endpoints")
assertFieldPresent(c, parts[2], "applications.wordpress.offers.offer1.acl.admin")
}
func assertFieldPresent(c *gc.C, part *BundleDataPart, path string) {
var (
segments = strings.Split(path, ".")
next interface{} = part.PresenceMap
)
for segIndex, segment := range segments {
c.Assert(next, gc.NotNil, gc.Commentf("incomplete path: %s", strings.Join(segments[:segIndex], ".")))
switch typ := next.(type) {
case FieldPresenceMap:
next = typ[segment]
c.Assert(next, gc.NotNil, gc.Commentf("incomplete path: %s", strings.Join(segments[:segIndex+1], ".")))
default:
c.Fatalf("unexpected type %T at path: %s", typ, strings.Join(segments[:segIndex], "."))
}
}
}
func (s *BundleDataSourceSuite) TestParseBundlePartsStrict(c *gc.C) {
r := strings.NewReader(`
applications:
wordpress:
charm: wordpress
constrain: "mem=8G"
mysql:
charm: mysql
num_uns: 1
relations:
- ["wordpress:db", "mysql:server"]
--- # overlay.yaml
applications:
wordpress:
offers:
offer1:
endpoints:
- "some-endpoint"
--- # overlay2.yaml
applications:
wordpress:
offer:
offer1:
acl:
admin: "admin"
foo: "consume"
`)
parts, err := parseBundleParts(r)
c.Assert(err, gc.IsNil)
c.Assert(parts, gc.HasLen, 3)
c.Assert(parts[0].UnmarshallError, gc.NotNil)
c.Assert(parts[0].UnmarshallError.Error(), gc.Matches, ""+
"unmarshal document 0: yaml: unmarshal errors:\n"+
" line 5: field constrain not found in applications\n"+
" line 8: field num_uns not found in applications")
c.Assert(parts[1].UnmarshallError, jc.ErrorIsNil)
c.Assert(parts[2].UnmarshallError, gc.NotNil)
c.Assert(parts[2].UnmarshallError.Error(), gc.Matches, ""+
"unmarshal document 2: yaml: unmarshal errors:\n"+
" line 21: field offer not found in applications")
}
func (s *BundleDataSourceSuite) TestResolveAbsoluteFileInclude(c *gc.C) {
target, err := filepath.Abs(filepath.Join(c.MkDir(), "example"))
c.Assert(err, gc.IsNil)
expContent := "example content\n"
c.Assert(ioutil.WriteFile(target, []byte(expContent), os.ModePerm), gc.IsNil)
ds := new(resolvedBundleDataSource)
got, err := ds.ResolveInclude(target)
c.Assert(err, gc.IsNil)
c.Assert(string(got), gc.Equals, expContent)
}
func (s *BundleDataSourceSuite) TestResolveRelativeFileInclude(c *gc.C) {
relTo := c.MkDir()
target, err := filepath.Abs(filepath.Join(relTo, "example"))
c.Assert(err, gc.IsNil)
expContent := "example content\n"
c.Assert(ioutil.WriteFile(target, []byte(expContent), os.ModePerm), gc.IsNil)
ds := &resolvedBundleDataSource{
basePath: relTo,
}
got, err := ds.ResolveInclude("./example")
c.Assert(err, gc.IsNil)
c.Assert(string(got), gc.Equals, expContent)
}
func (s *BundleDataSourceSuite) TestResolveIncludeErrors(c *gc.C) {
cwd, err := os.Getwd()
c.Assert(err, gc.IsNil)
tmpDir := c.MkDir()
specs := []struct {
descr string
incPath string
exp string
}{
{
descr: "abs path does not exist",
incPath: "/some/invalid/path",
exp: `include file "/some/invalid/path" not found`,
},
{
descr: "rel path does not exist",
incPath: "./missing",
exp: `include file "` + cwd + `/missing" not found`,
},
{
descr: "path points to directory",
incPath: tmpDir,
exp: fmt.Sprintf("include path %q resolves to a folder", tmpDir),
},
}
ds := new(resolvedBundleDataSource)
for specIndex, spec := range specs {
c.Logf("[test %d] %s", specIndex, spec.descr)
_, err := ds.ResolveInclude(spec.incPath)
c.Assert(err, gc.Not(gc.IsNil))
c.Assert(err.Error(), gc.Equals, spec.exp)
}
}
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)
}
func archiveBundleDirPath(c *gc.C, name string) string {
src := filepath.Join("internal/test-charm-repo/bundle", name, "bundle.yaml")
srcYaml, err := ioutil.ReadFile(src)
c.Assert(err, gc.IsNil)
dstPath := filepath.Join(c.MkDir(), "bundle.zip")
f, err := os.Create(dstPath)
c.Assert(err, gc.IsNil)
defer func() { c.Assert(f.Close(), gc.IsNil) }()
zw := zip.NewWriter(f)
defer func() { c.Assert(zw.Close(), gc.IsNil) }()
w, err := zw.Create("bundle.yaml")
c.Assert(err, gc.IsNil)
_, err = w.Write(srcYaml)
c.Assert(err, gc.IsNil)
return dstPath
}