python-blivet/SOURCES/0024-Fix-util.detect_virt-function.patch
2021-09-10 02:35:30 +00:00

92 lines
3.7 KiB
Diff

From 471d43cbfe99db1c8246fb863e3ce49b3403fc61 Mon Sep 17 00:00:00 2001
From: Vojtech Trefny <vtrefny@redhat.com>
Date: Wed, 11 Sep 2019 10:48:19 +0200
Subject: [PATCH] Fix util.detect_virt function
Fixed the systemd Manager object path, also get_property_sync
returns a tuple so we need to check its first element.
Resolves: rhbz#1676935
---
blivet/util.py | 8 ++++----
tests/formats_test/disklabel_test.py | 26 ++++++++++++++------------
tests/util_test.py | 4 ++++
3 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/blivet/util.py b/blivet/util.py
index 2932e8b5..27468992 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -40,7 +40,7 @@ program_log_lock = Lock()
SYSTEMD_SERVICE = "org.freedesktop.systemd1"
-SYSTEMD_MANAGER_PATH = "/org/freedesktop/systemd1/Manager"
+SYSTEMD_MANAGER_PATH = "/org/freedesktop/systemd1"
SYSTEMD_MANAGER_IFACE = "org.freedesktop.systemd1.Manager"
VIRT_PROP_NAME = "Virtualization"
@@ -1115,6 +1115,6 @@ def detect_virt():
vm = safe_dbus.get_property_sync(SYSTEMD_SERVICE, SYSTEMD_MANAGER_PATH,
SYSTEMD_MANAGER_IFACE, VIRT_PROP_NAME)
except (safe_dbus.DBusCallError, safe_dbus.DBusPropertyError):
- vm = None
-
- return vm in ('qemu', 'kvm')
+ return False
+ else:
+ return vm[0] in ('qemu', 'kvm')
diff --git a/tests/formats_test/disklabel_test.py b/tests/formats_test/disklabel_test.py
index 4b105da6..94f3775f 100644
--- a/tests/formats_test/disklabel_test.py
+++ b/tests/formats_test/disklabel_test.py
@@ -163,16 +163,18 @@ class DiskLabelTestCase(unittest.TestCase):
arch.is_efi.return_value = False
arch.is_s390.return_value = True
- with mock.patch.object(dl, '_label_type_size_check') as size_check:
- size_check.return_value = True
- with mock.patch("blivet.formats.disklabel.blockdev.s390") as _s390:
- _s390.dasd_is_fba.return_value = False
- self.assertEqual(dl._get_best_label_type(), "msdos")
-
- _s390.dasd_is_fba.return_value = True
- self.assertEqual(dl._get_best_label_type(), "msdos")
-
- _s390.dasd_is_fba.return_value = False
- dl._parted_device.type = parted.DEVICE_DASD
- self.assertEqual(dl._get_best_label_type(), "dasd")
+ with mock.patch('blivet.util.detect_virt') as virt:
+ virt.return_value = False
+ with mock.patch.object(dl, '_label_type_size_check') as size_check:
+ size_check.return_value = True
+ with mock.patch("blivet.formats.disklabel.blockdev.s390") as _s390:
+ _s390.dasd_is_fba.return_value = False
+ self.assertEqual(dl._get_best_label_type(), "msdos")
+
+ _s390.dasd_is_fba.return_value = True
+ self.assertEqual(dl._get_best_label_type(), "msdos")
+
+ _s390.dasd_is_fba.return_value = False
+ dl._parted_device.type = parted.DEVICE_DASD
+ self.assertEqual(dl._get_best_label_type(), "dasd")
arch.is_s390.return_value = False
diff --git a/tests/util_test.py b/tests/util_test.py
index 5fa3070e..9a2ff492 100644
--- a/tests/util_test.py
+++ b/tests/util_test.py
@@ -37,6 +37,10 @@ class MiscTest(unittest.TestCase):
# real deduplication
self.assertEqual([1, 2, 3, 4, 5, 6], util.dedup_list([1, 2, 3, 4, 2, 2, 2, 1, 3, 5, 3, 6, 6, 2, 3, 1, 5]))
+ def test_detect_virt(self):
+ in_virt = not util.run_program(["systemd-detect-virt", "--vm"])
+ self.assertEqual(util.detect_virt(), in_virt)
+
class TestDefaultNamedtuple(unittest.TestCase):
def test_default_namedtuple(self):
--
2.20.1