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