def test_command_failed_exception(self): returncode = 1 cmd = "foo" stdout = "output" stderr = "error" try: raise exceptions.CommandFailed(returncode, cmd, stdout, stderr) except exceptions.CommandFailed as e: self.assertIn(str(returncode), str(e)) self.assertIn(cmd, str(e)) self.assertIn(stdout, str(e)) self.assertIn(stderr, str(e))
def execute(cmd, fail_ok=False, merge_stderr=False): """Executes specified command for the given action.""" cmdlist = shlex.split(cmd) result = '' result_err = '' stdout = subprocess.PIPE stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE proc = subprocess.Popen(cmdlist, stdout=stdout, stderr=stderr) result, result_err = proc.communicate() result = result.decode('utf-8') if not fail_ok and proc.returncode != 0: raise exceptions.CommandFailed(proc.returncode, cmd, result, result_err) return result
def execute(cmd, action, flags='', params='', fail_ok=False, merge_stderr=False, cli_dir='/usr/bin'): """Executes specified command for the given action. :param cmd: command to be executed :type cmd: string :param action: string of the cli command to run :type action: string :param flags: any optional cli flags to use :type flags: string :param params: string of any optional positional args to use :type params: string :param fail_ok: boolean if True an exception is not raised when the cli return code is non-zero :type fail_ok: boolean :param merge_stderr: boolean if True the stderr buffer is merged into stdout :type merge_stderr: boolean :param cli_dir: The path where the cmd can be executed :type cli_dir: string """ cmd = ' '.join([os.path.join(cli_dir, cmd), flags, action, params]) LOG.info("running: '%s'" % cmd) if six.PY2: cmd = cmd.encode('utf-8') cmd = shlex.split(cmd) result = '' result_err = '' stdout = subprocess.PIPE stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) result, result_err = proc.communicate() if not fail_ok and proc.returncode != 0: raise exceptions.CommandFailed(proc.returncode, cmd, result, result_err) if six.PY2: return result else: return os.fsdecode(result)
def _cmd(self, cmd, param): """Executes specified command.""" cmd = ' '.join([cmd, param]) LOG.info("running: '%s'" % cmd) cmd_str = cmd cmd = shlex.split(cmd.encode('utf-8')) result = '' result_err = '' try: stdout = subprocess.PIPE stderr = subprocess.PIPE proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) result, result_err = proc.communicate() if proc.returncode != 0: LOG.debug('error of %s:\n%s' % (cmd_str, result_err)) raise exceptions.CommandFailed(proc.returncode, cmd, result) finally: LOG.debug('output of %s:\n%s' % (cmd_str, result)) return proc.returncode
def gnocchi(self, action, flags='', params='', fail_ok=False, merge_stderr=False, input=None): creds = ("--os-auth-plugin gnocchi-noauth " "--user-id %s --project-id %s " "--endpoint %s") % (self.user_id, self.project_id, self.endpoint) flags = creds + ' ' + flags # FIXME(sileht): base.execute is broken in py3 in tempest-lib # see: https://review.openstack.org/#/c/218870/ # return base.execute("gnocchi", action, flags, params, fail_ok, # merge_stderr, self.cli_dir) cmd = "gnocchi" # from fixed tempestlib cmd = ' '.join( [os.path.join(self.cli_dir, cmd), flags, action, params]) if six.PY2: cmd = cmd.encode('utf-8') cmd = shlex.split(cmd) result = '' result_err = '' stdin = None if input is None else subprocess.PIPE stdout = subprocess.PIPE stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE proc = subprocess.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr) result, result_err = proc.communicate(input=input) if not fail_ok and proc.returncode != 0: raise exceptions.CommandFailed(proc.returncode, cmd, result, result_err) if six.PY2: return result else: return os.fsdecode(result)