-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints_test.go
686 lines (642 loc) · 21 KB
/
constraints_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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package constraints_test
import (
"encoding/json"
"fmt"
"strings"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
goyaml "gopkg.in/yaml.v2"
"github.com/juju/juju/constraints"
"github.com/juju/juju/instance"
)
type ConstraintsSuite struct{}
var _ = gc.Suite(&ConstraintsSuite{})
var parseConstraintsTests = []struct {
summary string
args []string
err string
}{
// Simple errors.
{
summary: "nothing at all",
}, {
summary: "empty",
args: []string{" "},
}, {
summary: "complete nonsense",
args: []string{"cheese"},
err: `malformed constraint "cheese"`,
}, {
summary: "missing name",
args: []string{"=cheese"},
err: `malformed constraint "=cheese"`,
}, {
summary: "unknown constraint",
args: []string{"cheese=edam"},
err: `unknown constraint "cheese"`,
},
// "container" in detail.
{
summary: "set container empty",
args: []string{"container="},
}, {
summary: "set container to none",
args: []string{"container=none"},
}, {
summary: "set container lxd",
args: []string{"container=lxd"},
}, {
summary: "set nonsense container",
args: []string{"container=foo"},
err: `bad "container" constraint: invalid container type "foo"`,
}, {
summary: "double set container together",
args: []string{"container=lxd container=lxd"},
err: `bad "container" constraint: already set`,
}, {
summary: "double set container separately",
args: []string{"container=lxd", "container="},
err: `bad "container" constraint: already set`,
},
// "arch" in detail.
{
summary: "set arch empty",
args: []string{"arch="},
}, {
summary: "set arch amd64",
args: []string{"arch=amd64"},
}, {
summary: "set arch i386",
args: []string{"arch=i386"},
}, {
summary: "set arch armhf",
args: []string{"arch=armhf"},
}, {
summary: "set nonsense arch 1",
args: []string{"arch=cheese"},
err: `bad "arch" constraint: "cheese" not recognized`,
}, {
summary: "set nonsense arch 2",
args: []string{"arch=123.45"},
err: `bad "arch" constraint: "123.45" not recognized`,
}, {
summary: "double set arch together",
args: []string{"arch=amd64 arch=amd64"},
err: `bad "arch" constraint: already set`,
}, {
summary: "double set arch separately",
args: []string{"arch=armhf", "arch="},
err: `bad "arch" constraint: already set`,
},
// "cores" in detail.
{
summary: "set cores empty",
args: []string{"cores="},
}, {
summary: "set cores zero",
args: []string{"cores=0"},
}, {
summary: "set cores",
args: []string{"cores=4"},
}, {
summary: "set nonsense cores 1",
args: []string{"cores=cheese"},
err: `bad "cores" constraint: must be a non-negative integer`,
}, {
summary: "set nonsense cores 2",
args: []string{"cores=-1"},
err: `bad "cores" constraint: must be a non-negative integer`,
}, {
summary: "set nonsense cores 3",
args: []string{"cores=123.45"},
err: `bad "cores" constraint: must be a non-negative integer`,
}, {
summary: "double set cores together",
args: []string{"cores=128 cores=1"},
err: `bad "cores" constraint: already set`,
}, {
summary: "double set cores separately",
args: []string{"cores=128", "cores=1"},
err: `bad "cores" constraint: already set`,
},
// "cpu-cores"
{
summary: "set cpu-cores",
args: []string{"cpu-cores=4"},
},
// "cpu-power" in detail.
{
summary: "set cpu-power empty",
args: []string{"cpu-power="},
}, {
summary: "set cpu-power zero",
args: []string{"cpu-power=0"},
}, {
summary: "set cpu-power",
args: []string{"cpu-power=44"},
}, {
summary: "set nonsense cpu-power 1",
args: []string{"cpu-power=cheese"},
err: `bad "cpu-power" constraint: must be a non-negative integer`,
}, {
summary: "set nonsense cpu-power 2",
args: []string{"cpu-power=-1"},
err: `bad "cpu-power" constraint: must be a non-negative integer`,
}, {
summary: "double set cpu-power together",
args: []string{" cpu-power=300 cpu-power=1700 "},
err: `bad "cpu-power" constraint: already set`,
}, {
summary: "double set cpu-power separately",
args: []string{"cpu-power=300 ", " cpu-power=1700"},
err: `bad "cpu-power" constraint: already set`,
},
// "mem" in detail.
{
summary: "set mem empty",
args: []string{"mem="},
}, {
summary: "set mem zero",
args: []string{"mem=0"},
}, {
summary: "set mem without suffix",
args: []string{"mem=512"},
}, {
summary: "set mem with M suffix",
args: []string{"mem=512M"},
}, {
summary: "set mem with G suffix",
args: []string{"mem=1.5G"},
}, {
summary: "set mem with T suffix",
args: []string{"mem=36.2T"},
}, {
summary: "set mem with P suffix",
args: []string{"mem=18.9P"},
}, {
summary: "set nonsense mem 1",
args: []string{"mem=cheese"},
err: `bad "mem" constraint: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense mem 2",
args: []string{"mem=-1"},
err: `bad "mem" constraint: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense mem 3",
args: []string{"mem=32Y"},
err: `bad "mem" constraint: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "double set mem together",
args: []string{"mem=1G mem=2G"},
err: `bad "mem" constraint: already set`,
}, {
summary: "double set mem separately",
args: []string{"mem=1G", "mem=2G"},
err: `bad "mem" constraint: already set`,
},
// "root-disk" in detail.
{
summary: "set root-disk empty",
args: []string{"root-disk="},
}, {
summary: "set root-disk zero",
args: []string{"root-disk=0"},
}, {
summary: "set root-disk without suffix",
args: []string{"root-disk=512"},
}, {
summary: "set root-disk with M suffix",
args: []string{"root-disk=512M"},
}, {
summary: "set root-disk with G suffix",
args: []string{"root-disk=1.5G"},
}, {
summary: "set root-disk with T suffix",
args: []string{"root-disk=36.2T"},
}, {
summary: "set root-disk with P suffix",
args: []string{"root-disk=18.9P"},
}, {
summary: "set nonsense root-disk 1",
args: []string{"root-disk=cheese"},
err: `bad "root-disk" constraint: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense root-disk 2",
args: []string{"root-disk=-1"},
err: `bad "root-disk" constraint: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense root-disk 3",
args: []string{"root-disk=32Y"},
err: `bad "root-disk" constraint: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "double set root-disk together",
args: []string{"root-disk=1G root-disk=2G"},
err: `bad "root-disk" constraint: already set`,
}, {
summary: "double set root-disk separately",
args: []string{"root-disk=1G", "root-disk=2G"},
err: `bad "root-disk" constraint: already set`,
},
// tags
{
summary: "single tag",
args: []string{"tags=foo"},
}, {
summary: "multiple tags",
args: []string{"tags=foo,bar"},
}, {
summary: "no tags",
args: []string{"tags="},
},
// spaces
{
summary: "single space",
args: []string{"spaces=space1"},
}, {
summary: "multiple spaces - positive",
args: []string{"spaces=space1,space2"},
}, {
summary: "multiple spaces - negative",
args: []string{"spaces=^dmz,^public"},
}, {
summary: "multiple spaces - positive and negative",
args: []string{"spaces=admin,^area52,dmz,^public"},
}, {
summary: "no spaces",
args: []string{"spaces="},
},
// instance type
{
summary: "set instance type",
args: []string{"instance-type=foo"},
}, {
summary: "instance type empty",
args: []string{"instance-type="},
},
// "virt-type" in detail.
{
summary: "set virt-type empty",
args: []string{"virt-type="},
}, {
summary: "set virt-type kvm",
args: []string{"virt-type=kvm"},
}, {
summary: "set virt-type lxd",
args: []string{"virt-type=lxd"},
}, {
summary: "double set virt-type together",
args: []string{"virt-type=kvm virt-type=kvm"},
err: `bad "virt-type" constraint: already set`,
}, {
summary: "double set virt-type separately",
args: []string{"virt-type=kvm", "virt-type="},
err: `bad "virt-type" constraint: already set`,
},
// Everything at once.
{
summary: "kitchen sink together",
args: []string{
"root-disk=8G mem=2T arch=i386 cores=4096 cpu-power=9001 container=lxd " +
"tags=foo,bar spaces=space1,^space2 instance-type=foo",
"virt-type=kvm"},
}, {
summary: "kitchen sink separately",
args: []string{
"root-disk=8G", "mem=2T", "cores=4096", "cpu-power=9001", "arch=armhf",
"container=lxd", "tags=foo,bar", "spaces=space1,^space2",
"instance-type=foo", "virt-type=kvm"},
},
}
func (s *ConstraintsSuite) TestParseConstraints(c *gc.C) {
// TODO(dimitern): This test is inadequate and needs to check for
// more than just the reparsed output of String() matches the
// expected.
for i, t := range parseConstraintsTests {
c.Logf("test %d: %s", i, t.summary)
cons0, err := constraints.Parse(t.args...)
if t.err == "" {
c.Assert(err, jc.ErrorIsNil)
} else {
c.Assert(err, gc.ErrorMatches, t.err)
continue
}
cons1, err := constraints.Parse(cons0.String())
c.Check(err, jc.ErrorIsNil)
c.Check(cons1, gc.DeepEquals, cons0)
}
}
func (s *ConstraintsSuite) TestParseAliases(c *gc.C) {
v, aliases, err := constraints.ParseWithAliases("cpu-cores=5 arch=amd64")
c.Assert(err, jc.ErrorIsNil)
c.Assert(v, gc.DeepEquals, constraints.Value{
CpuCores: uint64p(5),
Arch: strp("amd64"),
})
c.Assert(aliases, gc.DeepEquals, map[string]string{
"cpu-cores": "cores",
})
}
func (s *ConstraintsSuite) TestMerge(c *gc.C) {
con1 := constraints.MustParse("arch=amd64 mem=4G")
con2 := constraints.MustParse("cores=42")
con3 := constraints.MustParse(
"root-disk=8G container=lxd spaces=space1,^space2",
)
merged, err := constraints.Merge(con1, con2)
c.Assert(err, jc.ErrorIsNil)
c.Assert(merged, jc.DeepEquals, constraints.MustParse("arch=amd64 mem=4G cores=42"))
merged, err = constraints.Merge(con1)
c.Assert(err, jc.ErrorIsNil)
c.Assert(merged, jc.DeepEquals, con1)
merged, err = constraints.Merge(con1, con2, con3)
c.Assert(err, jc.ErrorIsNil)
c.Assert(merged, jc.DeepEquals, constraints.
MustParse("arch=amd64 mem=4G cores=42 root-disk=8G container=lxd spaces=space1,^space2"),
)
merged, err = constraints.Merge()
c.Assert(err, jc.ErrorIsNil)
c.Assert(merged, jc.DeepEquals, constraints.Value{})
foo := "foo"
merged, err = constraints.Merge(constraints.Value{Arch: &foo}, con2)
c.Assert(err, gc.NotNil)
c.Assert(err, gc.ErrorMatches, `bad "arch" constraint: "foo" not recognized`)
c.Assert(merged, jc.DeepEquals, constraints.Value{})
merged, err = constraints.Merge(con1, con1)
c.Assert(err, gc.NotNil)
c.Assert(err, gc.ErrorMatches, `bad "arch" constraint: already set`)
c.Assert(merged, jc.DeepEquals, constraints.Value{})
}
func (s *ConstraintsSuite) TestParseMissingTagsAndSpaces(c *gc.C) {
con := constraints.MustParse("arch=amd64 mem=4G cores=1 root-disk=8G")
c.Check(con.Tags, gc.IsNil)
c.Check(con.Spaces, gc.IsNil)
}
func (s *ConstraintsSuite) TestParseNoTagsNoSpaces(c *gc.C) {
con := constraints.MustParse(
"arch=amd64 mem=4G cores=1 root-disk=8G tags= spaces=",
)
c.Assert(con.Tags, gc.Not(gc.IsNil))
c.Assert(con.Spaces, gc.Not(gc.IsNil))
c.Check(*con.Tags, gc.HasLen, 0)
c.Check(*con.Spaces, gc.HasLen, 0)
}
func (s *ConstraintsSuite) TestIncludeExcludeAndHaveSpaces(c *gc.C) {
con := constraints.MustParse("spaces=space1,^space2,space3,^space4")
c.Assert(con.Spaces, gc.Not(gc.IsNil))
c.Check(*con.Spaces, gc.HasLen, 4)
c.Check(con.IncludeSpaces(), jc.SameContents, []string{"space1", "space3"})
c.Check(con.ExcludeSpaces(), jc.SameContents, []string{"space2", "space4"})
c.Check(con.HaveSpaces(), jc.IsTrue)
con = constraints.MustParse("mem=4G")
c.Check(con.HaveSpaces(), jc.IsFalse)
con = constraints.MustParse("mem=4G spaces=space-foo,^space-bar")
c.Check(con.IncludeSpaces(), jc.SameContents, []string{"space-foo"})
c.Check(con.ExcludeSpaces(), jc.SameContents, []string{"space-bar"})
c.Check(con.HaveSpaces(), jc.IsTrue)
}
func (s *ConstraintsSuite) TestInvalidSpaces(c *gc.C) {
invalidNames := []string{
"%$pace", "^foo#2", "+", "tcp:ip",
"^^myspace", "^^^^^^^^", "space^x",
"&-foo", "space/3", "^bar=4", "&#!",
}
for _, name := range invalidNames {
con, err := constraints.Parse("spaces=" + name)
expectName := strings.TrimPrefix(name, "^")
expectErr := fmt.Sprintf(`bad "spaces" constraint: %q is not a valid space name`, expectName)
c.Check(err, gc.NotNil)
c.Check(err.Error(), gc.Equals, expectErr)
c.Check(con, jc.DeepEquals, constraints.Value{})
}
}
func (s *ConstraintsSuite) TestIsEmpty(c *gc.C) {
con := constraints.Value{}
c.Check(&con, jc.Satisfies, constraints.IsEmpty)
con = constraints.MustParse("arch=amd64")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("")
c.Check(&con, jc.Satisfies, constraints.IsEmpty)
con = constraints.MustParse("tags=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("spaces=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("mem=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("arch=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("root-disk=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("cpu-power=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("cores=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("container=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
con = constraints.MustParse("instance-type=")
c.Check(&con, gc.Not(jc.Satisfies), constraints.IsEmpty)
}
func uint64p(i uint64) *uint64 {
return &i
}
func strp(s string) *string {
return &s
}
func ctypep(ctype string) *instance.ContainerType {
res := instance.ContainerType(ctype)
return &res
}
type roundTrip struct {
Name string
Value constraints.Value
}
var constraintsRoundtripTests = []roundTrip{
{"empty", constraints.Value{}},
{"Arch1", constraints.Value{Arch: strp("")}},
{"Arch2", constraints.Value{Arch: strp("amd64")}},
{"Container1", constraints.Value{Container: ctypep("")}},
{"Container2", constraints.Value{Container: ctypep("lxd")}},
{"Container3", constraints.Value{Container: nil}},
{"CpuCores1", constraints.Value{CpuCores: nil}},
{"CpuCores2", constraints.Value{CpuCores: uint64p(0)}},
{"CpuCores3", constraints.Value{CpuCores: uint64p(128)}},
{"CpuPower1", constraints.Value{CpuPower: nil}},
{"CpuPower2", constraints.Value{CpuPower: uint64p(0)}},
{"CpuPower3", constraints.Value{CpuPower: uint64p(250)}},
{"Mem1", constraints.Value{Mem: nil}},
{"Mem2", constraints.Value{Mem: uint64p(0)}},
{"Mem3", constraints.Value{Mem: uint64p(98765)}},
{"RootDisk1", constraints.Value{RootDisk: nil}},
{"RootDisk2", constraints.Value{RootDisk: uint64p(0)}},
{"RootDisk2", constraints.Value{RootDisk: uint64p(109876)}},
{"Tags1", constraints.Value{Tags: nil}},
{"Tags2", constraints.Value{Tags: &[]string{}}},
{"Tags3", constraints.Value{Tags: &[]string{"foo", "bar"}}},
{"Spaces1", constraints.Value{Spaces: nil}},
{"Spaces2", constraints.Value{Spaces: &[]string{}}},
{"Spaces3", constraints.Value{Spaces: &[]string{"space1", "^space2"}}},
{"InstanceType1", constraints.Value{InstanceType: strp("")}},
{"InstanceType2", constraints.Value{InstanceType: strp("foo")}},
{"All", constraints.Value{
Arch: strp("i386"),
Container: ctypep("lxd"),
CpuCores: uint64p(4096),
CpuPower: uint64p(9001),
Mem: uint64p(18000000000),
RootDisk: uint64p(24000000000),
Tags: &[]string{"foo", "bar"},
Spaces: &[]string{"space1", "^space2"},
InstanceType: strp("foo"),
}},
}
func (s *ConstraintsSuite) TestRoundtripGnuflagValue(c *gc.C) {
for _, t := range constraintsRoundtripTests {
c.Logf("test %s", t.Name)
var cons constraints.Value
val := constraints.ConstraintsValue{&cons}
err := val.Set(t.Value.String())
c.Check(err, jc.ErrorIsNil)
c.Check(cons, jc.DeepEquals, t.Value)
}
}
func (s *ConstraintsSuite) TestRoundtripString(c *gc.C) {
for _, t := range constraintsRoundtripTests {
c.Logf("test %s", t.Name)
cons, err := constraints.Parse(t.Value.String())
c.Check(err, jc.ErrorIsNil)
c.Check(cons, jc.DeepEquals, t.Value)
}
}
func (s *ConstraintsSuite) TestRoundtripJson(c *gc.C) {
for _, t := range constraintsRoundtripTests {
c.Logf("test %s", t.Name)
data, err := json.Marshal(t.Value)
c.Assert(err, jc.ErrorIsNil)
var cons constraints.Value
err = json.Unmarshal(data, &cons)
c.Check(err, jc.ErrorIsNil)
c.Check(cons, jc.DeepEquals, t.Value)
}
}
func (s *ConstraintsSuite) TestRoundtripYaml(c *gc.C) {
for _, t := range constraintsRoundtripTests {
c.Logf("test %s", t.Name)
data, err := goyaml.Marshal(t.Value)
c.Assert(err, jc.ErrorIsNil)
var cons constraints.Value
err = goyaml.Unmarshal(data, &cons)
c.Check(err, jc.ErrorIsNil)
c.Check(cons, jc.DeepEquals, t.Value)
}
}
var hasContainerTests = []struct {
constraints string
hasContainer bool
}{
{
hasContainer: false,
}, {
constraints: "container=lxd",
hasContainer: true,
}, {
constraints: "container=none",
hasContainer: false,
},
}
func (s *ConstraintsSuite) TestHasContainer(c *gc.C) {
for i, t := range hasContainerTests {
c.Logf("test %d", i)
cons := constraints.MustParse(t.constraints)
c.Check(cons.HasContainer(), gc.Equals, t.hasContainer)
}
}
func (s *ConstraintsSuite) TestHasInstanceType(c *gc.C) {
cons := constraints.MustParse("arch=amd64")
c.Check(cons.HasInstanceType(), jc.IsFalse)
cons = constraints.MustParse("arch=amd64 instance-type=foo")
c.Check(cons.HasInstanceType(), jc.IsTrue)
}
const initialWithoutCons = "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 spaces=space1,^space2 tags=foo container=lxd instance-type=bar"
var withoutTests = []struct {
initial string
without []string
final string
}{{
initial: initialWithoutCons,
without: []string{"root-disk"},
final: "mem=4G arch=amd64 cpu-power=1000 cores=4 tags=foo spaces=space1,^space2 container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"mem"},
final: "root-disk=8G arch=amd64 cpu-power=1000 cores=4 tags=foo spaces=space1,^space2 container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"arch"},
final: "root-disk=8G mem=4G cpu-power=1000 cores=4 tags=foo spaces=space1,^space2 container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"cpu-power"},
final: "root-disk=8G mem=4G arch=amd64 cores=4 tags=foo spaces=space1,^space2 container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"cores"},
final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 tags=foo spaces=space1,^space2 container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"tags"},
final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 spaces=space1,^space2 container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"spaces"},
final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 tags=foo container=lxd instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"container"},
final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 tags=foo spaces=space1,^space2 instance-type=bar",
}, {
initial: initialWithoutCons,
without: []string{"instance-type"},
final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4 tags=foo spaces=space1,^space2 container=lxd",
}, {
initial: initialWithoutCons,
without: []string{"root-disk", "mem", "arch"},
final: "cpu-power=1000 cores=4 tags=foo spaces=space1,^space2 container=lxd instance-type=bar",
}}
func (s *ConstraintsSuite) TestWithout(c *gc.C) {
for i, t := range withoutTests {
c.Logf("test %d", i)
initial := constraints.MustParse(t.initial)
final := constraints.Without(initial, t.without...)
c.Check(final, jc.DeepEquals, constraints.MustParse(t.final))
}
}
var hasAnyTests = []struct {
cons string
attrs []string
expected []string
}{
{
cons: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 spaces=space1,^space2 cores=4",
attrs: []string{"root-disk", "tags", "mem", "spaces"},
expected: []string{"root-disk", "mem", "spaces"},
},
{
cons: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4",
attrs: []string{"root-disk", "tags", "mem"},
expected: []string{"root-disk", "mem"},
},
{
cons: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cores=4",
attrs: []string{"tags", "spaces"},
expected: []string{},
},
}
func (s *ConstraintsSuite) TestHasAny(c *gc.C) {
for i, t := range hasAnyTests {
c.Logf("test %d", i)
cons := constraints.MustParse(t.cons)
obtained := constraints.HasAny(cons, t.attrs...)
c.Check(obtained, jc.DeepEquals, t.expected)
}
}