def execute(self): tmp = self._env.copy() futils.add_env_common(tmp, os.environ.copy()) self.file_gdb = tempfile.NamedTemporaryFile(mode='r+') self.file_gdb.writelines('%s' % command for command in self.commands) self.file_gdb.seek(0) run_list = self._prepare_args() try: self._process = sp.run(args=run_list, env=tmp, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True, timeout=self.timeout) except sp.TimeoutExpired: self._close() raise logging.info(self._process.stdout) logging.error(self._process.stderr) self.validate_gdb() self._close()
def exec(self, cmd, *args, expected_exitcode=0, stderr_file=None): """Execute binary in current test context""" tmp = self._env.copy() futils.add_env_common(tmp, os.environ.copy()) # change cmd into list for supbrocess type compliance cmd = [ cmd, ] if sys.platform == 'win32': cmd[0] = os.path.join(self.build.exedir, cmd[0]) + '.exe' else: cmd[0] = os.path.join(self.cwd, cmd[0]) + \ self.build.exesuffix if self.valgrind: cmd = self.valgrind.cmd + cmd # cast all provided args to strings (required by subprocess run()) # so that exec() can accept args of any printable type cmd.extend([str(a) for a in args]) if self.conf.tracer: cmd = shlex.split(self.conf.tracer) + cmd # process stdout and stderr are not redirected - this lets running # tracer command in an interactive session proc = sp.run(cmd, env=tmp, cwd=self.cwd) else: if stderr_file: f = open(os.path.join(self.cwd, stderr_file), 'w') # let's create a dictionary of arguments to the run func run_kwargs = { 'env': tmp, 'cwd': self.cwd, 'timeout': self.conf.timeout, 'stdout': sp.PIPE, 'universal_newlines': True, 'stderr': sp.STDOUT if stderr_file is None else f } proc = sp.run(cmd, **run_kwargs) if stderr_file: f.close() if expected_exitcode is not None and \ proc.returncode != expected_exitcode: futils.fail(proc.stdout, exit_code=proc.returncode) self.msg.print_verbose(proc.stdout)
def exec(self, cmd, *args, expected_exitcode=0): """Execute binary in current test context""" tmp = self._env.copy() futils.add_env_common(tmp, os.environ.copy()) # change cmd into list for supbrocess type compliance cmd = [ cmd, ] if sys.platform == 'win32': cmd[0] = os.path.join(self.build.exedir, cmd[0]) + '.exe' else: cmd[0] = os.path.join(self.cwd, cmd[0]) + \ self.build.exesuffix if self.valgrind: cmd = self.valgrind.cmd + cmd # cast all provided args to strings (required by subprocess run()) # so that exec() can accept args of any printable type cmd.extend([str(a) for a in args]) if self.conf.tracer: cmd = shlex.split(self.conf.tracer) + cmd # process stdout and stderr are not redirected - this lets running # tracer command in an interactive session proc = sp.run(cmd, env=tmp, cwd=self.cwd) else: proc = sp.run(cmd, env=tmp, cwd=self.cwd, timeout=self.conf.timeout, stdout=sp.PIPE, stderr=sp.STDOUT, universal_newlines=True) if expected_exitcode is not None and \ proc.returncode != expected_exitcode: futils.fail(proc.stdout, exit_code=proc.returncode) self.msg.print_verbose(proc.stdout)
def __init__(self, env, build): self.env = env self.build = build global_lib_path = envconfig['GLOBAL_LIB_PATH'] if sys.platform == 'win32': futils.add_env_common(self.env, {'PATH': global_lib_path}) futils.add_env_common(self.env, {'PATH': build.libdir}) else: futils.add_env_common(self.env, {'LD_LIBRARY_PATH': global_lib_path}) futils.add_env_common(self.env, {'LD_LIBRARY_PATH': build.libdir})
def add_env(self, env): """Add environment variables to those stored by context""" futils.add_env_common(self._env, env)
def exec(self, cmd, *args, expected_exitcode=0, stderr_file=None, stdout_file=None): """ Execute binary in the current test context as a separate process. Execution takes place in test cwd and uses environment variables stored in Context 'env' attribute. Timeout for the execution is set based on the execution configuration. Args: cmd (str): command to be executed *args: Variable length command arguments list expected_exitcode (int): if process exit code differs from expected, Fail is thrown. Defaults to 0. Ignored if set to None. stderr_file (str): path to file in which stderr output is stored. Stored in a string if None. Defaults to None. stdout_file (str): path to file in which stdout output is stored. Stored in a string if None. Defaults to None. If neither stderr_file nor stdout_file are set, both outputs are merged into single stdout output and stored in a string. """ tmp = self._env.copy() futils.add_env_common(tmp, os.environ.copy()) # change cmd into list for supbrocess type compliance cmd = [cmd, ] if sys.platform == 'win32': cmd[0] = os.path.join(self.build.exedir, cmd[0]) + '.exe' else: cmd[0] = os.path.join(self.cwd, cmd[0]) + \ self.build.exesuffix if self.valgrind: cmd = self.valgrind.cmd + cmd # cast all provided args to strings (required by subprocess run()) # so that exec() can accept args of any printable type cmd.extend([str(a) for a in args]) if self.conf.tracer: cmd = shlex.split(self.conf.tracer) + cmd # process stdout and stderr are not redirected - this lets running # tracer command in an interactive session proc = sp.run(cmd, env=tmp, cwd=self.cwd) else: if stderr_file: f = open(os.path.join(self.cwd, stderr_file), 'w') # let's create a dictionary of arguments to the run func run_kwargs = { 'env': tmp, 'cwd': self.cwd, 'timeout': self.conf.timeout, 'stdout': sp.PIPE, 'universal_newlines': True, 'stderr': sp.STDOUT if stderr_file is None else f} proc = sp.run(cmd, **run_kwargs) if stderr_file: f.close() if expected_exitcode is not None and \ proc.returncode != expected_exitcode: futils.fail(proc.stdout, exit_code=proc.returncode) if stdout_file is not None: with open(os.path.join(self.cwd, stdout_file), 'w') as f: f.write(proc.stdout) self.msg.print_verbose(proc.stdout)