Revision 7352d33b test/py/ganeti.cmdlib_unittest.py
b/test/py/ganeti.cmdlib_unittest.py | ||
---|---|---|
34 | 34 |
from ganeti import constants |
35 | 35 |
from ganeti import mcpu |
36 | 36 |
from ganeti import cmdlib |
37 |
from ganeti.cmdlib import cluster |
|
38 |
from ganeti.cmdlib import common |
|
37 | 39 |
from ganeti import opcodes |
38 | 40 |
from ganeti import errors |
39 | 41 |
from ganeti import utils |
... | ... | |
61 | 63 |
shutil.rmtree(self.tmpdir) |
62 | 64 |
|
63 | 65 |
def testVerifyCertificate(self): |
64 |
cmdlib._VerifyCertificate(testutils.TestDataFilename("cert1.pem"))
|
|
66 |
cluster._VerifyCertificate(testutils.TestDataFilename("cert1.pem"))
|
|
65 | 67 |
|
66 | 68 |
nonexist_filename = os.path.join(self.tmpdir, "does-not-exist") |
67 | 69 |
|
68 |
(errcode, msg) = cmdlib._VerifyCertificate(nonexist_filename)
|
|
69 |
self.assertEqual(errcode, cmdlib.LUClusterVerifyConfig.ETYPE_ERROR)
|
|
70 |
(errcode, msg) = cluster._VerifyCertificate(nonexist_filename)
|
|
71 |
self.assertEqual(errcode, cluster.LUClusterVerifyConfig.ETYPE_ERROR)
|
|
70 | 72 |
|
71 | 73 |
# Try to load non-certificate file |
72 | 74 |
invalid_cert = testutils.TestDataFilename("bdev-net.txt") |
73 |
(errcode, msg) = cmdlib._VerifyCertificate(invalid_cert)
|
|
74 |
self.assertEqual(errcode, cmdlib.LUClusterVerifyConfig.ETYPE_ERROR)
|
|
75 |
(errcode, msg) = cluster._VerifyCertificate(invalid_cert)
|
|
76 |
self.assertEqual(errcode, cluster.LUClusterVerifyConfig.ETYPE_ERROR)
|
|
75 | 77 |
|
76 | 78 |
|
77 | 79 |
class TestOpcodeParams(testutils.GanetiTestCase): |
... | ... | |
228 | 230 |
|
229 | 231 |
class TestClusterVerifySsh(unittest.TestCase): |
230 | 232 |
def testMultipleGroups(self): |
231 |
fn = cmdlib.LUClusterVerifyGroup._SelectSshCheckNodes
|
|
233 |
fn = cluster.LUClusterVerifyGroup._SelectSshCheckNodes
|
|
232 | 234 |
mygroupnodes = [ |
233 | 235 |
objects.Node(name="node20", group="my", offline=False), |
234 | 236 |
objects.Node(name="node21", group="my", offline=False), |
... | ... | |
266 | 268 |
}) |
267 | 269 |
|
268 | 270 |
def testSingleGroup(self): |
269 |
fn = cmdlib.LUClusterVerifyGroup._SelectSshCheckNodes
|
|
271 |
fn = cluster.LUClusterVerifyGroup._SelectSshCheckNodes
|
|
270 | 272 |
nodes = [ |
271 | 273 |
objects.Node(name="node1", group="default", offline=True), |
272 | 274 |
objects.Node(name="node2", group="default", offline=False), |
... | ... | |
299 | 301 |
if cond: |
300 | 302 |
errors.append((item, msg)) |
301 | 303 |
|
302 |
_VerifyFiles = cmdlib.LUClusterVerifyGroup._VerifyFiles
|
|
304 |
_VerifyFiles = cluster.LUClusterVerifyGroup._VerifyFiles
|
|
303 | 305 |
|
304 | 306 |
def test(self): |
305 | 307 |
errors = [] |
... | ... | |
518 | 520 |
}, |
519 | 521 |
} |
520 | 522 |
|
521 |
verified = cmdlib._UpdateAndVerifySubDict(old_test, test, self.type_check)
|
|
523 |
verified = common._UpdateAndVerifySubDict(old_test, test, self.type_check)
|
|
522 | 524 |
self.assertEqual(verified, mv) |
523 | 525 |
|
524 | 526 |
def testWrong(self): |
... | ... | |
536 | 538 |
} |
537 | 539 |
|
538 | 540 |
self.assertRaises(errors.TypeEnforcementError, |
539 |
cmdlib._UpdateAndVerifySubDict, {}, test, self.type_check) |
|
541 |
common._UpdateAndVerifySubDict, {}, test, |
|
542 |
self.type_check) |
|
540 | 543 |
|
541 | 544 |
|
542 | 545 |
class TestHvStateHelper(unittest.TestCase): |
543 | 546 |
def testWithoutOpData(self): |
544 |
self.assertEqual(cmdlib._MergeAndVerifyHvState(None, NotImplemented), None) |
|
547 |
self.assertEqual(common._MergeAndVerifyHvState(None, NotImplemented), |
|
548 |
None) |
|
545 | 549 |
|
546 | 550 |
def testWithoutOldData(self): |
547 | 551 |
new = { |
... | ... | |
549 | 553 |
constants.HVST_MEMORY_TOTAL: 4096, |
550 | 554 |
}, |
551 | 555 |
} |
552 |
self.assertEqual(cmdlib._MergeAndVerifyHvState(new, None), new)
|
|
556 |
self.assertEqual(common._MergeAndVerifyHvState(new, None), new)
|
|
553 | 557 |
|
554 | 558 |
def testWithWrongHv(self): |
555 | 559 |
new = { |
... | ... | |
557 | 561 |
constants.HVST_MEMORY_TOTAL: 4096, |
558 | 562 |
}, |
559 | 563 |
} |
560 |
self.assertRaises(errors.OpPrereqError, cmdlib._MergeAndVerifyHvState, new,
|
|
561 |
None) |
|
564 |
self.assertRaises(errors.OpPrereqError, common._MergeAndVerifyHvState,
|
|
565 |
new, None)
|
|
562 | 566 |
|
563 | 567 |
class TestDiskStateHelper(unittest.TestCase): |
564 | 568 |
def testWithoutOpData(self): |
565 |
self.assertEqual(cmdlib._MergeAndVerifyDiskState(None, NotImplemented),
|
|
569 |
self.assertEqual(common._MergeAndVerifyDiskState(None, NotImplemented),
|
|
566 | 570 |
None) |
567 | 571 |
|
568 | 572 |
def testWithoutOldData(self): |
... | ... | |
573 | 577 |
}, |
574 | 578 |
}, |
575 | 579 |
} |
576 |
self.assertEqual(cmdlib._MergeAndVerifyDiskState(new, None), new)
|
|
580 |
self.assertEqual(common._MergeAndVerifyDiskState(new, None), new)
|
|
577 | 581 |
|
578 | 582 |
def testWithWrongStorageType(self): |
579 | 583 |
new = { |
... | ... | |
583 | 587 |
}, |
584 | 588 |
}, |
585 | 589 |
} |
586 |
self.assertRaises(errors.OpPrereqError, cmdlib._MergeAndVerifyDiskState,
|
|
590 |
self.assertRaises(errors.OpPrereqError, common._MergeAndVerifyDiskState,
|
|
587 | 591 |
new, None) |
588 | 592 |
|
589 | 593 |
|
... | ... | |
601 | 605 |
} |
602 | 606 |
|
603 | 607 |
def testNoneValue(self): |
604 |
self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_MEM_SIZE, None,
|
|
608 |
self.assertTrue(common._ComputeMinMaxSpec(constants.ISPEC_MEM_SIZE, None,
|
|
605 | 609 |
self.ispecs, None) is None) |
606 | 610 |
|
607 | 611 |
def testAutoValue(self): |
608 |
self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_MEM_SIZE, None,
|
|
612 |
self.assertTrue(common._ComputeMinMaxSpec(constants.ISPEC_MEM_SIZE, None,
|
|
609 | 613 |
self.ispecs, |
610 | 614 |
constants.VALUE_AUTO) is None) |
611 | 615 |
|
612 | 616 |
def testNotDefined(self): |
613 |
self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_NIC_COUNT, None,
|
|
617 |
self.assertTrue(common._ComputeMinMaxSpec(constants.ISPEC_NIC_COUNT, None,
|
|
614 | 618 |
self.ispecs, 3) is None) |
615 | 619 |
|
616 | 620 |
def testNoMinDefined(self): |
617 |
self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_DISK_SIZE, None,
|
|
621 |
self.assertTrue(common._ComputeMinMaxSpec(constants.ISPEC_DISK_SIZE, None,
|
|
618 | 622 |
self.ispecs, 128) is None) |
619 | 623 |
|
620 | 624 |
def testNoMaxDefined(self): |
621 |
self.assertTrue(cmdlib._ComputeMinMaxSpec(constants.ISPEC_DISK_COUNT, None,
|
|
622 |
self.ispecs, 16) is None)
|
|
625 |
self.assertTrue(common._ComputeMinMaxSpec(constants.ISPEC_DISK_COUNT,
|
|
626 |
None, self.ispecs, 16) is None)
|
|
623 | 627 |
|
624 | 628 |
def testOutOfRange(self): |
625 | 629 |
for (name, val) in ((constants.ISPEC_MEM_SIZE, 64), |
... | ... | |
628 | 632 |
(constants.ISPEC_DISK_COUNT, 0)): |
629 | 633 |
min_v = self.ispecs[constants.ISPECS_MIN].get(name, val) |
630 | 634 |
max_v = self.ispecs[constants.ISPECS_MAX].get(name, val) |
631 |
self.assertEqual(cmdlib._ComputeMinMaxSpec(name, None,
|
|
635 |
self.assertEqual(common._ComputeMinMaxSpec(name, None,
|
|
632 | 636 |
self.ispecs, val), |
633 | 637 |
"%s value %s is not in range [%s, %s]" % |
634 | 638 |
(name, val,min_v, max_v)) |
635 |
self.assertEqual(cmdlib._ComputeMinMaxSpec(name, "1",
|
|
639 |
self.assertEqual(common._ComputeMinMaxSpec(name, "1",
|
|
636 | 640 |
self.ispecs, val), |
637 | 641 |
"%s/1 value %s is not in range [%s, %s]" % |
638 | 642 |
(name, val,min_v, max_v)) |
... | ... | |
645 | 649 |
(constants.ISPEC_DISK_SIZE, 0), |
646 | 650 |
(constants.ISPEC_DISK_COUNT, 1), |
647 | 651 |
(constants.ISPEC_DISK_COUNT, 5)): |
648 |
self.assertTrue(cmdlib._ComputeMinMaxSpec(name, None, self.ispecs, val)
|
|
652 |
self.assertTrue(common._ComputeMinMaxSpec(name, None, self.ispecs, val)
|
|
649 | 653 |
is None) |
650 | 654 |
|
651 | 655 |
|
... | ... | |
678 | 682 |
|
679 | 683 |
def test(self): |
680 | 684 |
compute_fn = _ValidateComputeMinMaxSpec |
681 |
ret = cmdlib._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
685 |
ret = common._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
682 | 686 |
[1024], 1, constants.DT_PLAIN, |
683 | 687 |
_compute_fn=compute_fn) |
684 | 688 |
self.assertEqual(ret, []) |
685 | 689 |
|
686 | 690 |
def testDiskFull(self): |
687 | 691 |
compute_fn = _NoDiskComputeMinMaxSpec |
688 |
ret = cmdlib._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
692 |
ret = common._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
689 | 693 |
[1024], 1, constants.DT_PLAIN, |
690 | 694 |
_compute_fn=compute_fn) |
691 | 695 |
self.assertEqual(ret, [constants.ISPEC_DISK_COUNT]) |
692 | 696 |
|
693 | 697 |
def testDiskLess(self): |
694 | 698 |
compute_fn = _NoDiskComputeMinMaxSpec |
695 |
ret = cmdlib._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
699 |
ret = common._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
696 | 700 |
[1024], 1, constants.DT_DISKLESS, |
697 | 701 |
_compute_fn=compute_fn) |
698 | 702 |
self.assertEqual(ret, []) |
699 | 703 |
|
700 | 704 |
def testWrongTemplates(self): |
701 | 705 |
compute_fn = _ValidateComputeMinMaxSpec |
702 |
ret = cmdlib._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
706 |
ret = common._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
703 | 707 |
[1024], 1, constants.DT_DRBD8, |
704 | 708 |
_compute_fn=compute_fn) |
705 | 709 |
self.assertEqual(len(ret), 1) |
706 | 710 |
self.assertTrue("Disk template" in ret[0]) |
707 | 711 |
|
708 | 712 |
def testInvalidArguments(self): |
709 |
self.assertRaises(AssertionError, cmdlib._ComputeIPolicySpecViolation,
|
|
713 |
self.assertRaises(AssertionError, common._ComputeIPolicySpecViolation,
|
|
710 | 714 |
self._MICRO_IPOL, 1024, 1, 1, 1, [], 1, |
711 | 715 |
constants.DT_PLAIN,) |
712 | 716 |
|
713 | 717 |
def testInvalidSpec(self): |
714 | 718 |
spec = _SpecWrapper([None, False, "foo", None, "bar", None]) |
715 | 719 |
compute_fn = spec.ComputeMinMaxSpec |
716 |
ret = cmdlib._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
720 |
ret = common._ComputeIPolicySpecViolation(self._MICRO_IPOL, 1024, 1, 1, 1,
|
|
717 | 721 |
[1024], 1, constants.DT_PLAIN, |
718 | 722 |
_compute_fn=compute_fn) |
719 | 723 |
self.assertEqual(ret, ["foo", "bar"]) |
... | ... | |
770 | 774 |
constants.IPOLICY_DTS: [disk_template], |
771 | 775 |
} |
772 | 776 |
def AssertComputeViolation(ipolicy, violations): |
773 |
ret = cmdlib._ComputeIPolicySpecViolation(ipolicy, mem_size, cpu_count,
|
|
777 |
ret = common._ComputeIPolicySpecViolation(ipolicy, mem_size, cpu_count,
|
|
774 | 778 |
disk_count, nic_count, |
775 | 779 |
disk_sizes, spindle_use, |
776 | 780 |
disk_template) |
... | ... | |
838 | 842 |
disk_template=constants.DT_PLAIN) |
839 | 843 |
stub = _StubComputeIPolicySpecViolation(2048, 2, 1, 0, [512], 4, |
840 | 844 |
constants.DT_PLAIN) |
841 |
ret = cmdlib._ComputeIPolicyInstanceViolation(NotImplemented, instance,
|
|
845 |
ret = common._ComputeIPolicyInstanceViolation(NotImplemented, instance,
|
|
842 | 846 |
cfg, _compute_fn=stub) |
843 | 847 |
self.assertEqual(ret, []) |
844 | 848 |
instance2 = objects.Instance(beparams={}, disks=disks, nics=[], |
845 | 849 |
disk_template=constants.DT_PLAIN) |
846 |
ret = cmdlib._ComputeIPolicyInstanceViolation(NotImplemented, instance2,
|
|
850 |
ret = common._ComputeIPolicyInstanceViolation(NotImplemented, instance2,
|
|
847 | 851 |
cfg, _compute_fn=stub) |
848 | 852 |
self.assertEqual(ret, []) |
849 | 853 |
|
... | ... | |
1672 | 1676 |
] |
1673 | 1677 |
|
1674 | 1678 |
|
1675 |
class _LuTestVerifyErrors(cmdlib._VerifyErrors):
|
|
1679 |
class _LuTestVerifyErrors(cluster._VerifyErrors):
|
|
1676 | 1680 |
def __init__(self, **kwargs): |
1677 |
cmdlib._VerifyErrors.__init__(self)
|
|
1681 |
cluster._VerifyErrors.__init__(self)
|
|
1678 | 1682 |
self.op = _OpTestVerifyErrors(**kwargs) |
1679 | 1683 |
self.op.Validate(True) |
1680 | 1684 |
self.msglist = [] |
... | ... | |
1879 | 1883 |
} |
1880 | 1884 |
if not isgroup: |
1881 | 1885 |
diff_policy[constants.ISPECS_STD] = diff_std |
1882 |
new_policy = cmdlib._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1886 |
new_policy = common._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1883 | 1887 |
group_policy=isgroup) |
1884 | 1888 |
|
1885 | 1889 |
self.assertTrue(constants.ISPECS_MINMAX in new_policy) |
... | ... | |
1901 | 1905 |
self.assertEqual(new_std[key], old_std[key]) |
1902 | 1906 |
|
1903 | 1907 |
def _TestSet(self, old_policy, diff_policy, isgroup): |
1904 |
new_policy = cmdlib._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1908 |
new_policy = common._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1905 | 1909 |
group_policy=isgroup) |
1906 | 1910 |
for key in diff_policy: |
1907 | 1911 |
self.assertTrue(key in new_policy) |
... | ... | |
1928 | 1932 |
diff_policy = { |
1929 | 1933 |
constants.IPOLICY_SPINDLE_RATIO: constants.VALUE_DEFAULT, |
1930 | 1934 |
} |
1931 |
new_policy = cmdlib._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1935 |
new_policy = common._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1932 | 1936 |
group_policy=True) |
1933 | 1937 |
for key in diff_policy: |
1934 | 1938 |
self.assertFalse(key in new_policy) |
... | ... | |
1937 | 1941 |
self.assertTrue(key in new_policy) |
1938 | 1942 |
self.assertEqual(new_policy[key], old_policy[key]) |
1939 | 1943 |
|
1940 |
self.assertRaises(errors.OpPrereqError, cmdlib._GetUpdatedIPolicy,
|
|
1944 |
self.assertRaises(errors.OpPrereqError, common._GetUpdatedIPolicy,
|
|
1941 | 1945 |
old_policy, diff_policy, group_policy=False) |
1942 | 1946 |
|
1943 | 1947 |
def testUnsetEmpty(self): |
... | ... | |
1946 | 1950 |
diff_policy = { |
1947 | 1951 |
key: constants.VALUE_DEFAULT, |
1948 | 1952 |
} |
1949 |
new_policy = cmdlib._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1953 |
new_policy = common._GetUpdatedIPolicy(old_policy, diff_policy,
|
|
1950 | 1954 |
group_policy=True) |
1951 | 1955 |
self.assertEqual(new_policy, old_policy) |
1952 | 1956 |
|
... | ... | |
1956 | 1960 |
INVALID_KEY: 3, |
1957 | 1961 |
} |
1958 | 1962 |
invalid_policy = INVALID_DICT |
1959 |
self.assertRaises(errors.OpPrereqError, cmdlib._GetUpdatedIPolicy,
|
|
1963 |
self.assertRaises(errors.OpPrereqError, common._GetUpdatedIPolicy,
|
|
1960 | 1964 |
old_policy, invalid_policy, group_policy=isgroup) |
1961 | 1965 |
invalid_ispecs = { |
1962 | 1966 |
constants.ISPECS_MINMAX: [INVALID_DICT], |
1963 | 1967 |
} |
1964 |
self.assertRaises(errors.TypeEnforcementError, cmdlib._GetUpdatedIPolicy,
|
|
1968 |
self.assertRaises(errors.TypeEnforcementError, common._GetUpdatedIPolicy,
|
|
1965 | 1969 |
old_policy, invalid_ispecs, group_policy=isgroup) |
1966 | 1970 |
if isgroup: |
1967 | 1971 |
invalid_for_group = { |
1968 | 1972 |
constants.ISPECS_STD: constants.IPOLICY_DEFAULTS[constants.ISPECS_STD], |
1969 | 1973 |
} |
1970 |
self.assertRaises(errors.OpPrereqError, cmdlib._GetUpdatedIPolicy,
|
|
1974 |
self.assertRaises(errors.OpPrereqError, common._GetUpdatedIPolicy,
|
|
1971 | 1975 |
old_policy, invalid_for_group, group_policy=isgroup) |
1972 | 1976 |
good_ispecs = self._OLD_CLUSTER_POLICY[constants.ISPECS_MINMAX] |
1973 | 1977 |
invalid_ispecs = copy.deepcopy(good_ispecs) |
... | ... | |
1979 | 1983 |
ispec = minmax[key] |
1980 | 1984 |
ispec[INVALID_KEY] = None |
1981 | 1985 |
self.assertRaises(errors.TypeEnforcementError, |
1982 |
cmdlib._GetUpdatedIPolicy, old_policy,
|
|
1986 |
common._GetUpdatedIPolicy, old_policy,
|
|
1983 | 1987 |
invalid_policy, group_policy=isgroup) |
1984 | 1988 |
del ispec[INVALID_KEY] |
1985 | 1989 |
for par in constants.ISPECS_PARAMETERS: |
1986 | 1990 |
oldv = ispec[par] |
1987 | 1991 |
ispec[par] = "this_is_not_good" |
1988 | 1992 |
self.assertRaises(errors.TypeEnforcementError, |
1989 |
cmdlib._GetUpdatedIPolicy,
|
|
1993 |
common._GetUpdatedIPolicy,
|
|
1990 | 1994 |
old_policy, invalid_policy, group_policy=isgroup) |
1991 | 1995 |
ispec[par] = oldv |
1992 | 1996 |
# This is to make sure that no two errors were present during the tests |
1993 |
cmdlib._GetUpdatedIPolicy(old_policy, invalid_policy, group_policy=isgroup) |
|
1997 |
common._GetUpdatedIPolicy(old_policy, invalid_policy, |
|
1998 |
group_policy=isgroup) |
|
1994 | 1999 |
|
1995 | 2000 |
def testInvalidKeys(self): |
1996 | 2001 |
self._TestInvalidKeys(self._OLD_GROUP_POLICY, True) |
... | ... | |
2002 | 2007 |
bad_policy = { |
2003 | 2008 |
par: "invalid_value", |
2004 | 2009 |
} |
2005 |
self.assertRaises(errors.OpPrereqError, cmdlib._GetUpdatedIPolicy, {},
|
|
2010 |
self.assertRaises(errors.OpPrereqError, common._GetUpdatedIPolicy, {},
|
|
2006 | 2011 |
bad_policy, group_policy=True) |
2007 | 2012 |
|
2008 | 2013 |
if __name__ == "__main__": |
Also available in: Unified diff