Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give more detail when we cannot process a non-JSON streamed line #1186

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ansible_runner/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,13 @@ def run(self):
try:
line = self._input.readline()
data = json.loads(line)
except (json.decoder.JSONDecodeError, IOError):
self.status_callback({'status': 'error', 'job_explanation': 'Failed to JSON parse a line from worker stream.'})
except (json.decoder.JSONDecodeError, IOError) as exc:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlanCoding Thanks for the patch. Could you please add a test for this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. I agree.

I just pushed a change where I modified some existing tests. There was effectively already coverage, but I added more detail to the assertions.

self.status_callback({
'status': 'error',
'job_explanation': (
f'Failed to JSON parse a line from worker stream. Error: {exc} Line with invalid JSON data: {line[:1000]}'
)
})
break

if 'status' in data:
Expand Down
31 changes: 22 additions & 9 deletions test/integration/test_transmit_worker_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,15 @@ def test_garbage_private_dir_worker(tmp_path):
_output=outgoing_buffer,
private_data_dir=worker_dir,
)
sent = outgoing_buffer.getvalue()
assert b'"status": "error"' in sent
outgoing_buffer.seek(0)
sent = outgoing_buffer.readline()
data = json.loads(sent)
assert data['status'] == 'error'
assert data['job_explanation'] == 'Failed to extract private data directory on worker.'
assert data['result_traceback']


def test_unparsable_private_dir_worker(tmp_path):
def test_unparsable_line_worker(tmp_path):
worker_dir = tmp_path / 'for_worker'
worker_dir.mkdir()
incoming_buffer = io.BytesIO(b'')
Expand All @@ -384,18 +388,27 @@ def test_unparsable_private_dir_worker(tmp_path):
_output=outgoing_buffer,
private_data_dir=worker_dir,
)
sent = outgoing_buffer.getvalue()
assert b'"status": "error"' in sent
outgoing_buffer.seek(0)
sent = outgoing_buffer.readline()
data = json.loads(sent)
assert data['status'] == 'error'
assert data['job_explanation'] == 'Failed to JSON parse a line from transmit stream.'


def test_unparsable_private_dir_processor(tmp_path):
def test_unparsable_really_big_line_processor(tmp_path):
process_dir = tmp_path / 'for_process'
process_dir.mkdir()
incoming_buffer = io.BytesIO(b'')
incoming_buffer = io.BytesIO(bytes(f'not-json-data with extra garbage:{"f"*10000}', encoding='utf-8'))

def status_receiver(status_data, runner_config):
assert status_data['status'] == 'error'
assert 'Failed to JSON parse a line from worker stream.' in status_data['job_explanation']
assert 'not-json-data with extra garbage:ffffffffff' in status_data['job_explanation']
assert len(status_data['job_explanation']) < 2000

processor = run(
run(
streamer='process',
_input=incoming_buffer,
private_data_dir=process_dir,
status_handler=status_receiver
)
assert processor.status == 'error'