Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1328)

Delta Between Two Patch Sets: constraints/constraints_test.go

Issue 12859043: Add an os-disk constraint
Left Patch Set: Created 11 years, 7 months ago
Right Patch Set: Add an os-disk constraint Created 11 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « constraints/constraints.go ('k') | environs/azure/instancetype.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2013 Canonical Ltd. 1 // Copyright 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package constraints_test 4 package constraints_test
5 5
6 import ( 6 import (
7 "encoding/json" 7 "encoding/json"
8 "testing" 8 "testing"
9 9
10 » . "launchpad.net/gocheck" 10 » gc "launchpad.net/gocheck"
11 "launchpad.net/goyaml" 11 "launchpad.net/goyaml"
12 12
13 "launchpad.net/juju-core/constraints" 13 "launchpad.net/juju-core/constraints"
14 "launchpad.net/juju-core/instance" 14 "launchpad.net/juju-core/instance"
15 ) 15 )
16 16
17 func TestPackage(t *testing.T) { 17 func TestPackage(t *testing.T) {
18 » TestingT(t) 18 » gc.TestingT(t)
19 } 19 }
20 20
21 type ConstraintsSuite struct{} 21 type ConstraintsSuite struct{}
22 22
23 var _ = Suite(&ConstraintsSuite{}) 23 var _ = gc.Suite(&ConstraintsSuite{})
24 24
25 var parseConstraintsTests = []struct { 25 var parseConstraintsTests = []struct {
26 summary string 26 summary string
27 args []string 27 args []string
28 err string 28 err string
29 }{ 29 }{
30 // Simple errors. 30 // Simple errors.
31 { 31 {
32 summary: "nothing at all", 32 summary: "nothing at all",
33 }, { 33 }, {
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 }, { 199 }, {
200 summary: "double set mem together", 200 summary: "double set mem together",
201 args: []string{"mem=1G mem=2G"}, 201 args: []string{"mem=1G mem=2G"},
202 err: `bad "mem" constraint: already set`, 202 err: `bad "mem" constraint: already set`,
203 }, { 203 }, {
204 summary: "double set mem separately", 204 summary: "double set mem separately",
205 args: []string{"mem=1G", "mem=2G"}, 205 args: []string{"mem=1G", "mem=2G"},
206 err: `bad "mem" constraint: already set`, 206 err: `bad "mem" constraint: already set`,
207 }, 207 },
208 208
209 » // "os-disk" in detail. 209 » // "root-disk" in detail.
210 » { 210 » {
211 » » summary: "set os-disk empty", 211 » » summary: "set root-disk empty",
212 » » args: []string{"os-disk="}, 212 » » args: []string{"root-disk="},
213 » }, { 213 » }, {
214 » » summary: "set os-disk zero", 214 » » summary: "set root-disk zero",
215 » » args: []string{"os-disk=0"}, 215 » » args: []string{"root-disk=0"},
216 » }, { 216 » }, {
217 » » summary: "set os-disk without suffix", 217 » » summary: "set root-disk without suffix",
218 » » args: []string{"os-disk=512"}, 218 » » args: []string{"root-disk=512"},
219 » }, { 219 » }, {
220 » » summary: "set os-disk with M suffix", 220 » » summary: "set root-disk with M suffix",
221 » » args: []string{"os-disk=512M"}, 221 » » args: []string{"root-disk=512M"},
222 » }, { 222 » }, {
223 » » summary: "set os-disk with G suffix", 223 » » summary: "set root-disk with G suffix",
224 » » args: []string{"os-disk=1.5G"}, 224 » » args: []string{"root-disk=1.5G"},
225 » }, { 225 » }, {
226 » » summary: "set os-disk with T suffix", 226 » » summary: "set root-disk with T suffix",
227 » » args: []string{"os-disk=36.2T"}, 227 » » args: []string{"root-disk=36.2T"},
228 » }, { 228 » }, {
229 » » summary: "set os-disk with P suffix", 229 » » summary: "set root-disk with P suffix",
230 » » args: []string{"os-disk=18.9P"}, 230 » » args: []string{"root-disk=18.9P"},
231 » }, { 231 » }, {
232 » » summary: "set nonsense os-disk 1", 232 » » summary: "set nonsense root-disk 1",
233 » » args: []string{"os-disk=cheese"}, 233 » » args: []string{"root-disk=cheese"},
234 » » err: `bad "os-disk" constraint: must be a non-negative float with optional M/G/T/P suffix`, 234 » » err: `bad "root-disk" constraint: must be a non-negative flo at with optional M/G/T/P suffix`,
235 » }, { 235 » }, {
236 » » summary: "set nonsense os-disk 2", 236 » » summary: "set nonsense root-disk 2",
237 » » args: []string{"os-disk=-1"}, 237 » » args: []string{"root-disk=-1"},
238 » » err: `bad "os-disk" constraint: must be a non-negative float with optional M/G/T/P suffix`, 238 » » err: `bad "root-disk" constraint: must be a non-negative flo at with optional M/G/T/P suffix`,
239 » }, { 239 » }, {
240 » » summary: "set nonsense os-disk 3", 240 » » summary: "set nonsense root-disk 3",
241 » » args: []string{"os-disk=32Y"}, 241 » » args: []string{"root-disk=32Y"},
242 » » err: `bad "os-disk" constraint: must be a non-negative float with optional M/G/T/P suffix`, 242 » » err: `bad "root-disk" constraint: must be a non-negative flo at with optional M/G/T/P suffix`,
243 » }, { 243 » }, {
244 » » summary: "double set os-disk together", 244 » » summary: "double set root-disk together",
245 » » args: []string{"os-disk=1G os-disk=2G"}, 245 » » args: []string{"root-disk=1G root-disk=2G"},
246 » » err: `bad "os-disk" constraint: already set`, 246 » » err: `bad "root-disk" constraint: already set`,
247 » }, { 247 » }, {
248 » » summary: "double set os-disk separately", 248 » » summary: "double set root-disk separately",
249 » » args: []string{"os-disk=1G", "os-disk=2G"}, 249 » » args: []string{"root-disk=1G", "root-disk=2G"},
250 » » err: `bad "os-disk" constraint: already set`, 250 » » err: `bad "root-disk" constraint: already set`,
251 }, 251 },
252 252
253 // Everything at once. 253 // Everything at once.
254 { 254 {
255 summary: "kitchen sink together", 255 summary: "kitchen sink together",
256 » » args: []string{" os-disk=8G mem=2T arch=i386 cpu-cores=4096 cpu-power=9001 container=lxc"}, 256 » » args: []string{" root-disk=8G mem=2T arch=i386 cpu-cores=40 96 cpu-power=9001 container=lxc"},
257 }, { 257 }, {
258 summary: "kitchen sink separately", 258 summary: "kitchen sink separately",
259 » » args: []string{"os-disk=8G", "mem=2T", "cpu-cores=4096", "cpu -power=9001", "arch=arm", "container=lxc"}, 259 » » args: []string{"root-disk=8G", "mem=2T", "cpu-cores=4096", "c pu-power=9001", "arch=arm", "container=lxc"},
260 » }, 260 » },
261 } 261 }
262 262
263 func (s *ConstraintsSuite) TestParseConstraints(c *C) { 263 func (s *ConstraintsSuite) TestParseConstraints(c *gc.C) {
264 for i, t := range parseConstraintsTests { 264 for i, t := range parseConstraintsTests {
265 c.Logf("test %d: %s", i, t.summary) 265 c.Logf("test %d: %s", i, t.summary)
266 cons0, err := constraints.Parse(t.args...) 266 cons0, err := constraints.Parse(t.args...)
267 if t.err == "" { 267 if t.err == "" {
268 » » » c.Assert(err, IsNil) 268 » » » c.Assert(err, gc.IsNil)
269 } else { 269 } else {
270 » » » c.Assert(err, ErrorMatches, t.err) 270 » » » c.Assert(err, gc.ErrorMatches, t.err)
271 continue 271 continue
272 } 272 }
273 cons1, err := constraints.Parse(cons0.String()) 273 cons1, err := constraints.Parse(cons0.String())
274 » » c.Assert(err, IsNil) 274 » » c.Assert(err, gc.IsNil)
275 » » c.Assert(cons1, DeepEquals, cons0) 275 » » c.Assert(cons1, gc.DeepEquals, cons0)
276 } 276 }
277 } 277 }
278 278
279 func uint64p(i uint64) *uint64 { 279 func uint64p(i uint64) *uint64 {
280 return &i 280 return &i
281 } 281 }
282 282
283 func strp(s string) *string { 283 func strp(s string) *string {
284 return &s 284 return &s
285 } 285 }
286 286
287 func ctypep(ctype string) *instance.ContainerType { 287 func ctypep(ctype string) *instance.ContainerType {
288 res := instance.ContainerType(ctype) 288 res := instance.ContainerType(ctype)
289 return &res 289 return &res
290 } 290 }
291 291
292 var constraintsRoundtripTests = []constraints.Value{ 292 var constraintsRoundtripTests = []constraints.Value{
293 {}, 293 {},
294 {Arch: strp("")}, 294 {Arch: strp("")},
295 {Arch: strp("amd64")}, 295 {Arch: strp("amd64")},
296 {Container: ctypep("")}, 296 {Container: ctypep("")},
297 {Container: ctypep("lxc")}, 297 {Container: ctypep("lxc")},
298 {CpuCores: uint64p(0)}, 298 {CpuCores: uint64p(0)},
299 {CpuCores: uint64p(128)}, 299 {CpuCores: uint64p(128)},
300 {CpuPower: uint64p(0)}, 300 {CpuPower: uint64p(0)},
301 {CpuPower: uint64p(250)}, 301 {CpuPower: uint64p(250)},
302 {Mem: uint64p(0)}, 302 {Mem: uint64p(0)},
303 {Mem: uint64p(98765)}, 303 {Mem: uint64p(98765)},
304 » {OsDisk: uint64p(98765)}, 304 » {RootDisk: uint64p(0)},
305 » {RootDisk: uint64p(109876)},
305 { 306 {
306 Arch: strp("i386"), 307 Arch: strp("i386"),
307 Container: ctypep("lxc"), 308 Container: ctypep("lxc"),
308 CpuCores: uint64p(4096), 309 CpuCores: uint64p(4096),
309 CpuPower: uint64p(9001), 310 CpuPower: uint64p(9001),
310 Mem: uint64p(18000000000), 311 Mem: uint64p(18000000000),
311 » » OsDisk: uint64p(18000000000), 312 » » RootDisk: uint64p(24000000000),
312 » }, 313 » },
313 } 314 }
314 315
315 func (s *ConstraintsSuite) TestRoundtripGnuflagValue(c *C) { 316 func (s *ConstraintsSuite) TestRoundtripGnuflagValue(c *gc.C) {
316 for i, t := range constraintsRoundtripTests { 317 for i, t := range constraintsRoundtripTests {
317 c.Logf("test %d", i) 318 c.Logf("test %d", i)
318 var cons constraints.Value 319 var cons constraints.Value
319 val := constraints.ConstraintsValue{&cons} 320 val := constraints.ConstraintsValue{&cons}
320 err := val.Set(t.String()) 321 err := val.Set(t.String())
321 » » c.Assert(err, IsNil) 322 » » c.Assert(err, gc.IsNil)
322 » » c.Assert(cons, DeepEquals, t) 323 » » c.Assert(cons, gc.DeepEquals, t)
323 » } 324 » }
324 } 325 }
325 326
326 func (s *ConstraintsSuite) TestRoundtripString(c *C) { 327 func (s *ConstraintsSuite) TestRoundtripString(c *gc.C) {
327 for i, t := range constraintsRoundtripTests { 328 for i, t := range constraintsRoundtripTests {
328 c.Logf("test %d", i) 329 c.Logf("test %d", i)
329 cons, err := constraints.Parse(t.String()) 330 cons, err := constraints.Parse(t.String())
330 » » c.Assert(err, IsNil) 331 » » c.Assert(err, gc.IsNil)
331 » » c.Assert(cons, DeepEquals, t) 332 » » c.Assert(cons, gc.DeepEquals, t)
332 » } 333 » }
333 } 334 }
334 335
335 func (s *ConstraintsSuite) TestRoundtripJson(c *C) { 336 func (s *ConstraintsSuite) TestRoundtripJson(c *gc.C) {
336 for i, t := range constraintsRoundtripTests { 337 for i, t := range constraintsRoundtripTests {
337 c.Logf("test %d", i) 338 c.Logf("test %d", i)
338 data, err := json.Marshal(t) 339 data, err := json.Marshal(t)
339 » » c.Assert(err, IsNil) 340 » » c.Assert(err, gc.IsNil)
340 var cons constraints.Value 341 var cons constraints.Value
341 err = json.Unmarshal(data, &cons) 342 err = json.Unmarshal(data, &cons)
342 » » c.Assert(err, IsNil) 343 » » c.Assert(err, gc.IsNil)
343 » » c.Assert(cons, DeepEquals, t) 344 » » c.Assert(cons, gc.DeepEquals, t)
344 » } 345 » }
345 } 346 }
346 347
347 func (s *ConstraintsSuite) TestRoundtripYaml(c *C) { 348 func (s *ConstraintsSuite) TestRoundtripYaml(c *gc.C) {
348 for i, t := range constraintsRoundtripTests { 349 for i, t := range constraintsRoundtripTests {
349 c.Logf("test %d", i) 350 c.Logf("test %d", i)
350 data, err := goyaml.Marshal(t) 351 data, err := goyaml.Marshal(t)
351 » » c.Assert(err, IsNil) 352 » » c.Assert(err, gc.IsNil)
352 c.Logf("%s", data) 353 c.Logf("%s", data)
353 var cons constraints.Value 354 var cons constraints.Value
354 err = goyaml.Unmarshal(data, &cons) 355 err = goyaml.Unmarshal(data, &cons)
355 » » c.Assert(err, IsNil) 356 » » c.Assert(err, gc.IsNil)
356 » » c.Assert(cons, DeepEquals, t) 357 » » c.Assert(cons, gc.DeepEquals, t)
357 } 358 }
358 } 359 }
359 360
360 var withFallbacksTests = []struct { 361 var withFallbacksTests = []struct {
361 desc string 362 desc string
362 initial string 363 initial string
363 fallbacks string 364 fallbacks string
364 final string 365 final string
365 }{ 366 }{
366 { 367 {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 }, { 420 }, {
420 desc: "mem with ignored fallback", 421 desc: "mem with ignored fallback",
421 initial: "mem=4G", 422 initial: "mem=4G",
422 fallbacks: "mem=8G", 423 fallbacks: "mem=8G",
423 final: "mem=4G", 424 final: "mem=4G",
424 }, { 425 }, {
425 desc: "mem from fallback", 426 desc: "mem from fallback",
426 fallbacks: "mem=8G", 427 fallbacks: "mem=8G",
427 final: "mem=8G", 428 final: "mem=8G",
428 }, { 429 }, {
429 » » desc: "os-disk with empty fallback", 430 » » desc: "root-disk with empty fallback",
430 » » initial: "os-disk=4G", 431 » » initial: "root-disk=4G",
431 » » final: "os-disk=4G", 432 » » final: "root-disk=4G",
432 » }, { 433 » }, {
433 » » desc: "os-disk with ignored fallback", 434 » » desc: "root-disk with ignored fallback",
434 » » initial: "os-disk=4G", 435 » » initial: "root-disk=4G",
435 » » fallbacks: "os-disk=8G", 436 » » fallbacks: "root-disk=8G",
436 » » final: "os-disk=4G", 437 » » final: "root-disk=4G",
437 » }, { 438 » }, {
438 » » desc: "os-disk from fallback", 439 » » desc: "root-disk from fallback",
439 » » fallbacks: "os-disk=8G", 440 » » fallbacks: "root-disk=8G",
440 » » final: "os-disk=8G", 441 » » final: "root-disk=8G",
441 }, { 442 }, {
442 desc: "non-overlapping mix", 443 desc: "non-overlapping mix",
443 » » initial: "os-disk=8G mem=4G arch=amd64", 444 » » initial: "root-disk=8G mem=4G arch=amd64",
444 fallbacks: "cpu-power=1000 cpu-cores=4", 445 fallbacks: "cpu-power=1000 cpu-cores=4",
445 » » final: "os-disk=8G mem=4G arch=amd64 cpu-power=1000 cpu-core s=4", 446 » » final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cpu-co res=4",
446 }, { 447 }, {
447 desc: "overlapping mix", 448 desc: "overlapping mix",
448 » » initial: "os-disk=8G mem=4G arch=amd64", 449 » » initial: "root-disk=8G mem=4G arch=amd64",
449 fallbacks: "cpu-power=1000 cpu-cores=4 mem=8G", 450 fallbacks: "cpu-power=1000 cpu-cores=4 mem=8G",
450 » » final: "os-disk=8G mem=4G arch=amd64 cpu-power=1000 cpu-core s=4", 451 » » final: "root-disk=8G mem=4G arch=amd64 cpu-power=1000 cpu-co res=4",
451 » }, 452 » },
452 } 453 }
453 454
454 func (s *ConstraintsSuite) TestWithFallbacks(c *C) { 455 func (s *ConstraintsSuite) TestWithFallbacks(c *gc.C) {
455 for i, t := range withFallbacksTests { 456 for i, t := range withFallbacksTests {
456 c.Logf("test %d", i) 457 c.Logf("test %d", i)
457 initial := constraints.MustParse(t.initial) 458 initial := constraints.MustParse(t.initial)
458 fallbacks := constraints.MustParse(t.fallbacks) 459 fallbacks := constraints.MustParse(t.fallbacks)
459 final := constraints.MustParse(t.final) 460 final := constraints.MustParse(t.final)
460 » » c.Assert(initial.WithFallbacks(fallbacks), DeepEquals, final) 461 » » c.Assert(initial.WithFallbacks(fallbacks), gc.DeepEquals, final)
461 } 462 }
462 } 463 }
463 464
464 var hasContainerTests = []struct { 465 var hasContainerTests = []struct {
465 constraints string 466 constraints string
466 hasContainer bool 467 hasContainer bool
467 }{ 468 }{
468 { 469 {
469 hasContainer: false, 470 hasContainer: false,
470 }, { 471 }, {
471 constraints: "container=lxc", 472 constraints: "container=lxc",
472 hasContainer: true, 473 hasContainer: true,
473 }, { 474 }, {
474 constraints: "container=none", 475 constraints: "container=none",
475 hasContainer: false, 476 hasContainer: false,
476 }, 477 },
477 } 478 }
478 479
479 func (s *ConstraintsSuite) TestHasContainer(c *C) { 480 func (s *ConstraintsSuite) TestHasContainer(c *gc.C) {
480 for i, t := range hasContainerTests { 481 for i, t := range hasContainerTests {
481 c.Logf("test %d", i) 482 c.Logf("test %d", i)
482 cons := constraints.MustParse(t.constraints) 483 cons := constraints.MustParse(t.constraints)
483 » » c.Assert(cons.HasContainer(), Equals, t.hasContainer) 484 » » c.Assert(cons.HasContainer(), gc.Equals, t.hasContainer)
484 » } 485 » }
485 } 486 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b