From 952ecd2494baac97a6a1a130e1021b4f9256f42b Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Thu, 29 Nov 2018 11:00:34 -0500 Subject: [PATCH] selftests/functional/test_lv_utils.py: split test_disk_space into own class Given that the setUp() phase is not useful for test_disk_space, and the custom cleanup done in the test is better had at the tearDown() phase. Additionally, the following changes where made to make it more reliable: * Avoid the busy loop giving modprobe some time between attempts * Use "modprobe -r" instead of rmmod, which handlers module dependencies * Do an explicit check for status * If "modprobe -r" doesn't succeed, it's not a test failure, but a test a test error (given that the get_diskspace() function already ran and succeeded). Ideally, the module removal would be done by the linux_modules.unload_module() but there are some issues with that function that needs addressing first (such as returning a meaningful result). Signed-off-by: Cleber Rosa --- selftests/functional/test_lv_utils.py | 71 +++++++++++++-------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/selftests/functional/test_lv_utils.py b/selftests/functional/test_lv_utils.py index 9e95cabb..b7a5c1a9 100644 --- a/selftests/functional/test_lv_utils.py +++ b/selftests/functional/test_lv_utils.py @@ -9,6 +9,7 @@ import os import sys import shutil import tempfile +import time import unittest from six.moves import xrange as range @@ -39,42 +40,6 @@ class LVUtilsTest(unittest.TestCase): for vg_name in self.vgs: lv_utils.vg_remove(vg_name) - @unittest.skipIf(sys.platform.startswith('darwin'), - 'macOS does not support LVM') - @unittest.skipIf(process.system("modinfo scsi_debug", shell=True, - ignore_status=True), - "Kernel mod 'scsi_debug' not available.") - @unittest.skipIf(linux_modules.module_is_loaded("scsi_debug"), - "Kernel mod 'scsi_debug' is already loaded.") - def test_get_diskspace(self): - """ - Use scsi_debug device to check disk size - """ - pre = glob.glob("/dev/sd*") - try: - process.system("modprobe scsi_debug", sudo=True) - disks = set(glob.glob("/dev/sd*")).difference(pre) - self.assertEqual(len(disks), 1, "pre: %s\npost: %s" - % (disks, glob.glob("/dev/sd*"))) - disk = disks.pop() - self.assertEqual(lv_utils.get_diskspace(disk), "8388608") - except BaseException: - for _ in range(10): - res = process.run("rmmod scsi_debug", ignore_status=True, - sudo=True) - if not res.exit_status: - print("scsi_debug removed") - break - else: - print("Fail to remove scsi_debug: %s" % res) - for _ in range(10): - res = process.run("rmmod scsi_debug", ignore_status=True, - sudo=True) - if not res.exit_status: - break - else: - self.fail("Fail to remove scsi_debug after testing: %s" % res) - @unittest.skipIf(sys.platform.startswith('darwin'), 'macOS does not support LVM') @unittest.skipIf(process.system("vgs --all | grep -q avocado_testing_vg_" @@ -141,7 +106,39 @@ class LVUtilsTest(unittest.TestCase): vg_name, loop_device) except BaseException as details: print("Fail to cleanup vg_ramdisk: %s" % details) - raise + + +class DiskSpace(unittest.TestCase): + + @unittest.skipIf(process.system("modinfo scsi_debug", shell=True, + ignore_status=True), + "Kernel mod 'scsi_debug' not available.") + @unittest.skipIf(linux_modules.module_is_loaded("scsi_debug"), + "Kernel mod 'scsi_debug' is already loaded.") + @unittest.skipIf(sys.platform.startswith('darwin'), + 'macOS does not support scsi_debug module') + @unittest.skipIf(not process.can_sudo(), "This test requires root or " + "passwordless sudo configured.") + def test_get_diskspace(self): + """ + Use scsi_debug device to check disk size + """ + pre = glob.glob("/dev/sd*") + process.system("modprobe scsi_debug", sudo=True) + disks = set(glob.glob("/dev/sd*")).difference(pre) + self.assertEqual(len(disks), 1, "pre: %s\npost: %s" + % (disks, glob.glob("/dev/sd*"))) + disk = disks.pop() + self.assertEqual(lv_utils.get_diskspace(disk), "8388608") + + def tearDown(self): + for _ in range(10): + if process.run("modprobe -r scsi_debug", + ignore_status=True, + sudo=True).exit_status == 0: + return + time.sleep(0.05) + raise RuntimeError("Failed to remove scsi_debug after testing") if __name__ == '__main__': -- GitLab