-
Notifications
You must be signed in to change notification settings - Fork 59
/
url_test.go
302 lines (272 loc) · 8.05 KB
/
url_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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/juju/mgo/v3/bson"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"github.com/juju/charm/v12"
)
type URLSuite struct{}
var _ = gc.Suite(&URLSuite{})
var urlTests = []struct {
s, err string
exact string
url *charm.URL
}{{
s: "local:series/name-1",
url: &charm.URL{"local", "name", 1, "series", ""},
}, {
s: "local:series/name",
url: &charm.URL{"local", "name", -1, "series", ""},
}, {
s: "local:series/n0-0n-n0",
url: &charm.URL{"local", "n0-0n-n0", -1, "series", ""},
}, {
s: "local:name",
url: &charm.URL{"local", "name", -1, "", ""},
}, {
s: "bs:~user/series/name-1",
err: `cannot parse URL $URL: schema "bs" not valid`,
}, {
s: ":foo",
err: `cannot parse charm or bundle URL: $URL`,
}, {
s: "local:~user/series/name",
err: `local charm or bundle URL with user name: $URL`,
}, {
s: "local:~user/name",
err: `local charm or bundle URL with user name: $URL`,
}, {
s: "amd64/name",
url: &charm.URL{"ch", "name", -1, "", "amd64"},
exact: "ch:amd64/name",
}, {
s: "foo",
url: &charm.URL{"ch", "foo", -1, "", ""},
exact: "ch:foo",
}, {
s: "foo-1",
exact: "ch:foo-1",
url: &charm.URL{"ch", "foo", 1, "", ""},
}, {
s: "n0-n0-n0",
exact: "ch:n0-n0-n0",
url: &charm.URL{"ch", "n0-n0-n0", -1, "", ""},
}, {
s: "local:foo",
exact: "local:foo",
url: &charm.URL{"local", "foo", -1, "", ""},
}, {
s: "arm64/series/bar",
url: &charm.URL{"ch", "bar", -1, "series", "arm64"},
exact: "ch:arm64/series/bar",
}, {
s: "ch:name",
url: &charm.URL{"ch", "name", -1, "", ""},
}, {
s: "ch:name-suffix",
url: &charm.URL{"ch", "name-suffix", -1, "", ""},
}, {
s: "ch:name-1",
url: &charm.URL{"ch", "name", 1, "", ""},
}, {
s: "ch:focal/istio-gateway-74",
url: &charm.URL{"ch", "istio-gateway", 74, "focal", ""},
}, {
s: "ch:amd64/istio-gateway-74",
url: &charm.URL{"ch", "istio-gateway", 74, "", "amd64"},
}, {
s: "ch:arm64/name",
url: &charm.URL{"ch", "name", -1, "", "arm64"},
exact: "ch:arm64/name",
}, {
s: "ch:~user/name",
err: `charmhub charm or bundle URL with user name: "ch:~user/name" not valid`,
}, {
s: "ch:purple/series/name-0",
err: `in URL "ch:purple/series/name-0": architecture name "purple" not valid`,
}, {
s: "ch:nam-!e",
err: `cannot parse name and/or revision in URL "ch:nam-!e": name "nam-!e" not valid`,
}, {
s: "cs:testme",
err: `cannot parse URL "cs:testme": schema "cs" not valid`,
}}
func (s *URLSuite) TestParseURL(c *gc.C) {
for i, t := range urlTests {
c.Logf("test %d: %q", i, t.s)
expectStr := t.s
if t.exact != "" {
expectStr = t.exact
}
url, uerr := charm.ParseURL(t.s)
if t.err != "" {
t.err = strings.Replace(t.err, "$URL", regexp.QuoteMeta(fmt.Sprintf("%q", t.s)), -1)
c.Check(uerr, gc.ErrorMatches, t.err)
c.Check(url, gc.IsNil)
continue
}
c.Assert(uerr, gc.IsNil)
c.Check(url, gc.DeepEquals, t.url)
c.Check(url.String(), gc.Equals, expectStr)
// URL strings are generated as expected. Reversability is preserved
// with v1 URLs.
if t.exact != "" {
c.Check(url.String(), gc.Equals, t.exact)
} else {
c.Check(url.String(), gc.Equals, t.s)
}
}
}
var ensureSchemaTests = []struct {
input, expected, err string
}{
{input: "foo", expected: "ch:foo"},
{input: "foo-1", expected: "ch:foo-1"},
{input: "~user/foo", expected: "ch:~user/foo"},
{input: "series/foo", expected: "ch:series/foo"},
{input: "local:foo", expected: "local:foo"},
{
input: "unknown:foo",
err: `schema "unknown" not valid`,
},
}
func (s *URLSuite) TestInferURLNoDefaultSeries(c *gc.C) {
for i, t := range ensureSchemaTests {
c.Logf("%d: %s", i, t.input)
inferred, err := charm.EnsureSchema(t.input, charm.CharmHub)
if t.err != "" {
c.Assert(err, gc.ErrorMatches, t.err)
continue
}
c.Assert(err, gc.IsNil)
c.Assert(inferred, gc.Equals, t.expected)
}
}
var validTests = []struct {
valid func(string) bool
string string
expect bool
}{
{charm.IsValidName, "", false},
{charm.IsValidName, "wordpress", true},
{charm.IsValidName, "Wordpress", false},
{charm.IsValidName, "word-press", true},
{charm.IsValidName, "word press", false},
{charm.IsValidName, "word^press", false},
{charm.IsValidName, "-wordpress", false},
{charm.IsValidName, "wordpress-", false},
{charm.IsValidName, "wordpress2", true},
{charm.IsValidName, "wordpress-2", false},
{charm.IsValidName, "word2-press2", true},
{charm.IsValidSeries, "", false},
{charm.IsValidSeries, "precise", true},
{charm.IsValidSeries, "Precise", false},
{charm.IsValidSeries, "pre cise", false},
{charm.IsValidSeries, "pre-cise", false},
{charm.IsValidSeries, "pre^cise", false},
{charm.IsValidSeries, "prec1se", true},
{charm.IsValidSeries, "-precise", false},
{charm.IsValidSeries, "precise-", false},
{charm.IsValidSeries, "precise-1", false},
{charm.IsValidSeries, "precise1", true},
{charm.IsValidSeries, "pre-c1se", false},
{charm.IsValidArchitecture, "amd64", true},
{charm.IsValidArchitecture, "~amd64", false},
{charm.IsValidArchitecture, "not-an-arch", false},
}
func (s *URLSuite) TestValidCheckers(c *gc.C) {
for i, t := range validTests {
c.Logf("test %d: %s", i, t.string)
c.Assert(t.valid(t.string), gc.Equals, t.expect, gc.Commentf("%s", t.string))
}
}
func (s *URLSuite) TestMustParseURL(c *gc.C) {
url := charm.MustParseURL("ch:series/name")
c.Assert(url, gc.DeepEquals, &charm.URL{"ch", "name", -1, "series", ""})
f := func() { charm.MustParseURL("local:@@/name") }
c.Assert(f, gc.PanicMatches, "cannot parse URL \"local:@@/name\": series name \"@@\" not valid")
}
func (s *URLSuite) TestWithRevision(c *gc.C) {
url := charm.MustParseURL("ch:series/name")
other := url.WithRevision(1)
c.Assert(url, gc.DeepEquals, &charm.URL{"ch", "name", -1, "series", ""})
c.Assert(other, gc.DeepEquals, &charm.URL{"ch", "name", 1, "series", ""})
// Should always copy. The opposite behavior is error prone.
c.Assert(other.WithRevision(1), gc.Not(gc.Equals), other)
c.Assert(other.WithRevision(1), gc.DeepEquals, other)
}
var codecs = []struct {
Name string
Marshal func(interface{}) ([]byte, error)
Unmarshal func([]byte, interface{}) error
}{{
Name: "bson",
Marshal: bson.Marshal,
Unmarshal: bson.Unmarshal,
}, {
Name: "json",
Marshal: json.Marshal,
Unmarshal: json.Unmarshal,
}, {
Name: "yaml",
Marshal: yaml.Marshal,
Unmarshal: yaml.Unmarshal,
}}
func (s *URLSuite) TestURLCodecs(c *gc.C) {
for i, codec := range codecs {
c.Logf("codec %d: %v", i, codec.Name)
type doc struct {
URL *charm.URL `json:",omitempty" bson:",omitempty" yaml:",omitempty"`
}
url := charm.MustParseURL("ch:name")
v0 := doc{url}
data, err := codec.Marshal(v0)
c.Assert(err, gc.IsNil)
var v doc
err = codec.Unmarshal(data, &v)
c.Assert(v, gc.DeepEquals, v0)
// Check that the underlying representation
// is a string.
type strDoc struct {
URL string
}
var vs strDoc
err = codec.Unmarshal(data, &vs)
c.Assert(err, gc.IsNil)
c.Assert(vs.URL, gc.Equals, "ch:name")
data, err = codec.Marshal(doc{})
c.Assert(err, gc.IsNil)
v = doc{}
err = codec.Unmarshal(data, &v)
c.Assert(err, gc.IsNil)
c.Assert(v.URL, gc.IsNil, gc.Commentf("data: %q", data))
}
}
func (s *URLSuite) TestJSONGarbage(c *gc.C) {
// unmarshalling json gibberish
for _, value := range []string{":{", `"ch:{}+<"`, `"ch:~_~/f00^^&^/baaaar$%-?"`} {
err := json.Unmarshal([]byte(value), new(struct{ URL *charm.URL }))
c.Check(err, gc.NotNil)
}
}
type QuoteSuite struct{}
var _ = gc.Suite(&QuoteSuite{})
func (s *QuoteSuite) TestUnmodified(c *gc.C) {
// Check that a string containing only valid
// chars stays unmodified.
in := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-"
out := charm.Quote(in)
c.Assert(out, gc.Equals, in)
}
func (s *QuoteSuite) TestQuote(c *gc.C) {
// Check that invalid chars are translated correctly.
in := "hello_there/how'are~you-today.sir"
out := charm.Quote(in)
c.Assert(out, gc.Equals, "hello_5f_there_2f_how_27_are_7e_you-today.sir")
}