From cf8d3811b89b90211cac0cbd1e5ceb40ea7b641b Mon Sep 17 00:00:00 2001
From: Vendula Poncova <vponcova@redhat.com>
Date: Mon, 7 Sep 2020 17:09:15 +0200
Subject: [PATCH] Add the DBus method IsDeviceShrinkable (#1875677)

Replace the DBus method IsDeviceResizable with IsDeviceShrinkable and fix its
implementation. A shrinkable device has to be resizable and its minimal size
has to be lower then the current size. This should fix the issue with XFS, that
is resizable, but not shrinkable.

Resolves: rhbz#1875677
---
 .../automatic/resizable_interface.py          |  6 ++--
 .../automatic/resizable_module.py             |  6 ++--
 pyanaconda/ui/gui/spokes/lib/resize.py        | 10 +++----
 .../pyanaconda_tests/module_resizable_test.py | 29 +++++++++++++++----
 4 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/pyanaconda/modules/storage/partitioning/automatic/resizable_interface.py b/pyanaconda/modules/storage/partitioning/automatic/resizable_interface.py
index 760a49ecb..c531a0b42 100644
--- a/pyanaconda/modules/storage/partitioning/automatic/resizable_interface.py
+++ b/pyanaconda/modules/storage/partitioning/automatic/resizable_interface.py
@@ -37,13 +37,13 @@ class ResizableDeviceTreeInterface(DeviceTreeInterface):
         """
         return self.implementation.is_device_partitioned(device_name)
 
-    def IsDeviceResizable(self, device_name: Str) -> Bool:
-        """Is the specified device resizable?
+    def IsDeviceShrinkable(self, device_name: Str) -> Bool:
+        """Is the specified device shrinkable?
 
         :param device_name: a name of the device
         :return: True or False
         """
-        return self.implementation.is_device_resizable(device_name)
+        return self.implementation.is_device_shrinkable(device_name)
 
     def GetDevicePartitions(self, device_name: Str) -> List[Str]:
         """Get partitions of the specified device.
diff --git a/pyanaconda/modules/storage/partitioning/automatic/resizable_module.py b/pyanaconda/modules/storage/partitioning/automatic/resizable_module.py
index 9603dfc1b..12d32e891 100644
--- a/pyanaconda/modules/storage/partitioning/automatic/resizable_module.py
+++ b/pyanaconda/modules/storage/partitioning/automatic/resizable_module.py
@@ -52,14 +52,14 @@ class ResizableDeviceTreeModule(DeviceTreeModule):
         """Is the specified device partitioned?"""
         return device.is_disk and device.partitioned and device.format.supported
 
-    def is_device_resizable(self, device_name):
-        """Is the specified device resizable?
+    def is_device_shrinkable(self, device_name):
+        """Is the specified device shrinkable?
 
         :param device_name: a name of the device
         :return: True or False
         """
         device = self._get_device(device_name)
-        return device.resizable
+        return device.resizable and device.min_size < device.size
 
     def get_device_partitions(self, device_name):
         """Get partitions of the specified device.
diff --git a/pyanaconda/ui/gui/spokes/lib/resize.py b/pyanaconda/ui/gui/spokes/lib/resize.py
index 4695e5332..ee165ada7 100644
--- a/pyanaconda/ui/gui/spokes/lib/resize.py
+++ b/pyanaconda/ui/gui/spokes/lib/resize.py
@@ -228,13 +228,13 @@ class ResizeDialog(GUIObject):
 
         # Calculate the free size.
         # Devices that are not resizable are still deletable.
-        is_resizable = self._device_tree.IsDeviceResizable(device_name)
+        is_shrinkable = self._device_tree.IsDeviceShrinkable(device_name)
         size_limits = self._device_tree.GetDeviceSizeLimits(device_name)
 
         min_size = Size(size_limits[0])
         device_size = Size(device_data.size)
 
-        if is_resizable:
+        if is_shrinkable:
             free_size = device_size - min_size
             resize_string = _("%(freeSize)s of %(devSize)s") % {
                 "freeSize": free_size.human_readable(max_places=1),
@@ -394,10 +394,10 @@ class ResizeDialog(GUIObject):
 
         # If the selected filesystem does not support shrinking, make that
         # button insensitive.
-        is_resizable = self._device_tree.IsDeviceResizable(device_name)
-        self._shrink_button.set_sensitive(is_resizable)
+        is_shrinkable = self._device_tree.IsDeviceShrinkable(device_name)
+        self._shrink_button.set_sensitive(is_shrinkable)
 
-        if is_resizable:
+        if is_shrinkable:
             min_size = self._device_tree.GetDeviceSizeLimits(device_name)[0]
             self._setup_slider(min_size, device_data.size, Size(obj.target))
 
diff --git a/tests/nosetests/pyanaconda_tests/module_resizable_test.py b/tests/nosetests/pyanaconda_tests/module_resizable_test.py
index 3c60e166b..42880b4ca 100644
--- a/tests/nosetests/pyanaconda_tests/module_resizable_test.py
+++ b/tests/nosetests/pyanaconda_tests/module_resizable_test.py
@@ -18,9 +18,11 @@
 # Red Hat Author(s): Vendula Poncova <vponcova@redhat.com>
 #
 import unittest
+from unittest.mock import patch
 
 from blivet.devices import StorageDevice, DiskDevice, PartitionDevice
 from blivet.formats import get_format
+from blivet.formats.fs import FS
 from blivet.size import Size
 
 from pyanaconda.modules.storage.partitioning.automatic.resizable_interface import \
@@ -66,13 +68,28 @@ class ResizableDeviceTreeTestCase(unittest.TestCase):
         self.assertEqual(self.interface.IsDevicePartitioned("dev1"), False)
         self.assertEqual(self.interface.IsDevicePartitioned("dev2"), True)
 
-    def is_device_resizable_test(self):
-        """Test IsDeviceResizable."""
+    @patch.object(FS, "update_size_info")
+    def is_device_shrinkable_test(self, update_size_info):
+        """Test IsDeviceShrinkable."""
         self.module.on_storage_changed(create_storage())
-        self._add_device(StorageDevice(
-            "dev1"
-        ))
-        self.assertEqual(self.interface.IsDeviceResizable("dev1"), False)
+
+        dev1 = StorageDevice(
+            "dev1",
+            exists=True,
+            size=Size("10 GiB"),
+            fmt=get_format(None, exists=True)
+        )
+
+        self._add_device(dev1)
+        self.assertEqual(self.interface.IsDeviceShrinkable("dev1"), False)
+
+        dev1._resizable = True
+        dev1.format._resizable = True
+        dev1.format._min_size = Size("1 GiB")
+        self.assertEqual(self.interface.IsDeviceShrinkable("dev1"), True)
+
+        dev1.format._min_size = Size("10 GiB")
+        self.assertEqual(self.interface.IsDeviceShrinkable("dev1"), False)
 
     def get_device_partitions_test(self):
         """Test GetDevicePartitions."""
-- 
2.23.0

