Revision da5f09ef test/py/ganeti.objects_unittest.py

b/test/py/ganeti.objects_unittest.py
1 1
#!/usr/bin/python
2 2
#
3 3

  
4
# Copyright (C) 2006, 2007, 2008, 2010, 2012 Google Inc.
4
# Copyright (C) 2006, 2007, 2008, 2010, 2012, 2013 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
414 414

  
415 415
  def _AssertIPolicyIsFull(self, policy):
416 416
    self.assertEqual(frozenset(policy.keys()), constants.IPOLICY_ALL_KEYS)
417
    for key in constants.IPOLICY_ISPECS:
418
      spec = policy[key]
419
      self.assertEqual(frozenset(spec.keys()), constants.ISPECS_PARAMETERS)
417
    minmax = policy[constants.ISPECS_MINMAX]
418
    self.assertEqual(frozenset(minmax.keys()), constants.ISPECS_MINMAX_KEYS)
419
    for key in constants.ISPECS_MINMAX_KEYS:
420
      self.assertEqual(frozenset(minmax[key].keys()),
421
                       constants.ISPECS_PARAMETERS)
422
    self.assertEqual(frozenset(policy[constants.ISPECS_STD].keys()),
423
                     constants.ISPECS_PARAMETERS)
420 424

  
421 425
  def testDefaultIPolicy(self):
422 426
    objects.InstancePolicy.CheckParameterSyntax(constants.IPOLICY_DEFAULTS,
......
426 430
  def testCheckISpecSyntax(self):
427 431
    par = "my_parameter"
428 432
    for check_std in [True, False]:
429
      if check_std:
430
        allkeys = constants.IPOLICY_ISPECS
431
      else:
432
        allkeys = constants.IPOLICY_ISPECS - frozenset([constants.ISPECS_STD])
433 433
      # Only one policy limit
434
      for key in allkeys:
435
        policy = dict((k, {}) for k in allkeys)
436
        policy[key][par] = 11
437
        objects.InstancePolicy.CheckISpecSyntax(policy, par, check_std)
434
      for key in constants.ISPECS_MINMAX_KEYS:
435
        minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
436
        minmax[key][par] = 11
437
        objects.InstancePolicy.CheckISpecSyntax(minmax, {}, par, check_std)
438
      if check_std:
439
        minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
440
        stdspec = {par: 11}
441
        objects.InstancePolicy.CheckISpecSyntax(minmax, stdspec, par, check_std)
442

  
438 443
      # Min and max only
439 444
      good_values = [(11, 11), (11, 40), (0, 0)]
440 445
      for (mn, mx) in good_values:
441
        policy = dict((k, {}) for k in allkeys)
442
        policy[constants.ISPECS_MIN][par] = mn
443
        policy[constants.ISPECS_MAX][par] = mx
444
        objects.InstancePolicy.CheckISpecSyntax(policy, par, check_std)
445
      policy = dict((k, {}) for k in allkeys)
446
      policy[constants.ISPECS_MIN][par] = 11
447
      policy[constants.ISPECS_MAX][par] = 5
446
        minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
447
        minmax[constants.ISPECS_MIN][par] = mn
448
        minmax[constants.ISPECS_MAX][par] = mx
449
        objects.InstancePolicy.CheckISpecSyntax(minmax, {}, par, check_std)
450
      minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
451
      minmax[constants.ISPECS_MIN][par] = 11
452
      minmax[constants.ISPECS_MAX][par] = 5
448 453
      self.assertRaises(errors.ConfigurationError,
449 454
                        objects.InstancePolicy.CheckISpecSyntax,
450
                        policy, par, check_std)
455
                        minmax, {}, par, check_std)
451 456
    # Min, std, max
452 457
    good_values = [
453 458
      (11, 11, 11),
......
455 460
      (11, 40, 40),
456 461
      ]
457 462
    for (mn, st, mx) in good_values:
458
      policy = {
463
      minmax = {
459 464
        constants.ISPECS_MIN: {par: mn},
460
        constants.ISPECS_STD: {par: st},
461 465
        constants.ISPECS_MAX: {par: mx},
462 466
        }
463
      objects.InstancePolicy.CheckISpecSyntax(policy, par, True)
467
      stdspec = {par: st}
468
      objects.InstancePolicy.CheckISpecSyntax(minmax, stdspec, par, True)
464 469
    bad_values = [
465 470
      (11, 11,  5),
466 471
      (40, 11, 11),
......
470 475
      (40, 40, 11),
471 476
      ]
472 477
    for (mn, st, mx) in bad_values:
473
      policy = {
478
      minmax = {
474 479
        constants.ISPECS_MIN: {par: mn},
475
        constants.ISPECS_STD: {par: st},
476 480
        constants.ISPECS_MAX: {par: mx},
477 481
        }
482
      stdspec = {par: st}
478 483
      self.assertRaises(errors.ConfigurationError,
479 484
                        objects.InstancePolicy.CheckISpecSyntax,
480
                        policy, par, True)
485
                        minmax, stdspec, par, True)
481 486

  
482 487
  def testCheckDiskTemplates(self):
483 488
    invalid = "this_is_not_a_good_template"
......
499 504
  def testCheckParameterSyntax(self):
500 505
    invalid = "this_key_shouldnt_be_here"
501 506
    for check_std in [True, False]:
502
      self.assertRaises(KeyError,
503
                        objects.InstancePolicy.CheckParameterSyntax,
504
                        {}, check_std)
505
      policy = objects.MakeEmptyIPolicy()
506
      policy[invalid] = None
507
      objects.InstancePolicy.CheckParameterSyntax({}, check_std)
508
      policy = {invalid: None}
507 509
      self.assertRaises(errors.ConfigurationError,
508 510
                        objects.InstancePolicy.CheckParameterSyntax,
509 511
                        policy, check_std)
510 512
      for par in constants.IPOLICY_PARAMETERS:
511
        policy = objects.MakeEmptyIPolicy()
512 513
        for val in ("blah", None, {}, [42]):
513
          policy[par] = val
514
          policy = {par: val}
514 515
          self.assertRaises(errors.ConfigurationError,
515 516
                            objects.InstancePolicy.CheckParameterSyntax,
516 517
                            policy, check_std)
......
530 531
  def _AssertIPolicyMerged(self, default_pol, diff_pol, merged_pol):
531 532
    for (key, value) in merged_pol.items():
532 533
      if key in diff_pol:
533
        if key in constants.IPOLICY_ISPECS:
534
        if key == constants.ISPECS_MINMAX:
535
          self.assertEqual(frozenset(value), constants.ISPECS_MINMAX_KEYS)
536
          for k in constants.ISPECS_MINMAX_KEYS:
537
            self._AssertISpecsMerged(default_pol[key][k], diff_pol[key][k],
538
                                     value[k])
539
        elif key == constants.ISPECS_STD:
534 540
          self._AssertISpecsMerged(default_pol[key], diff_pol[key], value)
535 541
        else:
536 542
          self.assertEqual(value, diff_pol[key])
......
550 556
      self._AssertIPolicyMerged(constants.IPOLICY_DEFAULTS, diff_pol, policy)
551 557

  
552 558
  def testFillIPolicySpecs(self):
553
    partial_policies = [
554
      {constants.ISPECS_MIN: {constants.ISPEC_MEM_SIZE: 32},
555
       constants.ISPECS_MAX: {constants.ISPEC_CPU_COUNT: 1024}},
556
      {constants.ISPECS_STD: {constants.ISPEC_DISK_SIZE: 2048},
557
       constants.ISPECS_MAX: {
558
          constants.ISPEC_DISK_COUNT: constants.MAX_DISKS - 1,
559
          constants.ISPEC_NIC_COUNT: constants.MAX_NICS - 1,
560
          }},
561
      {constants.ISPECS_STD: {constants.ISPEC_SPINDLE_USE: 3}},
559
    partial_ipolicies = [
560
      {
561
        constants.ISPECS_MINMAX: {
562
          constants.ISPECS_MIN: {constants.ISPEC_MEM_SIZE: 32},
563
          constants.ISPECS_MAX: {constants.ISPEC_CPU_COUNT: 1024}
564
          },
565
        },
566
      {
567
        constants.ISPECS_MINMAX: {
568
          constants.ISPECS_MAX: {
569
            constants.ISPEC_DISK_COUNT: constants.MAX_DISKS - 1,
570
            constants.ISPEC_NIC_COUNT: constants.MAX_NICS - 1,
571
            },
572
          constants.ISPECS_MIN: {},
573
          },
574
          constants.ISPECS_STD: {constants.ISPEC_DISK_SIZE: 2048},
575
        },
576
      {
577
        constants.ISPECS_STD: {constants.ISPEC_SPINDLE_USE: 3},
578
        },
562 579
      ]
563
    for diff_pol in partial_policies:
580
    for diff_pol in partial_ipolicies:
564 581
      policy = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, diff_pol)
565 582
      objects.InstancePolicy.CheckParameterSyntax(policy, True)
566 583
      self._AssertIPolicyIsFull(policy)

Also available in: Unified diff