Revision 74a50c46 test/py/ganeti.hypervisor.hv_xen_unittest.py
b/test/py/ganeti.hypervisor.hv_xen_unittest.py | ||
---|---|---|
25 | 25 |
import unittest |
26 | 26 |
import tempfile |
27 | 27 |
import shutil |
28 |
import random |
|
28 | 29 |
|
29 | 30 |
from ganeti import constants |
30 | 31 |
from ganeti import objects |
... | ... | |
38 | 39 |
import testutils |
39 | 40 |
|
40 | 41 |
|
42 |
# Map from hypervisor class to hypervisor name |
|
43 |
HVCLASS_TO_HVNAME = utils.InvertDict(hypervisor._HYPERVISOR_MAP) |
|
44 |
|
|
45 |
|
|
41 | 46 |
class TestConsole(unittest.TestCase): |
42 | 47 |
def test(self): |
43 | 48 |
for cls in [hv_xen.XenPvmHypervisor, hv_xen.XenHvmHypervisor]: |
... | ... | |
324 | 329 |
self.fail("Exception was not raised") |
325 | 330 |
|
326 | 331 |
|
332 |
class _TestXenHypervisor(object): |
|
333 |
TARGET = NotImplemented |
|
334 |
CMD = NotImplemented |
|
335 |
HVNAME = NotImplemented |
|
336 |
|
|
337 |
def setUp(self): |
|
338 |
super(_TestXenHypervisor, self).setUp() |
|
339 |
|
|
340 |
self.tmpdir = tempfile.mkdtemp() |
|
341 |
|
|
342 |
self.vncpw = "".join(random.sample(string.ascii_letters, 10)) |
|
343 |
|
|
344 |
self.vncpw_path = utils.PathJoin(self.tmpdir, "vncpw") |
|
345 |
utils.WriteFile(self.vncpw_path, data=self.vncpw) |
|
346 |
|
|
347 |
def tearDown(self): |
|
348 |
super(_TestXenHypervisor, self).tearDown() |
|
349 |
|
|
350 |
shutil.rmtree(self.tmpdir) |
|
351 |
|
|
352 |
def _GetHv(self, run_cmd=NotImplemented): |
|
353 |
return self.TARGET(_cfgdir=self.tmpdir, _run_cmd_fn=run_cmd, _cmd=self.CMD) |
|
354 |
|
|
355 |
def _SuccessCommand(self, stdout, cmd): |
|
356 |
self.assertEqual(cmd[0], self.CMD) |
|
357 |
|
|
358 |
return utils.RunResult(constants.EXIT_SUCCESS, None, stdout, "", None, |
|
359 |
NotImplemented, NotImplemented) |
|
360 |
|
|
361 |
def _FailingCommand(self, cmd): |
|
362 |
self.assertEqual(cmd[0], self.CMD) |
|
363 |
|
|
364 |
return utils.RunResult(constants.EXIT_FAILURE, None, |
|
365 |
"", "This command failed", None, |
|
366 |
NotImplemented, NotImplemented) |
|
367 |
|
|
368 |
|
|
369 |
def _MakeTestClass(cls, cmd): |
|
370 |
"""Makes a class for testing. |
|
371 |
|
|
372 |
The returned class has structure as shown in the following pseudo code: |
|
373 |
|
|
374 |
class Test{cls.__name__}{cmd}(_TestXenHypervisor, unittest.TestCase): |
|
375 |
TARGET = {cls} |
|
376 |
CMD = {cmd} |
|
377 |
HVNAME = {Hypervisor name retrieved using class} |
|
378 |
|
|
379 |
@type cls: class |
|
380 |
@param cls: Hypervisor class to be tested |
|
381 |
@type cmd: string |
|
382 |
@param cmd: Hypervisor command |
|
383 |
@rtype: tuple |
|
384 |
@return: Class name and class object (not instance) |
|
385 |
|
|
386 |
""" |
|
387 |
name = "Test%sCmd%s" % (cls.__name__, cmd.title()) |
|
388 |
bases = (_TestXenHypervisor, unittest.TestCase) |
|
389 |
hvname = HVCLASS_TO_HVNAME[cls] |
|
390 |
|
|
391 |
return (name, type(name, bases, dict(TARGET=cls, CMD=cmd, HVNAME=hvname))) |
|
392 |
|
|
393 |
|
|
394 |
# Create test classes programmatically instead of manually to reduce the risk |
|
395 |
# of forgetting some combinations |
|
396 |
for cls in [hv_xen.XenPvmHypervisor, hv_xen.XenHvmHypervisor]: |
|
397 |
for cmd in constants.KNOWN_XEN_COMMANDS: |
|
398 |
(name, testcls) = _MakeTestClass(cls, cmd) |
|
399 |
|
|
400 |
assert name not in locals() |
|
401 |
|
|
402 |
locals()[name] = testcls |
|
403 |
|
|
404 |
|
|
327 | 405 |
if __name__ == "__main__": |
328 | 406 |
testutils.GanetiTestProgram() |
Also available in: Unified diff