tests: More work to make dir search test host independent

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-01-30 16:17:42 -05:00
parent 8e7745f5e1
commit 9ddf2799a8
3 changed files with 33 additions and 11 deletions

View File

@ -199,22 +199,44 @@ class TestXMLMisc(unittest.TestCase):
# to ensure it isn't horribly broken # to ensure it isn't horribly broken
conn = utils.URIs.open_kvm() conn = utils.URIs.open_kvm()
with tempfile.TemporaryDirectory() as tmpdir: def _set_caps_baselabel_uid(uid):
secmodel = [s for s in conn.caps.host.secmodels
if s.model == "dac"][0]
for baselabel in [b for b in secmodel.baselabels
if b.type in ["qemu", "kvm"]]:
baselabel.content = "+%s:+%s" % (uid, uid)
tmpobj = tempfile.TemporaryDirectory(prefix="virtinst-test-search")
tmpdir = tmpobj.name
try:
# Invalid uid
_set_caps_baselabel_uid(-1)
searchdata = virtinst.DeviceDisk.check_path_search(conn, tmpdir) searchdata = virtinst.DeviceDisk.check_path_search(conn, tmpdir)
self.assertTrue(bool(searchdata.fixlist)) self.assertEqual(searchdata.uid, None)
# Use our uid, verify it shows we have expected access
_set_caps_baselabel_uid(os.getuid())
searchdata = virtinst.DeviceDisk.check_path_search(conn, tmpdir)
self.assertEqual(searchdata.uid, os.getuid())
self.assertEqual(searchdata.fixlist, [])
# Remove perms on the tmpdir, now it should report failures
os.chmod(tmpdir, 0o000)
searchdata = virtinst.DeviceDisk.check_path_search(conn, tmpdir)
self.assertEqual(searchdata.fixlist, [tmpdir])
errdict = virtinst.DeviceDisk.fix_path_search(searchdata) errdict = virtinst.DeviceDisk.fix_path_search(searchdata)
self.assertTrue(not bool(errdict)) self.assertTrue(not bool(errdict))
# Mock setfacl to definitely, as getfacl won't accept args # Mock setfacl to definitely fail
with unittest.mock.patch("virtinst.diskbackend.SETFACL", with unittest.mock.patch("virtinst.diskbackend.SETFACL",
"getfacl"): "getfacl"):
errdict = virtinst.DeviceDisk.fix_path_search(searchdata) errdict = virtinst.DeviceDisk.fix_path_search(searchdata)
# Test uid check short circuiting finally:
searchdata.uid = os.getuid() # Reset changes we made
os.chown(tmpdir, os.getuid(), os.getgid()) conn.invalidate_caps()
assert virtinst.diskbackend.is_path_searchable( os.chmod(tmpdir, 0o777)
tmpdir, os.getuid(), "foo") == []
def test_path_in_use(self): def test_path_in_use(self):
# Extra tests for DeviceDisk.path_in_use # Extra tests for DeviceDisk.path_in_use

View File

@ -87,10 +87,10 @@ class _CapsHost(XMLBuilder):
uid = int(label.split(":")[0].replace("+", "")) uid = int(label.split(":")[0].replace("+", ""))
user = pwd.getpwuid(uid)[0] user = pwd.getpwuid(uid)[0]
return user, uid return user, uid
except Exception: # pragma: no cover except Exception:
log.debug("Exception parsing qemu dac baselabel=%s", log.debug("Exception parsing qemu dac baselabel=%s",
label, exc_info=True) label, exc_info=True)
return None, None # pragma: no cover return None, None
################################ ################################

View File

@ -128,7 +128,7 @@ 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 # pragma: no cover return searchdata
if uid == 0: if uid == 0:
return searchdata return searchdata