コード例 #1
0
 def setUp(self):
     """Set up the module."""
     self.module = AutoPartitioningModule()
     self.interface = AutoPartitioningInterface(self.module)
コード例 #2
0
class AutopartitioningInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the auto partitioning module."""
    def setUp(self):
        """Set up the module."""
        self.module = AutoPartitioningModule()
        self.interface = AutoPartitioningInterface(self.module)

    def _check_dbus_property(self, *args, **kwargs):
        check_dbus_property(AUTO_PARTITIONING, self.interface, *args, **kwargs)

    def test_publication(self):
        """Test the DBus representation."""
        assert isinstance(self.module.for_publication(),
                          AutoPartitioningInterface)

    @patch_dbus_publish_object
    def test_device_tree(self, publisher):
        """Test the device tree."""
        self.module.on_storage_changed(Mock())
        path = self.interface.GetDeviceTree()
        obj = check_dbus_object_creation(path, publisher,
                                         ResizableDeviceTreeModule)
        assert obj.implementation.storage == self.module.storage

        self.module.on_partitioning_reset()
        assert obj.implementation.storage == self.module.storage

        self.module.on_storage_changed(Mock())
        assert obj.implementation.storage == self.module.storage

    def test_request_property(self):
        """Test the property request."""
        request = {
            'partitioning-scheme':
            get_variant(Int, AUTOPART_TYPE_LVM_THINP),
            'file-system-type':
            get_variant(Str, 'ext4'),
            'excluded-mount-points':
            get_variant(List[Str], ['/home', '/boot', 'swap']),
            'encrypted':
            get_variant(Bool, True),
            'passphrase':
            get_variant(Str, '123456'),
            'cipher':
            get_variant(Str, 'aes-xts-plain64'),
            'luks-version':
            get_variant(Str, 'luks1'),
            'pbkdf':
            get_variant(Str, 'argon2i'),
            'pbkdf-memory':
            get_variant(Int, 256),
            'pbkdf-time':
            get_variant(Int, 100),
            'pbkdf-iterations':
            get_variant(Int, 1000),
            'escrow-certificate':
            get_variant(Str, 'file:///tmp/escrow.crt'),
            'backup-passphrase-enabled':
            get_variant(Bool, True),
        }
        self._check_dbus_property("Request", request)

    def test_requires_passphrase(self):
        """Test RequiresPassphrase."""
        assert self.interface.RequiresPassphrase() is False

        self.module.request.encrypted = True
        assert self.interface.RequiresPassphrase() is True

        self.module.request.passphrase = "123456"
        assert self.interface.RequiresPassphrase() is False

    def test_reset(self):
        """Test the reset of the storage."""
        with pytest.raises(UnavailableStorageError):
            if self.module.storage:
                self.fail("The storage shouldn't be available.")

        storage = Mock()
        self.module.on_storage_changed(storage)

        assert self.module._current_storage == storage
        assert self.module._storage_playground is None

        assert self.module.storage != storage
        assert self.module._storage_playground is not None

    @patch_dbus_publish_object
    def test_configure_with_task(self, publisher):
        """Test ConfigureWithTask."""
        self.module.on_storage_changed(Mock())
        task_path = self.interface.ConfigureWithTask()

        obj = check_task_creation(task_path, publisher,
                                  AutomaticPartitioningTask)

        assert obj.implementation._storage == self.module.storage
        assert obj.implementation._request == self.module.request

    @patch_dbus_publish_object
    def test_validate_with_task(self, publisher):
        """Test ValidateWithTask."""
        self.module.on_storage_changed(Mock())
        task_path = self.interface.ValidateWithTask()

        obj = check_task_creation(task_path, publisher, StorageValidateTask)
        assert obj.implementation._storage == self.module.storage

        report = ValidationReport()
        report.error_messages = [
            "Something is wrong.", "Something is very wrong."
        ]
        report.warning_messages = ["Something might be wrong."]
        obj.implementation._set_result(report)

        result = obj.GetResult()
        expected_result = get_variant(
            Structure, {
                "error-messages":
                get_variant(
                    List[Str],
                    ["Something is wrong.", "Something is very wrong."]),
                "warning-messages":
                get_variant(List[Str], ["Something might be wrong."])
            })

        assert isinstance(result, Variant)
        assert get_native(result) == get_native(expected_result)
        assert result.equal(expected_result)
コード例 #3
0
class AutopartitioningInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the auto partitioning module."""
    def setUp(self):
        """Set up the module."""
        self.module = AutoPartitioningModule()
        self.interface = AutoPartitioningInterface(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_dbus_property(self, *args, **kwargs):
        check_dbus_property(self, AUTO_PARTITIONING, self.interface, *args,
                            **kwargs)

    def publication_test(self):
        """Test the DBus representation."""
        self.assertIsInstance(self.module.for_publication(),
                              AutoPartitioningInterface)

    def enabled_property_test(self):
        """Test the property enabled."""
        self._test_dbus_property("Enabled", True)

    def request_property_test(self):
        """Test the property request."""
        in_value = {
            'partitioning-scheme': AUTOPART_TYPE_LVM_THINP,
            'file-system-type': 'ext4',
            'excluded-mount-points': ['/home', '/boot', 'swap'],
            'encrypted': True,
            'passphrase': '123456',
            'cipher': 'aes-xts-plain64',
            'luks-version': 'luks1',
            'pbkdf': 'argon2i',
            'pbkdf-memory': 256,
            'pbkdf-time': 100,
            'pbkdf-iterations': 1000,
            'escrow-certificate': 'file:///tmp/escrow.crt',
            'backup-passphrase-enabled': True,
        }

        out_value = {
            'partitioning-scheme':
            get_variant(Int, AUTOPART_TYPE_LVM_THINP),
            'file-system-type':
            get_variant(Str, 'ext4'),
            'excluded-mount-points':
            get_variant(List[Str], ['/home', '/boot', 'swap']),
            'encrypted':
            get_variant(Bool, True),
            'passphrase':
            get_variant(Str, '123456'),
            'cipher':
            get_variant(Str, 'aes-xts-plain64'),
            'luks-version':
            get_variant(Str, 'luks1'),
            'pbkdf':
            get_variant(Str, 'argon2i'),
            'pbkdf-memory':
            get_variant(Int, 256),
            'pbkdf-time':
            get_variant(Int, 100),
            'pbkdf-iterations':
            get_variant(Int, 1000),
            'escrow-certificate':
            get_variant(Str, 'file:///tmp/escrow.crt'),
            'backup-passphrase-enabled':
            get_variant(Bool, True),
        }

        self._test_dbus_property("Request", in_value, out_value)

    def requires_passphrase_test(self):
        """Test RequiresPassphrase."""
        self.assertEqual(self.interface.RequiresPassphrase(), False)

        self.module.request.encrypted = True
        self.assertEqual(self.interface.RequiresPassphrase(), True)

        self.module.request.passphrase = "123456"
        self.assertEqual(self.interface.RequiresPassphrase(), False)

    def reset_test(self):
        """Test the reset of the storage."""
        with self.assertRaises(UnavailableStorageError):
            if self.module.storage:
                self.fail("The storage shouldn't be available.")

        storage = Mock()
        self.module.on_storage_changed(storage)

        self.assertEqual(self.module._current_storage, storage)
        self.assertIsNone(self.module._storage_playground)

        self.assertNotEqual(self.module.storage, storage)
        self.assertIsNotNone(self.module._storage_playground)

    def remove_device_test(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)

    def shrink_device_test(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 is_device_partitioned_test(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)

    def get_device_partitions_test(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 is_device_resizable_test(self):
        """Test IsDeviceResizable."""
        self.module.on_storage_changed(create_storage())
        self._add_device(StorageDevice("dev1"))
        self.assertEqual(self.interface.IsDeviceResizable("dev1"), False)

    def get_device_size_limits_test(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)

    @patch_dbus_publish_object
    def configure_with_task_test(self, publisher):
        """Test ConfigureWithTask."""
        self.module.on_storage_changed(Mock())
        task_path = self.interface.ConfigureWithTask()

        obj = check_task_creation(self, task_path, publisher,
                                  AutomaticPartitioningTask)

        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._request, self.module.request)

    @patch_dbus_publish_object
    def validate_with_task_test(self, publisher):
        """Test ValidateWithTask."""
        self.module.on_storage_changed(Mock())
        task_path = self.interface.ValidateWithTask()

        obj = check_task_creation(self, task_path, publisher,
                                  StorageValidateTask)
        self.assertEqual(obj.implementation._storage, self.module.storage)

        report = ValidationReport()
        report.error_messages = [
            "Something is wrong.", "Something is very wrong."
        ]
        report.warning_messages = ["Something might be wrong."]
        obj.implementation._set_result(report)

        result = obj.GetResult()
        expected_result = get_variant(
            Structure, {
                "error-messages":
                get_variant(
                    List[Str],
                    ["Something is wrong.", "Something is very wrong."]),
                "warning-messages":
                get_variant(List[Str], ["Something might be wrong."])
            })

        self.assertIsInstance(result, Variant)
        self.assertEqual(get_native(result), get_native(expected_result))
        self.assertTrue(result.equal(expected_result))
コード例 #4
0
 def for_publication(self):
     """Return a DBus representation."""
     return AutoPartitioningInterface(self)
コード例 #5
0
class AutopartitioningInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the auto partitioning module."""

    def setUp(self):
        """Set up the module."""
        self.module = AutoPartitioningModule()
        self.interface = AutoPartitioningInterface(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_dbus_property(self, *args, **kwargs):
        check_dbus_property(
            self,
            AUTO_PARTITIONING,
            self.interface,
            *args, **kwargs
        )

    def publication_test(self):
        """Test the DBus representation."""
        self.assertIsInstance(self.module.for_publication(), AutoPartitioningInterface)

    @patch_dbus_publish_object
    def device_tree_test(self, publisher):
        """Test the device tree."""
        self.module.on_storage_changed(Mock())
        path = self.interface.GetDeviceTree()
        check_dbus_object_creation(self, path, publisher, ResizableDeviceTreeModule)

    def enabled_property_test(self):
        """Test the property enabled."""
        self._test_dbus_property(
            "Enabled",
            True
        )

    def request_property_test(self):
        """Test the property request."""
        in_value = {
            'partitioning-scheme': AUTOPART_TYPE_LVM_THINP,
            'file-system-type': 'ext4',
            'excluded-mount-points': ['/home', '/boot', 'swap'],
            'encrypted': True,
            'passphrase': '123456',
            'cipher': 'aes-xts-plain64',
            'luks-version': 'luks1',
            'pbkdf': 'argon2i',
            'pbkdf-memory': 256,
            'pbkdf-time': 100,
            'pbkdf-iterations': 1000,
            'escrow-certificate': 'file:///tmp/escrow.crt',
            'backup-passphrase-enabled': True,
        }

        out_value = {
            'partitioning-scheme': get_variant(Int, AUTOPART_TYPE_LVM_THINP),
            'file-system-type': get_variant(Str, 'ext4'),
            'excluded-mount-points': get_variant(List[Str], ['/home', '/boot', 'swap']),
            'encrypted': get_variant(Bool, True),
            'passphrase': get_variant(Str, '123456'),
            'cipher': get_variant(Str, 'aes-xts-plain64'),
            'luks-version': get_variant(Str, 'luks1'),
            'pbkdf': get_variant(Str, 'argon2i'),
            'pbkdf-memory': get_variant(Int, 256),
            'pbkdf-time': get_variant(Int, 100),
            'pbkdf-iterations': get_variant(Int, 1000),
            'escrow-certificate': get_variant(Str, 'file:///tmp/escrow.crt'),
            'backup-passphrase-enabled': get_variant(Bool, True),
        }

        self._test_dbus_property(
            "Request",
            in_value,
            out_value
        )

    def requires_passphrase_test(self):
        """Test RequiresPassphrase."""
        self.assertEqual(self.interface.RequiresPassphrase(), False)

        self.module.request.encrypted = True
        self.assertEqual(self.interface.RequiresPassphrase(), True)

        self.module.request.passphrase = "123456"
        self.assertEqual(self.interface.RequiresPassphrase(), False)

    def reset_test(self):
        """Test the reset of the storage."""
        with self.assertRaises(UnavailableStorageError):
            if self.module.storage:
                self.fail("The storage shouldn't be available.")

        storage = Mock()
        self.module.on_storage_changed(storage)

        self.assertEqual(self.module._current_storage, storage)
        self.assertIsNone(self.module._storage_playground)

        self.assertNotEqual(self.module.storage, storage)
        self.assertIsNotNone(self.module._storage_playground)

    @patch_dbus_publish_object
    def configure_with_task_test(self, publisher):
        """Test ConfigureWithTask."""
        self.module.on_storage_changed(Mock())
        task_path = self.interface.ConfigureWithTask()

        obj = check_task_creation(self, task_path, publisher, AutomaticPartitioningTask)

        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._request, self.module.request)

    @patch_dbus_publish_object
    def validate_with_task_test(self, publisher):
        """Test ValidateWithTask."""
        self.module.on_storage_changed(Mock())
        task_path = self.interface.ValidateWithTask()

        obj = check_task_creation(self, task_path, publisher, StorageValidateTask)
        self.assertEqual(obj.implementation._storage, self.module.storage)

        report = ValidationReport()
        report.error_messages = [
            "Something is wrong.",
            "Something is very wrong."
        ]
        report.warning_messages = [
            "Something might be wrong."
        ]
        obj.implementation._set_result(report)

        result = obj.GetResult()
        expected_result = get_variant(Structure, {
            "error-messages": get_variant(List[Str], [
                "Something is wrong.",
                "Something is very wrong."
            ]),
            "warning-messages": get_variant(List[Str], [
                "Something might be wrong."
            ])
        })

        self.assertIsInstance(result, Variant)
        self.assertEqual(get_native(result), get_native(expected_result))
        self.assertTrue(result.equal(expected_result))