neil / rpms / python-blivet

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