From 37bb42087e4f5539041e0bc61e8c791f157f90e9 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Wed, 7 Oct 2020 12:00:10 -0400 Subject: [PATCH 1/2] Start on integration test for transmit worker process cmds --- .../test_transmit_worker_process.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 test/integration/test_transmit_worker_process.py diff --git a/test/integration/test_transmit_worker_process.py b/test/integration/test_transmit_worker_process.py new file mode 100644 index 000000000..0345cb895 --- /dev/null +++ b/test/integration/test_transmit_worker_process.py @@ -0,0 +1,61 @@ +import os +import io + +from ansible_runner.streaming import Transmitter, Worker, Processor + + +def peek_contents(buffer): + pos = buffer.tell() + content = buffer.read() + buffer.seek(pos) + return content + + +def test_remote_job_interface(tmpdir, test_data_dir): + worker_dir = str(tmpdir.mkdir('for_worker')) + process_dir = str(tmpdir.mkdir('for_process')) + + original_dir = os.path.join(test_data_dir, 'debug') + + outgoing_buffer = io.BytesIO() + + # Intended AWX and Tower use case + transmitter = Transmitter( + _output = outgoing_buffer, + private_data_dir = original_dir, + playbook = 'debug.yml' + ) + + print(transmitter.kwargs) + assert transmitter.kwargs.get('playbook', '') == 'debug.yml' + + status, rc = transmitter.run() + assert rc in (None, 0) + assert status == 'unstarted' + + outgoing_buffer.seek(0) # rewind so we can start reading + + sent = peek_contents(outgoing_buffer) + assert sent # should not be blank at least + assert b'zipfile' in sent + + incoming_buffer = io.BytesIO() + + worker = Worker( + _input = outgoing_buffer, + _output = incoming_buffer, + private_data_dir = worker_dir + ) + worker.run() + + assert set(os.listdir(worker_dir)) == set(['artifacts', 'inventory', 'project']), outgoing_buffer.getvalue() + + incoming_buffer.seek(0) # again, be kind, rewind + + processor = Processor( + _input = incoming_buffer, + private_data_dir = process_dir + ) + processor.run() + + assert os.listdir(process_dir) == ['project', 'artifacts'], outgoing_buffer.getvalue() From 22be6c01991178f3baa24a0ce12fa9613e1dddcc Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Wed, 7 Oct 2020 12:07:10 -0400 Subject: [PATCH 2/2] Get test running to finish --- ansible_runner/streaming.py | 4 ++-- test/integration/test_transmit_worker_process.py | 11 ++--------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/ansible_runner/streaming.py b/ansible_runner/streaming.py index d8c248909..a98a2d3c2 100644 --- a/ansible_runner/streaming.py +++ b/ansible_runner/streaming.py @@ -200,10 +200,10 @@ def event_callback(self, event_data): def artifacts_callback(self, artifacts_data): buf = io.BytesIO(artifacts_data) with zipfile.ZipFile(buf, 'r') as archive: - archive.extractall(path=self.config.artifact_dir) + archive.extractall(path=self.artifact_dir) if self.artifacts_handler is not None: - self.artifacts_handler(self.config.artifact_dir) + self.artifacts_handler(self.artifact_dir) def run(self): job_events_path = os.path.join(self.artifact_dir, 'job_events') diff --git a/test/integration/test_transmit_worker_process.py b/test/integration/test_transmit_worker_process.py index 0345cb895..1d7260a0a 100644 --- a/test/integration/test_transmit_worker_process.py +++ b/test/integration/test_transmit_worker_process.py @@ -4,13 +4,6 @@ from ansible_runner.streaming import Transmitter, Worker, Processor -def peek_contents(buffer): - pos = buffer.tell() - content = buffer.read() - buffer.seek(pos) - return content - - def test_remote_job_interface(tmpdir, test_data_dir): worker_dir = str(tmpdir.mkdir('for_worker')) process_dir = str(tmpdir.mkdir('for_process')) @@ -35,7 +28,7 @@ def test_remote_job_interface(tmpdir, test_data_dir): outgoing_buffer.seek(0) # rewind so we can start reading - sent = peek_contents(outgoing_buffer) + sent = outgoing_buffer.getvalue() assert sent # should not be blank at least assert b'zipfile' in sent @@ -58,4 +51,4 @@ def test_remote_job_interface(tmpdir, test_data_dir): ) processor.run() - assert os.listdir(process_dir) == ['project', 'artifacts'], outgoing_buffer.getvalue() + assert set(os.listdir(process_dir)) == set(['artifacts']), outgoing_buffer.getvalue()