-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_test.go
389 lines (373 loc) · 12 KB
/
validation_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
385
386
387
388
389
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package constraints_test
import (
"regexp"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/constraints"
)
type validationSuite struct{}
var _ = gc.Suite(&validationSuite{})
var validationTests = []struct {
desc string
cons string
unsupported []string
vocab map[string][]interface{}
reds []string
blues []string
err string
}{
{
desc: "base good",
cons: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4",
},
{
desc: "unsupported",
cons: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 tags=foo",
unsupported: []string{"tags"},
},
{
desc: "multiple unsupported",
cons: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 instance-type=foo",
unsupported: []string{"cpu-power", "instance-type"},
},
{
desc: "Ambiguous constraint errors take precedence over unsupported errors.",
cons: "root-disk=8G mem=4G cores=4 instance-type=foo",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
unsupported: []string{"cores"},
err: `ambiguous constraints: "instance-type" overlaps with "mem"`,
},
{
desc: "red conflicts",
cons: "root-disk=8G mem=4G arch=amd64 cores=4 instance-type=foo",
reds: []string{"mem", "arch"},
},
{
desc: "blue conflicts",
cons: "root-disk=8G mem=4G arch=amd64 cores=4 instance-type=foo",
blues: []string{"mem", "arch"},
},
{
desc: "red and blue conflicts",
cons: "root-disk=8G mem=4G arch=amd64 cores=4 instance-type=foo",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
err: `ambiguous constraints: "arch" overlaps with "instance-type"`,
},
{
desc: "ambiguous constraints red to blue",
cons: "root-disk=8G mem=4G arch=amd64 cores=4 instance-type=foo",
reds: []string{"instance-type"},
blues: []string{"mem", "arch"},
err: `ambiguous constraints: "arch" overlaps with "instance-type"`,
},
{
desc: "ambiguous constraints blue to red",
cons: "root-disk=8G mem=4G cores=4 instance-type=foo",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
err: `ambiguous constraints: "instance-type" overlaps with "mem"`,
},
{
desc: "arch vocab",
cons: "arch=amd64 mem=4G cores=4",
vocab: map[string][]interface{}{"arch": {"amd64", "i386"}},
},
{
desc: "cores vocab",
cons: "mem=4G cores=4",
vocab: map[string][]interface{}{"cores": {2, 4, 8}},
},
{
desc: "instance-type vocab",
cons: "mem=4G instance-type=foo",
vocab: map[string][]interface{}{"instance-type": {"foo", "bar"}},
},
{
desc: "tags vocab",
cons: "mem=4G tags=foo,bar",
vocab: map[string][]interface{}{"tags": {"foo", "bar", "another"}},
},
{
desc: "invalid arch vocab",
cons: "arch=i386 mem=4G cores=4",
vocab: map[string][]interface{}{"arch": {"amd64"}},
err: "invalid constraint value: arch=i386\nvalid values are:.*",
},
{
desc: "invalid cores vocab",
cons: "mem=4G cores=5",
vocab: map[string][]interface{}{"cores": {2, 4, 8}},
err: "invalid constraint value: cores=5\nvalid values are:.*",
},
{
desc: "invalid instance-type vocab",
cons: "mem=4G instance-type=foo",
vocab: map[string][]interface{}{"instance-type": {"bar"}},
err: "invalid constraint value: instance-type=foo\nvalid values are:.*",
},
{
desc: "invalid tags vocab",
cons: "mem=4G tags=foo,other",
vocab: map[string][]interface{}{"tags": {"foo", "bar", "another"}},
err: "invalid constraint value: tags=other\nvalid values are:.*",
},
{
desc: "instance-type and arch",
cons: "arch=i386 mem=4G instance-type=foo",
vocab: map[string][]interface{}{
"instance-type": {"foo", "bar"},
"arch": {"amd64", "i386"}},
},
{
desc: "virt-type",
cons: "virt-type=bar",
vocab: map[string][]interface{}{"virt-type": {"bar"}},
},
}
func (s *validationSuite) TestValidation(c *gc.C) {
for i, t := range validationTests {
c.Logf("test %d: %s", i, t.desc)
validator := constraints.NewValidator()
validator.RegisterUnsupported(t.unsupported)
validator.RegisterConflicts(t.reds, t.blues)
for a, v := range t.vocab {
validator.RegisterVocabulary(a, v)
}
cons := constraints.MustParse(t.cons)
unsupported, err := validator.Validate(cons)
if t.err == "" {
c.Assert(err, jc.ErrorIsNil)
c.Assert(unsupported, jc.SameContents, t.unsupported)
} else {
c.Assert(err, gc.ErrorMatches, t.err)
}
}
}
var mergeTests = []struct {
desc string
consFallback string
cons string
unsupported []string
reds []string
blues []string
expected string
}{
{
desc: "empty all round",
}, {
desc: "container with empty fallback",
cons: "container=lxd",
expected: "container=lxd",
}, {
desc: "container from fallback",
consFallback: "container=lxd",
expected: "container=lxd",
}, {
desc: "arch with empty fallback",
cons: "arch=amd64",
expected: "arch=amd64",
}, {
desc: "arch with ignored fallback",
cons: "arch=amd64",
consFallback: "arch=i386",
expected: "arch=amd64",
}, {
desc: "arch from fallback",
consFallback: "arch=i386",
expected: "arch=i386",
}, {
desc: "instance type with empty fallback",
cons: "instance-type=foo",
expected: "instance-type=foo",
}, {
desc: "instance type with ignored fallback",
cons: "instance-type=foo",
consFallback: "instance-type=bar",
expected: "instance-type=foo",
}, {
desc: "instance type from fallback",
consFallback: "instance-type=foo",
expected: "instance-type=foo",
}, {
desc: "cores with empty fallback",
cons: "cores=2",
expected: "cores=2",
}, {
desc: "cores with ignored fallback",
cons: "cores=4",
consFallback: "cores=8",
expected: "cores=4",
}, {
desc: "cores from fallback",
consFallback: "cores=8",
expected: "cores=8",
}, {
desc: "cpu-power with empty fallback",
cons: "cpu-power=100",
expected: "cpu-power=100",
}, {
desc: "cpu-power with ignored fallback",
cons: "cpu-power=100",
consFallback: "cpu-power=200",
expected: "cpu-power=100",
}, {
desc: "cpu-power from fallback",
consFallback: "cpu-power=200",
expected: "cpu-power=200",
}, {
desc: "tags with empty fallback",
cons: "tags=foo,bar",
expected: "tags=foo,bar",
}, {
desc: "tags with ignored fallback",
cons: "tags=foo,bar",
consFallback: "tags=baz",
expected: "tags=foo,bar",
}, {
desc: "tags from fallback",
consFallback: "tags=foo,bar",
expected: "tags=foo,bar",
}, {
desc: "tags inital empty",
cons: "tags=",
consFallback: "tags=foo,bar",
expected: "tags=",
}, {
desc: "mem with empty fallback",
cons: "mem=4G",
expected: "mem=4G",
}, {
desc: "mem with ignored fallback",
cons: "mem=4G",
consFallback: "mem=8G",
expected: "mem=4G",
}, {
desc: "mem from fallback",
consFallback: "mem=8G",
expected: "mem=8G",
}, {
desc: "root-disk with empty fallback",
cons: "root-disk=4G",
expected: "root-disk=4G",
}, {
desc: "root-disk with ignored fallback",
cons: "root-disk=4G",
consFallback: "root-disk=8G",
expected: "root-disk=4G",
}, {
desc: "root-disk from fallback",
consFallback: "root-disk=8G",
expected: "root-disk=8G",
}, {
desc: "non-overlapping mix",
cons: "root-disk=8G mem=4G arch=amd64",
consFallback: "cpu-power=1000 cores=4",
expected: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4",
}, {
desc: "overlapping mix",
cons: "root-disk=8G mem=4G arch=amd64",
consFallback: "cpu-power=1000 cores=4 mem=8G",
expected: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4",
}, {
desc: "fallback only, no conflicts",
consFallback: "root-disk=8G cores=4 instance-type=foo",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 instance-type=foo",
}, {
desc: "no fallback, no conflicts",
cons: "root-disk=8G cores=4 instance-type=foo",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 instance-type=foo",
}, {
desc: "conflict value from override",
consFallback: "root-disk=8G instance-type=foo",
cons: "root-disk=8G cores=4 instance-type=bar",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 instance-type=bar",
}, {
desc: "unsupported attributes ignored",
consFallback: "root-disk=8G instance-type=foo",
cons: "root-disk=8G cores=4 instance-type=bar",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
unsupported: []string{"instance-type"},
expected: "root-disk=8G cores=4 instance-type=bar",
}, {
desc: "red conflict masked from fallback",
consFallback: "root-disk=8G mem=4G",
cons: "root-disk=8G cores=4 instance-type=bar",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 instance-type=bar",
}, {
desc: "second red conflict masked from fallback",
consFallback: "root-disk=8G arch=amd64",
cons: "root-disk=8G cores=4 instance-type=bar",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 instance-type=bar",
}, {
desc: "blue conflict masked from fallback",
consFallback: "root-disk=8G cores=4 instance-type=bar",
cons: "root-disk=8G mem=4G",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 mem=4G",
}, {
desc: "both red conflicts used, blue mased from fallback",
consFallback: "root-disk=8G cores=4 instance-type=bar",
cons: "root-disk=8G arch=amd64 mem=4G",
reds: []string{"mem", "arch"},
blues: []string{"instance-type"},
expected: "root-disk=8G cores=4 arch=amd64 mem=4G",
},
}
func (s *validationSuite) TestMerge(c *gc.C) {
for i, t := range mergeTests {
c.Logf("test %d: %s", i, t.desc)
validator := constraints.NewValidator()
validator.RegisterConflicts(t.reds, t.blues)
consFallback := constraints.MustParse(t.consFallback)
cons := constraints.MustParse(t.cons)
merged, err := validator.Merge(consFallback, cons)
c.Assert(err, jc.ErrorIsNil)
expected := constraints.MustParse(t.expected)
c.Check(merged, gc.DeepEquals, expected)
}
}
func (s *validationSuite) TestMergeError(c *gc.C) {
validator := constraints.NewValidator()
validator.RegisterConflicts([]string{"instance-type"}, []string{"mem"})
consFallback := constraints.MustParse("instance-type=foo mem=4G")
cons := constraints.MustParse("cores=2")
_, err := validator.Merge(consFallback, cons)
c.Assert(err, gc.ErrorMatches, `ambiguous constraints: "instance-type" overlaps with "mem"`)
_, err = validator.Merge(cons, consFallback)
c.Assert(err, gc.ErrorMatches, `ambiguous constraints: "instance-type" overlaps with "mem"`)
}
func (s *validationSuite) TestUpdateVocabulary(c *gc.C) {
validator := constraints.NewValidator()
attributeName := "arch"
originalValues := []string{"amd64"}
validator.RegisterVocabulary(attributeName, originalValues)
cons := constraints.MustParse("arch=amd64")
_, err := validator.Validate(cons)
c.Assert(err, jc.ErrorIsNil)
cons2 := constraints.MustParse("arch=ppc64el")
_, err = validator.Validate(cons2)
c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`invalid constraint value: arch=ppc64el
valid values are: [amd64]`))
additionalValues := []string{"ppc64el"}
validator.UpdateVocabulary(attributeName, additionalValues)
_, err = validator.Validate(cons)
c.Assert(err, jc.ErrorIsNil)
_, err = validator.Validate(cons2)
c.Assert(err, jc.ErrorIsNil)
}