def test_bootloader_in_kickstart(self): ''' test that a bootloader such as prepboot/biosboot shows up in the kickstart data ''' with patch('blivet.osinstall.InstallerStorage.bootloader_device', new_callable=PropertyMock) as mock_bootloader_device: with patch('blivet.osinstall.InstallerStorage.mountpoints', new_callable=PropertyMock) as mock_mountpoints: # set up prepboot partition bootloader_device_obj = PartitionDevice("test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = formats.get_format("prepboot") blivet_obj = InstallerStorage() # mountpoints must exist for update_ksdata to run mock_bootloader_device.return_value = bootloader_device_obj mock_mountpoints.values.return_value = [] # initialize ksdata test_ksdata = returnClassForVersion()() blivet_obj.ksdata = test_ksdata blivet_obj.update_ksdata() self.assertTrue("part prepboot" in str(blivet_obj.ksdata))
def test_bootloader_in_kickstart(self): ''' test that a bootloader such as prepboot/biosboot shows up in the kickstart data ''' with patch('blivet.osinstall.InstallerStorage.bootloader_device', new_callable=PropertyMock) as mock_bootloader_device: with patch('blivet.osinstall.InstallerStorage.mountpoints', new_callable=PropertyMock) as mock_mountpoints: # set up prepboot partition bootloader_device_obj = PartitionDevice( "test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = formats.get_format("prepboot") blivet_obj = InstallerStorage() # mountpoints must exist for update_ksdata to run mock_bootloader_device.return_value = bootloader_device_obj mock_mountpoints.values.return_value = [] # initialize ksdata test_ksdata = returnClassForVersion()() blivet_obj.ksdata = test_ksdata blivet_obj.update_ksdata() self.assertTrue("part prepboot" in str(blivet_obj.ksdata))
def test_generate_device_factory_permissions(self): """Test GenerateDeviceFactoryPermissions.""" dev1 = DiskDevice("dev1", fmt=get_format("disklabel"), size=Size("10 GiB"), exists=True) dev2 = PartitionDevice("dev2", size=Size("5 GiB"), parents=[dev1], fmt=get_format("ext4", mountpoint="/", label="root")) self._add_device(dev1) self._add_device(dev2) request = self.interface.GenerateDeviceFactoryRequest("dev1") permissions = self.interface.GenerateDeviceFactoryPermissions(request) assert get_native(permissions) == { 'mount-point': False, 'reformat': False, 'format-type': True, 'label': False, 'device-type': False, 'device-name': False, 'device-size': False, 'device-encrypted': True, 'device-raid-level': False, 'disks': False, 'container-spec': False, 'container-name': False, 'container-size-policy': False, 'container-encrypted': False, 'container-raid-level': False, } request = self.interface.GenerateDeviceFactoryRequest("dev2") permissions = self.interface.GenerateDeviceFactoryPermissions(request) assert get_native(permissions) == { 'mount-point': True, 'reformat': False, 'format-type': True, 'label': True, 'device-type': True, 'device-name': False, 'device-size': True, 'device-encrypted': True, 'device-raid-level': True, 'disks': True, 'container-spec': False, 'container-name': False, 'container-size-policy': False, 'container-encrypted': False, 'container-raid-level': False, } dev2.protected = True permissions = self.interface.GenerateDeviceFactoryPermissions(request) for value in get_native(permissions).values(): assert value is False
def test_msdos_disk_chunk1(self): disk_size = Size("100 MiB") with sparsetmpfile("chunktest", disk_size) as disk_file: disk = DiskFile(disk_file) disk.format = get_format("disklabel", device=disk.path, exists=False, label_type="msdos") p1 = PartitionDevice("p1", size=Size("10 MiB"), grow=True) p2 = PartitionDevice("p2", size=Size("30 MiB"), grow=True) disks = [disk] partitions = [p1, p2] free = get_free_regions([disk]) self.assertEqual(len(free), 1, "free region count %d not expected" % len(free)) b = Mock(spec=Blivet) allocate_partitions(b, disks, partitions, free) requests = [PartitionRequest(p) for p in partitions] chunk = DiskChunk(free[0], requests=requests) # parted reports a first free sector of 32 for msdos on disk files. whatever. # XXX on gpt, the start is increased to 34 and the end is reduced from 204799 to 204766, # yielding an expected length of 204733 length_expected = 204768 self.assertEqual(chunk.length, length_expected) base_expected = sum(p.parted_partition.geometry.length for p in partitions) self.assertEqual(chunk.base, base_expected) pool_expected = chunk.length - base_expected self.assertEqual(chunk.pool, pool_expected) self.assertEqual(chunk.done, False) self.assertEqual(chunk.remaining, 2) chunk.grow_requests() self.assertEqual(chunk.done, True) self.assertEqual(chunk.pool, 0) self.assertEqual(chunk.remaining, 2) # # validate the growth (everything in sectors) # # The chunk length is 204768. The base of p1 is 20480. The base of # p2 is 61440. The chunk has a base of 81920 and a pool of 122848. # # p1 should grow by 30712 while p2 grows by 92136 since p2's base # size is exactly three times that of p1. self.assertEqual(requests[0].growth, 30712) self.assertEqual(requests[1].growth, 92136)
def setUp(self): """Create some device objects to test with. This sets up two disks (sda, sdb). The first partition of each is a biosboot partition. The second partitions comprise a RAID1 array formatted as /boot. sda additionally contains a third partition formatted as ext4. """ super(GRUBRaidSimpleTest, self).setUp() # Make some disks self.sda = DiskDevice(name="sda", size=Size("100 GiB")) self.sda.format = getFormat("disklabel") self.sdb = DiskDevice(name="sdb", size=Size("100 GiB")) self.sdb.format = getFormat("disklabel") # Set up biosboot partitions and an array for /boot on sda + sdb. # Start with the partitions self.sda1 = PartitionDevice(name="sda1", parents=[self.sda], size=Size("1 MiB")) self.sda1.format = getFormat("biosboot") self.sda2 = PartitionDevice(name="sda2", parents=[self.sda], size=Size("500 MiB")) self.sda2.format = getFormat("mdmember") self.sdb1 = PartitionDevice(name="sdb1", parents=[self.sdb], size=Size("1 MiB")) self.sdb1.format = getFormat("biosboot") self.sdb2 = PartitionDevice(name="sdb2", parents=[self.sdb], size=Size("500 MiB")) self.sdb2.format = getFormat("mdmember") # Add an extra partition for /boot on not-RAID self.sda3 = PartitionDevice(name="sda3", parents=[self.sda], size=Size("500 MiB")) self.sda3.format = getFormat("ext4", mountpoint="/boot") # Pretend that the partitions are real with real parent disks for part in (self.sda1, self.sda2, self.sdb1, self.sdb2): part.parents = part.req_disks self.boot_md = MDRaidArrayDevice(name="md1", parents=[self.sda2, self.sdb2], level=1) self.boot_md.format = getFormat("ext4", mountpoint="/boot") self.grub = GRUB()
def test_weight_2(self): for spec in weighted: part = PartitionDevice('weight_test') part._format = Mock(name="fmt", type=spec.fstype, mountpoint=spec.mountpoint, mountable=spec.mountpoint is not None) with patch('blivet.devices.partition.arch') as _arch: for func in arch_funcs: f = getattr(_arch, func) f.return_value = func in spec.true_funcs self.assertEqual(part.weight, spec.weight)
def setUp(self): disk1 = DiskDevice("testdisk", size=Size("300 GiB"), exists=True, fmt=get_format("disklabel", exists=True)) disk1.format._supported = False with self.assertLogs("blivet", level="INFO") as cm: partition1 = PartitionDevice("testpart1", size=Size("150 GiB"), exists=True, parents=[disk1], fmt=get_format("ext4", exists=True)) self.assertTrue("disklabel is unsupported" in "\n".join(cm.output)) with self.assertLogs("blivet", level="INFO") as cm: partition2 = PartitionDevice("testpart2", size=Size("100 GiB"), exists=True, parents=[disk1], fmt=get_format("lvmpv", exists=True)) self.assertTrue("disklabel is unsupported" in "\n".join(cm.output)) # To be supported, all of a devices ancestors must be supported. disk2 = DiskDevice("testdisk2", size=Size("300 GiB"), exists=True, fmt=get_format("lvmpv", exists=True)) vg = LVMVolumeGroupDevice("testvg", exists=True, parents=[partition2, disk2]) lv = LVMLogicalVolumeDevice("testlv", exists=True, size=Size("64 GiB"), parents=[vg], fmt=get_format("ext4", exists=True)) with sparsetmpfile("addparttest", Size("50 MiB")) as disk_file: disk3 = DiskFile(disk_file) disk3.format = get_format("disklabel", device=disk3.path, exists=False) self.disk1 = disk1 self.disk2 = disk2 self.disk3 = disk3 self.partition1 = partition1 self.partition2 = partition2 self.vg = vg self.lv = lv
def _create_partition(self, disk, size): disk.format = get_format("disklabel", device=disk.path, label_type="msdos") disk.format.create() pstart = disk.format.alignment.grainSize pend = pstart + int(Size(size) / disk.format.parted_device.sectorSize) disk.format.add_partition(pstart, pend, parted.PARTITION_NORMAL) disk.format.parted_disk.commit() part = disk.format.parted_disk.getPartitionBySector(pstart) device = PartitionDevice(os.path.basename(part.path)) device.disk = disk device.exists = True device.parted_partition = part return device
def change_device_test(self): """Test ChangeDevice.""" dev1 = DiskDevice("dev1") dev2 = PartitionDevice("dev2", size=Size("5 GiB"), parents=[dev1], fmt=get_format("ext4", mountpoint="/", label="root")) self._add_device(dev1) self._add_device(dev2) original_request = self.module.generate_device_factory_request("dev2") request = copy.deepcopy(original_request) request.device_type = DEVICE_TYPE_LVM request.mount_point = "/home" request.size = Size("4 GiB") request.label = "home" self.storage.factory_device = Mock() self.interface.ChangeDevice( DeviceFactoryRequest.to_structure(request), DeviceFactoryRequest.to_structure(original_request)) self.storage.factory_device.assert_called_once()
def validate_device_factory_request_test(self): """Test ValidateDeviceFactoryRequest.""" dev1 = DiskDevice("dev1") dev2 = DiskDevice("dev2") dev3 = PartitionDevice("dev3", size=Size("10 GiB"), parents=[dev1]) self._add_device(dev1) self._add_device(dev2) self._add_device(dev3) request = self.module.generate_device_factory_request("dev3") request.device_type = DEVICE_TYPE_LVM request.disks = ["dev1", "dev2"] request.format_type = "ext4" request.mount_point = "/boot" request.label = "root" request.reformat = True request.luks_version = "luks1" request.device_size = Size("5 GiB").get_bytes() request.device_encrypted = True request.device_raid_level = "raid1" result = self.interface.ValidateDeviceFactoryRequest( DeviceFactoryRequest.to_structure(request)) self._check_report(result, "/boot cannot be encrypted") request.mount_point = "/" result = self.interface.ValidateDeviceFactoryRequest( DeviceFactoryRequest.to_structure(request)) self._check_report(result, None)
def test_get_helper(self, *args): if self.helper_class is None: return partition = PartitionDevice("testpartitiondev") data = dict() fmt_class = get_device_format_class(self.helper_class._type_specifier) if fmt_class is None: self.skipTest("failed to look up format class for %s" % self.helper_class._type_specifier) data["ID_FS_TYPE"] = self.helper_class._base_type_specifier data["DEVTYPE"] = "partition" partition._bootable = self.helper_class._bootable partition._size = fmt_class._min_size self.assertEqual(get_format_helper(data, partition), self.helper_class)
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"])
def test_ctor_parted_partition_error_handling(self): disk = StorageDevice("testdisk", exists=True) disk._partitionable = True with patch.object(disk, "_format") as fmt: fmt.type = "disklabel" self.assertTrue(disk.partitioned) fmt.supported = True # Normal case, no exn. device = PartitionDevice("testpart1", exists=True, parents=[disk]) self.assertIn(device, disk.children) device.parents.remove(disk) self.assertEqual(len(disk.children), 0, msg="disk has children when it should not") # Parted doesn't find a partition, exn is raised. fmt.parted_disk.getPartitionByPath.return_value = None self.assertRaises(DeviceError, PartitionDevice, "testpart1", exists=True, parents=[disk]) self.assertEqual( len(disk.children), 0, msg="device is still attached to disk in spite of ctor error")
def generate_device_factory_request_test(self): """Test GenerateDeviceFactoryRequest.""" dev1 = DiskDevice("dev1") dev2 = PartitionDevice("dev2", size=Size("5 GiB"), parents=[dev1], fmt=get_format("ext4", mountpoint="/", label="root")) self._add_device(dev1) self._add_device(dev2) request = self.interface.GenerateDeviceFactoryRequest("dev2") self.assertEqual( get_native(request), { 'device-spec': 'dev2', 'disks': ['dev1'], 'mount-point': '/', 'reformat': True, 'format-type': 'ext4', 'label': 'root', 'luks-version': '', 'device-type': DEVICE_TYPE_PARTITION, 'device-name': 'dev2', 'device-size': Size("5 GiB").get_bytes(), 'device-encrypted': False, 'device-raid-level': '', 'container-name': '', 'container-size-policy': SIZE_POLICY_AUTO, 'container-encrypted': False, 'container-raid-level': '', })
def test_biosboot_bootloader_in_kickstart(self, mock_mountpoints, mock_devices, dbus): """Test that a biosboot bootloader shows up in the ks data.""" # set up biosboot partition biosboot_device_obj = PartitionDevice("biosboot_partition_device") biosboot_device_obj.size = Size('1MiB') biosboot_device_obj.format = formats.get_format("biosboot") # mountpoints must exist for updateKSData to run mock_devices.return_value = [biosboot_device_obj] mock_mountpoints.values.return_value = [] # initialize ksdata biosboot_blivet_obj = InstallerStorage(makeVersion()) biosboot_blivet_obj.update_ksdata() self.assertIn("part biosboot", str(biosboot_blivet_obj.ksdata))
def test_prepboot_bootloader_in_kickstart(self, mock_mountpoints, mock_bootloader_device, dbus): """Test that a prepboot bootloader shows up in the ks data.""" # set up prepboot partition bootloader_device_obj = PartitionDevice("test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = formats.get_format("prepboot") # mountpoints must exist for update_ksdata to run mock_bootloader_device.return_value = bootloader_device_obj mock_mountpoints.values.return_value = [] # initialize ksdata prepboot_blivet_obj = InstallerStorage(makeVersion()) prepboot_blivet_obj.update_ksdata() self.assertIn("part prepboot", str(prepboot_blivet_obj.ksdata))
def test_biosboot_bootloader_in_kickstart(self, mock_mountpoints, mock_devices, dbus): """Test that a biosboot bootloader shows up in the ks data.""" # set up biosboot partition biosboot_device_obj = PartitionDevice("biosboot_partition_device") biosboot_device_obj.size = Size('1MiB') biosboot_device_obj.format = formats.get_format("biosboot") # mountpoints must exist for updateKSData to run mock_devices.return_value = [biosboot_device_obj] mock_mountpoints.values.return_value = [] # initialize ksdata ksdata = makeVersion() biosboot_blivet_obj = InstallerStorage() update_storage_ksdata(biosboot_blivet_obj, ksdata) self.assertIn("part biosboot", str(ksdata))
def test_bootloader_in_kickstart(self): ''' test that a bootloader such as prepboot/biosboot shows up in the kickstart data ''' # prepboot test case with patch( 'pyanaconda.storage.osinstall.InstallerStorage.bootloader_device', new_callable=PropertyMock) as mock_bootloader_device: with patch( 'pyanaconda.storage.osinstall.InstallerStorage.mountpoints', new_callable=PropertyMock) as mock_mountpoints: # set up prepboot partition bootloader_device_obj = PartitionDevice( "test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = formats.get_format("prepboot") prepboot_blivet_obj = InstallerStorage() # mountpoints must exist for update_ksdata to run mock_bootloader_device.return_value = bootloader_device_obj mock_mountpoints.values.return_value = [] # initialize ksdata prepboot_ksdata = returnClassForVersion()() prepboot_blivet_obj.ksdata = prepboot_ksdata prepboot_blivet_obj.update_ksdata() self.assertIn("part prepboot", str(prepboot_blivet_obj.ksdata)) # biosboot test case with patch('pyanaconda.storage.osinstall.InstallerStorage.devices', new_callable=PropertyMock) as mock_devices: with patch( 'pyanaconda.storage.osinstall.InstallerStorage.mountpoints', new_callable=PropertyMock) as mock_mountpoints: # set up biosboot partition biosboot_device_obj = PartitionDevice( "biosboot_partition_device") biosboot_device_obj.size = Size('1MiB') biosboot_device_obj.format = formats.get_format("biosboot") biosboot_blivet_obj = InstallerStorage() # mountpoints must exist for updateKSData to run mock_devices.return_value = [biosboot_device_obj] mock_mountpoints.values.return_value = [] # initialize ksdata biosboot_ksdata = returnClassForVersion()() biosboot_blivet_obj.ksdata = biosboot_ksdata biosboot_blivet_obj.update_ksdata() self.assertIn("part biosboot", str(biosboot_blivet_obj.ksdata))
def test_biosboot_bootloader_in_kickstart(self, mock_mountpoints, mock_devices, dbus): """Test that a biosboot bootloader shows up in the ks data.""" # set up biosboot partition biosboot_device_obj = PartitionDevice("biosboot_partition_device") biosboot_device_obj.size = Size('1MiB') biosboot_device_obj.format = get_format("biosboot") # mountpoints must exist for updateKSData to run mock_devices.return_value = [biosboot_device_obj] mock_mountpoints.values.return_value = [] # set up the storage self.module.on_storage_changed(create_storage()) self.assertTrue(self.module.storage) # initialize ksdata ksdata = self._setup_kickstart() self.assertIn("part biosboot", str(ksdata))
def test_disk_is_empty(self): disk = StorageDevice("testdisk", exists=True) disk._partitionable = True with patch.object(disk, "_format") as fmt: fmt.type = "disklabel" self.assertTrue(disk.is_empty) PartitionDevice("testpart1", exists=True, parents=[disk]) self.assertFalse(disk.is_empty)
def setUp(self): dev1 = DiskDevice("name", fmt=getFormat("mdmember")) dev2 = DiskDevice("other") self.part = PartitionDevice("part", fmt=getFormat("mdmember"), parents=[dev2]) self.dev = MDRaidArrayDevice("dev", level="raid1", parents=[dev1, self.part], fmt=getFormat("luks")) self.luks = LUKSDevice("luks", parents=[self.dev], fmt=getFormat("ext4")) self.mdraid_method = availability.BLOCKDEV_MDRAID_PLUGIN._method self.dm_method = availability.BLOCKDEV_DM_PLUGIN._method self.cache_availability = availability.CACHE_AVAILABILITY
def test_prepboot_bootloader_in_kickstart(self, mock_mountpoints, dbus): """Test that a prepboot bootloader shows up in the ks data.""" # set up prepboot partition bootloader_device_obj = PartitionDevice("test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = get_format("prepboot") # mountpoints must exist for update_ksdata to run mock_mountpoints.values.return_value = [] # set up the storage self.module.on_storage_changed(create_storage()) assert self.module.storage self.module.storage.bootloader.stage1_device = bootloader_device_obj # initialize ksdata ksdata = self._setup_kickstart() assert "part prepboot" in str(ksdata)
def test_prepboot_bootloader_in_kickstart(self, mock_mountpoints, dbus): """Test that a prepboot bootloader shows up in the ks data.""" # disable other partitioning modules dbus.return_value.Enabled = False # set up prepboot partition bootloader_device_obj = PartitionDevice("test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = formats.get_format("prepboot") # mountpoints must exist for update_ksdata to run mock_mountpoints.values.return_value = [] # set up the storage prepboot_blivet_obj = InstallerStorage() prepboot_blivet_obj.bootloader.stage1_device = bootloader_device_obj # initialize ksdata ksdata = makeVersion() update_storage_ksdata(prepboot_blivet_obj, ksdata) self.assertIn("part prepboot", str(ksdata))
def test_match(self): """Test boot format populator helper match method""" if self.helper_class is None: return partition = PartitionDevice("testpartitiondev") storagedev = StorageDevice("teststoragedev") storagedev.bootable = True data = dict() fmt_class = get_device_format_class(self.helper_class._type_specifier) if fmt_class is None: self.skipTest("failed to look up format class for %s" % self.helper_class._type_specifier) data["ID_FS_TYPE"] = self.helper_class._base_type_specifier partition._bootable = self.helper_class._bootable min_size = fmt_class._min_size max_size = fmt_class._max_size partition._size = min_size storagedev._size = min_size self.assertTrue(self.helper_class.match(data, partition)) # These are only valid for partitions. self.assertFalse(self.helper_class.match(data, storagedev)) data["ID_FS_TYPE"] += "x" self.assertFalse(self.helper_class.match(data, partition)) data["ID_FS_TYPE"] = self.helper_class._base_type_specifier if self.helper_class._bootable: partition._bootable = False self.assertFalse(self.helper_class.match(data, partition)) partition._bootable = True if max_size: partition._size = max_size + 1 elif min_size: partition._size = min_size - 1 self.assertFalse(self.helper_class.match(data, partition)) partition._size = min_size
def _add_lvmvg_parent(self, container, parent): """ Add new parent to existing lvmg :param container: existing lvmvg :type container: class blivet.LVMVolumeGroupDevice :param parent: new parent -- existing device or free space :type parent: class blivet.Device or class blivetgui.utils.FreeSpaceDevice """ assert container.type == "lvmvg" actions = [] if parent.type == "free space": dev = PartitionDevice(name="req%d" % self.storage.nextID, size=parent.size, parents=parent.parents) ac_part = blivet.deviceaction.ActionCreateDevice(dev) fmt = blivet.formats.getFormat(fmt_type="lvmpv") ac_fmt = blivet.deviceaction.ActionCreateFormat(dev, fmt) actions.extend([ac_part, ac_fmt]) for ac in (ac_part, ac_fmt): self.storage.devicetree.registerAction(ac) blivet.partitioning.doPartitioning(self.storage) parent = dev try: ac_add = blivet.deviceaction.ActionAddMember(container, parent) self.storage.devicetree.registerAction(ac_add) actions.append(ac_add) except Exception as e: # pylint: disable=broad-except return ReturnList(success=False, actions=None, message=None, exception=e, traceback=sys.exc_info()[2]) return ReturnList(success=True, actions=actions, message=None, exception=None, traceback=None)
def test_extended_min_size(self): with sparsetmpfile("extendedtest", Size("10 MiB")) as disk_file: disk = DiskFile(disk_file) disk.format = get_format("disklabel", device=disk.path, label_type="msdos") grain_size = Size(disk.format.alignment.grainSize) sector_size = Size(disk.format.parted_device.sectorSize) extended_start = int(grain_size) extended_end = extended_start + int(Size("6 MiB") / sector_size) disk.format.add_partition(extended_start, extended_end, parted.PARTITION_EXTENDED) extended = disk.format.extended_partition self.assertNotEqual(extended, None) extended_device = PartitionDevice(os.path.basename(extended.path)) extended_device.disk = disk extended_device.exists = True extended_device.parted_partition = extended # no logical partitions --> min size should be max of 1 KiB and grain_size self.assertEqual(extended_device.min_size, extended_device.align_target_size(max(grain_size, Size("1 KiB")))) logical_start = extended_start + 1 logical_end = extended_end // 2 disk.format.add_partition(logical_start, logical_end, parted.PARTITION_LOGICAL) logical = disk.format.parted_disk.getPartitionBySector(logical_start) self.assertNotEqual(logical, None) logical_device = PartitionDevice(os.path.basename(logical.path)) logical_device.disk = disk logical_device.exists = True logical_device.parted_partition = logical # logical partition present --> min size should be based on its end sector end_free = (extended_end - logical_end) * sector_size self.assertEqual(extended_device.min_size, extended_device.align_target_size(extended_device.current_size - end_free))
def test_get_device_partitions(self): """Test GetDevicePartitions.""" self.module.on_storage_changed(create_storage()) dev1 = DiskDevice("dev1") self._add_device(dev1) dev2 = DiskDevice("dev2", fmt=get_format("disklabel")) self._add_device(dev2) dev3 = PartitionDevice("dev3") dev2.add_child(dev3) self._add_device(dev3) self.assertEqual(self.interface.GetDevicePartitions("dev1"), []) self.assertEqual(self.interface.GetDevicePartitions("dev2"), ["dev3"]) self.assertEqual(self.interface.GetDevicePartitions("dev3"), [])
def generate_device_factory_permissions_test(self): """Test GenerateDeviceFactoryPermissions.""" dev1 = DiskDevice("dev1", fmt=get_format("disklabel"), size=Size("10 GiB"), exists=True) dev2 = PartitionDevice("dev2", size=Size("5 GiB"), parents=[dev1], fmt=get_format("ext4", mountpoint="/", label="root")) self._add_device(dev1) self._add_device(dev2) request = self.interface.GenerateDeviceFactoryRequest("dev1") permissions = self.interface.GenerateDeviceFactoryPermissions(request) self.assertEqual( get_native(permissions), { 'mount-point': False, 'reformat': True, 'format-type': True, 'label': True, 'device-type': False, 'device-name': False, 'device-size': False, 'device-encrypted': True, 'device-raid-level': False, }) request = self.interface.GenerateDeviceFactoryRequest("dev2") permissions = self.interface.GenerateDeviceFactoryPermissions(request) self.assertEqual( get_native(permissions), { 'mount-point': True, 'reformat': False, 'format-type': True, 'label': True, 'device-type': True, 'device-name': False, 'device-size': True, 'device-encrypted': True, 'device-raid-level': True, })
def setUp(self): dev1 = DiskDevice("name", fmt=get_format("mdmember"), size=Size("1 GiB")) dev2 = DiskDevice("other") self.part = PartitionDevice("part", fmt=get_format("mdmember"), parents=[dev2]) self.dev = MDRaidArrayDevice("dev", level="raid1", parents=[dev1, self.part], fmt=get_format("luks"), total_devices=2, member_devices=2) self.luks = LUKSDevice("luks", parents=[self.dev], fmt=get_format("ext4")) self.mdraid_method = availability.BLOCKDEV_MDRAID_PLUGIN._method self.dm_method = availability.BLOCKDEV_DM_PLUGIN._method self.hfsplus_method = availability.MKFS_HFSPLUS_APP._method self.cache_availability = availability.CACHE_AVAILABILITY self.addCleanup(self._clean_up)
def test_weight_1(self, *patches): arch = patches[0] dev = PartitionDevice('req1', exists=False) arch.is_x86.return_value = False arch.is_efi.return_value = False arch.is_arm.return_value = False arch.is_ppc.return_value = False dev.req_base_weight = -7 self.assertEqual(dev.weight, -7) dev.req_base_weight = None with patch.object(dev, "_format") as fmt: fmt.mountable = True # weights for / and /boot are not platform-specific (except for arm) fmt.mountpoint = "/" self.assertEqual(dev.weight, 0) fmt.mountpoint = "/boot" self.assertEqual(dev.weight, 2000) # # x86 (BIOS) # arch.is_x86.return_value = True arch.is_efi.return_value = False # user-specified weight should override other logic dev.req_base_weight = -7 self.assertEqual(dev.weight, -7) dev.req_base_weight = None fmt.mountpoint = "" self.assertEqual(dev.weight, 0) fmt.type = "biosboot" self.assertEqual(dev.weight, 5000) fmt.mountpoint = "/boot/efi" fmt.type = "efi" self.assertEqual(dev.weight, 0) # # UEFI # arch.is_x86.return_value = False arch.is_efi.return_value = True self.assertEqual(dev.weight, 5000) fmt.type = "biosboot" self.assertEqual(dev.weight, 0) fmt.mountpoint = "/" self.assertEqual(dev.weight, 0) # # arm # arch.is_x86.return_value = False arch.is_efi.return_value = False arch.is_arm.return_value = True fmt.mountpoint = "/" self.assertEqual(dev.weight, -100) # # ppc # arch.is_arm.return_value = False arch.is_ppc.return_value = True arch.is_pmac.return_value = False arch.is_ipseries.return_value = False fmt.mountpoint = "/" self.assertEqual(dev.weight, 0) fmt.type = "prepboot" self.assertEqual(dev.weight, 0) fmt.type = "appleboot" self.assertEqual(dev.weight, 0) arch.is_pmac.return_value = True self.assertEqual(dev.weight, 5000) fmt.type = "prepboot" self.assertEqual(dev.weight, 0) arch.is_pmac.return_value = False arch.is_ipseries.return_value = True self.assertEqual(dev.weight, 5000) fmt.type = "appleboot" self.assertEqual(dev.weight, 0) fmt.mountpoint = "/boot/efi" fmt.type = "efi" self.assertEqual(dev.weight, 0) fmt.type = "biosboot" self.assertEqual(dev.weight, 0) fmt.mountpoint = "/" self.assertEqual(dev.weight, 0) fmt.mountpoint = "/boot" self.assertEqual(dev.weight, 2000)
def generate_device_factory_request_test(self, blockdev): device = StorageDevice("dev1") with self.assertRaises(UnsupportedDeviceError): utils.generate_device_factory_request(self.storage, device) disk = DiskDevice("dev2") request = utils.generate_device_factory_request(self.storage, disk) self.assertEqual(DeviceFactoryRequest.to_structure(request), { "device-spec": get_variant(Str, "dev2"), "disks": get_variant(List[Str], ["dev2"]), "mount-point": get_variant(Str, ""), "reformat": get_variant(Bool, False), "format-type": get_variant(Str, ""), "label": get_variant(Str, ""), "luks-version": get_variant(Str, ""), "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_DISK), "device-name": get_variant(Str, "dev2"), "device-size": get_variant(UInt64, 0), "device-encrypted": get_variant(Bool, False), "device-raid-level": get_variant(Str, ""), "container-spec": get_variant(Str, ""), "container-name": get_variant(Str, ""), "container-size-policy": get_variant(Int64, devicefactory.SIZE_POLICY_AUTO), "container-encrypted": get_variant(Bool, False), "container-raid-level": get_variant(Str, ""), }) partition = PartitionDevice( "dev3", size=Size("5 GiB"), parents=[disk], fmt=get_format("ext4", mountpoint="/", label="root") ) request = utils.generate_device_factory_request(self.storage, partition) self.assertEqual(DeviceFactoryRequest.to_structure(request), { "device-spec": get_variant(Str, "dev3"), "disks": get_variant(List[Str], ["dev2"]), "mount-point": get_variant(Str, "/"), "reformat": get_variant(Bool, True), "format-type": get_variant(Str, "ext4"), "label": get_variant(Str, "root"), "luks-version": get_variant(Str, ""), "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_PARTITION), "device-name": get_variant(Str, "dev3"), "device-size": get_variant(UInt64, Size("5 GiB").get_bytes()), "device-encrypted": get_variant(Bool, False), "device-raid-level": get_variant(Str, ""), "container-spec": get_variant(Str, ""), "container-name": get_variant(Str, ""), "container-size-policy": get_variant(Int64, devicefactory.SIZE_POLICY_AUTO), "container-encrypted": get_variant(Bool, False), "container-raid-level": get_variant(Str, ""), }) 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] ) request = utils.generate_device_factory_request(self.storage, lv) self.assertEqual(DeviceFactoryRequest.to_structure(request), { "device-spec": get_variant(Str, "testvg-testlv"), "disks": get_variant(List[Str], []), "mount-point": get_variant(Str, ""), "reformat": get_variant(Bool, True), "format-type": get_variant(Str, "xfs"), "label": get_variant(Str, ""), "luks-version": get_variant(Str, ""), "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_LVM), "device-name": get_variant(Str, "testlv"), "device-size": get_variant(UInt64, Size("508 MiB").get_bytes()), "device-encrypted": get_variant(Bool, False), "device-raid-level": get_variant(Str, ""), "container-spec": get_variant(Str, "testvg"), "container-name": get_variant(Str, "testvg"), "container-size-policy": get_variant(Int64, Size("1.5 GiB")), "container-encrypted": get_variant(Bool, False), "container-raid-level": get_variant(Str, ""), })
def generate_device_factory_request_partition_test(self, blockdev): disk = DiskDevice("dev2") request = utils.generate_device_factory_request(self.storage, disk) self.assertEqual( DeviceFactoryRequest.to_structure(request), { "device-spec": get_variant(Str, "dev2"), "disks": get_variant(List[Str], ["dev2"]), "mount-point": get_variant(Str, ""), "reformat": get_variant(Bool, False), "format-type": get_variant(Str, ""), "label": get_variant(Str, ""), "luks-version": get_variant(Str, ""), "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_DISK), "device-name": get_variant(Str, "dev2"), "device-size": get_variant(UInt64, 0), "device-encrypted": get_variant(Bool, False), "device-raid-level": get_variant(Str, ""), "container-spec": get_variant(Str, ""), "container-name": get_variant(Str, ""), "container-size-policy": get_variant(Int64, devicefactory.SIZE_POLICY_AUTO), "container-encrypted": get_variant(Bool, False), "container-raid-level": get_variant(Str, ""), }) partition = PartitionDevice("dev3", size=Size("5 GiB"), parents=[disk], fmt=get_format("ext4", mountpoint="/", label="root")) request = utils.generate_device_factory_request( self.storage, partition) self.assertEqual( DeviceFactoryRequest.to_structure(request), { "device-spec": get_variant(Str, "dev3"), "disks": get_variant(List[Str], ["dev2"]), "mount-point": get_variant(Str, "/"), "reformat": get_variant(Bool, True), "format-type": get_variant(Str, "ext4"), "label": get_variant(Str, "root"), "luks-version": get_variant(Str, ""), "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_PARTITION), "device-name": get_variant(Str, "dev3"), "device-size": get_variant(UInt64, Size("5 GiB").get_bytes()), "device-encrypted": get_variant(Bool, False), "device-raid-level": get_variant(Str, ""), "container-spec": get_variant(Str, ""), "container-name": get_variant(Str, ""), "container-size-policy": get_variant(Int64, devicefactory.SIZE_POLICY_AUTO), "container-encrypted": get_variant(Bool, False), "container-raid-level": get_variant(Str, ""), })
def setUp(self): """Create some device objects to test with. This sets up two disks (sda, sdb). The first partition of each is a biosboot partition. The second partitions comprise a RAID1 array formatted as /boot. sda additionally contains a third partition formatted as ext4. """ super(GRUBRaidSimpleTest, self).setUp() # Make some disks self.sda = DiskDevice(name="sda", size=Size("100 GiB")) self.sda.format = get_format("disklabel") self.sdb = DiskDevice(name="sdb", size=Size("100 GiB")) self.sdb.format = get_format("disklabel") # Set up biosboot partitions, an mdarray for /boot, and a btrfs array on sda + sdb. # Start with the partitions self.sda1 = PartitionDevice(name="sda1", parents=[self.sda], size=Size("1 MiB")) self.sda1.format = get_format("biosboot") self.sda2 = PartitionDevice(name="sda2", parents=[self.sda], size=Size("500 MiB")) self.sda2.format = get_format("mdmember") self.sda4 = PartitionDevice(name="sda4", parents=[self.sda], size=Size("500 MiB")) self.sda4.format = get_format("btrfs") self.sdb1 = PartitionDevice(name="sdb1", parents=[self.sdb], size=Size("1 MiB")) self.sdb1.format = get_format("biosboot") self.sdb2 = PartitionDevice(name="sdb2", parents=[self.sdb], size=Size("500 MiB")) self.sdb2.format = get_format("mdmember") self.sdb4 = PartitionDevice(name="sdb4", parents=[self.sdb], size=Size("4 GiB")) self.sdb4.format = get_format("btrfs") # Add an extra partition for /boot on not-RAID self.sda3 = PartitionDevice(name="sda3", parents=[self.sda], size=Size("500 MiB")) self.sda3.format = get_format("ext4", mountpoint="/boot") # Pretend that the partitions are real with real parent disks for part in (self.sda1, self.sda2, self.sda3, self.sda4, self.sdb1, self.sdb2, self.sdb4): part.parents = part.req_disks self.boot_md = MDRaidArrayDevice(name="md1", size=Size("500 MiB"), parents=[self.sda2, self.sdb2], level=1, member_devices=2, total_devices=2) self.boot_md.format = get_format("ext4", mountpoint="/boot") # Set up the btrfs raid1 volume with a subvolume for /boot self.btrfs_volume = BTRFSVolumeDevice(parents=[self.sda4, self.sdb4], data_level=RAID1) self.btrfs_volume.format = get_format("btrfs") self.boot_btrfs = BTRFSSubVolumeDevice(parents=[self.btrfs_volume]) self.boot_btrfs.format = get_format("btrfs", mountpoint="/boot") self.grub = GRUB2()
def test_target_size(self): with sparsetmpfile("targetsizetest", Size("10 MiB")) as disk_file: disk = DiskFile(disk_file) disk.format = get_format("disklabel", device=disk.path, label_type="msdos") grain_size = Size(disk.format.alignment.grainSize) sector_size = Size(disk.format.parted_device.sectorSize) start = int(grain_size) orig_size = Size("6 MiB") end = start + int(orig_size / sector_size) - 1 disk.format.add_partition(start, end) partition = disk.format.parted_disk.getPartitionBySector(start) self.assertNotEqual(partition, None) self.assertEqual(orig_size, Size(partition.getLength(unit='B'))) device = PartitionDevice(os.path.basename(partition.path), size=orig_size) device.disk = disk device.exists = True device.parted_partition = partition device.format = get_format("ext4", device=device.path) device.format.exists = True # grain size should be 1 MiB device.format._min_instance_size = Size("2 MiB") + (grain_size / 2) device.format._resizable = True # Make sure things are as expected to begin with. self.assertEqual(device.size, orig_size) self.assertEqual(device.min_size, Size("3 MiB")) # start sector's at 1 MiB self.assertEqual(device.max_size, Size("9 MiB")) # ValueError if not Size with six.assertRaisesRegex(self, ValueError, "new size must.*type Size"): device.target_size = 22 self.assertEqual(device.target_size, orig_size) # ValueError if size smaller than min_size with six.assertRaisesRegex(self, ValueError, "size.*smaller than the minimum"): device.target_size = Size("1 MiB") self.assertEqual(device.target_size, orig_size) # ValueError if size larger than max_size with six.assertRaisesRegex(self, ValueError, "size.*larger than the maximum"): device.target_size = Size("11 MiB") self.assertEqual(device.target_size, orig_size) # ValueError if unaligned with six.assertRaisesRegex(self, ValueError, "new size.*not.*aligned"): device.target_size = Size("3.1 MiB") self.assertEqual(device.target_size, orig_size) # successfully set a new target size new_target = device.max_size device.target_size = new_target self.assertEqual(device.target_size, new_target) self.assertEqual(device.size, new_target) parted_size = Size(device.parted_partition.getLength(unit='B')) self.assertEqual(parted_size, device.target_size) # reset target size to original size device.target_size = orig_size self.assertEqual(device.target_size, orig_size) self.assertEqual(device.size, orig_size) parted_size = Size(device.parted_partition.getLength(unit='B')) self.assertEqual(parted_size, device.target_size)
def test_min_max_size_alignment(self): with sparsetmpfile("minsizetest", Size("10 MiB")) as disk_file: disk = DiskFile(disk_file) disk.format = get_format("disklabel", device=disk.path) grain_size = Size(disk.format.alignment.grainSize) sector_size = Size(disk.format.parted_device.sectorSize) start = int(grain_size) end = start + int(Size("6 MiB") / sector_size) disk.format.add_partition(start, end) partition = disk.format.parted_disk.getPartitionBySector(start) self.assertNotEqual(partition, None) device = PartitionDevice(os.path.basename(partition.path)) device.disk = disk device.exists = True device.parted_partition = partition # Typical sector size is 512 B. # Default optimum alignment grain size is 2048 sectors, or 1 MiB. device.format = get_format("ext4", device=device.path) device.format.exists = True device.format._min_instance_size = Size("2 MiB") + (grain_size / 2) device.format._resizable = True ## # min_size ## # The end sector based only on format min size should be unaligned. min_sectors = int(device.format.min_size / sector_size) min_end_sector = partition.geometry.start + min_sectors - 1 self.assertEqual( disk.format.end_alignment.isAligned(partition.geometry, min_end_sector), False) # The end sector based on device min size should be aligned. min_sectors = int(device.min_size / sector_size) min_end_sector = partition.geometry.start + min_sectors - 1 self.assertEqual( disk.format.end_alignment.isAligned(partition.geometry, min_end_sector), True) ## # max_size ## # Add a partition starting three sectors past an aligned sector and # extending to the end of the disk so that there's a free region # immediately following the first partition with an unaligned end # sector. free = disk.format.parted_disk.getFreeSpaceRegions()[-1] raw_start = int(Size("9 MiB") / sector_size) start = disk.format.alignment.alignUp(free, raw_start) + 3 disk.format.add_partition(start, disk.format.parted_device.length - 1) # Verify the end of the free region immediately following the first # partition is unaligned. free = disk.format.parted_disk.getFreeSpaceRegions()[1] self.assertEqual(disk.format.end_alignment.isAligned(free, free.end), False) # The end sector based on device min size should be aligned. max_sectors = int(device.max_size / sector_size) max_end_sector = partition.geometry.start + max_sectors - 1 self.assertEqual( disk.format.end_alignment.isAligned(free, max_end_sector), True)
def test_match(self): """Test boot format populator helper match method""" if self.helper_class is None: return partition = PartitionDevice("testpartitiondev") storagedev = StorageDevice("teststoragedev") storagedev.bootable = True data = dict() fmt_class = get_device_format_class(self.helper_class._type_specifier) if fmt_class is None: self.skipTest("failed to look up format class for %s" % self.helper_class._type_specifier) data["ID_FS_TYPE"] = self.helper_class._base_type_specifier partition._bootable = self.helper_class._bootable min_size = fmt_class._min_size max_size = fmt_class._max_size partition._size = min_size storagedev._size = min_size if fmt_class._name: partition._parted_partition = FakePartedPart(partition, fmt_class._name) self.assertTrue(self.helper_class.match(data, partition)) # These are only valid for partitions. self.assertFalse(self.helper_class.match(data, storagedev)) data["ID_FS_TYPE"] += "x" self.assertFalse(self.helper_class.match(data, partition)) data["ID_FS_TYPE"] = self.helper_class._base_type_specifier if self.helper_class._bootable: partition._bootable = False self.assertFalse(self.helper_class.match(data, partition)) partition._bootable = True if max_size: partition._size = max_size + 1 elif min_size: partition._size = min_size - 1 self.assertFalse(self.helper_class.match(data, partition)) partition._size = min_size # we don't always match on the parted partition name, so allow # subclasses to decide if not self.name_mismatch_ok: orig = partition._parted_partition partition._parted_partition = FakePartedPart(partition, 'dontmatchanything') self.assertFalse(self.helper_class.match(data, partition)) # shouldn't crash partition._parted_partition = None self.assertFalse(self.helper_class.match(data, partition)) partition._parted_partition = orig