virtinst: Unify test code paths when run as root

CI runs the code as root by default, fix up some of the differences

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-07-17 18:27:14 -04:00
parent aa89a48371
commit 2170efc9a5
5 changed files with 24 additions and 17 deletions

View File

@ -195,7 +195,6 @@ class TestXMLMisc(unittest.TestCase):
g._metadata.libosinfo.os_id = "http://example.com/idontexit" # pylint: disable=protected-access g._metadata.libosinfo.os_id = "http://example.com/idontexit" # pylint: disable=protected-access
self.assertEqual(g.osinfo.name, "generic") self.assertEqual(g.osinfo.name, "generic")
@utils.run_without_testsuite_hacks
def test_dir_searchable(self): def test_dir_searchable(self):
# Normally the dir searchable test is skipped in the unittest, # Normally the dir searchable test is skipped in the unittest,
# but let's contrive an example that should trigger all the code # but let's contrive an example that should trigger all the code

View File

@ -128,8 +128,8 @@ class DeviceDisk(Device):
user, uid = conn.caps.host.get_qemu_baselabel() user, uid = conn.caps.host.get_qemu_baselabel()
if not user: if not user:
return searchdata return searchdata
if uid == 0: if uid == 0 and not xmlutil.in_testsuite():
return searchdata return searchdata # pragma: no cover
searchdata.user = user searchdata.user = user
searchdata.uid = uid searchdata.uid = uid

View File

@ -50,12 +50,12 @@ def _lookup_vol_by_basename(pool, path):
return pool.storageVolLookupByName(name) return pool.storageVolLookupByName(name)
def _get_block_size(path): def _get_block_size(path): # pragma: no cover
try: try:
fd = os.open(path, os.O_RDONLY) fd = os.open(path, os.O_RDONLY)
# os.SEEK_END is not present on all systems # os.SEEK_END is not present on all systems
size = os.lseek(fd, 0, 2) # pragma: no cover size = os.lseek(fd, 0, 2)
os.close(fd) # pragma: no cover os.close(fd)
except Exception: except Exception:
size = 0 size = 0
return size return size
@ -65,7 +65,7 @@ def _get_size(path):
if not os.path.exists(path): if not os.path.exists(path):
return 0 return 0
if _stat_is_block(path): if _stat_is_block(path):
return _get_block_size(path) return _get_block_size(path) # pragma: no cover
return os.path.getsize(path) return os.path.getsize(path)
@ -214,7 +214,7 @@ def _get_dev_type(path, vol_xml, vol_object, pool_xml, remote):
if os.path.isdir(path): if os.path.isdir(path):
return "dir" return "dir"
elif _stat_is_block(path): elif _stat_is_block(path):
return "block" return "block" # pragma: no cover
return "file" return "file"

View File

@ -7,6 +7,7 @@
import os import os
from .. import progress from .. import progress
from .. import xmlutil
from ..devices import DeviceDisk from ..devices import DeviceDisk
from ..logger import log from ..logger import log
from ..storage import StoragePool, StorageVolume from ..storage import StoragePool, StorageVolume
@ -136,7 +137,8 @@ def upload_kernel_initrd(conn, scratchdir, system_scratchdir,
tmpvols = [] tmpvols = []
if (not conn.is_remote() and if (not conn.is_remote() and
(conn.is_unprivileged() or scratchdir == system_scratchdir)): (conn.is_unprivileged() or scratchdir == system_scratchdir) and
not xmlutil.in_testsuite()):
# We have access to system scratchdir, don't jump through hoops # We have access to system scratchdir, don't jump through hoops
log.debug("Have access to preferred scratchdir so" log.debug("Have access to preferred scratchdir so"
" nothing to upload") # pragma: no cover " nothing to upload") # pragma: no cover

View File

@ -20,6 +20,18 @@ from .. import progress
from ..logger import log from ..logger import log
def _is_user_login_safe(login):
return login != "root"
def _login_from_hostuser():
hostuser = getpass.getuser()
realname = pwd.getpwnam(hostuser).pw_gecos
if not _is_user_login_safe(hostuser):
return None, None # pragma: no cover
return hostuser, realname # pragma: no cover
def _make_installconfig(script, osobj, unattended_data, arch, hostname, url): def _make_installconfig(script, osobj, unattended_data, arch, hostname, url):
""" """
Build a Libosinfo.InstallConfig instance Build a Libosinfo.InstallConfig instance
@ -34,9 +46,6 @@ def _make_installconfig(script, osobj, unattended_data, arch, hostname, url):
def get_language(): def get_language():
return locale.getlocale()[0] return locale.getlocale()[0]
def is_user_login_safe(login):
return login != "root"
config = Libosinfo.InstallConfig() config = Libosinfo.InstallConfig()
# Set user login and name # Set user login and name
@ -45,14 +54,11 @@ def _make_installconfig(script, osobj, unattended_data, arch, hostname, url):
login = unattended_data.user_login login = unattended_data.user_login
realname = unattended_data.user_login realname = unattended_data.user_login
if not login: if not login:
hostuser = getpass.getuser() login, realname = _login_from_hostuser()
if is_user_login_safe(hostuser):
login = getpass.getuser()
realname = pwd.getpwnam(login).pw_gecos
if login: if login:
login = login.lower() login = login.lower()
if not is_user_login_safe(login): if not _is_user_login_safe(login):
raise RuntimeError( raise RuntimeError(
_("%(osname)s cannot use '%(loginname)s' as user-login.") % _("%(osname)s cannot use '%(loginname)s' as user-login.") %
{"osname": osobj.name, "loginname": login}) {"osname": osobj.name, "loginname": login})