未验证 提交 0a62d937 编写于 作者: C Cleber Rosa

Merge remote-tracking branch 'ldoktor/test_utils_partition4'

......@@ -99,12 +99,20 @@ class CmdError(Exception):
return "CmdError"
def can_sudo():
def can_sudo(cmd=None):
"""
:return: True when sudo is available (or is root)
Check whether sudo is available (or running as root)
"""
if os.getuid() == 0:
if os.getuid() == 0: # Root
return True
try: # Does sudo binary exists?
path.find_command('sudo')
except path.CmdNotFoundError:
return False
if cmd: # Am I able to run the cmd or plain sudo id?
return not system(cmd, ignore_status=True, sudo=True)
elif system_output("id -u", ignore_status=True, sudo=True).strip() == "0":
return True
else:
......
......@@ -26,6 +26,8 @@ class LVUtilsTest(unittest.TestCase):
@unittest.skipIf(process.system("which vgs", ignore_status=True),
"LVM utils not installed (command vgs is missing)")
@unittest.skipIf(not process.can_sudo(), "This test requires root or "
"passwordless sudo configured.")
def setUp(self):
try:
process.system("/bin/true", sudo=True)
......
......@@ -43,8 +43,8 @@ class BaseIso9660(unittest.TestCase):
self.iso.close()
self.iso.close() # check that double-close won't fail
@unittest.skipIf(not process.can_sudo(),
"This test requires sudo or root")
@unittest.skipIf(not process.can_sudo("mount"),
"This test requires mount to run under sudo or root")
def mnt_dir_workflow(self):
"""
Check the mnt_dir functionality
......@@ -116,7 +116,7 @@ class IsoMount(BaseIso9660):
Mount-based check
"""
@unittest.skipIf(not process.can_sudo(),
@unittest.skipIf(not process.can_sudo("mount"),
"This test requires sudo or root")
def setUp(self):
super(IsoMount, self).setUp()
......
......@@ -29,28 +29,17 @@ def missing_binary(binary):
return True
def cannot_sudo(command):
try:
process.run(command, sudo=True)
False
except (process.CmdError, OSError):
return True
class TestPartition(unittest.TestCase):
"""
Unit tests for avocado.utils.partition
"""
@unittest.skipIf(missing_binary('mkfs.ext2'),
"mkfs.ext2 is required for these tests to run.")
@unittest.skipIf(missing_binary('sudo'),
"sudo is required for these tests to run.")
@unittest.skipIf(cannot_sudo('mount'),
@unittest.skipIf(not process.can_sudo('mount'),
'current user must be allowed to run "mount" under sudo')
@unittest.skipIf(cannot_sudo('mkfs.ext2 -V'),
'current user must be allowed to run "mkfs.ext2" under sudo')
@unittest.skipIf(not process.can_sudo('mkfs.ext2 -V'),
'current user must be allowed to run "mkfs.ext2" under '
'sudo')
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="avocado_" + __name__)
self.mountpoint = os.path.join(self.tmpdir, "disk")
......@@ -68,6 +57,8 @@ class TestPartition(unittest.TestCase):
self.disk.unmount()
self.assertNotIn(self.mountpoint, open("/proc/mounts").read())
@unittest.skipIf(not process.can_sudo('kill -l'),
"requires running kill as a privileged user")
def test_force_unmount(self):
""" Test force-unmount feature """
self.disk.mkfs()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册