neil / rpms / udisks2

Forked from rpms/udisks2 a year ago
Clone
7b1f2e
From dd6ef8393a8f27fefad66cce136e52f13350f0df Mon Sep 17 00:00:00 2001
7b1f2e
From: Tomas Bzatek <tbzatek@redhat.com>
7b1f2e
Date: Fri, 1 Oct 2021 18:23:10 +0200
7b1f2e
Subject: [PATCH] tests: Add LVM2 teardown object existence checks
7b1f2e
7b1f2e
This adds a check for the created stack objects presence after the teardown.
7b1f2e
7b1f2e
Due to the nature of the global lvm2 module update some objects may still
7b1f2e
hang around after the org.freedesktop.UDisks2.VolumeGroup.Delete(options='tear-down')
7b1f2e
method call has returned. Until this is properly fixed an explicit timeout
7b1f2e
had to be added.
7b1f2e
---
7b1f2e
 src/tests/dbus-tests/test_20_LVM.py | 80 +++++++++++++++++------------
7b1f2e
 1 file changed, 46 insertions(+), 34 deletions(-)
7b1f2e
7b1f2e
diff --git a/src/tests/dbus-tests/test_20_LVM.py b/src/tests/dbus-tests/test_20_LVM.py
7b1f2e
index 56915e580..7fbff0434 100644
7b1f2e
--- a/src/tests/dbus-tests/test_20_LVM.py
7b1f2e
+++ b/src/tests/dbus-tests/test_20_LVM.py
7b1f2e
@@ -3,6 +3,7 @@
7b1f2e
 import re
7b1f2e
 import time
7b1f2e
 import unittest
7b1f2e
+import six
7b1f2e
 
7b1f2e
 from distutils.version import LooseVersion
7b1f2e
 
7b1f2e
@@ -637,7 +638,8 @@ def _remove_luks(self, device, name, close=True):
7b1f2e
                 device.Lock(self.no_options, dbus_interface=self.iface_prefix + '.Encrypted')
7b1f2e
             except dbus.exceptions.DBusException as e:
7b1f2e
                 # ignore when luks is actually already locked
7b1f2e
-                if not str(e).endswith('is not unlocked') and not 'No such interface' in str(e):
7b1f2e
+                if not str(e).endswith('is not unlocked') and not 'No such interface' in str(e) and \
7b1f2e
+                   not 'Object does not exist at path' in str(e):
7b1f2e
                     raise e
7b1f2e
 
7b1f2e
         try:
7b1f2e
@@ -645,7 +647,7 @@ def _remove_luks(self, device, name, close=True):
7b1f2e
             d['erase'] = True
7b1f2e
             device.Format('empty', d, dbus_interface=self.iface_prefix + '.Block')
7b1f2e
         except dbus.exceptions.DBusException as e:
7b1f2e
-            if not 'No such interface' in str(e):
7b1f2e
+            if not 'No such interface' in str(e) and not 'Object does not exist at path' in str(e):
7b1f2e
                 raise e
7b1f2e
 
7b1f2e
     def _init_stack(self, name):
7b1f2e
@@ -663,18 +665,19 @@ def _init_stack(self, name):
7b1f2e
         self.assertIsNotNone(self.pv)
7b1f2e
 
7b1f2e
         self.vg = self._create_vg(vgname, dbus.Array([self.pv]))
7b1f2e
+        self.vg_path = self.vg.object_path
7b1f2e
         self.addCleanup(self._remove_vg, self.vg, tear_down=True, ignore_removed=True)
7b1f2e
 
7b1f2e
         # create an LV on it
7b1f2e
-        lv_path = self.vg.CreatePlainVolume(lvname, dbus.UInt64(200 * 1024**2), self.no_options,
7b1f2e
-                                            dbus_interface=self.iface_prefix + '.VolumeGroup')
7b1f2e
-        self.lv = self.bus.get_object(self.iface_prefix, lv_path)
7b1f2e
+        self.lv_path = self.vg.CreatePlainVolume(lvname, dbus.UInt64(200 * 1024**2), self.no_options,
7b1f2e
+                                                 dbus_interface=self.iface_prefix + '.VolumeGroup')
7b1f2e
+        self.lv = self.bus.get_object(self.iface_prefix, self.lv_path)
7b1f2e
         self.assertIsNotNone(self.lv)
7b1f2e
 
7b1f2e
-        lv_block_path = self.lv.Activate(self.no_options, dbus_interface=self.iface_prefix + '.LogicalVolume')
7b1f2e
-        self.assertIsNotNone(lv_block_path)
7b1f2e
+        self.lv_block_path = self.lv.Activate(self.no_options, dbus_interface=self.iface_prefix + '.LogicalVolume')
7b1f2e
+        self.assertIsNotNone(self.lv_block_path)
7b1f2e
 
7b1f2e
-        self.lv_block = self.get_object(lv_block_path)
7b1f2e
+        self.lv_block = self.get_object(self.lv_block_path)
7b1f2e
         self.assertIsNotNone(self.lv_block)
7b1f2e
 
7b1f2e
         # create LUKS on the LV
7b1f2e
@@ -702,10 +705,10 @@ def _init_stack(self, name):
7b1f2e
         self.addCleanup(self._remove_luks, self.lv_block, vgname)
7b1f2e
         self.luks_uuid = self.get_property_raw(self.lv_block, '.Block', 'IdUUID')
7b1f2e
 
7b1f2e
-        luks_block_path = self.get_property(self.lv_block, '.Encrypted', 'CleartextDevice')
7b1f2e
-        self.luks_block = self.get_object(luks_block_path.value)
7b1f2e
-        self.assertIsNotNone(self.luks_block)
7b1f2e
-        self.fs_uuid = self.get_property_raw(self.luks_block, '.Block', 'IdUUID')
7b1f2e
+        self.luks_block_path = self.get_property_raw(self.lv_block, '.Encrypted', 'CleartextDevice')
7b1f2e
+        luks_block = self.get_object(self.luks_block_path)
7b1f2e
+        self.assertIsNotNone(luks_block)
7b1f2e
+        self.fs_uuid = self.get_property_raw(luks_block, '.Block', 'IdUUID')
7b1f2e
 
7b1f2e
         # check for present crypttab configuration item
7b1f2e
         conf = self.get_property(self.lv_block, '.Block', 'Configuration')
7b1f2e
@@ -713,7 +716,7 @@ def _init_stack(self, name):
7b1f2e
         self.assertEqual(conf.value[0][0], 'crypttab')
7b1f2e
 
7b1f2e
         # check for present fstab configuration item on a cleartext block device
7b1f2e
-        conf = self.get_property(self.luks_block, '.Block', 'Configuration')
7b1f2e
+        conf = self.get_property(luks_block, '.Block', 'Configuration')
7b1f2e
         conf.assertTrue()
7b1f2e
         self.assertEqual(conf.value[0][0], 'fstab')
7b1f2e
 
7b1f2e
@@ -730,6 +733,32 @@ def _init_stack(self, name):
7b1f2e
         self.assertIn(vgname, fstab)
7b1f2e
         self.assertIn(self.fs_uuid, fstab)
7b1f2e
 
7b1f2e
+    def _check_torn_down_stack(self, name):
7b1f2e
+        # check that all created objects don't exist anymore
7b1f2e
+        msg = r'Object does not exist at path|No such interface'
7b1f2e
+        with six.assertRaisesRegex(self, dbus.exceptions.DBusException, msg):
7b1f2e
+            luks_block = self.get_object(self.luks_block_path)
7b1f2e
+            self.get_property_raw(luks_block, '.Block', 'DeviceNumber')
7b1f2e
+        with six.assertRaisesRegex(self, dbus.exceptions.DBusException, msg):
7b1f2e
+            lv_block = self.get_object(self.lv_block_path)
7b1f2e
+            self.get_property_raw(lv_block, '.Block', 'DeviceNumber')
7b1f2e
+        with six.assertRaisesRegex(self, dbus.exceptions.DBusException, msg):
7b1f2e
+            # the lvm2 udisks module is not fully synchronous, see https://github.com/storaged-project/udisks/pull/814
7b1f2e
+            time.sleep(2)
7b1f2e
+            lv = self.get_object(self.lv_path)
7b1f2e
+            self.get_property_raw(lv, '.LogicalVolume', 'Name')
7b1f2e
+        with six.assertRaisesRegex(self, dbus.exceptions.DBusException, msg):
7b1f2e
+            vg = self.get_object(self.vg_path)
7b1f2e
+            self.get_property_raw(vg, '.VolumeGroup', 'Name')
7b1f2e
+
7b1f2e
+        # check that fstab and crypttab records have been removed
7b1f2e
+        crypttab = self.read_file('/etc/crypttab')
7b1f2e
+        self.assertNotIn(name, crypttab)
7b1f2e
+        self.assertNotIn(self.luks_uuid, crypttab)
7b1f2e
+        fstab = self.read_file('/etc/fstab')
7b1f2e
+        self.assertNotIn(name, fstab)
7b1f2e
+        self.assertNotIn(self.fs_uuid, fstab)
7b1f2e
+
7b1f2e
 
7b1f2e
     @udiskstestcase.tag_test(udiskstestcase.TestTags.UNSAFE)
7b1f2e
     def test_teardown_active_vg_unlocked(self):
7b1f2e
@@ -741,13 +770,7 @@ def test_teardown_active_vg_unlocked(self):
7b1f2e
 
7b1f2e
         self._remove_vg(self.vg, tear_down=True, ignore_removed=False)
7b1f2e
 
7b1f2e
-        # check that fstab and crypttab records have been removed
7b1f2e
-        crypttab = self.read_file('/etc/crypttab')
7b1f2e
-        self.assertNotIn(name, crypttab)
7b1f2e
-        self.assertNotIn(self.luks_uuid, crypttab)
7b1f2e
-        fstab = self.read_file('/etc/fstab')
7b1f2e
-        self.assertNotIn(name, fstab)
7b1f2e
-        self.assertNotIn(self.fs_uuid, fstab)
7b1f2e
+        self._check_torn_down_stack(name)
7b1f2e
 
7b1f2e
     @udiskstestcase.tag_test(udiskstestcase.TestTags.UNSAFE)
7b1f2e
     def test_teardown_active_vg_locked(self):
7b1f2e
@@ -760,13 +783,7 @@ def test_teardown_active_vg_locked(self):
7b1f2e
         self.lv_block.Lock(self.no_options, dbus_interface=self.iface_prefix + '.Encrypted')
7b1f2e
         self._remove_vg(self.vg, tear_down=True, ignore_removed=False)
7b1f2e
 
7b1f2e
-        # check that fstab and crypttab records have been removed
7b1f2e
-        crypttab = self.read_file('/etc/crypttab')
7b1f2e
-        self.assertNotIn(name, crypttab)
7b1f2e
-        self.assertNotIn(self.luks_uuid, crypttab)
7b1f2e
-        fstab = self.read_file('/etc/fstab')
7b1f2e
-        self.assertNotIn(name, fstab)
7b1f2e
-        self.assertNotIn(self.fs_uuid, fstab)
7b1f2e
+        self._check_torn_down_stack(name)
7b1f2e
 
7b1f2e
     @udiskstestcase.tag_test(udiskstestcase.TestTags.UNSAFE)
7b1f2e
     def test_teardown_inactive_vg_locked(self):
7b1f2e
@@ -780,13 +797,7 @@ def test_teardown_inactive_vg_locked(self):
7b1f2e
         self.lv.Deactivate(self.no_options, dbus_interface=self.iface_prefix + '.LogicalVolume')
7b1f2e
         self._remove_vg(self.vg, tear_down=True, ignore_removed=False)
7b1f2e
 
7b1f2e
-        # check that fstab and crypttab records have been removed
7b1f2e
-        crypttab = self.read_file('/etc/crypttab')
7b1f2e
-        self.assertNotIn(name, crypttab)
7b1f2e
-        self.assertNotIn(self.luks_uuid, crypttab)
7b1f2e
-        fstab = self.read_file('/etc/fstab')
7b1f2e
-        self.assertNotIn(name, fstab)
7b1f2e
-        self.assertNotIn(self.fs_uuid, fstab)
7b1f2e
+        self._check_torn_down_stack(name)
7b1f2e
 
7b1f2e
     @udiskstestcase.tag_test(udiskstestcase.TestTags.UNSAFE)
7b1f2e
     def test_reformat_inactive_vg_locked(self):
7b1f2e
@@ -812,6 +823,7 @@ def test_reformat_inactive_vg_locked(self):
7b1f2e
 
7b1f2e
         # check that fstab and crypttab records have been removed
7b1f2e
         # TODO: these checks are the opposite - record shouldn't be present, once this is fixed
7b1f2e
+        # self._check_torn_down_stack(name)
7b1f2e
         crypttab = self.read_file('/etc/crypttab')
7b1f2e
         self.assertIn(name, crypttab)
7b1f2e
         self.assertIn(self.luks_uuid, crypttab)