def _complete_device_factory_request(self, storage, request: DeviceFactoryRequest): """Complete the device factory request. :param storage: an instance of Blivet :param request: a device factory request """ # Set the defaults. if not request.luks_version: request.luks_version = storage.default_luks_version # Set the file system type for the given mount point. if not request.format_type: request.format_type = storage.get_fstype(request.mount_point) # Fix the mount point. if lowerASCII(request.mount_point) in ("swap", "biosboot", "prepboot"): request.mount_point = "" # We should create a partition in some cases. # These devices should never be encrypted. if (request.mount_point.startswith("/boot") or request.format_type in PARTITION_ONLY_FORMAT_TYPES): request.device_type = devicefactory.DEVICE_TYPE_PARTITION request.device_encrypted = False # We shouldn't create swap on a thinly provisioned volume. if (request.format_type == "swap" and request.device_type == devicefactory.DEVICE_TYPE_LVM_THINP): request.device_type = devicefactory.DEVICE_TYPE_LVM # Encryption of thinly provisioned volumes isn't supported. if request.device_type == devicefactory.DEVICE_TYPE_LVM_THINP: request.device_encrypted = False
def update_container_data_test(self): """Test UpdateContainerData.""" pv1 = StorageDevice( "pv1", size=Size("1025 MiB"), fmt=get_format("lvmpv") ) pv2 = StorageDevice( "pv2", size=Size("513 MiB"), fmt=get_format("lvmpv") ) vg = LVMVolumeGroupDevice( "testvg", parents=[pv1, pv2] ) self._add_device(pv1) self._add_device(pv2) self._add_device(vg) request = DeviceFactoryRequest() request.device_type = DEVICE_TYPE_PARTITION with self.assertRaises(StorageError): self.interface.UpdateContainerData( DeviceFactoryRequest.to_structure(request), "anaconda" ) request.device_type = DEVICE_TYPE_BTRFS request = DeviceFactoryRequest.from_structure( self.interface.UpdateContainerData( DeviceFactoryRequest.to_structure(request), "anaconda" ) ) self.assertEqual(request.container_spec, "") self.assertEqual(request.container_name, "anaconda") self.assertEqual(request.container_encrypted, False) self.assertEqual(request.container_raid_level, "single") self.assertEqual(request.container_size_policy, 0) self.assertEqual(request.disks, []) request.device_type = DEVICE_TYPE_LVM request = DeviceFactoryRequest.from_structure( self.interface.UpdateContainerData( DeviceFactoryRequest.to_structure(request), "testvg" ) ) self.assertEqual(request.container_spec, "testvg") self.assertEqual(request.container_name, "testvg") self.assertEqual(request.container_encrypted, False) self.assertEqual(request.container_raid_level, "") self.assertEqual(request.container_size_policy, Size("1.5 GiB").get_bytes()) self.assertEqual(request.disks, [])
def test_update_container_data(self): """Test UpdateContainerData.""" pv1 = StorageDevice("pv1", size=Size("1025 MiB"), fmt=get_format("lvmpv")) pv2 = StorageDevice("pv2", size=Size("513 MiB"), fmt=get_format("lvmpv")) vg = LVMVolumeGroupDevice("testvg", parents=[pv1, pv2]) self._add_device(pv1) self._add_device(pv2) self._add_device(vg) request = DeviceFactoryRequest() request.device_type = DEVICE_TYPE_PARTITION with pytest.raises(StorageError): self.interface.UpdateContainerData( DeviceFactoryRequest.to_structure(request), "anaconda") request.device_type = DEVICE_TYPE_BTRFS request = DeviceFactoryRequest.from_structure( self.interface.UpdateContainerData( DeviceFactoryRequest.to_structure(request), "anaconda")) assert request.container_spec == "" assert request.container_name == "anaconda" assert request.container_encrypted is False assert request.container_raid_level == "single" assert request.container_size_policy == 0 assert request.disks == [] request.device_type = DEVICE_TYPE_LVM request = DeviceFactoryRequest.from_structure( self.interface.UpdateContainerData( DeviceFactoryRequest.to_structure(request), "testvg")) assert request.container_spec == "testvg" assert request.container_name == "testvg" assert request.container_encrypted is False assert request.container_raid_level == "" assert request.container_size_policy == Size("1.5 GiB").get_bytes() assert request.disks == []
def generate_device_factory_request(storage, device) -> DeviceFactoryRequest: """Generate a device info for the given device. :param storage: an instance of Blivet :param device: a device :return: a device factory request """ device_type = devicefactory.get_device_type(device) if device_type is None: raise UnsupportedDeviceError("Unsupported type of {}.".format( device.name)) # Generate the device data. request = DeviceFactoryRequest() request.device_spec = device.name request.device_name = getattr(device.raw_device, "lvname", device.raw_device.name) request.device_size = device.size.get_bytes() request.device_type = device_type request.reformat = not device.format.exists request.format_type = device.format.type or "" request.device_encrypted = isinstance(device, LUKSDevice) request.luks_version = get_device_luks_version(device) request.label = getattr(device.format, "label", "") or "" request.mount_point = getattr(device.format, "mountpoint", "") or "" request.device_raid_level = get_device_raid_level_name(device) if hasattr(device, "req_disks") and not device.exists: disks = device.req_disks else: disks = device.disks request.disks = [d.name for d in disks] if request.device_type not in CONTAINER_DEVICE_TYPES: return request # Generate the container data. factory = devicefactory.get_device_factory(storage, device_type=device_type, device=device.raw_device) container = factory.get_container() if container: set_container_data(request, container) return request
def add_device_test(self): """Test AddDevice.""" self._add_device( DiskDevice("dev1", exists=True, size=Size("15 GiB"), fmt=get_format("disklabel"))) request = DeviceFactoryRequest() request.device_type = DEVICE_TYPE_LVM request.mount_point = "/home" request.size = Size("5 GiB") request.disks = ["dev1"] self.storage.factory_device = Mock() self.interface.AddDevice(DeviceFactoryRequest.to_structure(request)) self.storage.factory_device.assert_called_once()
def get_device_factory_arguments_test(self): """Test get_device_factory_arguments.""" dev1 = StorageDevice("dev1") self._add_device(dev1) dev2 = StorageDevice("dev2") self._add_device(dev2) dev3 = StorageDevice("dev3") self._add_device(dev3) request = DeviceFactoryRequest() request.device_spec = "dev3" request.disks = ["dev1", "dev2"] request.device_name = "dev3" request.device_type = devicefactory.DEVICE_TYPE_LVM_THINP request.device_size = Size("10 GiB").get_bytes() request.mount_point = "/" request.format_type = "xfs" request.label = "root" request.device_encrypted = True request.luks_version = "luks1" request.device_raid_level = "raid1" self.assertEqual(utils.get_device_factory_arguments(self.storage, request), { "device": dev3, "disks": [dev1, dev2], "device_type": devicefactory.DEVICE_TYPE_LVM_THINP, "device_name": "dev3", "size": Size("10 GiB"), "mountpoint": "/", "fstype": "xfs", "label": "root", "encrypted": True, "luks_version": "luks1", "raid_level": raid.RAID1, "container_name": None, "container_size": devicefactory.SIZE_POLICY_AUTO, "container_raid_level": None, "container_encrypted": False }) request = DeviceFactoryRequest() request.device_spec = "dev3" request.disks = ["dev1", "dev2"] request.device_name = "dev3" request.container_name = "container1" request.container_size_policy = Size("10 GiB").get_bytes() request.container_encrypted = True request.container_raid_level = "raid1" self.assertEqual(utils.get_device_factory_arguments(self.storage, request), { "device": dev3, "disks": [dev1, dev2], "device_type": devicefactory.DEVICE_TYPE_LVM, "device_name": "dev3", "size": None, "mountpoint": None, "fstype": None, "label": None, "encrypted": False, "luks_version": None, "raid_level": None, "container_name": "container1", "container_size": Size("10 GiB"), "container_raid_level": raid.RAID1, "container_encrypted": True })
def generate_container_data_test(self, proxy_getter): """Test GenerateContainerData.""" network_proxy = Mock() network_proxy.Hostname = "localhost" network_proxy.GetCurrentHostname.return_value = "localhost" proxy_getter.return_value = network_proxy pv1 = StorageDevice("pv1", size=Size("1025 MiB"), fmt=get_format("lvmpv")) pv2 = StorageDevice("pv2", size=Size("513 MiB"), fmt=get_format("lvmpv")) vg = LVMVolumeGroupDevice("testvg", parents=[pv1, pv2]) lv = LVMLogicalVolumeDevice("testlv", size=Size("512 MiB"), parents=[vg], fmt=get_format("xfs"), exists=False, seg_type="raid1", pvs=[pv1, pv2]) self._add_device(pv1) self._add_device(pv2) self._add_device(vg) self._add_device(lv) request = DeviceFactoryRequest() request.device_spec = lv.name request.device_type = DEVICE_TYPE_LVM request = DeviceFactoryRequest.from_structure( self.interface.GenerateContainerData( DeviceFactoryRequest.to_structure(request))) self.assertEqual(request.container_spec, "testvg") self.assertEqual(request.container_name, "testvg") self.assertEqual(request.container_encrypted, False) self.assertEqual(request.container_raid_level, "") self.assertEqual(request.container_size_policy, Size("1.5 GiB").get_bytes()) request.device_type = DEVICE_TYPE_BTRFS request = DeviceFactoryRequest.from_structure( self.interface.GenerateContainerData( DeviceFactoryRequest.to_structure(request))) self.assertEqual(request.container_spec, "") self.assertEqual(request.container_name, "anaconda") self.assertEqual(request.container_encrypted, False) self.assertEqual(request.container_raid_level, "single") self.assertEqual(request.container_size_policy, 0) request.device_type = DEVICE_TYPE_PARTITION request = DeviceFactoryRequest.from_structure( self.interface.GenerateContainerData( DeviceFactoryRequest.to_structure(request))) self.assertEqual(request.container_spec, "") self.assertEqual(request.container_name, "") self.assertEqual(request.container_encrypted, False) self.assertEqual(request.container_raid_level, "") self.assertEqual(request.container_size_policy, 0)
def generate_request_description_test(self): """Test generate_request_description.""" request = DeviceFactoryRequest() request.device_spec = "dev3" request.disks = ["dev1", "dev2"] request.device_name = "dev3" request.device_type = devicefactory.DEVICE_TYPE_LVM_THINP request.device_size = Size("10 GiB").get_bytes() request.mount_point = "/" request.format_type = "xfs" request.label = "root" request.device_encrypted = True request.luks_version = "luks1" request.device_raid_level = "raid1" expected = dedent(""" { container-encrypted = False container-name = '' container-raid-level = '' container-size-policy = 0 container-spec = '' device-encrypted = True device-name = 'dev3' device-raid-level = 'raid1' device-size = 10737418240 device-spec = 'dev3' device-type = 5 disks = ['dev1', 'dev2'] format-type = 'xfs' label = 'root' luks-version = 'luks1' mount-point = '/' reformat = False } """).strip() self.assertEqual(generate_request_description(request), expected) original = copy.deepcopy(request) self.assertEqual(generate_request_description(request, original), expected) request.device_name = "dev4" request.disks = ["dev1"] request.device_encrypted = False expected = dedent(""" { container-encrypted = False container-name = '' container-raid-level = '' container-size-policy = 0 container-spec = '' device-encrypted = True -> False device-name = 'dev3' -> 'dev4' device-raid-level = 'raid1' device-size = 10737418240 device-spec = 'dev3' device-type = 5 disks = ['dev1', 'dev2'] -> ['dev1'] format-type = 'xfs' label = 'root' luks-version = 'luks1' mount-point = '/' reformat = False } """).strip() self.assertEqual(generate_request_description(request, original), expected)