From dede318cf50ae28997446646c6727b212b3dd771 Mon Sep 17 00:00:00 2001 From: Hao Liu <44379968+TheRealHaoLiu@users.noreply.github.com> Date: Fri, 3 Jun 2022 12:28:55 -0400 Subject: [PATCH] skip process isolation check for transmit and process (#1084) * skip process isolation check for transmit and process moving down the process isolation executable so that transmit and process no longer checks for its existance ansible-runner worker will still run the check since it will invoke ansible-runner run with process-isolation-executable and it will still be checked properly Co-Authored-By: Jeff Bradberry <685957+jbradberry@users.noreply.github.com> Signed-off-by: Hao Liu --- ansible_runner/interface.py | 12 ++-- .../test_transmit_worker_process.py | 60 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/ansible_runner/interface.py b/ansible_runner/interface.py index fab592e58..31e386d2d 100644 --- a/ansible_runner/interface.py +++ b/ansible_runner/interface.py @@ -83,12 +83,6 @@ def init_runner(**kwargs): if logfile: output.set_logfile(logfile) - if kwargs.get("process_isolation", False): - pi_executable = kwargs.get("process_isolation_executable", "podman") - if not check_isolation_executable_installed(pi_executable): - print(f'Unable to find process isolation executable: {pi_executable}') - sys.exit(1) - event_callback_handler = kwargs.pop('event_handler', None) status_callback_handler = kwargs.pop('status_handler', None) artifacts_handler = kwargs.pop('artifacts_handler', None) @@ -118,6 +112,12 @@ def init_runner(**kwargs): **kwargs) return stream_processor + if kwargs.get("process_isolation", False): + pi_executable = kwargs.get("process_isolation_executable", "podman") + if not check_isolation_executable_installed(pi_executable): + print(f'Unable to find process isolation executable: {pi_executable}') + sys.exit(1) + kwargs.pop('_input', None) kwargs.pop('_output', None) rc = RunnerConfig(**kwargs) diff --git a/test/integration/test_transmit_worker_process.py b/test/integration/test_transmit_worker_process.py index cabc3c08e..e7e9097a7 100644 --- a/test/integration/test_transmit_worker_process.py +++ b/test/integration/test_transmit_worker_process.py @@ -168,6 +168,66 @@ def process_method(results_sockfile_read): self.check_artifacts(str(process_dir), job_type) + def test_process_isolation_executable_not_exist(self, tmp_path, mocker): + """Case transmit should not fail if process isolation executable does not exist and + worker should fail if process isolation executable does not exist + """ + mocker.patch.object(ansible_runner.interface, 'check_isolation_executable_installed', return_value=False) + + job_kwargs = self.get_job_kwargs('run') + job_kwargs['process_isolation'] = True + job_kwargs['process_isolation_executable'] = 'does_not_exist' + + outgoing_buffer_file = tmp_path / 'buffer_out' + outgoing_buffer_file.touch() + outgoing_buffer = outgoing_buffer_file.open('b+r') + + transmitter = ansible_runner.interface.run( + streamer='transmit', + _output=outgoing_buffer, + **job_kwargs, + ) + + # valide process_isolation kwargs are passed to transmitter + assert transmitter.kwargs['process_isolation'] == job_kwargs['process_isolation'] + assert transmitter.kwargs['process_isolation_executable'] == job_kwargs['process_isolation_executable'] + + # validate that transmit did not fail due to missing process isolation executable + assert transmitter.rc in (None, 0) + + # validate that transmit buffer is not empty + outgoing_buffer.seek(0) + sent = outgoing_buffer.read() + assert sent # should not be blank at least + + # validate buffer contains kwargs + assert b'kwargs' in sent + + # validate kwargs in buffer contain correct process_isolation and process_isolation_executable + for line in sent.decode('utf-8').split('\n'): + if "kwargs" in line: + kwargs = json.loads(line).get("kwargs", {}) + assert kwargs['process_isolation'] == job_kwargs['process_isolation'] + assert kwargs['process_isolation_executable'] == job_kwargs['process_isolation_executable'] + break + + worker_dir = tmp_path / 'for_worker' + incoming_buffer_file = tmp_path / 'buffer_in' + incoming_buffer_file.touch() + incoming_buffer = incoming_buffer_file.open('b+r') + + outgoing_buffer.seek(0) + + # validate that worker fails raise sys.exit(1) when process isolation executable does not exist + with pytest.raises(SystemExit) as exc: + ansible_runner.interface.run( + streamer='worker', + _input=outgoing_buffer, + _output=incoming_buffer, + private_data_dir=worker_dir, + ) + assert exc.value.code == 1 + @pytest.fixture def transmit_stream(project_fixtures, tmp_path):