Esempio n. 1
0
class ResizableDeviceTreeTestCase(unittest.TestCase):
    """Test DBus interface of the auto partitioning module."""
    def setUp(self):
        """Set up the module."""
        self.module = ResizableDeviceTreeModule()
        self.interface = ResizableDeviceTreeInterface(self.module)

    @property
    def storage(self):
        """Get the storage object."""
        return self.module.storage

    def _add_device(self, device):
        """Add a device to the device tree."""
        self.storage.devicetree._add_device(device)

    def test_publication(self):
        """Test the DBus representation."""
        self.assertIsInstance(self.module.for_publication(),
                              ResizableDeviceTreeInterface)

    def test_is_device_partitioned(self):
        """Test IsDevicePartitioned."""
        self.module.on_storage_changed(create_storage())
        self._add_device(DiskDevice("dev1"))
        self._add_device(DiskDevice("dev2", fmt=get_format("disklabel")))

        self.assertEqual(self.interface.IsDevicePartitioned("dev1"), False)
        self.assertEqual(self.interface.IsDevicePartitioned("dev2"), True)

    @patch.object(FS, "update_size_info")
    def test_is_device_shrinkable(self, update_size_info):
        """Test IsDeviceShrinkable."""
        self.module.on_storage_changed(create_storage())

        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 test_get_device_partitions(self):
        """Test GetDevicePartitions."""
        self.module.on_storage_changed(create_storage())
        dev1 = DiskDevice("dev1")
        self._add_device(dev1)

        dev2 = DiskDevice("dev2", fmt=get_format("disklabel"))
        self._add_device(dev2)

        dev3 = PartitionDevice("dev3")
        dev2.add_child(dev3)
        self._add_device(dev3)

        self.assertEqual(self.interface.GetDevicePartitions("dev1"), [])
        self.assertEqual(self.interface.GetDevicePartitions("dev2"), ["dev3"])
        self.assertEqual(self.interface.GetDevicePartitions("dev3"), [])

    def test_get_device_size_limits(self):
        """Test GetDeviceSizeLimits."""
        self.module.on_storage_changed(create_storage())
        self._add_device(
            StorageDevice("dev1", fmt=get_format("ext4"), size=Size("10 MiB")))

        min_size, max_size = self.interface.GetDeviceSizeLimits("dev1")
        self.assertEqual(min_size, 0)
        self.assertEqual(max_size, 0)

    def test_shrink_device(self):
        """Test ShrinkDevice."""
        self.module.on_storage_changed(create_storage())

        sda1 = StorageDevice("sda1",
                             exists=False,
                             size=Size("10 GiB"),
                             fmt=get_format("ext4"))
        self.module.storage.devicetree._add_device(sda1)

        def resize_device(device, size):
            device.size = size

        self.module.storage.resize_device = resize_device

        sda1.protected = True
        with self.assertRaises(ProtectedDeviceError):
            self.interface.ShrinkDevice("sda1", Size("3 GiB").get_bytes())

        sda1.protected = False
        self.interface.ShrinkDevice("sda1", Size("3 GiB").get_bytes())
        self.assertEqual(sda1.size, Size("3 GiB"))

        self.interface.ShrinkDevice("sda1", Size("5 GiB").get_bytes())
        self.assertEqual(sda1.size, Size("3 GiB"))

    def test_remove_device(self):
        """Test RemoveDevice."""
        self.module.on_storage_changed(create_storage())

        dev1 = StorageDevice("dev1",
                             exists=False,
                             size=Size("15 GiB"),
                             fmt=get_format("disklabel"))
        dev2 = StorageDevice("dev2",
                             exists=False,
                             parents=[dev1],
                             size=Size("6 GiB"),
                             fmt=get_format("ext4"))
        dev3 = StorageDevice("dev3",
                             exists=False,
                             parents=[dev1],
                             size=Size("9 GiB"),
                             fmt=get_format("ext4"))

        self.module.storage.devicetree._add_device(dev1)
        self.module.storage.devicetree._add_device(dev2)
        self.module.storage.devicetree._add_device(dev3)

        dev1.protected = True
        with self.assertRaises(ProtectedDeviceError):
            self.interface.RemoveDevice("dev1")

        self.assertIn(dev1, self.module.storage.devices)
        self.assertIn(dev2, self.module.storage.devices)
        self.assertIn(dev3, self.module.storage.devices)

        dev1.protected = False
        dev2.protected = True
        self.interface.RemoveDevice("dev1")

        self.assertIn(dev1, self.module.storage.devices)
        self.assertIn(dev2, self.module.storage.devices)
        self.assertNotIn(dev3, self.module.storage.devices)

        dev2.protected = False
        self.interface.RemoveDevice("dev1")

        self.assertNotIn(dev1, self.module.storage.devices)
        self.assertNotIn(dev2, self.module.storage.devices)
        self.assertNotIn(dev3, self.module.storage.devices)
Esempio n. 2
0
 def setUp(self):
     """Set up the module."""
     self.module = ResizableDeviceTreeModule()
     self.interface = ResizableDeviceTreeInterface(self.module)
Esempio n. 3
0
 def for_publication(self):
     """Return a DBus representation."""
     return ResizableDeviceTreeInterface(self)