def nvme_present(cls): try: priv_rootwrap.custom_execute('nvme', 'version') return True except Exception as exc: if isinstance(exc, OSError) and exc.errno == errno.ENOENT: LOG.debug('nvme not present on system') else: LOG.warning('Unknown error when checking presence of nvme: %s', exc) return False
def _execute(*cmd, **kwargs): try: return rootwrap.custom_execute(*cmd, **kwargs) except OSError as e: sanitized_cmd = strutils.mask_password(' '.join(cmd)) raise putils.ProcessExecutionError( cmd=sanitized_cmd, description=six.text_type(e))
def test_custom_execute_timeout_no_raise(self): out, err = priv_rootwrap.custom_execute('sleep', '2', timeout=0.05, raise_timeout=False) self.assertEqual('', out) self.assertIsInstance(err, str)
def test_custom_execute_timeout_no_raise(self): t0 = time.time() out, err = priv_rootwrap.custom_execute('sleep', '2', timeout=0.05, raise_timeout=False) t1 = time.time() self.assertEqual('', out) self.assertIsInstance(err, six.string_types) self.assertLess(t1 - t0, 0.3)
def test_custom_execute(self): on_execute = mock.Mock() on_completion = mock.Mock() msg = 'hola' out, err = priv_rootwrap.custom_execute('echo', msg, on_execute=on_execute, on_completion=on_completion) self.assertEqual(msg + '\n', out) self.assertEqual('', err) on_execute.assert_called_once_with(mock.ANY) proc = on_execute.call_args[0][0] on_completion.assert_called_once_with(proc)
def create_hostnqn(): """Create the hostnqn file to speed up finding out the nqn. By having the /etc/nvme/hostnqn not only do we make sure that that value is always used on this system, but we are also able to just open the file to get the nqn on each get_connector_properties call instead of having to make a call to nvme show-hostnqn command. """ host_nqn = '' try: os.makedirs('/etc/nvme', mode=0o755, exist_ok=True) # Try to get existing nqn generated from dmi or systemd try: host_nqn, err = rootwrap.custom_execute('nvme', 'show-hostnqn') host_nqn = host_nqn.strip() # This is different from OSError's ENOENT, which is missing nvme # command. This ENOENT is when nvme says there isn't an nqn. except putils.ProcessExecutionError as e: if e.exit_code != errno.ENOENT: raise LOG.debug('No nqn could be formed from dmi or systemd.') if not host_nqn: LOG.debug('Generating nqn') host_nqn, err = rootwrap.custom_execute('nvme', 'gen-hostnqn') host_nqn = host_nqn.strip() with open('/etc/nvme/hostnqn', 'w') as f: LOG.debug('Writing hostnqn file') f.write(host_nqn) os.chmod('/etc/nvme/hostnqn', 0o644) except Exception as e: LOG.warning("Could not generate host nqn: %s" % str(e)) return host_nqn
def test_custom_execute_no_check_exit_code(self): out, err = priv_rootwrap.custom_execute('ls', '-y', check_exit_code=False) self.assertEqual('', out) self.assertIsInstance(err, six.string_types)
def test_custom_execute_default_timeout(self, mock_timer): """Confirm timeout defaults to 600 and the thread timer is started.""" priv_rootwrap.custom_execute('echo', 'hola') mock_timer.assert_called_once_with(600, mock.ANY, mock.ANY) mock_timer.return_value.start.assert_called_once_with()