未验证 提交 104f19ad 编写于 作者: C Caio Carrara

Merge remote-tracking branch 'clebergnu/partition_selftests'

Signed-off-by: NCaio Carrara <ccarrara@redhat.com>
...@@ -6,6 +6,7 @@ avocado.utils.partition unittests ...@@ -6,6 +6,7 @@ avocado.utils.partition unittests
import os import os
import shutil import shutil
import sys
import tempfile import tempfile
import unittest # pylint: disable=C0411 import unittest # pylint: disable=C0411
try: try:
...@@ -15,6 +16,7 @@ except ImportError: ...@@ -15,6 +16,7 @@ except ImportError:
from avocado.utils import partition, process from avocado.utils import partition, process
from avocado.utils import path as utils_path from avocado.utils import path as utils_path
from avocado.utils import wait
def missing_binary(binary): def missing_binary(binary):
...@@ -59,12 +61,12 @@ class TestPartition(Base): ...@@ -59,12 +61,12 @@ class TestPartition(Base):
self.disk.mount() self.disk.mount()
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
self.assertEqual(self.mountpoint, self.disk.get_mountpoint()) self.assertEqual(self.mountpoint, self.disk.get_mountpoint())
self.disk.unmount() self.disk.unmount()
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertNotIn(self.mountpoint, proc_mounts) self.assertNotIn(self.mountpoint, proc_mounts)
class TestPartitionMkfsMount(Base): class TestPartitionMkfsMount(Base):
...@@ -77,6 +79,18 @@ class TestPartitionMkfsMount(Base): ...@@ -77,6 +79,18 @@ class TestPartitionMkfsMount(Base):
super(TestPartitionMkfsMount, self).setUp() super(TestPartitionMkfsMount, self).setUp()
self.disk.mkfs() self.disk.mkfs()
self.disk.mount() self.disk.mount()
self.use_mnt_file = os.path.join(self.mountpoint, 'file')
self.use_mnt_cmd = ("%s -c 'import time; f = open(\"%s\", \"w\"); "
"time.sleep(60)'" % (sys.executable,
self.use_mnt_file))
def run_process_to_use_mnt(self):
proc = process.SubProcess(self.use_mnt_cmd)
proc.start()
self.assertTrue(wait.wait_for(lambda: os.path.exists(self.use_mnt_file),
timeout=1, first=0.1, step=0.1),
"File was not created within mountpoint")
return proc
@unittest.skipIf(missing_binary('lsof'), "requires running lsof") @unittest.skipIf(missing_binary('lsof'), "requires running lsof")
@unittest.skipIf(not process.can_sudo('kill -l'), @unittest.skipIf(not process.can_sudo('kill -l'),
...@@ -85,73 +99,71 @@ class TestPartitionMkfsMount(Base): ...@@ -85,73 +99,71 @@ class TestPartitionMkfsMount(Base):
""" Test force-unmount feature """ """ Test force-unmount feature """
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
proc = process.SubProcess("cd %s; while :; do echo a > a; rm a; done" proc = self.run_process_to_use_mnt()
% self.mountpoint, shell=True) self.assertTrue(self.disk.unmount())
proc.start() self.assertEqual(proc.poll(), -9) # Process should be killed -9
self.assertTrue(self.disk.unmount())
self.assertEqual(proc.poll(), -9) # Process should be killed -9
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertNotIn(self.mountpoint, proc_mounts) self.assertNotIn(self.mountpoint, proc_mounts)
@unittest.skipUnless(missing_binary('lsof'), "requires not having lsof") @unittest.skipUnless(missing_binary('lsof'), "requires not having lsof")
def test_force_unmount_no_lsof(self): def test_force_unmount_no_lsof(self):
""" Checks that a force-unmount will fail on systems without lsof """ """ Checks that a force-unmount will fail on systems without lsof """
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
proc = process.SubProcess("cd %s; while :; do echo a > a; rm a; done" proc = self.run_process_to_use_mnt()
% self.mountpoint, shell=True) self.assertRaises(partition.PartitionError, self.disk.unmount)
proc.start() proc.terminate()
self.assertRaises(partition.PartitionError, self.disk.unmount) proc.wait()
proc.terminate()
proc.wait()
def test_force_unmount_get_pids_fail(self): def test_force_unmount_get_pids_fail(self):
""" Checks PartitionError is raised if there's no lsof to get pids """ """ Checks PartitionError is raised if there's no lsof to get pids """
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
proc = process.SubProcess("cd %s; while :; do echo a > a; rm a; done" proc = self.run_process_to_use_mnt()
% self.mountpoint, shell=True) with mock.patch('avocado.utils.partition.process.run',
proc.start() side_effect=process.CmdError):
with mock.patch('avocado.utils.partition.process.run', with mock.patch('avocado.utils.partition.process.system_output',
side_effect=process.CmdError): side_effect=OSError) as mocked_system_output:
with mock.patch('avocado.utils.partition.process.system_output', self.assertRaises(partition.PartitionError, self.disk.unmount)
side_effect=OSError) as mocked_system_output: mocked_system_output.assert_called_with('lsof ' + self.mountpoint)
self.assertRaises(partition.PartitionError, self.disk.unmount) # TODO: process.terminate() should be enough, but currently isn't.
mocked_system_output.assert_called_with('lsof ' + self.mountpoint) # debug the root cause of why the process fails to terminate and the
proc.terminate() # test hangs on wait()
proc.wait() self.disk.unmount()
proc.terminate()
proc.wait()
def test_double_mount(self): def test_double_mount(self):
""" Check the attempt for second mount fails """ """ Check the attempt for second mount fails """
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
self.assertRaises(partition.PartitionError, self.disk.mount) self.assertRaises(partition.PartitionError, self.disk.mount)
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
def test_double_umount(self): def test_double_umount(self):
""" Check double unmount works well """ """ Check double unmount works well """
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
self.disk.unmount() self.disk.unmount()
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertNotIn(self.mountpoint, proc_mounts) self.assertNotIn(self.mountpoint, proc_mounts)
self.disk.unmount() self.disk.unmount()
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertNotIn(self.mountpoint, proc_mounts) self.assertNotIn(self.mountpoint, proc_mounts)
def test_format_mounted(self): def test_format_mounted(self):
""" Check format on mounted device fails """ """ Check format on mounted device fails """
with open("/proc/mounts") as proc_mounts_file: with open("/proc/mounts") as proc_mounts_file:
proc_mounts = proc_mounts_file.read() proc_mounts = proc_mounts_file.read()
self.assertIn(self.mountpoint, proc_mounts) self.assertIn(self.mountpoint, proc_mounts)
self.assertRaises(partition.PartitionError, self.disk.mkfs) self.assertRaises(partition.PartitionError, self.disk.mkfs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册