def test_with_exists_and_not_exists(self, m_subp): """with exists=file where file does not exist should invoke subp.""" mydev = self.tmp_path("mydev") util.udevadm_settle(exists=mydev) m_subp.called_once_with( ['udevadm', 'settle', '--exit-if-exists=%s' % mydev])
def test_with_timeout_int(self, m_subp): """timeout can be an integer.""" timeout = 9 util.udevadm_settle(timeout=timeout) m_subp.called_once_with( ['udevadm', 'settle', '--timeout=%s' % timeout])
def test_with_timeout_string(self, m_subp): """timeout can be a string.""" timeout = "555" util.udevadm_settle(timeout=timeout) m_subp.assert_called_once_with( ['udevadm', 'settle', '--timeout=%s' % timeout])
def test_with_exists_and_timeout(self, m_subp): """test call with both exists and timeout.""" mydev = self.tmp_path("mydev") timeout = "3" util.udevadm_settle(exists=mydev) m_subp.called_once_with( ['udevadm', 'settle', '--exit-if-exists=%s' % mydev, '--timeout=%s' % timeout])
def test_with_exists_and_timeout(self, m_subp): """test call with both exists and timeout.""" mydev = self.tmp_path("mydev") timeout = "3" util.udevadm_settle(exists=mydev) m_subp.called_once_with( ['udevadm', 'settle', '--exit-if-exists=%s' % mydev, '--timeout=%s' % timeout])
def user_data_rhevm(self): """ RHEVM specific userdata read If on RHEV-M the user data will be contained on the floppy device in file <user_data_file> To access it: modprobe floppy Leverage util.mount_cb to: mkdir <tmp mount dir> mount /dev/fd0 <tmp mount dir> The call back passed to util.mount_cb will do: read <tmp mount dir>/<user_data_file> """ return_str = None # modprobe floppy try: modprobe_floppy() except subp.ProcessExecutionError as e: util.logexc(LOG, "Failed modprobe: %s", e) return False floppy_dev = "/dev/fd0" # udevadm settle for floppy device try: util.udevadm_settle(exists=floppy_dev, timeout=5) except (subp.ProcessExecutionError, OSError) as e: util.logexc(LOG, "Failed udevadm_settle: %s\n", e) return False try: return_str = util.mount_cb(floppy_dev, read_user_data_callback) except OSError as err: if err.errno != errno.ENOENT: raise except util.MountFailedError: util.logexc( LOG, "Failed to mount %s when looking for user data", floppy_dev, ) self.userdata_raw = return_str self.metadata = META_DATA_NOT_SUPPORTED if return_str: return True else: return False
def assert_and_settle_device(device): """Assert that device exists and settle so it is fully recognized.""" if not os.path.exists(device): util.udevadm_settle() if not os.path.exists(device): raise RuntimeError("Device %s did not exist and was not created " "with a udevadm settle." % device) # Whether or not the device existed above, it is possible that udev # events that would populate udev database (for reading by lsdname) have # not yet finished. So settle again. util.udevadm_settle()
def assert_and_settle_device(device): """Assert that device exists and settle so it is fully recognized.""" if not os.path.exists(device): util.udevadm_settle() if not os.path.exists(device): raise RuntimeError("Device %s did not exist and was not created " "with a udevadm settle." % device) # Whether or not the device existed above, it is possible that udev # events that would populate udev database (for reading by lsdname) have # not yet finished. So settle again. util.udevadm_settle()
def read_parttbl(device): """ Use partprobe instead of 'udevadm'. Partprobe is the only reliable way to probe the partition table. """ blkdev_cmd = [BLKDEV_CMD, '--rereadpt', device] util.udevadm_settle() try: util.subp(blkdev_cmd) except Exception as e: util.logexc(LOG, "Failed reading the partition table %s" % e) util.udevadm_settle()
def read_parttbl(device): """ Use partprobe instead of 'udevadm'. Partprobe is the only reliable way to probe the partition table. """ blkdev_cmd = [BLKDEV_CMD, '--rereadpt', device] util.udevadm_settle() try: util.subp(blkdev_cmd) except Exception as e: util.logexc(LOG, "Failed reading the partition table %s" % e) util.udevadm_settle()
def read_parttbl(device): """ `Partprobe` is preferred over `blkdev` since it is more reliably able to probe the partition table. """ if PARTPROBE_CMD is not None: probe_cmd = [PARTPROBE_CMD, device] else: probe_cmd = [BLKDEV_CMD, '--rereadpt', device] util.udevadm_settle() try: subp.subp(probe_cmd) except Exception as e: util.logexc(LOG, "Failed reading the partition table %s" % e) util.udevadm_settle()
def user_data_rhevm(self): ''' RHEVM specific userdata read If on RHEV-M the user data will be contained on the floppy device in file <user_data_file> To access it: modprobe floppy Leverage util.mount_cb to: mkdir <tmp mount dir> mount /dev/fd0 <tmp mount dir> The call back passed to util.mount_cb will do: read <tmp mount dir>/<user_data_file> ''' return_str = None # modprobe floppy try: cmd = CMD_PROBE_FLOPPY (cmd_out, _err) = util.subp(cmd) LOG.debug('Command: %s\nOutput%s', ' '.join(cmd), cmd_out) except ProcessExecutionError as _err: util.logexc(LOG, 'Failed command: %s\n%s', ' '.join(cmd), _err) return False except OSError as _err: util.logexc(LOG, 'Failed command: %s\n%s', ' '.join(cmd), _err) return False floppy_dev = '/dev/fd0' # udevadm settle for floppy device try: (cmd_out, _err) = util.udevadm_settle(exists=floppy_dev, timeout=5) LOG.debug('Command: %s\nOutput%s', ' '.join(cmd), cmd_out) except ProcessExecutionError as _err: util.logexc(LOG, 'Failed command: %s\n%s', ' '.join(cmd), _err) return False except OSError as _err: util.logexc(LOG, 'Failed command: %s\n%s', ' '.join(cmd), _err) return False try: return_str = util.mount_cb(floppy_dev, read_user_data_callback) except OSError as err: if err.errno != errno.ENOENT: raise except util.MountFailedError: util.logexc(LOG, "Failed to mount %s when looking for user data", floppy_dev) self.userdata_raw = return_str self.metadata = META_DATA_NOT_SUPPORTED if return_str: return True else: return False
def test_with_exists_and_not_exists(self, m_subp): """with exists=file where file does not exist should invoke subp.""" mydev = self.tmp_path("mydev") util.udevadm_settle(exists=mydev) m_subp.called_once_with( ['udevadm', 'settle', '--exit-if-exists=%s' % mydev])
def test_with_exists_and_file_exists(self, m_subp): """with exists=file where file does exist should not invoke subp.""" mydev = self.tmp_path("mydev") util.write_file(mydev, "foo\n") util.udevadm_settle(exists=mydev) self.assertIsNone(m_subp.call_args)
def test_with_exists_and_file_exists(self, m_subp): """with exists=file where file does exist should not invoke subp.""" mydev = self.tmp_path("mydev") util.write_file(mydev, "foo\n") util.udevadm_settle(exists=mydev) self.assertIsNone(m_subp.call_args)
def test_with_no_params(self, m_subp): """called with no parameters.""" util.udevadm_settle() m_subp.called_once_with(mock.call(['udevadm', 'settle']))
def test_with_timeout_int(self, m_subp): """timeout can be an integer.""" timeout = 9 util.udevadm_settle(timeout=timeout) m_subp.called_once_with( ['udevadm', 'settle', '--timeout=%s' % timeout])
def settle(self, *, exists=None) -> None: if exists is not None: exists = net.sys_dev_path(exists) util.udevadm_settle(exists=exists)
def test_with_no_params(self, m_subp): """called with no parameters.""" util.udevadm_settle() m_subp.called_once_with(mock.call(['udevadm', 'settle']))
def test_with_timeout_string(self, m_subp): """timeout can be a string.""" timeout = "555" util.udevadm_settle(timeout=timeout) m_subp.assert_called_once_with( ['udevadm', 'settle', '--timeout=%s' % timeout])