Commit d2e4e099 authored by Jose A. Lopes's avatar Jose A. Lopes

Update tests

Update hypervisor unit tests.
Signed-off-by: default avatarJose A. Lopes <jabolopes@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent e52e0ddc
...@@ -37,6 +37,7 @@ from ganeti import utils ...@@ -37,6 +37,7 @@ from ganeti import utils
from ganeti import errors from ganeti import errors
from ganeti import compat from ganeti import compat
from ganeti.hypervisor import hv_base
from ganeti.hypervisor import hv_xen from ganeti.hypervisor import hv_xen
import testutils import testutils
...@@ -137,7 +138,7 @@ class TestParseInstanceList(testutils.GanetiTestCase): ...@@ -137,7 +138,7 @@ class TestParseInstanceList(testutils.GanetiTestCase):
self.assertEqual(result[0][3], 1) self.assertEqual(result[0][3], 1)
# State # State
self.assertEqual(result[0][4], "r-----") self.assertEqual(result[0][4], hv_base.HvInstanceState.RUNNING)
# Time # Time
self.assertAlmostEqual(result[0][5], 121152.6) self.assertAlmostEqual(result[0][5], 121152.6)
...@@ -166,7 +167,7 @@ class TestGetInstanceList(testutils.GanetiTestCase): ...@@ -166,7 +167,7 @@ class TestGetInstanceList(testutils.GanetiTestCase):
def testTimeout(self): def testTimeout(self):
fn = testutils.CallCounter(self._Fail) fn = testutils.CallCounter(self._Fail)
try: try:
hv_xen._GetInstanceList(fn, False, _timeout=0.1) hv_xen._GetRunningInstanceList(fn, False, _timeout=0.1)
except errors.HypervisorError, err: except errors.HypervisorError, err:
self.assertTrue("timeout exceeded" in str(err)) self.assertTrue("timeout exceeded" in str(err))
else: else:
...@@ -184,7 +185,7 @@ class TestGetInstanceList(testutils.GanetiTestCase): ...@@ -184,7 +185,7 @@ class TestGetInstanceList(testutils.GanetiTestCase):
fn = testutils.CallCounter(compat.partial(self._Success, data)) fn = testutils.CallCounter(compat.partial(self._Success, data))
result = hv_xen._GetInstanceList(fn, True, _timeout=0.1) result = hv_xen._GetRunningInstanceList(fn, True, _timeout=0.1)
self.assertEqual(len(result), 4) self.assertEqual(len(result), 4)
...@@ -379,7 +380,7 @@ class TestXenHypervisorGetInstanceList(unittest.TestCase): ...@@ -379,7 +380,7 @@ class TestXenHypervisorGetInstanceList(unittest.TestCase):
def testNoHvparams(self): def testNoHvparams(self):
expected_xen_cmd = "xm" expected_xen_cmd = "xm"
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) mock_run_cmd = mock.Mock(return_value=self.RESULT_OK)
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
_run_cmd_fn=mock_run_cmd) _run_cmd_fn=mock_run_cmd)
self.assertRaises(errors.HypervisorError, hv._GetInstanceList, True, None) self.assertRaises(errors.HypervisorError, hv._GetInstanceList, True, None)
...@@ -387,7 +388,7 @@ class TestXenHypervisorGetInstanceList(unittest.TestCase): ...@@ -387,7 +388,7 @@ class TestXenHypervisorGetInstanceList(unittest.TestCase):
def testFromHvparams(self): def testFromHvparams(self):
expected_xen_cmd = "xl" expected_xen_cmd = "xl"
hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL}
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) mock_run_cmd = mock.Mock(return_value=self.RESULT_OK)
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
_run_cmd_fn=mock_run_cmd) _run_cmd_fn=mock_run_cmd)
hv._GetInstanceList(True, hvparams) hv._GetInstanceList(True, hvparams)
...@@ -401,7 +402,7 @@ class TestXenHypervisorListInstances(unittest.TestCase): ...@@ -401,7 +402,7 @@ class TestXenHypervisorListInstances(unittest.TestCase):
def testNoHvparams(self): def testNoHvparams(self):
expected_xen_cmd = "xm" expected_xen_cmd = "xm"
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) mock_run_cmd = mock.Mock(return_value=self.RESULT_OK)
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
_run_cmd_fn=mock_run_cmd) _run_cmd_fn=mock_run_cmd)
self.assertRaises(errors.HypervisorError, hv.ListInstances) self.assertRaises(errors.HypervisorError, hv.ListInstances)
...@@ -409,7 +410,7 @@ class TestXenHypervisorListInstances(unittest.TestCase): ...@@ -409,7 +410,7 @@ class TestXenHypervisorListInstances(unittest.TestCase):
def testHvparamsXl(self): def testHvparamsXl(self):
expected_xen_cmd = "xl" expected_xen_cmd = "xl"
hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL}
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) mock_run_cmd = mock.Mock(return_value=self.RESULT_OK)
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
_run_cmd_fn=mock_run_cmd) _run_cmd_fn=mock_run_cmd)
hv.ListInstances(hvparams=hvparams) hv.ListInstances(hvparams=hvparams)
...@@ -446,7 +447,7 @@ class TestXenHypervisorCheckToolstack(unittest.TestCase): ...@@ -446,7 +447,7 @@ class TestXenHypervisorCheckToolstack(unittest.TestCase):
def testCheckToolstackXlNotConfigured(self): def testCheckToolstackXlNotConfigured(self):
RESULT_FAILED = utils.RunResult( RESULT_FAILED = utils.RunResult(
1, None, "", 1, None, "",
"ERROR: A different toolstack (xm) have been selected!", "ERROR: A different toolstack (xm) has been selected!",
"", None, None) "", None, None)
mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) mock_run_cmd = mock.Mock(return_value=RESULT_FAILED)
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
...@@ -610,7 +611,7 @@ class _TestXenHypervisor(object): ...@@ -610,7 +611,7 @@ class _TestXenHypervisor(object):
self.assertEqual(instid, 1) self.assertEqual(instid, 1)
self.assertEqual(memory, 1024) self.assertEqual(memory, 1024)
self.assertEqual(vcpus, 1) self.assertEqual(vcpus, 1)
self.assertEqual(state, "-b----") self.assertEqual(state, hv_base.HvInstanceState.RUNNING)
self.assertAlmostEqual(runtime, 167643.2) self.assertAlmostEqual(runtime, 167643.2)
def testGetInstanceInfoDom0(self): def testGetInstanceInfoDom0(self):
...@@ -625,7 +626,7 @@ class _TestXenHypervisor(object): ...@@ -625,7 +626,7 @@ class _TestXenHypervisor(object):
self.assertEqual(instid, 0) self.assertEqual(instid, 0)
self.assertEqual(memory, 1023) self.assertEqual(memory, 1023)
self.assertEqual(vcpus, 1) self.assertEqual(vcpus, 1)
self.assertEqual(state, "r-----") self.assertEqual(state, hv_base.HvInstanceState.RUNNING)
self.assertAlmostEqual(runtime, 154706.1) self.assertAlmostEqual(runtime, 154706.1)
def testGetInstanceInfoUnknown(self): def testGetInstanceInfoUnknown(self):
...@@ -738,10 +739,16 @@ class _TestXenHypervisor(object): ...@@ -738,10 +739,16 @@ class _TestXenHypervisor(object):
self.assertTrue(("extra = '%s'" % extra) in lines) self.assertTrue(("extra = '%s'" % extra) in lines)
def _StopInstanceCommand(self, instance_name, force, fail, cmd): def _StopInstanceCommand(self, instance_name, force, fail, cmd):
if ((force and cmd[:2] == [self.CMD, "destroy"]) or if (cmd == [self.CMD, "list"]):
(not force and cmd[:2] == [self.CMD, "shutdown"])): output = "Name ID Mem VCPUs State Time(s)\n" \
"Domain-0 0 1023 1 r----- 142691.0\n" \
"%s 417 128 1 r----- 3.2\n" % instance_name
elif cmd[:2] == [self.CMD, "destroy"]:
self.assertEqual(cmd[2:], [instance_name]) self.assertEqual(cmd[2:], [instance_name])
output = "" output = ""
elif not force and cmd[:3] == [self.CMD, "shutdown", "-w"]:
self.assertEqual(cmd[3:], [instance_name])
output = ""
else: else:
self.fail("Unhandled command: %s" % (cmd, )) self.fail("Unhandled command: %s" % (cmd, ))
...@@ -770,7 +777,8 @@ class _TestXenHypervisor(object): ...@@ -770,7 +777,8 @@ class _TestXenHypervisor(object):
try: try:
hv._StopInstance(name, force, None) hv._StopInstance(name, force, None)
except errors.HypervisorError, err: except errors.HypervisorError, err:
self.assertTrue(str(err).startswith("Failed to stop instance")) self.assertTrue(str(err).startswith("listing instances failed"),
msg=str(err))
else: else:
self.fail("Exception was not raised") self.fail("Exception was not raised")
self.assertEqual(utils.ReadFile(cfgfile), cfgdata, self.assertEqual(utils.ReadFile(cfgfile), cfgdata,
......
...@@ -34,6 +34,7 @@ from ganeti import objects ...@@ -34,6 +34,7 @@ from ganeti import objects
from ganeti import cmdlib from ganeti import cmdlib
import ganeti.masterd.instance as gmi import ganeti.masterd.instance as gmi
from ganeti.hypervisor import hv_base
import testutils import testutils
...@@ -806,6 +807,17 @@ class TestInstanceQuery(unittest.TestCase): ...@@ -806,6 +807,17 @@ class TestInstanceQuery(unittest.TestCase):
disks_active=False, disks_active=False,
beparams={}, beparams={},
osparams={}), osparams={}),
objects.Instance(name="inst9", hvparams={}, nics=[],
uuid="inst9-uuid",
ctime=None, mtime=None, serial_no=19478,
admin_state=constants.ADMINST_UP, hypervisor=constants.HT_XEN_HVM,
os="deb99",
primary_node="node6-uuid",
disk_template=constants.DT_DISKLESS,
disks=[],
disks_active=False,
beparams={},
osparams={}),
] ]
assert not utils.FindDuplicates(inst.uuid for inst in instances) assert not utils.FindDuplicates(inst.uuid for inst in instances)
...@@ -828,15 +840,23 @@ class TestInstanceQuery(unittest.TestCase): ...@@ -828,15 +840,23 @@ class TestInstanceQuery(unittest.TestCase):
live_data = { live_data = {
"inst2-uuid": { "inst2-uuid": {
"vcpus": 3, "vcpus": 3,
"state": hv_base.HvInstanceState.RUNNING,
}, },
"inst4-uuid": { "inst4-uuid": {
"memory": 123, "memory": 123,
"state": hv_base.HvInstanceState.RUNNING,
}, },
"inst6-uuid": { "inst6-uuid": {
"memory": 768, "memory": 768,
"state": hv_base.HvInstanceState.RUNNING,
}, },
"inst7-uuid": { "inst7-uuid": {
"vcpus": 3, "vcpus": 3,
"state": hv_base.HvInstanceState.RUNNING,
},
"inst9-uuid": {
"vcpus": 3,
"state": hv_base.HvInstanceState.SHUTDOWN,
}, },
} }
wrongnode_inst = set(["inst7-uuid"]) wrongnode_inst = set(["inst7-uuid"])
...@@ -880,16 +900,25 @@ class TestInstanceQuery(unittest.TestCase): ...@@ -880,16 +900,25 @@ class TestInstanceQuery(unittest.TestCase):
elif inst.uuid in live_data: elif inst.uuid in live_data:
if inst.uuid in wrongnode_inst: if inst.uuid in wrongnode_inst:
exp_status = constants.INSTST_WRONGNODE exp_status = constants.INSTST_WRONGNODE
elif inst.admin_state == constants.ADMINST_UP:
exp_status = constants.INSTST_RUNNING
else: else:
exp_status = constants.INSTST_ERRORUP instance_state = live_data[inst.uuid]["state"]
elif inst.admin_state == constants.ADMINST_UP: if hv_base.HvInstanceState.IsShutdown(instance_state):
exp_status = constants.INSTST_ERRORDOWN if inst.admin_state == constants.ADMINST_UP:
elif inst.admin_state == constants.ADMINST_DOWN: exp_status = constants.INSTST_USERDOWN
exp_status = constants.INSTST_ADMINDOWN else:
exp_status = constants.INSTST_ADMINDOWN
else:
if inst.admin_state == constants.ADMINST_UP:
exp_status = constants.INSTST_RUNNING
else:
exp_status = constants.INSTST_ERRORUP
else: else:
exp_status = constants.INSTST_ADMINOFFLINE if inst.admin_state == constants.ADMINST_UP:
exp_status = constants.INSTST_ERRORDOWN
elif inst.admin_state == constants.ADMINST_DOWN:
exp_status = constants.INSTST_ADMINDOWN
else:
exp_status = constants.INSTST_ADMINOFFLINE
self.assertEqual(row[fieldidx["status"]], self.assertEqual(row[fieldidx["status"]],
(constants.RS_NORMAL, exp_status)) (constants.RS_NORMAL, exp_status))
...@@ -975,7 +1004,7 @@ class TestInstanceQuery(unittest.TestCase): ...@@ -975,7 +1004,7 @@ class TestInstanceQuery(unittest.TestCase):
self._CheckInstanceConsole(inst, row[fieldidx["console"]]) self._CheckInstanceConsole(inst, row[fieldidx["console"]])
# Ensure all possible status' have been tested # Ensure all possible status' have been tested
self.assertEqual(tested_status, constants.INSTST_ALL) self.assertEqual(tested_status, set(constants.INSTST_ALL))
def _CheckInstanceConsole(self, instance, (status, consdata)): def _CheckInstanceConsole(self, instance, (status, consdata)):
if instance.name == "inst7": if instance.name == "inst7":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment