def test_run_and_wait_empty_env(self, mock_run_and_wait): """Test remote_process.run_and_wait() with an empty env.""" process_result = new_process.ProcessResult(['command', '123'], 0, 'output', 60.0, False) mock_run_and_wait.return_value = process_result request = untrusted_runner_pb2.RunAndWaitRequest() request.executable_path = '/path' request.popen_args.env_is_set = True remote_process.run_and_wait(request, None) mock_run_and_wait.assert_called_with(additional_args=[], env={})
def run_and_wait(self, additional_args=None, timeout=None, terminate_before_kill=False, terminate_wait_time=None, input_data=None, max_stdout_len=None, _extra_env=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **popen_args): """Remote version of new_process.ProcessRunner.run_and_wait.""" if stdout != subprocess.PIPE: raise AssertionError if stderr != subprocess.STDOUT: raise AssertionError request = untrusted_runner_pb2.RunAndWaitRequest( executable_path=self.executable_path, timeout=timeout, terminate_before_kill=terminate_before_kill, terminate_wait_time=terminate_wait_time, input_data=input_data, max_stdout_len=max_stdout_len) request.default_args.extend(self.default_args) request.additional_args.extend(additional_args) if 'bufsize' in popen_args: request.popen_args.bufsize = popen_args['bufsize'] if 'executable' in popen_args: request.popen_args.executable = popen_args['executable'] if 'shell' in popen_args: request.popen_args.shell = popen_args['shell'] if 'cwd' in popen_args: request.popen_args.cwd = popen_args['cwd'] passed_env = popen_args.get('env', None) if passed_env is not None: request.popen_args.env_is_set = True # Filter the passed environment to prevent leaking sensitive environment # variables if the caller passes e.g. os.environ.copy(). environment.set_environment_vars(request.popen_args.env, passed_env) response = host.stub().RunAndWait(request) return process_result_from_proto(response.result)
def test_run_and_wait(self, mock_run_and_wait): """Test remote_process.run_and_wait().""" process_result = new_process.ProcessResult(['command', '123'], 0, 'output', 60.0, False) mock_run_and_wait.return_value = process_result request = untrusted_runner_pb2.RunAndWaitRequest() request.executable_path = '/path' request.default_args.extend(['-default_arg']) request.additional_args.extend(['-additional_arg']) request.timeout = 100.0 request.terminate_before_kill = True request.terminate_wait_time = 10.0 request.input_data = 'input' request.max_stdout_len = 1337 request.popen_args.shell = True request.popen_args.env.update({'VAR': 'VAL'}) request.popen_args.env_is_set = True request.popen_args.cwd = '/' response = remote_process.run_and_wait(request, None) result = remote_process_host.process_result_from_proto(response.result) mock_run_and_wait.assert_called_with( additional_args=['-additional_arg'], timeout=100.0, terminate_before_kill=True, terminate_wait_time=10.0, input_data='input', max_stdout_len=1337, cwd='/', env={'VAR': 'VAL'}, shell=True) self.assertEqual(result.command, process_result.command) self.assertEqual(result.return_code, process_result.return_code) self.assertEqual(result.output, process_result.output) self.assertEqual(result.time_executed, process_result.time_executed) self.assertEqual(result.timed_out, process_result.timed_out)