From 792b6b29c7415a12f4d62856721358b62dcad4da Mon Sep 17 00:00:00 2001 From: Martin Gracik Date: Fri, 13 Mar 2009 14:58:19 +0100 Subject: Added the run_test.py script to easily run the test cases. Some small changes to test cases were needed. Added suite() function to them and changed the class names. Also __init__.py files were added to the directories. --- tests/__init__.py | 29 +++++++++++++ tests/storage/__init__.py | 0 tests/storage/devicelibs/__init__.py | 0 tests/storage/devicelibs/baseclass.py | 76 ++++++++++++++++++----------------- tests/storage/devicelibs/crypto.py | 11 +++-- tests/storage/devicelibs/lvm.py | 18 +++++---- tests/storage/devicelibs/mdraid.py | 12 +++--- tests/storage/devicelibs/swap.py | 11 +++-- 8 files changed, 99 insertions(+), 58 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/storage/__init__.py create mode 100644 tests/storage/devicelibs/__init__.py (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..d5b53a8af --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,29 @@ +import os + +# this has to be imported before running anything +import anaconda_log +import upgrade + + +def getAvailableSuites(): + root, tests_dir = os.path.split(os.path.dirname(__file__)) + modules = [] + + for root, dirs, files in os.walk(tests_dir): + for filename in files: + if filename.endswith(".py") and filename != "__init__.py": + basename, extension = os.path.splitext(filename) + modules.append(os.path.join(root, basename).replace("/", ".")) + + available_suites = {} + for module in modules: + imported = __import__(module, globals(), locals(), [module], -1) + try: + suite = getattr(imported, "suite") + except AttributeError as e: + continue + + if callable(suite): + available_suites[module] = suite() + + return available_suites diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/storage/devicelibs/__init__.py b/tests/storage/devicelibs/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/storage/devicelibs/baseclass.py b/tests/storage/devicelibs/baseclass.py index 01fe6c779..c19bfc376 100644 --- a/tests/storage/devicelibs/baseclass.py +++ b/tests/storage/devicelibs/baseclass.py @@ -2,11 +2,44 @@ import unittest import os import subprocess -# need to import these first before doing anything -import anaconda_log -import upgrade -class TestDevicelibs(unittest.TestCase): +def makeLoopDev(device_name, file_name): + proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name, + "bs=1024", "count=102400"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + while True: + proc.communicate() + if proc.returncode is not None: + rc = proc.returncode + break + if rc: + raise OSError, "dd failed creating the file %s" % file_name + + proc = subprocess.Popen(["losetup", device_name, file_name], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + while True: + proc.communicate() + if proc.returncode is not None: + rc = proc.returncode + break + if rc: + raise OSError, "losetup failed setting up the loop device %s" % device_name + +def removeLoopDev(device_name, file_name): + proc = subprocess.Popen(["losetup", "-d", device_name], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + while True: + proc.communicate() + if proc.returncode is not None: + rc = proc.returncode + break + if rc: + raise OSError, "losetup failed removing the loop device %s" % device_name + + os.unlink(file_name) + + +class DevicelibsTestCase(unittest.TestCase): _LOOP_DEVICES = (("/dev/loop0", "/tmp/test-virtdev0"), ("/dev/loop1", "/tmp/test-virtdev1")) @@ -15,39 +48,8 @@ class TestDevicelibs(unittest.TestCase): def setUp(self): for dev, file in self._LOOP_DEVICES: - self.makeLoopDev(dev, file) + makeLoopDev(dev, file) def tearDown(self): for dev, file in self._LOOP_DEVICES: - self.removeLoopDev(dev, file) - - def makeLoopDev(self, device_name, file_name): - proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name, "bs=1024", "count=102400"]) - while True: - proc.communicate() - if proc.returncode is not None: - rc = proc.returncode - break - if rc: - raise OSError, "dd failed creating the file %s" % file_name - - proc = subprocess.Popen(["losetup", device_name, file_name]) - while True: - proc.communicate() - if proc.returncode is not None: - rc = proc.returncode - break - if rc: - raise OSError, "losetup failed setting up the loop device %s" % device_name - - def removeLoopDev(self, device_name, file_name): - proc = subprocess.Popen(["losetup", "-d", device_name]) - while True: - proc.communicate() - if proc.returncode is not None: - rc = proc.returncode - break - if rc: - raise OSError, "losetup failed removing the loop device %s" % device_name - - os.remove(file_name) + removeLoopDev(dev, file) diff --git a/tests/storage/devicelibs/crypto.py b/tests/storage/devicelibs/crypto.py index 6a76569c5..0f9f7bdb5 100644 --- a/tests/storage/devicelibs/crypto.py +++ b/tests/storage/devicelibs/crypto.py @@ -5,9 +5,9 @@ import storage.devicelibs.crypto as crypto import tempfile import os -class TestCrypto(baseclass.TestDevicelibs): +class CryptoTestCase(baseclass.DevicelibsTestCase): - def runTest(self): + def testCrypto(self): ## ## is_luks ## @@ -118,6 +118,9 @@ class TestCrypto(baseclass.TestDevicelibs): os.unlink(new_keyfile) +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(CryptoTestCase) + + if __name__ == "__main__": - suite = unittest.TestLoader().loadTestsFromTestCase(TestCrypto) - unittest.TextTestRunner(verbosity=2).run(suite) + unittest.main() diff --git a/tests/storage/devicelibs/lvm.py b/tests/storage/devicelibs/lvm.py index 62c52d168..e6ba1a628 100644 --- a/tests/storage/devicelibs/lvm.py +++ b/tests/storage/devicelibs/lvm.py @@ -2,9 +2,9 @@ import baseclass import unittest import storage.devicelibs.lvm as lvm -class TestLVM(baseclass.TestDevicelibs): +class LVMTestCase(baseclass.DevicelibsTestCase): - def testLVMStuff(self): + def testLVM(self): ## ## pvcreate ## @@ -154,7 +154,7 @@ class TestLVM(baseclass.TestDevicelibs): self.assertEqual(lvm.has_lvm(), True) # fail - #TODO + # TODO ## ## lvremove @@ -214,15 +214,17 @@ class TestLVM(baseclass.TestDevicelibs): self.assertEqual(lvm.clampSize(10, 4, True), 12L) #def testVGUsedSpace(self): - #TODO + # TODO pass #def testVGFreeSpace(self): - #TODO + # TODO pass -if __name__ == "__main__": - suite = unittest.TestLoader().loadTestsFromTestCase(TestLVM) - unittest.TextTestRunner(verbosity=2).run(suite) +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(LVMTestCase) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/storage/devicelibs/mdraid.py b/tests/storage/devicelibs/mdraid.py index 6e49e55b0..3c0ee7224 100644 --- a/tests/storage/devicelibs/mdraid.py +++ b/tests/storage/devicelibs/mdraid.py @@ -4,9 +4,9 @@ import storage.devicelibs.mdraid as mdraid import time -class TestMDRaid(baseclass.TestDevicelibs): +class MDRaidTestCase(baseclass.DevicelibsTestCase): - def testMDRaidStuff(self): + def testMDRaid(self): ## ## getRaidLevels ## @@ -99,7 +99,9 @@ class TestMDRaid(baseclass.TestDevicelibs): self.assertRaises(mdraid.MDRaidError, mdraid.mddestroy, "/not/existing/device") -if __name__ == "__main__": - suite = unittest.TestLoader().loadTestsFromTestCase(TestMDRaid) - unittest.TextTestRunner(verbosity=2).run(suite) +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(MDRaidTestCase) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/storage/devicelibs/swap.py b/tests/storage/devicelibs/swap.py index b1e9bd3b5..b99d1f67c 100644 --- a/tests/storage/devicelibs/swap.py +++ b/tests/storage/devicelibs/swap.py @@ -2,9 +2,9 @@ import baseclass import unittest import storage.devicelibs.swap as swap -class TestSwap(baseclass.TestDevicelibs): +class SwapTestCase(baseclass.DevicelibsTestCase): - def runTest(self): + def testSwap(self): ## ## mkswap ## @@ -58,6 +58,9 @@ class TestSwap(baseclass.TestDevicelibs): self.assertRaises(swap.SwapError, swap.swapoff, self._LOOP_DEV0) +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(SwapTestCase) + + if __name__ == "__main__": - suite = unittest.TestLoader().loadTestsFromTestCase(TestSwap) - unittest.TextTestRunner(verbosity=2).run(suite) + unittest.main() -- cgit