X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/a59d5fa1351df43917514a1e30cd52f6a3789340..75bf31493fc1d65c588a3be5d29b0d0e4733683f:/test/py/ganeti.backend_unittest.py diff --git a/test/py/ganeti.backend_unittest.py b/test/py/ganeti.backend_unittest.py index 86f88bc..b6af4cd 100755 --- a/test/py/ganeti.backend_unittest.py +++ b/test/py/ganeti.backend_unittest.py @@ -26,6 +26,7 @@ import sys import shutil import tempfile import unittest +import mock from ganeti import utils from ganeti import constants @@ -33,6 +34,7 @@ from ganeti import backend from ganeti import netutils from ganeti import errors from ganeti import serializer +from ganeti import hypervisor import testutils import mocks @@ -75,11 +77,22 @@ class TestX509Certificates(unittest.TestCase): class TestNodeVerify(testutils.GanetiTestCase): + + def setUp(self): + testutils.GanetiTestCase.setUp(self) + self._mock_hv = None + + def _GetHypervisor(self, hv_name): + self._mock_hv = hypervisor.GetHypervisor(hv_name) + self._mock_hv.ValidateParameters = mock.Mock() + self._mock_hv.Verify = mock.Mock() + return self._mock_hv + def testMasterIPLocalhost(self): # this a real functional test, but requires localhost to be reachable local_data = (netutils.Hostname.GetSysName(), constants.IP4_ADDRESS_LOCALHOST) - result = backend.VerifyNode({constants.NV_MASTERIP: local_data}, None) + result = backend.VerifyNode({constants.NV_MASTERIP: local_data}, None, {}) self.failUnless(constants.NV_MASTERIP in result, "Master IP data not returned") self.failUnless(result[constants.NV_MASTERIP], "Cannot reach localhost") @@ -90,12 +103,32 @@ class TestNodeVerify(testutils.GanetiTestCase): bad_data = ("master.example.com", "192.0.2.1") # we just test that whatever TcpPing returns, VerifyNode returns too netutils.TcpPing = lambda a, b, source=None: False - result = backend.VerifyNode({constants.NV_MASTERIP: bad_data}, None) + result = backend.VerifyNode({constants.NV_MASTERIP: bad_data}, None, {}) self.failUnless(constants.NV_MASTERIP in result, "Master IP data not returned") self.failIf(result[constants.NV_MASTERIP], "Result from netutils.TcpPing corrupted") + def testVerifyHvparams(self): + test_hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} + test_what = {constants.NV_HVPARAMS: \ + [("mynode", constants.HT_XEN_PVM, test_hvparams)]} + result = {} + backend._VerifyHvparams(test_what, True, result, + get_hv_fn=self._GetHypervisor) + self._mock_hv.ValidateParameters.assert_called_with(test_hvparams) + + def testVerifyHypervisors(self): + hvname = constants.HT_XEN_PVM + hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} + all_hvparams = {hvname: hvparams} + test_what = {constants.NV_HYPERVISOR: [hvname]} + result = {} + backend._VerifyHypervisors( + test_what, True, result, all_hvparams=all_hvparams, + get_hv_fn=self._GetHypervisor) + self._mock_hv.Verify.assert_called_with(hvparams=hvparams) + def _DefRestrictedCmdOwner(): return (os.getuid(), os.getgid()) @@ -538,17 +571,26 @@ class TestGetBlockDevSymlinkPath(unittest.TestCase): self._Test("inst1.example.com", idx) -class TestInstReason(unittest.TestCase): - def testGetJson(self): - reason_text = "OS Update" - reason_source = constants.INSTANCE_REASON_SOURCE_CLI - origDict = dict(text=reason_text, source=reason_source) - - reason = backend.InstReason(reason_source, reason_text) - json = reason.GetJson() - resultDict = serializer.LoadJson(json) +class TestGetInstanceList(unittest.TestCase): - self.assertEqual(origDict, resultDict) + def setUp(self): + self._test_hv = self._TestHypervisor() + self._test_hv.ListInstances = mock.Mock( + return_value=["instance1", "instance2", "instance3"] ) + + class _TestHypervisor(hypervisor.hv_base.BaseHypervisor): + def __init__(self): + hypervisor.hv_base.BaseHypervisor.__init__(self) + + def _GetHypervisor(self, name): + return self._test_hv + + def testHvparams(self): + fake_hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} + hvparams = {constants.HT_FAKE: fake_hvparams} + backend.GetInstanceList([constants.HT_FAKE], all_hvparams=hvparams, + get_hv_fn=self._GetHypervisor) + self._test_hv.ListInstances.assert_called_with(hvparams=fake_hvparams) if __name__ == "__main__":