def setUp(self): super(TestBlockDevices, self).setUp() with mock.patch('glob.glob', return_value=[]): with mock.patch('chroma_agent.utils.BlkId', return_value={}): self.block_devices = BlockDevices() mock.patch('os.path.isfile', self.mock_isfile).start() self.existing_files = [] # Guaranteed cleanup with unittest2 self.addCleanup(mock.patch.stopall)
def setUp(self): super(TestDevMajorMinor, self).setUp() mock.patch('chroma_agent.utils.BlkId', return_value={ 0: { 'path': self.mock_devices.keys()[0], 'type': 'ext4' } }).start() mock.patch( 'chroma_agent.device_plugins.linux_components.block_devices.BlockDevices._parse_sys_block', return_value=(None, None)).start() self.addCleanup(mock.patch.stopall) self.block_devices = BlockDevices()
def _setup_zfs_devices(self): blockdevices = BlockDevices() zfs_devices = ZfsDevices() zfs_devices.full_scan(blockdevices) return zfs_devices
def setUp(self): super(TestBlockDevices, self).setUp() fixture = { "/devices/pci0000:00/0000:00:0d.0/ata11/host10/target10:0:0/10:0:0:0/block/sdi": { "ACTION": "add", "MAJOR": "8", "MINOR": "128", "DEVLINKS": "/dev/disk/by-id/ata-VBOX_HARDDISK_VB94952694-0a23e192 /dev/disk/by-label/fs-OST0006 /dev/disk/by-path/pci-0000:00:0d.0-ata-9.0 /dev/disk/by-uuid/f21688ec-5bde-44a5-9ace-c7c4b18a20f5", "PATHS": [ "/dev/sdi", "/dev/disk/by-id/ata-VBOX_HARDDISK_VB94952694-0a23e192", "/dev/disk/by-label/fs-OST0006", "/dev/disk/by-path/pci-0000:00:0d.0-ata-9.0", "/dev/disk/by-uuid/f21688ec-5bde-44a5-9ace-c7c4b18a20f5" ], "DEVNAME": "/dev/sdi", "DEVPATH": "/devices/pci0000:00/0000:00:0d.0/ata11/host10/target10:0:0/10:0:0:0/block/sdi", "DEVTYPE": "disk", "ID_VENDOR": None, "ID_MODEL": "VBOX_HARDDISK", "ID_SERIAL": "VBOX_HARDDISK_VB94952694-0a23e192", "ID_FS_TYPE": "ext4", "ID_PART_ENTRY_NUMBER": None, "IML_SIZE": "10485760", "IML_SCSI_80": "SATA VBOX HARDDISK VB94952694-0a23e192", "IML_SCSI_83": "1ATA VBOX HARDDISK VB94952694-0a23e192", "IML_IS_RO": False } } with mock.patch('glob.glob', return_value=[]): with mock.patch( 'chroma_agent.device_plugins.linux_components.block_devices.scanner_cmd', return_value=fixture): self.block_devices = BlockDevices() self.existing_files = [] # Guaranteed cleanup with unittest2 self.addCleanup(mock.patch.stopall)
def __init__(self, dmsetup_data, devices_data): self.lvs = devices_data['lvs'] self.vgs = devices_data['vgs'] self.mpaths = {} with mock.patch('chroma_agent.utils.BlkId', return_value={}): with mock.patch( 'chroma_agent.device_plugins.linux_components.block_devices.BlockDevices._parse_sys_block', return_value=(devices_data['block_device_nodes'], devices_data['node_block_devices'])): self.block_devices = BlockDevices() self._parse_dm_table(dmsetup_data)
class TestDevMajorMinor(LinuxAgentTests): MockDevice = collections.namedtuple('MockDevice', 'st_mode st_rdev') mock_devices = {'/dev/disk/by-id/adisk': MockDevice(25008, 6)} node_block_devices = {'/dev/disk/by-id/adisk': '12:24'} def mock_os_stat(self, path): if path in TestDevMajorMinor.mock_devices: return TestDevMajorMinor.mock_devices[path] else: raise OSError(errno.ENOENT, 'No such file or directory.') def setUp(self): super(TestDevMajorMinor, self).setUp() mock.patch('chroma_agent.utils.BlkId', return_value={ 0: { 'path': self.mock_devices.keys()[0], 'type': 'ext4' } }).start() mock.patch( 'chroma_agent.device_plugins.linux_components.block_devices.BlockDevices._parse_sys_block', return_value=(None, None)).start() self.addCleanup(mock.patch.stopall) self.block_devices = BlockDevices() def test_paths_to_major_minors_paths_exist(self): self.block_devices.node_block_devices = self.node_block_devices devices = self.block_devices.paths_to_major_minors( ['/dev/disk/by-id/adisk']) self.assertEqual(len(devices), 1) self.assertEqual(devices, ['12:24']) def test_paths_to_major_minors_a_path_doesnt_exist(self): self.block_devices.node_block_devices = self.node_block_devices devices = self.block_devices.paths_to_major_minors( ['/dev/disk/by-id/idontexist', '/dev/disk/by-id/adisk']) self.assertEqual(devices, ['12:24'])
def setUp(self): super(TestDevMajorMinor, self).setUp() self.stat_patcher = mock.patch('os.stat', self.mock_os_stat) self.stat_patcher.start() mock.patch('os.minor', lambda st_rdev: st_rdev * 4).start() mock.patch('os.major', lambda st_rdev: st_rdev * 2).start() mock.patch('stat.S_ISBLK', return_value=True).start() mock.patch('chroma_agent.lib.shell.AgentShell.try_run').start() mock.patch('chroma_agent.utils.BlkId', return_value={ 0: { 'path': self.mock_devices.keys()[0], 'type': 'ext4' } }).start() mock.patch( 'chroma_agent.device_plugins.linux_components.block_devices.BlockDevices._parse_sys_block', return_value=(None, None)).start() self.addCleanup(mock.patch.stopall) self.block_devices = BlockDevices() self.block_devices.non_existent_paths = set([])
def _setup_zfs_devices(self, mock_zfs_device, available_side_effect=None): # mock context manager __enter__ returned object type(mock_zfs_device.return_value.__enter__.return_value ).available = PropertyMock( return_value=True ) if available_side_effect is None else PropertyMock( side_effect=available_side_effect) self.mock_read_from_store.reset_mock() self.mock_write_to_store.reset_mock() block_devices = BlockDevices() zfs_devices = ZfsDevices() zfs_devices.full_scan(block_devices) return zfs_devices, block_devices
class TestBlockDevices(CommandCaptureTestCase): def setUp(self): super(TestBlockDevices, self).setUp() with mock.patch('glob.glob', return_value=[]): with mock.patch('chroma_agent.utils.BlkId', return_value={}): self.block_devices = BlockDevices() mock.patch('os.path.isfile', self.mock_isfile).start() self.existing_files = [] # Guaranteed cleanup with unittest2 self.addCleanup(mock.patch.stopall) def mock_isfile(self, file): return file in self.existing_files def test_device_node_versions(self): for scsi_id in ["/sbin/scsi_id", "/lib/udev/scsi_id"]: # Check runs with correct scsi_id self.existing_files = [scsi_id] self.reset_command_capture() self.add_commands( CommandCaptureCommand( ((scsi_id, '-g', '-p', '0x80', '/dev/blop'))), CommandCaptureCommand( ((scsi_id, '-g', '-p', '0x83', '/dev/blop')))) result = self.block_devices._device_node(1, "/dev/blop", 1, None, '1234') self.assertRanAllCommandsInOrder() self.assertEqual( result, { 'parent': None, 'major_minor': 1, 'serial_83': '', 'serial_80': '', 'path': '/dev/blop', 'filesystem_type': None, 'partition_number': '1234', 'size': 1 })
def _full_scan(self): # If we are a worker node then return nothing because our devices are not of interest. This is a short term # solution for HYD-3140. This plugin should really be loaded if it is not needed but for now this sorts out # and issue with PluginAgentResources being in the linux plugin. if config.get('settings', 'profile')['worker']: return {} # Before we do anything do a partprobe, this will ensure that everything gets an up to date view of the # device partitions. partprobe might throw errors so ignore return value AgentShell.run(["partprobe"]) # Map of block devices major:minors to /dev/ path. block_devices = BlockDevices() # Devicemapper: LVM and Multipath dmsetup = DmsetupTable(block_devices) # Software RAID mds = MdRaid(block_devices).all() # _zpools zfs_devices = ZfsDevices() zfs_devices.full_scan(block_devices) # EMCPower Devices emcpowers = EMCPower(block_devices).all() # Local filesystems (not lustre) in /etc/fstab or /proc/mounts local_fs = LocalFilesystems(block_devices).all() # We have scan devices, so set the devices scanned flags. LinuxDevicePlugin.devices_scanned = True return { "vgs": dmsetup.vgs, "lvs": dmsetup.lvs, "zfspools": zfs_devices.zpools, "zfsdatasets": zfs_devices.datasets, "zfsvols": zfs_devices.zvols, "mpath": dmsetup.mpaths, "devs": block_devices.block_device_nodes, "local_fs": local_fs, 'emcpower': emcpowers, 'mds': mds }
class TestDevMajorMinor(LinuxAgentTests): MockDevice = collections.namedtuple('MockDevice', 'st_mode st_rdev') mock_devices = {'/dev/disk/by-id/adisk': MockDevice(25008, 6)} node_block_devices = {'/dev/disk/by-id/adisk': '12:24'} def mock_os_stat(self, path): if path in TestDevMajorMinor.mock_devices: return TestDevMajorMinor.mock_devices[path] else: raise OSError(errno.ENOENT, 'No such file or directory.') def setUp(self): super(TestDevMajorMinor, self).setUp() self.stat_patcher = mock.patch('os.stat', self.mock_os_stat) self.stat_patcher.start() mock.patch('os.minor', lambda st_rdev: st_rdev * 4).start() mock.patch('os.major', lambda st_rdev: st_rdev * 2).start() mock.patch('stat.S_ISBLK', return_value=True).start() mock.patch('chroma_agent.lib.shell.AgentShell.try_run').start() mock.patch('chroma_agent.utils.BlkId', return_value={ 0: { 'path': self.mock_devices.keys()[0], 'type': 'ext4' } }).start() mock.patch( 'chroma_agent.device_plugins.linux_components.block_devices.BlockDevices._parse_sys_block', return_value=(None, None)).start() self.addCleanup(mock.patch.stopall) self.block_devices = BlockDevices() self.block_devices.non_existent_paths = set([]) def test_dev_major_minor_path_exists(self): """ After a successful attempt, path should be removed from no-retry list """ path = '/dev/disk/by-id/adisk' self.block_devices.non_existent_paths.add(path) device = self.block_devices._dev_major_minor(path) self.assertNotIn(path, self.block_devices.non_existent_paths) self.assertEqual(device, '12:24') def test_dev_major_minor_path_doesnt_exist(self): """ After un-successful attempts, path should be added to no-retry list """ path = '/dev/disk/by-id/idontexist' device = self.block_devices._dev_major_minor(path) self.assertIn(path, self.block_devices.non_existent_paths) self.assertEqual(device, None) def test_dev_major_minor_path_exists_retries(self): """ With existing path, method only calls stat once """ path = '/dev/disk/by-id/adisk' self.stat_patcher.stop() mock_stat = mock.patch( 'os.stat', return_value=TestDevMajorMinor.mock_devices[path]).start() self.block_devices._dev_major_minor(path) self.assertEqual(mock_stat.call_count, 1) self.assertNotIn(path, self.block_devices.non_existent_paths) def test_dev_major_minor_path_retry_doesnt_exist_retries(self): """ Test non-existent path retries specified amount, and is subsequently added to the no-retry list. On the next attempt with the same path, there should be no retries. """ path = '/dev/disk/by-id/idontexist' self.stat_patcher.stop() self.assertNotIn(path, self.block_devices.non_existent_paths) mock_stat = mock.patch('os.stat').start() mock_stat.side_effect = OSError(errno.ENOENT, 'No such file or directory.') self.block_devices._dev_major_minor(path) self.assertEqual(mock_stat.call_count, BlockDevices.MAXRETRIES) self.assertIn(path, self.block_devices.non_existent_paths) mock_stat.reset_mock() self.block_devices._dev_major_minor(path) self.assertEqual(mock_stat.call_count, 1) self.assertIn(path, self.block_devices.non_existent_paths) def test_paths_to_major_minors_paths_exist(self): self.block_devices.node_block_devices = self.node_block_devices devices = self.block_devices.paths_to_major_minors( ['/dev/disk/by-id/adisk']) self.assertEqual(len(devices), 1) self.assertEqual(devices, ['12:24']) def test_paths_to_major_minors_a_path_doesnt_exist(self): self.block_devices.node_block_devices = self.node_block_devices devices = self.block_devices.paths_to_major_minors( ['/dev/disk/by-id/idontexist', '/dev/disk/by-id/adisk']) self.assertEqual(devices, ['12:24'])
def _quick_scan(self): """Lightweight enumeration of available block devices""" return ZfsDevices().quick_scan() + BlockDevices.quick_scan()