Revision da5f09ef test/py/ganeti.cmdlib_unittest.py

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

  
4
# Copyright (C) 2008, 2011, 2012 Google Inc.
4
# Copyright (C) 2008, 2011, 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
......
589 589

  
590 590
class TestComputeMinMaxSpec(unittest.TestCase):
591 591
  def setUp(self):
592
    self.ipolicy = {
592
    self.ispecs = {
593 593
      constants.ISPECS_MAX: {
594 594
        constants.ISPEC_MEM_SIZE: 512,
595 595
        constants.ISPEC_DISK_SIZE: 1024,
......
602 602

  
603 603
  def testNoneValue(self):
604 604
    self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_MEM_SIZE, None,
605
                                              self.ipolicy, None) is None)
605
                                              self.ispecs, None) is None)
606 606

  
607 607
  def testAutoValue(self):
608 608
    self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_MEM_SIZE, None,
609
                                              self.ipolicy,
609
                                              self.ispecs,
610 610
                                              constants.VALUE_AUTO) is None)
611 611

  
612 612
  def testNotDefined(self):
613 613
    self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_NIC_COUNT, None,
614
                                              self.ipolicy, 3) is None)
614
                                              self.ispecs, 3) is None)
615 615

  
616 616
  def testNoMinDefined(self):
617 617
    self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_DISK_SIZE, None,
618
                                              self.ipolicy, 128) is None)
618
                                              self.ispecs, 128) is None)
619 619

  
620 620
  def testNoMaxDefined(self):
621 621
    self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_DISK_COUNT, None,
622
                                                self.ipolicy, 16) is None)
622
                                                self.ispecs, 16) is None)
623 623

  
624 624
  def testOutOfRange(self):
625 625
    for (name, val) in ((constants.ISPEC_MEM_SIZE, 64),
626 626
                        (constants.ISPEC_MEM_SIZE, 768),
627 627
                        (constants.ISPEC_DISK_SIZE, 4096),
628 628
                        (constants.ISPEC_DISK_COUNT, 0)):
629
      min_v = self.ipolicy[constants.ISPECS_MIN].get(name, val)
630
      max_v = self.ipolicy[constants.ISPECS_MAX].get(name, val)
629
      min_v = self.ispecs[constants.ISPECS_MIN].get(name, val)
630
      max_v = self.ispecs[constants.ISPECS_MAX].get(name, val)
631 631
      self.assertEqual(cmdlib._ComputeMinMaxSpec(name, None,
632
                                                 self.ipolicy, val),
632
                                                 self.ispecs, val),
633 633
                       "%s value %s is not in range [%s, %s]" %
634 634
                       (name, val,min_v, max_v))
635 635
      self.assertEqual(cmdlib._ComputeMinMaxSpec(name, "1",
636
                                                 self.ipolicy, val),
636
                                                 self.ispecs, val),
637 637
                       "%s/1 value %s is not in range [%s, %s]" %
638 638
                       (name, val,min_v, max_v))
639 639

  
......
645 645
                        (constants.ISPEC_DISK_SIZE, 0),
646 646
                        (constants.ISPEC_DISK_COUNT, 1),
647 647
                        (constants.ISPEC_DISK_COUNT, 5)):
648
      self.assertTrue(cmdlib._ComputeMinMaxSpec(name, None, self.ipolicy, val)
648
      self.assertTrue(cmdlib._ComputeMinMaxSpec(name, None, self.ispecs, val)
649 649
                      is None)
650 650

  
651 651

  
......
673 673
  # Minimal policy accepted by _ComputeIPolicySpecViolation()
674 674
  _MICRO_IPOL = {
675 675
    constants.IPOLICY_DTS: [constants.DT_PLAIN, constants.DT_DISKLESS],
676
    constants.ISPECS_MINMAX: NotImplemented,
676 677
    }
677 678

  
678 679
  def test(self):
......
1732 1733
  """Tests for cmdlib._GetUpdatedIPolicy()"""
1733 1734
  _OLD_CLUSTER_POLICY = {
1734 1735
    constants.IPOLICY_VCPU_RATIO: 1.5,
1735
    constants.ISPECS_MIN: {
1736
      constants.ISPEC_MEM_SIZE: 20,
1737
      constants.ISPEC_CPU_COUNT: 2,
1736
    constants.ISPECS_MINMAX: {
1737
      constants.ISPECS_MIN: {
1738
        constants.ISPEC_MEM_SIZE: 20,
1739
        constants.ISPEC_CPU_COUNT: 2,
1740
        },
1741
      constants.ISPECS_MAX: {},
1738 1742
      },
1739
    constants.ISPECS_MAX: {},
1740 1743
    constants.ISPECS_STD: {},
1741 1744
    }
1742

  
1743 1745
  _OLD_GROUP_POLICY = {
1744 1746
    constants.IPOLICY_SPINDLE_RATIO: 2.5,
1745
    constants.ISPECS_MIN: {
1746
      constants.ISPEC_DISK_SIZE: 20,
1747
      constants.ISPEC_NIC_COUNT: 2,
1747
    constants.ISPECS_MINMAX: {
1748
      constants.ISPECS_MIN: {
1749
        constants.ISPEC_DISK_SIZE: 20,
1750
        constants.ISPEC_NIC_COUNT: 2,
1751
        },
1752
      constants.ISPECS_MAX: {},
1748 1753
      },
1749
    constants.ISPECS_MAX: {},
1750 1754
    }
1751 1755

  
1752 1756
  def _TestSetSpecs(self, old_policy, isgroup):
......
1756 1760
      constants.ISPEC_DISK_SIZE: 30,
1757 1761
      }
1758 1762
    diff_policy = {
1759
      ispec_key: diff_ispec
1763
      constants.ISPECS_MINMAX: {
1764
        ispec_key: diff_ispec,
1765
        },
1760 1766
      }
1767
    if not isgroup:
1768
      diff_std = {
1769
        constants.ISPEC_CPU_COUNT: 3,
1770
        constants.ISPEC_DISK_COUNT: 3,
1771
        }
1772
      diff_policy[constants.ISPECS_STD] = diff_std
1761 1773
    new_policy = cmdlib._GetUpdatedIPolicy(old_policy, diff_policy,
1762 1774
                                           group_policy=isgroup)
1763
    new_ispec = new_policy[ispec_key]
1775

  
1776
    self.assertTrue(constants.ISPECS_MINMAX in new_policy)
1777
    new_ispec = new_policy[constants.ISPECS_MINMAX][ispec_key]
1764 1778
    for key in diff_ispec:
1765 1779
      self.assertTrue(key in new_ispec)
1766 1780
      self.assertEqual(new_ispec[key], diff_ispec[key])
......
1768 1782
      if not key in diff_policy:
1769 1783
        self.assertTrue(key in new_policy)
1770 1784
        self.assertEqual(new_policy[key], old_policy[key])
1771
    old_ispec = old_policy[ispec_key]
1772
    for key in old_ispec:
1773
      if not key in diff_ispec:
1774
        self.assertTrue(key in new_ispec)
1775
        self.assertEqual(new_ispec[key], old_ispec[key])
1785

  
1786
    if constants.ISPECS_MINMAX in old_policy:
1787
      old_minmax = old_policy[constants.ISPECS_MINMAX]
1788
      for key in old_minmax:
1789
        if key != ispec_key:
1790
          self.assertTrue(key in new_policy[constants.ISPECS_MINMAX])
1791
          self.assertEqual(new_policy[constants.ISPECS_MINMAX][key],
1792
                           old_minmax[key])
1793
      old_ispec = old_policy[constants.ISPECS_MINMAX][ispec_key]
1794
      for key in old_ispec:
1795
        if not key in diff_ispec:
1796
          self.assertTrue(key in new_ispec)
1797
          self.assertEqual(new_ispec[key], old_ispec[key])
1798

  
1799
    if not isgroup:
1800
      new_std = new_policy[constants.ISPECS_STD]
1801
      for key in diff_std:
1802
        self.assertTrue(key in new_std)
1803
        self.assertEqual(new_std[key], diff_std[key])
1804

  
1776 1805

  
1777 1806
  def _TestSet(self, old_policy, isgroup):
1778 1807
    diff_policy = {
......
1816 1845
    invalid_policy = INVALID_DICT
1817 1846
    self.assertRaises(errors.OpPrereqError, cmdlib._GetUpdatedIPolicy,
1818 1847
                      old_policy, invalid_policy, group_policy=isgroup)
1819
    for key in constants.IPOLICY_ISPECS:
1848
    invalid_ispecs = {
1849
      constants.ISPECS_MINMAX: INVALID_DICT,
1850
      }
1851
    self.assertRaises(errors.OpPrereqError, cmdlib._GetUpdatedIPolicy,
1852
                      old_policy, invalid_ispecs, group_policy=isgroup)
1853
    for key in constants.ISPECS_MINMAX_KEYS:
1820 1854
      invalid_ispec = {
1821
        key: INVALID_DICT,
1855
        constants.ISPECS_MINMAX: {
1856
          key: INVALID_DICT,
1857
          },
1822 1858
        }
1823 1859
      self.assertRaises(errors.TypeEnforcementError, cmdlib._GetUpdatedIPolicy,
1824 1860
                        old_policy, invalid_ispec, group_policy=isgroup)

Also available in: Unified diff