Revision 0fb81174 test/ganeti.cmdlib_unittest.py
b/test/ganeti.cmdlib_unittest.py | ||
---|---|---|
569 | 569 |
new, None) |
570 | 570 |
|
571 | 571 |
|
572 |
def _ValidateCheckMinMaxSpec(name, *_): |
|
573 |
assert name in constants.ISPECS_PARAMETERS |
|
574 |
return None |
|
575 |
|
|
576 |
|
|
577 |
class _SpecWrapper: |
|
578 |
def __init__(self, spec): |
|
579 |
self.spec = spec |
|
580 |
|
|
581 |
def CheckMinMaxSpec(self, *args): |
|
582 |
return self.spec.pop(0) |
|
583 |
|
|
584 |
|
|
585 |
class TestComputeIPolicySpecViolation(unittest.TestCase): |
|
586 |
def test(self): |
|
587 |
check_fn = _ValidateCheckMinMaxSpec |
|
588 |
ret = cmdlib._ComputeIPolicySpecViolation(NotImplemented, 1024, 1, 1, 1, |
|
589 |
[1024], _check_spec_fn=check_fn) |
|
590 |
self.assertEqual(ret, []) |
|
591 |
|
|
592 |
def testInvalidArguments(self): |
|
593 |
self.assertRaises(AssertionError, cmdlib._ComputeIPolicySpecViolation, |
|
594 |
NotImplemented, 1024, 1, 1, 1, []) |
|
595 |
|
|
596 |
def testInvalidSpec(self): |
|
597 |
spec = _SpecWrapper([None, False, "foo", None, "bar"]) |
|
598 |
check_fn = spec.CheckMinMaxSpec |
|
599 |
ret = cmdlib._ComputeIPolicySpecViolation(NotImplemented, 1024, 1, 1, 1, |
|
600 |
[1024], _check_spec_fn=check_fn) |
|
601 |
self.assertEqual(ret, ["foo", "bar"]) |
|
602 |
self.assertFalse(spec.spec) |
|
603 |
|
|
604 |
|
|
605 |
class _StubComputeIPolicySpecViolation: |
|
606 |
def __init__(self, mem_size, cpu_count, disk_count, nic_count, disk_sizes): |
|
607 |
self.mem_size = mem_size |
|
608 |
self.cpu_count = cpu_count |
|
609 |
self.disk_count = disk_count |
|
610 |
self.nic_count = nic_count |
|
611 |
self.disk_sizes = disk_sizes |
|
612 |
|
|
613 |
def __call__(self, _, mem_size, cpu_count, disk_count, nic_count, disk_sizes): |
|
614 |
assert self.mem_size == mem_size |
|
615 |
assert self.cpu_count == cpu_count |
|
616 |
assert self.disk_count == disk_count |
|
617 |
assert self.nic_count == nic_count |
|
618 |
assert self.disk_sizes == disk_sizes |
|
619 |
|
|
620 |
return [] |
|
621 |
|
|
622 |
|
|
623 |
class TestComputeIPolicyInstanceViolation(unittest.TestCase): |
|
624 |
def test(self): |
|
625 |
beparams = { |
|
626 |
constants.BE_MAXMEM: 2048, |
|
627 |
constants.BE_VCPUS: 2, |
|
628 |
} |
|
629 |
disks = [objects.Disk(size=512)] |
|
630 |
instance = objects.Instance(beparams=beparams, disks=disks, nics=[]) |
|
631 |
stub = _StubComputeIPolicySpecViolation(2048, 2, 1, 0, [512]) |
|
632 |
ret = cmdlib._ComputeIPolicyInstanceViolation(NotImplemented, instance, |
|
633 |
_compute_fn=stub) |
|
634 |
self.assertEqual(ret, []) |
|
635 |
|
|
636 |
|
|
637 |
class TestComputeIPolicyInstanceSpecViolation(unittest.TestCase): |
|
638 |
def test(self): |
|
639 |
ispec = { |
|
640 |
constants.ISPEC_MEM_SIZE: 2048, |
|
641 |
constants.ISPEC_CPU_COUNT: 2, |
|
642 |
constants.ISPEC_DISK_COUNT: 1, |
|
643 |
constants.ISPEC_DISK_SIZE: [512], |
|
644 |
constants.ISPEC_NIC_COUNT: 0, |
|
645 |
} |
|
646 |
stub = _StubComputeIPolicySpecViolation(2048, 2, 1, 0, [512]) |
|
647 |
ret = cmdlib._ComputeIPolicyInstanceSpecViolation(NotImplemented, ispec, |
|
648 |
_compute_fn=stub) |
|
649 |
self.assertEqual(ret, []) |
|
650 |
|
|
651 |
|
|
652 |
class _CallRecorder: |
|
653 |
def __init__(self, return_value=None): |
|
654 |
self.called = False |
|
655 |
self.return_value = return_value |
|
656 |
|
|
657 |
def __call__(self, *args): |
|
658 |
self.called = True |
|
659 |
return self.return_value |
|
660 |
|
|
661 |
|
|
662 |
class TestComputeIPolicyNodeViolation(unittest.TestCase): |
|
663 |
def setUp(self): |
|
664 |
self.recorder = _CallRecorder(return_value=[]) |
|
665 |
|
|
666 |
def testSameGroup(self): |
|
667 |
ret = cmdlib._ComputeIPolicyNodeViolation(NotImplemented, NotImplemented, |
|
668 |
"foo", "foo", |
|
669 |
_compute_fn=self.recorder) |
|
670 |
self.assertFalse(self.recorder.called) |
|
671 |
self.assertEqual(ret, []) |
|
672 |
|
|
673 |
def testDifferentGroup(self): |
|
674 |
ret = cmdlib._ComputeIPolicyNodeViolation(NotImplemented, NotImplemented, |
|
675 |
"foo", "bar", |
|
676 |
_compute_fn=self.recorder) |
|
677 |
self.assertTrue(self.recorder.called) |
|
678 |
self.assertEqual(ret, []) |
|
679 |
|
|
680 |
|
|
681 |
class TestCheckTargetNodeIPolicy(unittest.TestCase): |
|
682 |
def setUp(self): |
|
683 |
self.instance = objects.Instance(primary_node=objects.Node(group="foo")) |
|
684 |
self.target_node = objects.Node(group="bar") |
|
685 |
self.lu = _FakeLU() |
|
686 |
|
|
687 |
def testNoViolation(self): |
|
688 |
compute_recoder = _CallRecorder(return_value=[]) |
|
689 |
cmdlib._CheckTargetNodeIPolicy(self.lu, NotImplemented, self.instance, |
|
690 |
self.target_node, |
|
691 |
_compute_fn=compute_recoder) |
|
692 |
self.assertTrue(compute_recoder.called) |
|
693 |
self.assertEqual(self.lu.warning_log, []) |
|
694 |
|
|
695 |
def testNoIgnore(self): |
|
696 |
compute_recoder = _CallRecorder(return_value=["mem_size not in range"]) |
|
697 |
self.assertRaises(errors.OpPrereqError, cmdlib._CheckTargetNodeIPolicy, |
|
698 |
self.lu, NotImplemented, self.instance, self.target_node, |
|
699 |
_compute_fn=compute_recoder) |
|
700 |
self.assertTrue(compute_recoder.called) |
|
701 |
self.assertEqual(self.lu.warning_log, []) |
|
702 |
|
|
703 |
def testIgnoreViolation(self): |
|
704 |
compute_recoder = _CallRecorder(return_value=["mem_size not in range"]) |
|
705 |
cmdlib._CheckTargetNodeIPolicy(self.lu, NotImplemented, self.instance, |
|
706 |
self.target_node, ignore=True, |
|
707 |
_compute_fn=compute_recoder) |
|
708 |
self.assertTrue(compute_recoder.called) |
|
709 |
msg = ("Instance does not meet target node group's (bar) instance policy:" |
|
710 |
" mem_size not in range") |
|
711 |
self.assertEqual(self.lu.warning_log, [(msg, ())]) |
|
712 |
|
|
713 |
|
|
572 | 714 |
if __name__ == "__main__": |
573 | 715 |
testutils.GanetiTestProgram() |
Also available in: Unified diff