def test__extract_size_from_fdisk_good_output_partition(self, _populate_disks_entries_mock, run_cmd_mock): run_cmd_mock.return_value = FAKE_FDISK_GOOD_OUTPUT_PARTITION dq = LinuxDiskDeviceQueries() fake_device_filepath = "/dev/sda6" actual_size = dq._extract_size_from_fdisk(fake_device_filepath) run_cmd_mock.assert_called_once_with(SIZE_FROM_FDISK + [fake_device_filepath]) self.assertEqual(125998989312, actual_size)
def test_get_disk_identifier(self, run_cmd_mock): run_cmd_mock.return_value = FAKE_FDISK_GOOD_OUTPUT dq = LinuxDiskDeviceQueries() fake_device_filepath = "/dev/sdb" actual_identifier = dq.get_disk_identifier(fake_device_filepath) run_cmd_mock.assert_called_with(SIZE_FROM_FDISK + [fake_device_filepath]) self.assertEqual("0x0001ab30", actual_identifier)
def test__extract_size_from_fdisk_good_output_multipath_device(self, _populate_disks_entries_mock, run_cmd_mock): run_cmd_mock.return_value = FAKE_FDISK_GOOD_OUTPUT_MULTIPATH_DEVICE dq = LinuxDiskDeviceQueries() fake_device_filepath = "/dev/mapper/mpathc" actual_size = dq._extract_size_from_fdisk(fake_device_filepath) run_cmd_mock.assert_called_once_with(SIZE_FROM_FDISK + [fake_device_filepath]) self.assertEqual(17208180736, actual_size)
def test__extract_size_from_fdisk_good_output(self, _populate_disks_entries_mock, run_cmd_mock): run_cmd_mock.return_value = FAKE_FDISK_GOOD_OUTPUT dq = LinuxDiskDeviceQueries() fake_device_filepath = "/dev/sda" actual_size = dq._extract_size_from_fdisk(fake_device_filepath) run_cmd_mock.assert_called_with(SIZE_FROM_FDISK + [fake_device_filepath]) self.assertEqual(8185184256, actual_size)
def test__extract_hctl_from_device_link(self, _populate_disks_entries_mock, readlink_mock): def readlink_side_effect(device_name): links = { "/sys/block/sda/device": "../../../0:0:0:0", "/sys/block/sdb/device": "0000:00/0000:00:10:0/host0/target0:0:0/0:0:0:0", "/sys/block/sdc/device": "platform/host1/session1/target1:0:0:1/1:0:0:3", "/sys/block/sdd/device": "../../../15:16:17:333", } return links[device_name] device_names = ["sda", "sdb", "sdc", "sdd"] expected_results = [HctlInfo(0, 0, 0, 0), HctlInfo(0, 0, 0, 0), HctlInfo(1, 0, 0, 3), HctlInfo(15, 16, 17, 333)] readlink_mock.side_effect = readlink_side_effect dq = LinuxDiskDeviceQueries() for expected_result, device_name in map(None, expected_results, device_names): self.assertEqual(expected_result, dq._extract_hctl_from_device_link(device_name))
def get_disks(): basic_disks = [] multipath_disks = [] linux_disk_device_queries = LinuxDiskDeviceQueries() mp_disk_path_names = [] for multipath_disk_entry in linux_disk_device_queries.get_multipath_disk_entries(): for path_group in multipath_disk_entry.path_groups: mp_disk_path_names.extend([path.name for path in path_group.paths]) multipath_disks.append(LinuxMultipathDisk(multipath_disk_entry)) for basic_disk_entry in linux_disk_device_queries.get_basic_disk_entries(): if basic_disk_entry.name in mp_disk_path_names: # disk is a path of a multi path disk continue uuid = linux_disk_device_queries.get_uuid(basic_disk_entry.filepath) basic_disk_entry.disk_identifier = linux_disk_device_queries.get_disk_identifier(basic_disk_entry.filepath) basic_disks.append(LinuxDisk(basic_disk_entry, uuid, linux_disk_device_queries)) return basic_disks + multipath_disks
def test__get_sysfs_partitions(self, _populate_disks_entries_mock, extract_size_mock, glob_mock): glob_mock.return_value = ["/dev/sda1", "/dev/sda2", "/dev/sda3"] fake_size = 1024 extract_size_mock.return_value = fake_size dq = LinuxDiskDeviceQueries() expected_results = [ DiskEntry("sda1", "/dev/sda1", fake_size, (250, 1), (1, 1, 1, 1)), DiskEntry("sda2", "/dev/sda2", fake_size, (250, 2), (2, 2, 2, 2)), DiskEntry("sda3", "/dev/sda3", fake_size, (250, 3), (3, 3, 3, 3)), ] # TODO: This is kinda ugly, better would be to mock _map_hctl_to_disk_device_names dq.hctl_map = {"sda1": (1, 1, 1, 1), "sda2": (2, 2, 2, 2), "sda3": (3, 3, 3, 3)} actual_results = dq._get_sysfs_partitions("sda") self.assertEqual(len(expected_results), len(actual_results)) for expected_result, actual_result in map(None, expected_results, actual_results): self.assertEqual(expected_result.name, actual_result.name) self.assertEqual(expected_result.filepath, actual_result.filepath) self.assertEqual(expected_result.size, actual_result.size) self.assertEqual(expected_result.major_minor, actual_result.major_minor) self.assertEqual(expected_result.hctl, actual_result.hctl)
def test__extract_all_hctls_from_proc_scsi_file(self, _populate_disks_entries_mock): with patch("dmtcore.os.disk.linux.open", create=True) as open_mock: open_mock.return_value = MagicMock(spec=file) file_handle = open_mock.return_value.__enter__.return_value file_handle.readlines.return_value = FAKE_CONTENT_OF_PROC_SCSI.splitlines() dq = LinuxDiskDeviceQueries() expected_results = [ HctlInfo(0, 0, 0, 0), HctlInfo(1, 0, 2, 0), HctlInfo(1, 0, 2, 1), HctlInfo(1, 0, 3, 0), HctlInfo(1, 0, 3, 1), HctlInfo(0, 0, 0, 0), ] actual_results = dq._extract_all_hctls_from_proc_scsi_file() self.assertEqual(len(expected_results), len(actual_results)) for expected_result, actual_result in map(None, expected_results, actual_results): self.assertEqual(expected_result, actual_result)
def test__extract_uuid_from_blkid_no_uuid(self, _populate_disks_entries_mock, run_cmd_mock): run_cmd_mock.return_value = "" fake_device_filepath = "/dev/sda1" dq = LinuxDiskDeviceQueries() self.assertEqual(None, dq._extract_uuid_from_blkid(fake_device_filepath))
def test__extract_uuid_from_blkid(self, _populate_disks_entries_mock, run_cmd_mock): run_cmd_mock.return_value = FAKE_BLKID_OUTPUT fake_device_filepath = "/dev/sda1" dq = LinuxDiskDeviceQueries() self.assertEqual("51270839-1a9b-44a2-9786-e078206342c2", dq._extract_uuid_from_blkid(fake_device_filepath))
def test__extract_hctl_from_device_link_returns_none_on_oserror(self, _populate_disks_entries_mock, readlink_mock): readlink_mock.side_effect = OSError() dq = LinuxDiskDeviceQueries() self.assertEqual(None, dq._extract_hctl_from_device_link("sda"))
def test__extract_size_from_fdisk_bad_output(self, _populate_disks_entries_mock, run_cmd_mock): run_cmd_mock.return_value = FAKE_FDISK_BAD_OUTPUT dq = LinuxDiskDeviceQueries() self.assertEqual(None, dq._extract_size_from_fdisk("/dev/sda"))
def test__device_name_is_partition__device_is_not_valid(self, _populate_disks_entries_mock): dq = LinuxDiskDeviceQueries() self.assertFalse(dq._device_name_is_partition("/dev/sda1")) self.assertFalse(dq._device_name_is_partition("sda1a"))
def test__device_name_is_partition__device_is_not_a_partition(self, _populate_disks_entries_mock): dq = LinuxDiskDeviceQueries() self.assertTrue(dq._device_name_is_partition("sda1")) self.assertTrue(dq._device_name_is_partition("sdab12"))
def test__device_name_is_partition__device_is_a_partition(self, _populate_disks_entries_mock): dq = LinuxDiskDeviceQueries() self.assertEqual(1, _populate_disks_entries_mock.call_count) self.assertFalse(dq._device_name_is_partition("sda")) self.assertFalse(dq._device_name_is_partition("sdab"))