Beispiel #1
0
    def setUp(self):
        self.maxDiff = None
        self.module = DeviceTreeModule()
        self.interface = DeviceTreeInterface(self.module)

        # Set the storage.
        self.module.on_storage_changed(create_storage())
Beispiel #2
0
    def get_device_tree(self):
        """Get the device tree module.

        :return: a device tree module
        """
        module = self._device_tree_module

        if not module:
            module = DeviceTreeModule()
            module.on_storage_reset(self.storage)
            self._device_tree_module = module

        return module
Beispiel #3
0
    def get_device_tree(self):
        """Get the device tree.

        :return: a DBus path to a device tree
        """
        if not self._device_tree_module:
            module = DeviceTreeModule()
            module.on_storage_reset(self.storage)
            self._device_tree_module = module

        if not self._device_tree_path:
            self._device_tree_path = publish_device_tree(
                message_bus=DBus,
                namespace=STORAGE.namespace,
                device_tree=self._device_tree_module
            )

        return self._device_tree_path
Beispiel #4
0
    def publish_device_tree_test(self):
        """Test publish_device_tree."""
        DeviceTreeInterface._tree_counter = 1
        message_bus = Mock()

        object_path = publish_device_tree(message_bus, ("A", "B", "C"),
                                          DeviceTreeModule())
        self.assertEqual("/A/B/C/DeviceTree/1", object_path)
        message_bus.publish_object.assert_called_once()
        message_bus.reset_mock()

        object_path = publish_device_tree(message_bus, ("A", "B", "C"),
                                          DeviceTreeModule())
        self.assertEqual("/A/B/C/DeviceTree/2", object_path)
        message_bus.publish_object.assert_called_once()
        message_bus.reset_mock()

        object_path = publish_device_tree(message_bus, ("A", "B", "C"),
                                          DeviceTreeModule())
        self.assertEqual("/A/B/C/DeviceTree/3", object_path)
        message_bus.publish_object.assert_called_once()
        message_bus.reset_mock()
Beispiel #5
0
    def __init__(self):
        super().__init__()
        # Initialize Blivet.
        enable_installer_mode()

        # The storage model.
        self._current_storage = None
        self._storage_playground = None
        self.storage_changed = Signal()

        # The created partitioning modules.
        self._created_partitioning = []
        self.created_partitioning_changed = Signal()

        # The applied partitioning module.
        self._applied_partitioning = None
        self.applied_partitioning_changed = Signal()
        self.partitioning_reset = Signal()

        # Initialize modules.
        self._modules = []

        self._storage_checker_module = StorageCheckerModule()
        self._add_module(self._storage_checker_module)

        self._device_tree_module = DeviceTreeModule()
        self._add_module(self._device_tree_module)

        self._disk_init_module = DiskInitializationModule()
        self._add_module(self._disk_init_module)

        self._disk_selection_module = DiskSelectionModule()
        self._add_module(self._disk_selection_module)

        self._snapshot_module = SnapshotModule()
        self._add_module(self._snapshot_module)

        self._bootloader_module = BootloaderModule()
        self._add_module(self._bootloader_module)

        self._fcoe_module = FCOEModule()
        self._add_module(self._fcoe_module)

        self._iscsi_module = ISCSIModule()
        self._add_module(self._iscsi_module)

        self._nvdimm_module = NVDIMMModule()
        self._add_module(self._nvdimm_module)

        self._dasd_module = DASDModule()
        self._add_module(self._dasd_module)

        self._zfcp_module = ZFCPModule()
        self._add_module(self._zfcp_module)

        # Connect modules to signals.
        self.storage_changed.connect(
            self._device_tree_module.on_storage_changed)
        self.storage_changed.connect(self._disk_init_module.on_storage_changed)
        self.storage_changed.connect(
            self._disk_selection_module.on_storage_changed)
        self.storage_changed.connect(self._snapshot_module.on_storage_changed)
        self.storage_changed.connect(
            self._bootloader_module.on_storage_changed)
        self.storage_changed.connect(self._dasd_module.on_storage_changed)
        self._disk_init_module.format_unrecognized_enabled_changed.connect(
            self._dasd_module.on_format_unrecognized_enabled_changed)
        self._disk_init_module.format_ldl_enabled_changed.connect(
            self._dasd_module.on_format_ldl_enabled_changed)
        self._disk_selection_module.protected_devices_changed.connect(
            self.on_protected_devices_changed)
Beispiel #6
0
    def __init__(self):
        super().__init__()
        # Initialize Blivet.
        enable_installer_mode()

        # The storage model.
        self._storage = None
        self.storage_changed = Signal()

        # Initialize modules.
        self._modules = []

        self._storage_checker_module = StorageCheckerModule()
        self._add_module(self._storage_checker_module)

        self._device_tree_module = DeviceTreeModule()
        self._add_module(self._device_tree_module)

        self._disk_init_module = DiskInitializationModule()
        self._add_module(self._disk_init_module)

        self._disk_selection_module = DiskSelectionModule()
        self._add_module(self._disk_selection_module)

        self._snapshot_module = SnapshotModule()
        self._add_module(self._snapshot_module)

        self._bootloader_module = BootloaderModule()
        self._add_module(self._bootloader_module)

        self._fcoe_module = FCOEModule()
        self._add_module(self._fcoe_module)

        self._iscsi_module = ISCSIModule()
        self._add_module(self._iscsi_module)

        self._nvdimm_module = NVDIMMModule()
        self._add_module(self._nvdimm_module)

        self._dasd_module = None
        self._zfcp_module = None

        if arch.is_s390():
            self._dasd_module = DASDModule()
            self._add_module(self._dasd_module)

            self._zfcp_module = ZFCPModule()
            self._add_module(self._zfcp_module)

        # Initialize the partitioning modules.
        self._partitioning_modules = {}

        self._auto_part_module = AutoPartitioningModule()
        self._add_partitioning_module(AUTO_PARTITIONING.object_path,
                                      self._auto_part_module)

        self._manual_part_module = ManualPartitioningModule()
        self._add_partitioning_module(MANUAL_PARTITIONING.object_path,
                                      self._manual_part_module)

        self._custom_part_module = CustomPartitioningModule()
        self._add_partitioning_module(CUSTOM_PARTITIONING.object_path,
                                      self._custom_part_module)

        self._interactive_part_module = InteractivePartitioningModule()
        self._add_partitioning_module(INTERACTIVE_PARTITIONING.object_path,
                                      self._interactive_part_module)

        self._blivet_part_module = BlivetPartitioningModule()
        self._add_partitioning_module(BLIVET_PARTITIONING.object_path,
                                      self._blivet_part_module)

        # Connect modules to signals.
        self.storage_changed.connect(self._device_tree_module.on_storage_reset)
        self.storage_changed.connect(self._disk_init_module.on_storage_reset)
        self.storage_changed.connect(
            self._disk_selection_module.on_storage_reset)
        self.storage_changed.connect(self._snapshot_module.on_storage_reset)
        self.storage_changed.connect(self._bootloader_module.on_storage_reset)
        self._disk_selection_module.protected_devices_changed.connect(
            self.on_protected_devices_changed)
Beispiel #7
0
class DeviceTreeInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the device tree handler."""

    def setUp(self):
        self.maxDiff = None
        self.module = DeviceTreeModule()
        self.interface = DeviceTreeInterface(self.module)

        # Set the storage.
        self.module.on_storage_changed(create_storage())

    def test_publication(self):
        """Check the DBus representation."""
        self.assertIsInstance(self.module.for_publication(), DeviceTreeInterface)

    @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_get_root_device(self):
        """Test GetRootDevice."""
        self.assertEqual(self.interface.GetRootDevice(), "")

        self._add_device(StorageDevice("dev1", fmt=get_format("ext4", mountpoint="/")))
        self._add_device(StorageDevice("dev2", fmt=get_format("ext4", mountpoint="/home")))

        self.assertEqual(self.interface.GetRootDevice(), "dev1")

    def test_get_devices(self):
        """Test GetDevices."""
        self.assertEqual(self.interface.GetDevices(), [])

        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 GiB")
        ))

        self._add_device(StorageDevice(
            "dev2",
            fmt=get_format("ext4"),
            size=Size("10 GiB")
        ))

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

    def test_get_disks(self):
        """Test GetDisks."""
        self.assertEqual(self.interface.GetDisks(), [])

        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            exists=True,
            size=Size("10 GiB"))
        )

        self.assertEqual(self.interface.GetDisks(), ["dev1"])

    def test_get_mount_points(self):
        """Test GetMountPoints."""
        self.assertEqual(self.interface.GetMountPoints(), {})

        self._add_device(StorageDevice("dev1", fmt=get_format("ext4", mountpoint="/")))
        self._add_device(StorageDevice("dev2", fmt=get_format("ext4", mountpoint="/home")))

        self.assertEqual(self.interface.GetMountPoints(), {
            "/": "dev1",
            "/home": "dev2"
        })

    def test_get_device_data(self):
        """Test GetDeviceData."""
        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 MiB"),
            serial="SERIAL_ID",
            vendor="VENDOR_ID",
            model="MODEL_ID",
            bus="BUS_ID",
            wwn="0x0000000000000000",
            uuid="1234-56-7890"
        ))

        self.assertEqual(self.interface.GetDeviceData("dev1"), {
            'type': get_variant(Str, 'disk'),
            'name': get_variant(Str, 'dev1'),
            'path': get_variant(Str, '/dev/dev1'),
            'size': get_variant(UInt64, Size("10 MiB").get_bytes()),
            'is-disk': get_variant(Bool, True),
            'protected': get_variant(Bool, False),
            'removable': get_variant(Bool, False),
            'parents': get_variant(List[Str], []),
            'children': get_variant(List[Str], []),
            'attrs': get_variant(Dict[Str, Str], {
                "serial": "SERIAL_ID",
                "vendor": "VENDOR_ID",
                "model": "MODEL_ID",
                "bus": "BUS_ID",
                "wwn": "0x0000000000000000",
                "uuid": "1234-56-7890"
            }),
            'description': get_variant(
                Str, "VENDOR_ID MODEL_ID 0x0000000000000000"
            )
        })

    def test_get_unknown_device_data(self):
        """Test GetDeviceData for unknown."""
        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDeviceData("dev1")

    def test_get_dasd_device_data(self):
        """Test GetDeviceData for DASD."""
        self._add_device(DASDDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 GiB"),
            busid="0.0.0201",
            opts={}
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'dasd'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {
            "bus-id": "0.0.0201"
        }))

    def test_get_fcoe_device_data(self):
        """Test GetDeviceData for FCoE."""
        self._add_device(FcoeDiskDevice(
            "dev1",
            fmt=get_format("disklabel"),
            size=Size("10 GiB"),
            nic=None,
            identifier=None,
            id_path="pci-0000:00:00.0-bla-1"
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'fcoe'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {
            "path-id": "pci-0000:00:00.0-bla-1"
        }))

    def test_get_iscsi_device_data(self):
        """Test GetDeviceData for iSCSI."""
        self._add_device(iScsiDiskDevice(
            "dev1",
            fmt=get_format("disklabel"),
            size=Size("10 GiB"),
            port="3260",
            initiator="iqn.1994-05.com.redhat:blabla",
            lun="0",
            target="iqn.2014-08.com.example:t1",
            id_path="pci-0000:00:00.0-bla-1",
            node=None,
            ibft=None,
            nic=None,
            offload=None,
            name=None,
            address=None,
            iface=None
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'iscsi'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {
            "port": "3260",
            "initiator": "iqn.1994-05.com.redhat:blabla",
            "lun": "0",
            "target": "iqn.2014-08.com.example:t1",
            "path-id": "pci-0000:00:00.0-bla-1"
        }))

    def test_get_nvdimm_device_data(self):
        """Test GetDeviceData for NVDIMM."""
        self._add_device(NVDIMMNamespaceDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 GiB"),
            mode="sector",
            devname="namespace0.0",
            sector_size=512,
            id_path="pci-0000:00:00.0-bla-1"
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'nvdimm'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {
            "mode": "sector",
            "namespace": "namespace0.0",
            "path-id": "pci-0000:00:00.0-bla-1"
        }))

    def test_get_zfcp_device_data(self):
        """Test GetDeviceData for zFCP."""
        self._add_device(ZFCPDiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 GiB"),
            fcp_lun="0x5719000000000000",
            wwpn="0x5005076300c18154",
            hba_id="0.0.010a"
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'zfcp'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {
            "fcp-lun": "0x5719000000000000",
            "wwpn": "0x5005076300c18154",
            "hba-id": "0.0.010a"
        }))

    def test_get_format_data(self):
        """Test GetFormatData."""
        fmt1 = get_format(
            "ext4",
            uuid="1234-56-7890",
            label="LABEL",
            mountpoint="/home"
        )
        dev1 = StorageDevice(
            "dev1",
            fmt=fmt1,
            size=Size("10 GiB")
        )
        self._add_device(dev1)

        self.assertEqual(self.interface.GetFormatData("dev1"), {
            'type': get_variant(Str, 'ext4'),
            'mountable': get_variant(Bool, True),
            'attrs': get_variant(Dict[Str, Str], {
                "uuid": "1234-56-7890",
                "label": "LABEL",
                "mount-point": "/home"
            }),
            'description': get_variant(Str, 'ext4'),
        })

        fmt2 = get_format(
            "luks"
        )
        dev2 = LUKSDevice(
            "dev2",
            parents=[dev1],
            fmt=fmt2,
            size=Size("10 GiB")
        )
        self._add_device(dev2)

        self.assertEqual(self.interface.GetFormatData("dev2"), {
            'type': get_variant(Str, 'luks'),
            'mountable': get_variant(Bool, False),
            'attrs': get_variant(Dict[Str, Str], {}),
            'description': get_variant(Str, 'LUKS'),
        })

    def test_get_format_type_data(self):
        """Test GetFormatTypeData."""
        self.assertEqual(self.interface.GetFormatTypeData("swap"), {
            'type': get_variant(Str, 'swap'),
            'mountable': get_variant(Bool, False),
            'attrs': get_variant(Dict[Str, Str], {}),
            'description': get_variant(Str, 'swap'),
        })

    def test_get_all_format_type_data(self):
        """Test GetFormatTypeData for all format types."""
        for format_type in device_formats:
            data = DeviceFormatData.from_structure(
                self.interface.GetFormatTypeData(format_type)
            )
            self.assertEqual(format_type or "", data.type)

    def test_get_actions(self):
        """Test GetActions."""
        self.assertEqual(self.interface.GetActions(), [])

        dev1 = DiskDevice(
            "dev1",
            fmt=get_format("disklabel"),
            size=Size("1 GiB"),
            vendor="VENDOR",
            model="MODEL"
        )

        self._add_device(dev1)
        self.storage.initialize_disk(dev1)
        dev1.format._label_type = "msdos"

        action_1 = {
            'action-type': 'create',
            'action-description': 'create format',
            'object-type': 'format',
            'object-description': 'partition table (MSDOS)',
            'device-name': 'dev1',
            'device-description': 'VENDOR MODEL (dev1)',
            'attrs': {},
        }

        self.assertEqual(get_native(self.interface.GetActions()), [
            action_1
        ])

        dev2 = StorageDevice(
            "dev2",
            fmt=get_format("ext4", mountpoint="/boot"),
            size=Size("500 MiB"),
            serial="SERIAL",
            exists=True
        )

        self._add_device(dev2)
        self.storage.destroy_device(dev2)

        action_2 = {
            'action-type': 'destroy',
            'action-description': 'destroy device',
            'object-type': 'device',
            'object-description': 'blivet',
            'device-name': 'dev2',
            'device-description': 'dev2',
            'attrs': {
                'serial': 'SERIAL',
                'mount-point': '/boot'
            },
        }

        self.assertEqual(get_native(self.interface.GetActions()), [
            action_2,
            action_1
          ])

        dev3 = PartitionDevice(
            "dev3",
            fmt=get_format("ext4", mountpoint="/home"),
            size=Size("500 MiB"),
            parents=[dev1]
        )

        self.storage.create_device(dev3)
        dev3.disk = dev1

        action_3 = {
            'action-type': 'create',
            'action-description': 'create device',
            'object-type': 'device',
            'object-description': 'partition',
            'device-name': 'dev3',
            'device-description': 'dev3 on VENDOR MODEL',
            'attrs': {'mount-point': '/home'},
        }

        action_4 = {
            'action-type': 'create',
            'action-description': 'create format',
            'object-type': 'format',
            'object-description': 'ext4',
            'device-name': 'dev3',
            'device-description': 'dev3 on VENDOR MODEL',
            'attrs': {'mount-point': '/home'},
        }

        self.assertEqual(get_native(self.interface.GetActions()), [
            action_2,
            action_1,
            action_3,
            action_4,
          ])

    def test_get_supported_file_systems(self):
        """Test GetSupportedFileSystems."""
        result = self.interface.GetSupportedFileSystems()
        self.assertIsInstance(result, list)
        self.assertNotEqual(len(result), 0)

        for fs in result:
            self.assertIsInstance(fs, str)
            self.assertEqual(fs, get_format(fs).type)

    def test_get_required_device_size(self):
        """Test GetRequiredDeviceSize."""
        required_size = self.interface.GetRequiredDeviceSize(Size("1 GiB").get_bytes())
        self.assertEqual(Size("1280 MiB").get_bytes(), required_size, Size(required_size))

    def test_get_file_system_free_space(self):
        """Test GetFileSystemFreeSpace."""
        self._add_device(StorageDevice(
            "dev1",
            fmt=get_format("ext4", mountpoint="/"),
            size=Size("5 GiB"))
        )

        self._add_device(StorageDevice(
            "dev2",
            fmt=get_format("ext4", mountpoint="/usr"),
            size=Size("5 GiB"))
        )

        total_size = self.interface.GetFileSystemFreeSpace([])
        self.assertEqual(total_size, 0)

        total_size = self.interface.GetFileSystemFreeSpace(["/", "/usr"])
        self.assertLess(total_size, Size("10 GiB").get_bytes())
        self.assertGreater(total_size, Size("8 GiB").get_bytes())

    @patch("blivet.formats.disklabel.DiskLabel.free", new_callable=PropertyMock)
    @patch("blivet.formats.disklabel.DiskLabel.get_platform_label_types")
    def test_get_disk_free_space(self, label_types, free):
        """Test GetDiskFreeSpace."""
        label_types.return_value = ["msdos", "gpt"]
        free.return_value = Size("4 GiB")

        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("disklabel", label_type="msdos"),
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev2",
            fmt=get_format("disklabel", label_type="gpt"),
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev3",
            fmt=get_format("disklabel", label_type="dasd"),
            size=Size("5 GiB")
        ))

        total_size = self.interface.GetDiskFreeSpace([])
        self.assertEqual(total_size, 0)

        total_size = self.interface.GetDiskFreeSpace(["dev1", "dev2", "dev3"])
        self.assertEqual(total_size, Size("8 GiB").get_bytes())

        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDiskFreeSpace(["dev1", "dev2", "devX"])

    @patch("blivet.formats.disklabel.DiskLabel.get_platform_label_types")
    def test_get_disk_reclaimable_space(self, label_types):
        """Test GetDiskReclaimableSpace."""
        label_types.return_value = ["msdos", "gpt"]

        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("disklabel", label_type="msdos"),
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev2",
            fmt=get_format("disklabel", label_type="gpt"),
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev3",
            fmt=get_format("disklabel", label_type="dasd"),
            size=Size("5 GiB")
        ))

        total_size = self.interface.GetDiskReclaimableSpace([])
        self.assertEqual(total_size, 0)

        # FIXME: Test on devices with a reclaimable space.
        total_size = self.interface.GetDiskReclaimableSpace(["dev1", "dev2", "dev3"])
        self.assertEqual(total_size, 0)

        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDiskReclaimableSpace(["dev1", "dev2", "devX"])

    def test_get_disk_total_space(self):
        """Test GetDiskTotalSpace."""
        self._add_device(DiskDevice(
            "dev1",
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev2",
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev3",
            size=Size("5 GiB")
        ))

        total_size = self.interface.GetDiskTotalSpace(["dev1", "dev2"])
        self.assertEqual(total_size, Size("10 GiB").get_bytes())

    def test_resolve_device(self):
        """Test ResolveDevice."""
        self._add_device(DiskDevice("dev1"))

        self.assertEqual(self.interface.ResolveDevice("dev0"), "")
        self.assertEqual(self.interface.ResolveDevice("dev1"), "dev1")
        self.assertEqual(self.interface.ResolveDevice("/dev/dev1"), "dev1")

    def test_get_ancestors(self):
        """Test GetAncestors."""
        dev1 = StorageDevice("dev1")
        self._add_device(dev1)

        dev2 = StorageDevice("dev2", parents=[dev1])
        self._add_device(dev2)

        dev3 = StorageDevice("dev3", parents=[dev2])
        self._add_device(dev3)

        dev4 = StorageDevice("dev4")
        self._add_device(dev4)

        dev5 = StorageDevice("dev5", parents=[dev4])
        self._add_device(dev5)

        self.assertEqual(self.interface.GetAncestors(["dev1"]), [])
        self.assertEqual(self.interface.GetAncestors(["dev2"]), ["dev1"])
        self.assertEqual(self.interface.GetAncestors(["dev3"]), ["dev1", "dev2"])
        self.assertEqual(self.interface.GetAncestors(["dev2", "dev3"]), ["dev1", "dev2"])
        self.assertEqual(self.interface.GetAncestors(["dev2", "dev5"]), ["dev1", "dev4"])

    @patch.object(StorageDevice, "setup")
    def test_setup_device(self, setup):
        """Test SetupDevice."""
        self._add_device(StorageDevice("dev1"))

        self.interface.SetupDevice("dev1")
        setup.assert_called_once()

    @patch.object(StorageDevice, "teardown")
    def test_teardown_device(self, teardown):
        """Test TeardownDevice."""
        self._add_device(StorageDevice("dev1"))

        self.interface.TeardownDevice("dev1")
        teardown.assert_called_once()

    @patch.object(FS, "mount")
    def test_mount_device(self, mount):
        """Test MountDevice."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.MountDevice("dev1", d, "")
            mount.assert_called_once_with(mountpoint=d, options=None)

        mount.side_effect = FSError("Fake error.")
        with self.assertRaises(MountFilesystemError) as cm:
            self.interface.MountDevice("dev1", "/path", "")

        self.assertEqual(
            str(cm.exception), "Failed to mount dev1 at /path: Fake error."
        )

    @patch.object(FS, "mount")
    def test_mount_device_with_options(self, mount):
        """Test MountDevice with options specified."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.MountDevice("dev1", d, "ro,auto")
            mount.assert_called_once_with(mountpoint=d, options="ro,auto")

        mount.side_effect = FSError("Fake error.")
        with self.assertRaises(MountFilesystemError) as cm:
            self.interface.MountDevice("dev1", "/path", "ro,auto")

        self.assertEqual(
            str(cm.exception), "Failed to mount dev1 at /path: Fake error."
        )

    @patch.object(FS, "unmount")
    def test_unmount_device(self, unmount):
        """Test UnmountDevice."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.UnmountDevice("dev1", d)
            unmount.assert_called_once_with(mountpoint=d)

        unmount.side_effect = FSError("Fake error.")
        with self.assertRaises(MountFilesystemError) as cm:
            self.interface.UnmountDevice("dev1", "/path")

        self.assertEqual(
            str(cm.exception), "Failed to unmount dev1 from /path: Fake error."
        )

    @patch.object(Iso9660FS, "check_module")
    def test_find_install_media(self, check_module):
        """Test FindInstallMedia."""
        dev1 = OpticalDevice("dev1")
        dev1.size = Size("2 GiB")
        dev1.format = get_format("iso9660")
        dev1.controllable = True
        self._add_device(dev1)

        dev2 = StorageDevice("dev2")
        dev2.size = Size("2 GiB")
        dev2.format = get_format("iso9660")
        dev2.controllable = True
        self._add_device(dev2)

        dev3 = StorageDevice("dev3")
        dev3.size = Size("2 GiB")
        dev3.format = get_format("ext4")
        dev3.controllable = True
        self._add_device(dev3)

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

    @patch.object(FS, "update_size_info")
    def test_find_mountable_partitions(self, update_size_info):
        """Test FindMountablePartitions."""
        self._add_device(StorageDevice(
            "dev1",
            fmt=get_format("ext4"))
        )
        self._add_device(PartitionDevice(
            "dev2",
            fmt=get_format("ext4", exists=True)
        ))

        self.assertEqual(self.interface.FindMountablePartitions(), ["dev2"])

    @patch.object(LUKS, "setup")
    @patch.object(LUKSDevice, "teardown")
    @patch.object(LUKSDevice, "setup")
    def test_unlock_device(self, device_setup, device_teardown, format_setup):
        """Test UnlockDevice."""
        self.storage.devicetree.populate = Mock()
        self.storage.devicetree.teardown_all = Mock()

        dev1 = StorageDevice("dev1", fmt=get_format("ext4"), size=Size("10 GiB"))
        self._add_device(dev1)

        dev2 = LUKSDevice("dev2", parents=[dev1], fmt=get_format("luks"), size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.UnlockDevice("dev2", "passphrase"), True)

        device_setup.assert_called_once()
        format_setup.assert_called_once()
        device_teardown.assert_not_called()
        self.storage.devicetree.populate.assert_called_once()
        self.storage.devicetree.teardown_all.assert_called_once()
        self.assertTrue(dev2.format.has_key)

        device_setup.side_effect = StorageError("Fake error")
        self.assertEqual(self.interface.UnlockDevice("dev2", "passphrase"), False)

        device_teardown.assert_called_once()
        self.assertFalse(dev2.format.has_key)

    def test_find_unconfigured_luks(self):
        """Test FindUnconfiguredLUKS."""
        self.assertEqual(self.interface.FindUnconfiguredLUKS(), [])

        dev1 = StorageDevice("dev1", fmt=get_format("ext4"), size=Size("10 GiB"))
        self._add_device(dev1)

        self.assertEqual(self.interface.FindUnconfiguredLUKS(), [])

        dev2 = LUKSDevice("dev2", parents=[dev1], fmt=get_format("luks"), size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.FindUnconfiguredLUKS(), ["dev2"])

    def test_set_device_passphrase(self):
        """Test SetDevicePassphrase."""
        dev1 = StorageDevice("dev1", fmt=get_format("ext4"), size=Size("10 GiB"))
        self._add_device(dev1)

        dev2 = LUKSDevice("dev2", parents=[dev1], fmt=get_format("luks"), size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.FindUnconfiguredLUKS(), ["dev2"])
        self.interface.SetDevicePassphrase("dev2", "123456")
        self.assertEqual(self.interface.FindUnconfiguredLUKS(), [])

    def test_get_fstab_spec(self):
        """Test GetFstabSpec."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4", uuid="123")))
        self.assertEqual(self.interface.GetFstabSpec("dev1"), "UUID=123")

    def test_get_existing_systems(self):
        """Test GetExistingSystems."""
        self.assertEqual(self.interface.GetExistingSystems(), [])

        root_device = StorageDevice("dev1", fmt=get_format("ext4"))
        swap_device = StorageDevice("dev2", fmt=get_format("swap"))

        self.storage.roots = [Root(
            name="My Linux",
            mounts={"/": root_device},
            swaps=[swap_device]
        )]

        self.assertEqual(self.interface.GetExistingSystems(), [{
            'os-name': get_variant(Str, 'My Linux'),
            'mount-points': get_variant(Dict[Str, Str], {'/': 'dev1'}),
            'swap-devices': get_variant(List[Str], ['dev2'])
        }])

    @patch_dbus_publish_object
    def test_find_existing_systems_with_task(self, publisher):
        """Test FindExistingSystemsWithTask."""
        task_path = self.interface.FindExistingSystemsWithTask()

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

        self.assertEqual(obj.implementation._devicetree, self.module.storage.devicetree)

        roots = [Root(name="My Linux")]
        obj.implementation._set_result(roots)
        obj.implementation.succeeded_signal.emit()
        self.assertEqual(self.storage.roots, roots)

    @patch_dbus_publish_object
    def test_mount_existing_system_with_task(self, publisher):
        """Test MountExistingSystemWithTask."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        task_path = self.interface.MountExistingSystemWithTask("dev1", True)

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

        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._device.name, "dev1")
        self.assertEqual(obj.implementation._read_only, True)

    @patch_dbus_publish_object
    def test_find_devices_with_task(self, publisher):
        """Test FindDevicesWithTask."""
        task_path = self.interface.FindDevicesWithTask()

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

        self.assertEqual(obj.implementation._devicetree, self.module.storage.devicetree)

    def test_get_device_mount_options(self):
        """Test GetDeviceMountOptions."""
        dev1 = StorageDevice(
            "dev1",
            size=Size("10 GiB")
        )
        self._add_device(dev1)
        self.assertEqual(self.interface.GetDeviceMountOptions("dev1"), "")

        dev1.format = get_format("ext4")
        dev1.format.options = "defaults,ro"
        self.assertEqual(self.interface.GetDeviceMountOptions("dev1"), "defaults,ro")

    def test_set_device_mount_options(self):
        """Test SetDeviceMountOptions."""
        dev1 = StorageDevice(
            "dev1",
            size=Size("10 GiB")
        )
        self._add_device(dev1)

        self.interface.SetDeviceMountOptions("dev1", "auto")
        self.assertEqual(dev1.format.options, "auto")

        self.interface.SetDeviceMountOptions("dev1", "")
        self.assertEqual(dev1.format.options, None)

        dev1.format = get_format("ext4")
        dev1.format.options = "defaults,ro"
        self.interface.SetDeviceMountOptions("dev1", "")
        self.assertEqual(dev1.format.options, "defaults")
    def setUp(self):
        self.module = DeviceTreeModule()
        self.interface = DeviceTreeInterface(self.module)

        # Set the storage.
        self.module.on_storage_reset(create_storage())
class DeviceTreeInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the device tree handler."""
    def setUp(self):
        self.module = DeviceTreeModule()
        self.interface = DeviceTreeInterface(self.module)

        # Set the storage.
        self.module.on_storage_reset(create_storage())

    def publication_test(self):
        """Check the DBus representation."""
        self.assertIsInstance(self.module.for_publication(),
                              DeviceTreeInterface)

    @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 get_root_device_test(self):
        """Test GetRootDevice."""
        self.assertEqual(self.interface.GetRootDevice(), "")

        self._add_device(
            StorageDevice("dev1", fmt=get_format("ext4", mountpoint="/")))
        self._add_device(
            StorageDevice("dev2", fmt=get_format("ext4", mountpoint="/home")))

        self.assertEqual(self.interface.GetRootDevice(), "dev1")

    def get_devices_test(self):
        """Test GetDevices."""
        self.assertEqual(self.interface.GetDevices(), [])

        self._add_device(
            DiskDevice("dev1", fmt=get_format("ext4"), size=Size("10 GiB")))

        self._add_device(
            StorageDevice("dev2", fmt=get_format("ext4"), size=Size("10 GiB")))

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

    def get_disks_test(self):
        """Test GetDisks."""
        self.assertEqual(self.interface.GetDisks(), [])

        self._add_device(
            DiskDevice("dev1",
                       fmt=get_format("ext4"),
                       exists=True,
                       size=Size("10 GiB")))

        self.assertEqual(self.interface.GetDisks(), ["dev1"])

    def get_mount_points_test(self):
        """Test GetMountPoints."""
        self.assertEqual(self.interface.GetMountPoints(), {})

        self._add_device(
            StorageDevice("dev1", fmt=get_format("ext4", mountpoint="/")))
        self._add_device(
            StorageDevice("dev2", fmt=get_format("ext4", mountpoint="/home")))

        self.assertEqual(self.interface.GetMountPoints(), {
            "/": "dev1",
            "/home": "dev2"
        })

    def get_device_data_test(self):
        """Test GetDeviceData."""
        self._add_device(
            DiskDevice("dev1",
                       fmt=get_format("ext4"),
                       size=Size("10 MiB"),
                       serial="SERIAL_ID",
                       vendor="VENDOR_ID",
                       model="MODEL_ID",
                       bus="BUS_ID",
                       wwn="0x0000000000000000",
                       uuid="1234-56-7890"))

        self.assertEqual(
            self.interface.GetDeviceData("dev1"), {
                'type':
                get_variant(Str, 'disk'),
                'name':
                get_variant(Str, 'dev1'),
                'path':
                get_variant(Str, '/dev/dev1'),
                'size':
                get_variant(UInt64,
                            Size("10 MiB").get_bytes()),
                'is-disk':
                get_variant(Bool, True),
                'parents':
                get_variant(List[Str], []),
                'attrs':
                get_variant(
                    Dict[Str, Str], {
                        "serial": "SERIAL_ID",
                        "vendor": "VENDOR_ID",
                        "model": "MODEL_ID",
                        "bus": "BUS_ID",
                        "wwn": "0x0000000000000000",
                        "uuid": "1234-56-7890"
                    }),
                'description':
                get_variant(Str, "VENDOR_ID MODEL_ID 0x0000000000000000")
            })

    def get_unknown_device_data_test(self):
        """Test GetDeviceData for unknown."""
        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDeviceData("dev1")

    def get_dasd_device_data_test(self):
        """Test GetDeviceData for DASD."""
        self._add_device(
            DASDDevice("dev1",
                       fmt=get_format("ext4"),
                       size=Size("10 GiB"),
                       busid="0.0.0201",
                       opts={}))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'dasd'))
        self.assertEqual(data['attrs'],
                         get_variant(Dict[Str, Str], {"busid": "0.0.0201"}))

    def get_zfcp_device_data_test(self):
        """Test GetDeviceData for zFCP."""
        self._add_device(
            ZFCPDiskDevice("dev1",
                           fmt=get_format("ext4"),
                           size=Size("10 GiB"),
                           fcp_lun="0x5719000000000000",
                           wwpn="0x5005076300c18154",
                           hba_id="0.0.010a"))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'zfcp'))
        self.assertEqual(
            data['attrs'],
            get_variant(
                Dict[Str, Str], {
                    "fcp_lun": "0x5719000000000000",
                    "wwpn": "0x5005076300c18154",
                    "hba_id": "0.0.010a"
                }))

    def get_format_data_test(self):
        """Test GetFormatData."""
        fmt1 = get_format("ext4", uuid="1234-56-7890", label="LABEL")
        dev1 = StorageDevice("dev1", fmt=fmt1, size=Size("10 GiB"))

        self._add_device(dev1)

        self.assertEqual(
            self.interface.GetFormatData("dev1"), {
                'type':
                get_variant(Str, 'ext4'),
                'mountable':
                get_variant(Bool, True),
                'attrs':
                get_variant(Dict[Str, Str], {
                    "uuid": "1234-56-7890",
                    "label": "LABEL",
                }),
                'description':
                get_variant(Str, 'ext4'),
            })

        fmt2 = get_format("luks")
        dev2 = LUKSDevice("dev2",
                          parents=[dev1],
                          fmt=fmt2,
                          size=Size("10 GiB"))

        self._add_device(dev2)

        self.assertEqual(
            self.interface.GetFormatData("dev2"), {
                'type': get_variant(Str, 'luks'),
                'mountable': get_variant(Bool, False),
                'attrs': get_variant(Dict[Str, Str], {}),
                'description': get_variant(Str, 'LUKS'),
            })

    def get_format_type_data_test(self):
        """Test GetFormatTypeData."""
        self.assertEqual(
            self.interface.GetFormatTypeData("swap"), {
                'type': get_variant(Str, 'swap'),
                'mountable': get_variant(Bool, False),
                'attrs': get_variant(Dict[Str, Str], {}),
                'description': get_variant(Str, 'swap'),
            })

    def get_actions_test(self):
        """Test GetActions."""
        self.assertEqual(self.interface.GetActions(), [])

        self._add_device(
            DiskDevice(
                "dev1",
                fmt=get_format("ext4"),
                size=Size("10 MiB"),
            ))

        device = self.storage.devicetree.get_device_by_name("dev1")
        self.storage.destroy_device(device)

        self.assertEqual(self.interface.GetActions(),
                         [{
                             'action-type': get_variant(Str, 'destroy'),
                             'action-object': get_variant(Str, 'device'),
                             'device-name': get_variant(Str, 'dev1'),
                             'description': get_variant(Str, 'destroy device'),
                         }])

    def get_supported_file_systems_test(self):
        """Test GetSupportedFileSystems."""
        result = self.interface.GetSupportedFileSystems()
        self.assertIsInstance(result, list)
        self.assertNotEqual(len(result), 0)

        for fs in result:
            fmt = get_format(fs)
            self.assertIsInstance(fs, str)
            self.assertIsNotNone(fmt.type)

    def get_required_device_size_test(self):
        """Test GetRequiredDeviceSize."""
        required_size = self.interface.GetRequiredDeviceSize(
            Size("1 GiB").get_bytes())
        self.assertEqual(
            Size("1280 MiB").get_bytes(), required_size, Size(required_size))

    def get_file_system_free_space_test(self):
        """Test GetFileSystemFreeSpace."""
        self._add_device(
            StorageDevice("dev1",
                          fmt=get_format("ext4", mountpoint="/"),
                          size=Size("5 GiB")))

        self._add_device(
            StorageDevice("dev2",
                          fmt=get_format("ext4", mountpoint="/usr"),
                          size=Size("5 GiB")))

        total_size = self.interface.GetFileSystemFreeSpace([])
        self.assertEqual(total_size, 0)

        total_size = self.interface.GetFileSystemFreeSpace(["/", "/usr"])
        self.assertLess(total_size, Size("10 GiB").get_bytes())
        self.assertGreater(total_size, Size("8 GiB").get_bytes())

    def get_disk_free_space_test(self):
        """Test GetDiskFreeSpace."""
        self._add_device(DiskDevice("dev1", size=Size("5 GiB")))

        self._add_device(DiskDevice("dev2", size=Size("5 GiB")))

        total_size = self.interface.GetDiskFreeSpace([])
        self.assertEqual(total_size, 0)

        total_size = self.interface.GetDiskFreeSpace(["dev1", "dev2"])
        self.assertEqual(total_size, Size("10 GiB").get_bytes())

        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDiskFreeSpace(["dev1", "dev2", "dev3"])

    def get_disk_reclaimable_space_test(self):
        """Test GetDiskReclaimableSpace."""
        self._add_device(DiskDevice("dev1", size=Size("5 GiB")))

        self._add_device(DiskDevice("dev2", size=Size("5 GiB")))

        total_size = self.interface.GetDiskReclaimableSpace([])
        self.assertEqual(total_size, 0)

        # FIXME: Test on devices with a reclaimable space.
        total_size = self.interface.GetDiskReclaimableSpace(["dev1", "dev2"])
        self.assertEqual(total_size, 0)

        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDiskReclaimableSpace(["dev1", "dev2", "dev3"])

    def resolve_device_test(self):
        """Test ResolveDevice."""
        self._add_device(DiskDevice("dev1"))

        self.assertEqual(self.interface.ResolveDevice("dev0"), "")
        self.assertEqual(self.interface.ResolveDevice("dev1"), "dev1")
        self.assertEqual(self.interface.ResolveDevice("/dev/dev1"), "dev1")

    def get_device_ancestors_test(self):
        """Test GetDeviceAncestors."""
        dev1 = StorageDevice("dev1")
        self._add_device(dev1)

        dev2 = StorageDevice("dev2", parents=[dev1])
        self._add_device(dev2)

        dev3 = StorageDevice("dev3", parents=[dev2])
        self._add_device(dev3)

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

    @patch.object(StorageDevice, "setup")
    def setup_device_test(self, setup):
        """Test SetupDevice."""
        self._add_device(StorageDevice("dev1"))

        self.interface.SetupDevice("dev1")
        setup.assert_called_once()

    @patch.object(StorageDevice, "teardown")
    def teardown_device_test(self, teardown):
        """Test TeardownDevice."""
        self._add_device(StorageDevice("dev1"))

        self.interface.TeardownDevice("dev1")
        teardown.assert_called_once()

    @patch.object(FS, "mount")
    def mount_device_test(self, mount):
        """Test MountDevice."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.MountDevice("dev1", d)
            mount.assert_called_once_with(mountpoint=d)

    @patch.object(FS, "unmount")
    def unmount_device_test(self, unmount):
        """Test UnmountDevice."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.UnmountDevice("dev1", d)
            unmount.assert_called_once_with(mountpoint=d)

    def find_install_media_test(self):
        """Test FindInstallMedia."""
        self.assertEqual(self.interface.FindOpticalMedia(), [])

    @patch.object(FS, "update_size_info")
    def find_mountable_partitions_test(self, update_size_info):
        """Test FindMountablePartitions."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))
        self._add_device(
            PartitionDevice("dev2", fmt=get_format("ext4", exists=True)))

        self.assertEqual(self.interface.FindMountablePartitions(), ["dev2"])

    @patch("pyanaconda.storage.utils.try_populate_devicetree")
    @patch.object(LUKS, "setup")
    @patch.object(LUKSDevice, "teardown")
    @patch.object(LUKSDevice, "setup")
    def unlock_device_test(self, device_setup, device_teardown, format_setup,
                           populate):
        """Test UnlockDevice."""
        dev1 = StorageDevice("dev1",
                             fmt=get_format("ext4"),
                             size=Size("10 GiB"))
        self._add_device(dev1)

        dev2 = LUKSDevice("dev2",
                          parents=[dev1],
                          fmt=get_format("luks"),
                          size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.UnlockDevice("dev2", "passphrase"),
                         True)

        device_setup.assert_called_once()
        format_setup.assert_called_once()
        populate.assert_called_once()
        device_teardown.assert_not_called()
        self.assertTrue(dev2.format.has_key)

        device_setup.side_effect = StorageError("Fake error")
        self.assertEqual(self.interface.UnlockDevice("dev2", "passphrase"),
                         False)

        device_teardown.assert_called_once()
        self.assertFalse(dev2.format.has_key)

    def find_unconfigured_luks_test(self):
        """Test FindUnconfiguredLUKS."""
        self.assertEqual(self.interface.FindUnconfiguredLUKS(), [])

        dev1 = StorageDevice("dev1",
                             fmt=get_format("ext4"),
                             size=Size("10 GiB"))
        self._add_device(dev1)

        self.assertEqual(self.interface.FindUnconfiguredLUKS(), [])

        dev2 = LUKSDevice("dev2",
                          parents=[dev1],
                          fmt=get_format("luks"),
                          size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.FindUnconfiguredLUKS(), ["dev2"])

    def set_device_passphrase_test(self):
        """Test SetDevicePassphrase."""
        dev1 = StorageDevice("dev1",
                             fmt=get_format("ext4"),
                             size=Size("10 GiB"))
        self._add_device(dev1)

        dev2 = LUKSDevice("dev2",
                          parents=[dev1],
                          fmt=get_format("luks"),
                          size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.FindUnconfiguredLUKS(), ["dev2"])
        self.interface.SetDevicePassphrase("dev2", "123456")
        self.assertEqual(self.interface.FindUnconfiguredLUKS(), [])

    def get_fstab_spec_test(self):
        """Test GetFstabSpec."""
        self._add_device(
            StorageDevice("dev1", fmt=get_format("ext4", uuid="123")))
        self.assertEqual(self.interface.GetFstabSpec("dev1"), "UUID=123")

    def get_existing_systems_test(self):
        """Test GetExistingSystems."""
        self.assertEqual(self.interface.GetExistingSystems(), [])

        root_device = StorageDevice("dev1", fmt=get_format("ext4"))
        swap_device = StorageDevice("dev2", fmt=get_format("swap"))

        self.storage.roots = [
            Root(name="My Linux",
                 mounts={"/": root_device},
                 swaps=[swap_device])
        ]

        self.assertEqual(
            self.interface.GetExistingSystems(),
            [{
                'os-name': get_variant(Str, 'My Linux'),
                'mount-points': get_variant(Dict[Str, Str], {'/': 'dev1'}),
                'swap-devices': get_variant(List[Str], ['dev2'])
            }])

    @patch_dbus_publish_object
    def find_existing_systems_with_task_test(self, publisher):
        """Test FindExistingSystemsWithTask."""
        task_path = self.interface.FindExistingSystemsWithTask()

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

        self.assertEqual(obj.implementation._devicetree,
                         self.module.storage.devicetree)

        roots = [Root(name="My Linux")]
        obj.implementation._set_result(roots)
        obj.implementation.succeeded_signal.emit()
        self.assertEqual(self.storage.roots, roots)

    @patch_dbus_publish_object
    def mount_existing_system_with_task_test(self, publisher):
        """Test MountExistingSystemWithTask."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        task_path = self.interface.MountExistingSystemWithTask("dev1", True)

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

        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._device.name, "dev1")
        self.assertEqual(obj.implementation._read_only, True)

    @patch_dbus_publish_object
    def find_devices_with_task_test(self, publisher):
        """Test FindDevicesWithTask."""
        task_path = self.interface.FindDevicesWithTask()

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

        self.assertEqual(obj.implementation._devicetree,
                         self.module.storage.devicetree)
Beispiel #10
0
    def __init__(self):
        super().__init__()
        # Initialize Blivet.
        enable_installer_mode()

        # The storage model.
        self._storage = None
        self.storage_changed = Signal()
        self.storage_reset = Signal()

        # The created partitioning modules.
        self._created_partitioning = []
        self.created_partitioning_changed = Signal()

        # The applied partitioning module.
        self._applied_partitioning = None
        self.applied_partitioning_changed = Signal()

        # Initialize modules.
        self._modules = []

        self._storage_checker_module = StorageCheckerModule()
        self._add_module(self._storage_checker_module)

        self._device_tree_module = DeviceTreeModule()
        self._add_module(self._device_tree_module)

        self._disk_init_module = DiskInitializationModule()
        self._add_module(self._disk_init_module)

        self._disk_selection_module = DiskSelectionModule()
        self._add_module(self._disk_selection_module)

        self._snapshot_module = SnapshotModule()
        self._add_module(self._snapshot_module)

        self._bootloader_module = BootloaderModule()
        self._add_module(self._bootloader_module)

        self._fcoe_module = FCOEModule()
        self._add_module(self._fcoe_module)

        self._iscsi_module = ISCSIModule()
        self._add_module(self._iscsi_module)

        self._nvdimm_module = NVDIMMModule()
        self._add_module(self._nvdimm_module)

        self._dasd_module = None
        self._zfcp_module = None

        if arch.is_s390():
            self._dasd_module = DASDModule()
            self._add_module(self._dasd_module)

            self._zfcp_module = ZFCPModule()
            self._add_module(self._zfcp_module)

        # Initialize the partitioning modules.
        # TODO: Remove the static partitioning modules.
        self._add_module(self.create_partitioning(
            PartitioningMethod.AUTOMATIC))
        self._add_module(self.create_partitioning(PartitioningMethod.MANUAL))
        self._add_module(self.create_partitioning(PartitioningMethod.CUSTOM))
        self._add_module(
            self.create_partitioning(PartitioningMethod.INTERACTIVE))
        self._add_module(self.create_partitioning(PartitioningMethod.BLIVET))
        # Forget the static partitioning modules.
        # TODO: Remove with the static partitioning modules.
        self._created_partitioning = []

        # Connect modules to signals.
        self.storage_changed.connect(self._device_tree_module.on_storage_reset)
        self.storage_changed.connect(self._disk_init_module.on_storage_reset)
        self.storage_changed.connect(
            self._disk_selection_module.on_storage_reset)
        self.storage_changed.connect(self._snapshot_module.on_storage_reset)
        self.storage_changed.connect(self._bootloader_module.on_storage_reset)
        self._disk_selection_module.protected_devices_changed.connect(
            self.on_protected_devices_changed)
Beispiel #11
0
    def _create_device_tree(self):
        """Create the device tree module.

        :return: a device tree module
        """
        return DeviceTreeModule()
    def setUp(self):
        self.module = DeviceTreeModule()
        self.interface = DeviceTreeInterface(self.module)

        # Set the storage.
        self.module.on_storage_reset(create_storage())
Beispiel #13
0
class DeviceTreeInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the device tree handler."""

    def setUp(self):
        self.module = DeviceTreeModule()
        self.interface = DeviceTreeInterface(self.module)

        # Set the storage.
        self.module.on_storage_reset(create_storage())

    @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 get_root_device_test(self):
        """Test GetRootDevice."""
        self.assertEqual(self.interface.GetRootDevice(), "")

        self._add_device(StorageDevice("dev1", fmt=get_format("ext4", mountpoint="/")))
        self._add_device(StorageDevice("dev2", fmt=get_format("ext4", mountpoint="/home")))

        self.assertEqual(self.interface.GetRootDevice(), "dev1")

    def get_devices_test(self):
        """Test GetDevices."""
        self.assertEqual(self.interface.GetDevices(), [])

        self._add_device(DiskDevice("dev1", fmt=get_format("ext4")))
        self._add_device(StorageDevice("dev2", fmt=get_format("ext4")))

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

    def get_disks_test(self):
        """Test GetDisks."""
        self.assertEqual(self.interface.GetDisks(), [])

        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            exists=True,
            size=Size("10 GiB"))
        )

        self.assertEqual(self.interface.GetDisks(), ["dev1"])

    def get_mount_points_test(self):
        """Test GetMountPoints."""
        self.assertEqual(self.interface.GetMountPoints(), {})

        self._add_device(StorageDevice("dev1", fmt=get_format("ext4", mountpoint="/")))
        self._add_device(StorageDevice("dev2", fmt=get_format("ext4", mountpoint="/home")))

        self.assertEqual(self.interface.GetMountPoints(), {
            "/": "dev1",
            "/home": "dev2"
        })

    def get_device_data_test(self):
        """Test GetDeviceData."""
        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 MiB"),
            serial="SERIAL_ID",
            vendor="VENDOR_ID",
            model="MODEL_ID",
            bus="BUS_ID",
            wwn="0x0000000000000000",
            uuid="1234-56-7890"
        ))

        self.assertEqual(self.interface.GetDeviceData("dev1"), {
            'type': get_variant(Str, 'disk'),
            'name': get_variant(Str, 'dev1'),
            'path': get_variant(Str, '/dev/dev1'),
            'size': get_variant(UInt64, Size("10 MiB").get_bytes()),
            'is-disk': get_variant(Bool, True),
            'parents': get_variant(List[Str], []),
            'attrs': get_variant(Dict[Str, Str], {
                "serial": "SERIAL_ID",
                "vendor": "VENDOR_ID",
                "model": "MODEL_ID",
                "bus": "BUS_ID",
                "wwn": "0x0000000000000000",
                "uuid": "1234-56-7890"
            }),
            'description': get_variant(
                Str, "VENDOR_ID MODEL_ID 0x0000000000000000"
            )
        })

    def get_unknown_device_data_test(self):
        """Test GetDeviceData for unknown."""
        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDeviceData("dev1")

    def get_dasd_device_data_test(self):
        """Test GetDeviceData for DASD."""
        self._add_device(DASDDevice(
            "dev1",
            fmt=get_format("ext4"),
            busid="0.0.0201",
            opts={}
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'dasd'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {"busid": "0.0.0201"}))

    def get_zfcp_device_data_test(self):
        """Test GetDeviceData for zFCP."""
        self._add_device(ZFCPDiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            fcp_lun="0x5719000000000000",
            wwpn="0x5005076300c18154",
            hba_id="0.0.010a"
        ))

        data = self.interface.GetDeviceData("dev1")
        self.assertEqual(data['type'], get_variant(Str, 'zfcp'))
        self.assertEqual(data['attrs'], get_variant(Dict[Str, Str], {
            "fcp_lun": "0x5719000000000000",
            "wwpn": "0x5005076300c18154",
            "hba_id": "0.0.010a"
        }))

    def get_format_data_test(self):
        """Test GetFormatData."""
        fmt1 = get_format("ext4", uuid="1234-56-7890", label="LABEL")
        dev1 = StorageDevice("dev1", fmt=fmt1, size=Size("10 GiB"))

        self._add_device(dev1)

        self.assertEqual(self.interface.GetFormatData("dev1"), {
            'type': get_variant(Str, 'ext4'),
            'attrs': get_variant(Dict[Str, Str], {
                "uuid": "1234-56-7890",
                "label": "LABEL",
            }),
            'description': get_variant(Str, 'ext4'),
        })

        fmt2 = get_format("luks")
        dev2 = LUKSDevice("dev2", parents=[dev1], fmt=fmt2, size=Size("10 GiB"))

        self._add_device(dev2)

        self.assertEqual(self.interface.GetFormatData("dev2"), {
            'type': get_variant(Str, 'luks'),
            'attrs': get_variant(Dict[Str, Str], {}),
            'description': get_variant(Str, 'LUKS'),
        })

    def get_actions_test(self):
        """Test GetActions."""
        self.assertEqual(self.interface.GetActions(), [])

        self._add_device(DiskDevice(
            "dev1",
            fmt=get_format("ext4"),
            size=Size("10 MiB"),
        ))

        device = self.storage.devicetree.get_device_by_name("dev1")
        self.storage.destroy_device(device)

        self.assertEqual(self.interface.GetActions(), [{
            'action-type': get_variant(Str, 'destroy'),
            'action-object': get_variant(Str, 'device'),
            'device-name': get_variant(Str, 'dev1'),
            'description': get_variant(Str, 'destroy device'),
        }])

    def get_required_device_size_test(self):
        """Test GetRequiredDeviceSize."""
        required_size = self.interface.GetRequiredDeviceSize(Size("1 GiB").get_bytes())
        self.assertEqual(Size("1280 MiB").get_bytes(), required_size, Size(required_size))

    def get_file_system_free_space_test(self):
        """Test GetFileSystemFreeSpace."""
        self._add_device(StorageDevice(
            "dev1",
            fmt=get_format("ext4", mountpoint="/"),
            size=Size("5 GiB"))
        )

        self._add_device(StorageDevice(
            "dev2",
            fmt=get_format("ext4", mountpoint="/usr"),
            size=Size("5 GiB"))
        )

        total_size = self.interface.GetFileSystemFreeSpace([])
        self.assertEqual(total_size, 0)

        total_size = self.interface.GetFileSystemFreeSpace(["/", "/usr"])
        self.assertLess(total_size, Size("10 GiB").get_bytes())
        self.assertGreater(total_size, Size("8 GiB").get_bytes())

    def get_disk_free_space_test(self):
        """Test GetDiskFreeSpace."""
        self._add_device(DiskDevice(
            "dev1",
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev2",
            size=Size("5 GiB"))
        )

        total_size = self.interface.GetDiskFreeSpace([])
        self.assertEqual(total_size, 0)

        total_size = self.interface.GetDiskFreeSpace(["dev1", "dev2"])
        self.assertEqual(total_size, Size("10 GiB").get_bytes())

        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDiskFreeSpace(["dev1", "dev2", "dev3"])

    def get_disk_reclaimable_space_test(self):
        """Test GetDiskReclaimableSpace."""
        self._add_device(DiskDevice(
            "dev1",
            size=Size("5 GiB"))
        )

        self._add_device(DiskDevice(
            "dev2",
            size=Size("5 GiB"))
        )

        total_size = self.interface.GetDiskReclaimableSpace([])
        self.assertEqual(total_size, 0)

        # FIXME: Test on devices with a reclaimable space.
        total_size = self.interface.GetDiskReclaimableSpace(["dev1", "dev2"])
        self.assertEqual(total_size, 0)

        with self.assertRaises(UnknownDeviceError):
            self.interface.GetDiskReclaimableSpace(["dev1", "dev2", "dev3"])

    def resolve_device_test(self):
        """Test ResolveDevice."""
        self._add_device(DiskDevice("dev1"))

        self.assertEqual(self.interface.ResolveDevice("dev0"), "")
        self.assertEqual(self.interface.ResolveDevice("dev1"), "dev1")
        self.assertEqual(self.interface.ResolveDevice("/dev/dev1"), "dev1")

    @patch.object(StorageDevice, "setup")
    def setup_device_test(self, setup):
        """Test SetupDevice."""
        self._add_device(StorageDevice("dev1"))

        self.interface.SetupDevice("dev1")
        setup.assert_called_once()

    @patch.object(StorageDevice, "teardown")
    def teardown_device_test(self, teardown):
        """Test TeardownDevice."""
        self._add_device(StorageDevice("dev1"))

        self.interface.TeardownDevice("dev1")
        teardown.assert_called_once()

    @patch.object(FS, "mount")
    def mount_device_test(self, mount):
        """Test MountDevice."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.MountDevice("dev1", d)
            mount.assert_called_once_with(mountpoint=d)

    @patch.object(FS, "unmount")
    def unmount_device_test(self, unmount):
        """Test UnmountDevice."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4")))

        with tempfile.TemporaryDirectory() as d:
            self.interface.UnmountDevice("dev1", d)
            unmount.assert_called_once_with(mountpoint=d)

    def find_install_media_test(self):
        """Test FindInstallMedia."""
        self.assertEqual(self.interface.FindOpticalMedia(), [])

    @patch.object(FS, "update_size_info")
    def find_mountable_partitions_test(self, update_size_info):
        """Test FindMountablePartitions."""
        self._add_device(StorageDevice(
            "dev1",
            fmt=get_format("ext4"))
        )
        self._add_device(PartitionDevice(
            "dev2",
            fmt=get_format("ext4", exists=True)
        ))

        self.assertEqual(self.interface.FindMountablePartitions(), ["dev2"])

    @patch("pyanaconda.storage.utils.try_populate_devicetree")
    @patch.object(LUKS, "setup")
    @patch.object(LUKSDevice, "teardown")
    @patch.object(LUKSDevice, "setup")
    def unlock_device_test(self, device_setup, device_teardown, format_setup, populate):
        """Test UnlockDevice."""
        dev1 = StorageDevice("dev1", fmt=get_format("ext4"), size=Size("10 GiB"))
        self._add_device(dev1)

        dev2 = LUKSDevice("dev2", parents=[dev1], fmt=get_format("luks"), size=Size("10 GiB"))
        self._add_device(dev2)

        self.assertEqual(self.interface.UnlockDevice("dev2", "passphrase"), True)

        device_setup.assert_called_once()
        format_setup.assert_called_once()
        populate.assert_called_once()
        device_teardown.assert_not_called()
        self.assertTrue(dev2.format.has_key)

        device_setup.side_effect = StorageError("Fake error")
        self.assertEqual(self.interface.UnlockDevice("dev2", "passphrase"), False)

        device_teardown.assert_called_once()
        self.assertFalse(dev2.format.has_key)

    def get_fstab_spec_test(self):
        """Test GetFstabSpec."""
        self._add_device(StorageDevice("dev1", fmt=get_format("ext4", uuid="123")))
        self.assertEqual(self.interface.GetFstabSpec("dev1"), "UUID=123")