def test_run_dry_run(self, mock_subprocess_popen): """Test run() in dry_run mode.""" cli_result = cli.run('ls', dry_run=True) mock_subprocess_popen.assert_not_called() mock_subprocess_popen.return_value.communicate.assert_not_called() assert cli_result.stdout == '' assert cli_result.stderr == '' assert cli_result.returncode == 0
def racadm(self, command): result = cli.run('sh {} {}'.format(self.racadm_path, command), raise_exception=False, ignore_error=True) if result.returncode: raise SimpleRACException( 'Error running racadm command: {}, code {}'.format( command, result.returncode)) return result.stdout
def run_omreport(self, command, fmt='xml'): full_command = '{0} {1} -fmt {2}'.format(self.omreport_path, command, fmt) res = run(full_command) if res.returncode: raise OMReportRunException( 'Had trouble running {0}'.format(full_command), res, res.stderr) return res.encode()
def runner(command, _input=''): """ Run a shell command :param command: The shell command to use :param _input: Optional data to pass to stdin :return: """ log.info('Running: %s' % command) r = run(command, ignore_error=True, raise_exception=False, _input=_input) return {'stdout': r.stdout, 'stderr': r.stderr, 'returncode': r.returncode}
def cpu_stress(self, timeout): """ Stress test the cpu on all cores. """ log.info('Stress testing the cpu for %s seconds.' % timeout) cmd = '{0} --cpu {1} -t {2}'.format(self.stress_path, cpu_count(), timeout) log.info('Executing: {}'.format(cmd)) result = cli.run(cmd) if result.returncode: self.killall()
def query_mcelog_daemon(mcelog_path='mcelog'): """ Used to expose memory error counts :param mcelog_path: :return: """ mcelog = cli.find_in_path(mcelog_path) if not mcelog: raise MercuryGeneralException('Could not find mcelog') result = cli.run(f'{mcelog} --client', raise_exception=False) return result.stdout
def _extract(tarball_path, extract_path): os.makedirs(extract_path) log.info('Extracting {0}'.format(tarball_path)) cmd = 'tar --strip-components=1 -xvf {0} -C {1}'.format(tarball_path, extract_path) result = cli.run(cmd) if result.returncode: log.error('Could not extract archive: {0}'.format(result)) return False return True
def get_system_memory(): """ Gets the amount of system memory in a server. """ log.info('Getting the system memory') cmd = 'grep MemFree /proc/meminfo' result = cli.run(cmd) if not result.returncode: k = result.stdout.split()[1].strip() g = int(k) / 1024 / 1024 return '{}G'.format(int(g))
def run(self, cmd, ignore_error=False): """ Run a storecli command :param cmd: A valid storcli command :param ignore_error: Do not raise an exception on non-zero return code :return: AttributeString """ result = cli.run('{} {}'.format(self.storecli_path, cmd), ignore_error=ignore_error) if not ignore_error: if result.returncode: raise StorcliException( 'Command returned: {}, Error: {}'.format( result.returncode, result)) return result
def _check_all(): update_list = list() check_args = ' -q -c' for installer in os.listdir(firmware_path): check_cmd = os.path.join(firmware_path, installer) check_cmd = check_cmd + check_args result = cli.run(check_cmd) if result.returncode == 5: log.info('{0} is not applicable'.format(installer)) elif result.returncode == 3: log.info('{0} is already updated'.format(installer)) else: log.info('{0} update must run'.format(installer)) update_list.append(installer) return update_list
def test_run(self, mock_subprocess_popen, mock_os_environ): """Test run()""" mock_subprocess_popen.return_value.returncode = 0 mock_subprocess_popen.return_value.communicate.return_value = ('out', 'err') mock_os_environ.return_value = {'key': 'val'} cli_result = cli.run('ls') mock_subprocess_popen.assert_called_once_with(['ls'], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1048567, env={'key': 'val'}) mock_subprocess_popen.return_value.communicate.assert_called_once_with( input=None) assert cli_result.stdout == 'out' assert cli_result.stderr == 'err' assert cli_result.returncode == 0
def test_run_with_input(self, mock_subprocess_popen, mock_os_environ): """Test run() with data passed into stdin.""" mock_subprocess_popen.return_value.returncode = 0 mock_subprocess_popen.return_value.communicate.return_value = ('foo', '') mock_os_environ.return_value = {'key': 'val'} cli_result = cli.run('python', _input='print "foo"') mock_subprocess_popen.assert_called_once_with(['python'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1048567, env={'key': 'val'}) mock_subprocess_popen.return_value.communicate.assert_called_once_with( input='print "foo"') assert cli_result.stdout == 'foo' assert cli_result.stderr == '' assert cli_result.returncode == 0
def memory_stress(self, timeout, amount=None): """ Stress test the memory half of free memory. Amount should be in 1024K, 1024M, 1024G format. """ # If we did not pass in a specific memory lets # try and run against all system memory, but if that # fails we default to 1024M. if not amount: amount = self.get_system_memory() if not amount: amount = '1024M' log.info('Stressing memory for %s seconds at %s.' % (timeout, amount)) cmd = '{0} --vm 1 --vm-bytes {1} --vm-hang 5 -t {2}'.format( self.stress_path, amount, timeout) log.info('Executing: {}'.format(cmd)) result = cli.run(cmd) if result.returncode: self.killall()
def get_lldp_info(interface, lldplight_path): command = '{} {}'.format(lldplight_path, interface) result = cli.run(command, raise_exception=False, quiet=True, ignore_error=True) if result.returncode == 5: log.debug( 'Timed out waiting for LLDP broadcast on {}'.format(interface)) return None elif result.returncode: log.error("Problem running lldplite: {}".format(result.stderr)) return None switch_name = result.split()[0] port_number = int(result.split()[1].split('/')[-1]) log.info('LLDP info: {} {}'.format(switch_name, port_number)) return {'switch_name': switch_name, 'port_number': port_number}
def ip(self, args): command = '%s %s' % (self.ip_path, args) return cli.run(command)
def extract_tar_archive(tarball_path, extract_path): os.makedirs(extract_path) cmd = 'tar --strip-components=1 -xvf {0} -C {1}'.format( tarball_path, extract_path) return cli.run(cmd)
def run_cmd_and_parse_xml(cmd, xml_element): result = cli.run(cmd).stdout return xml_to_dict(result, xml_element)
def megacli(self, args, bufsize=1048567): cmd = [self.megacli_bin] + args.split() cmd = cmd + ['-NoLog'] out = run(' '.join(cmd), bufsize=bufsize) return out, out.stderr, out.returncode
def run(self, args, bufsize=1048567, raise_on_error=True, ignore_error=False): cmd = self.ipmitool_path + ' ' + args return cli.run(cmd, bufsize=bufsize, raise_exception=raise_on_error, ignore_error=ignore_error)
def get_mcelog_journal_stream(): p = cli.run(JOURNALCTL_COMMAND, raw=True) if p.returncode: raise MercuryGeneralException('Error getting mcelog') return p.stdout
def hpasm_run(self, command): result = cli.run('{} -s \'{}\''.format(self.hpasmcli, command)) if result.returncode: raise HPAsmException('Error running command: {}'.format(command)) return result
def _hpsum_cmd(cmd): # TODO: Figure out how to have cli.run() support timeouts # Agent probably already supports timeout for capabilities, verify! hpsum_exec_path = os.path.join(firmware_path, 'smartupdate') command = '{0} /s {1}'.format(hpsum_exec_path, cmd) return cli.run(command)