提交 952ecd24 编写于 作者: C Cleber Rosa

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: NCleber Rosa <crosa@redhat.com>
上级 7edea205
...@@ -9,6 +9,7 @@ import os ...@@ -9,6 +9,7 @@ import os
import sys import sys
import shutil import shutil
import tempfile import tempfile
import time
import unittest import unittest
from six.moves import xrange as range from six.moves import xrange as range
...@@ -39,42 +40,6 @@ class LVUtilsTest(unittest.TestCase): ...@@ -39,42 +40,6 @@ class LVUtilsTest(unittest.TestCase):
for vg_name in self.vgs: for vg_name in self.vgs:
lv_utils.vg_remove(vg_name) 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'), @unittest.skipIf(sys.platform.startswith('darwin'),
'macOS does not support LVM') 'macOS does not support LVM')
@unittest.skipIf(process.system("vgs --all | grep -q avocado_testing_vg_" @unittest.skipIf(process.system("vgs --all | grep -q avocado_testing_vg_"
...@@ -141,7 +106,39 @@ class LVUtilsTest(unittest.TestCase): ...@@ -141,7 +106,39 @@ class LVUtilsTest(unittest.TestCase):
vg_name, loop_device) vg_name, loop_device)
except BaseException as details: except BaseException as details:
print("Fail to cleanup vg_ramdisk: %s" % 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__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册