From f2c5859fde19db12d839c157bff8a7a6853e9420 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Thu, 8 Feb 2018 11:37:05 -0500 Subject: [PATCH] properly handle STDOUT_MAX_BYTES_DISPLAY for system jobs see: https://github.com/ansible/ansible-tower/issues/7890 --- awx/api/serializers.py | 9 ++++++++- .../functional/api/test_unified_jobs_stdout.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 003a80e317..e517955db9 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -2968,7 +2968,14 @@ class SystemJobSerializer(UnifiedJobSerializer): return res def get_result_stdout(self, obj): - return obj.result_stdout + try: + return obj.result_stdout + except StdoutMaxBytesExceeded as e: + return _( + "Standard Output too large to display ({text_size} bytes), " + "only download supported for sizes over {supported_size} bytes").format( + text_size=e.total, supported_size=e.supported + ) class SystemJobCancelSerializer(SystemJobSerializer): diff --git a/awx/main/tests/functional/api/test_unified_jobs_stdout.py b/awx/main/tests/functional/api/test_unified_jobs_stdout.py index 99b5619a7f..6cec0ab4fe 100644 --- a/awx/main/tests/functional/api/test_unified_jobs_stdout.py +++ b/awx/main/tests/functional/api/test_unified_jobs_stdout.py @@ -158,6 +158,24 @@ def test_text_stdout_from_system_job_events(sqlite_copy_expert, get, admin): assert response.data['result_stdout'].splitlines() == ['Testing %d' % i for i in range(3)] +@pytest.mark.django_db +def test_text_stdout_with_max_stdout(sqlite_copy_expert, get, admin): + job = SystemJob() + job.save() + total_bytes = settings.STDOUT_MAX_BYTES_DISPLAY + 1 + large_stdout = 'X' * total_bytes + SystemJobEvent(system_job=job, stdout=large_stdout, start_line=0).save() + url = reverse('api:system_job_detail', kwargs={'pk': job.pk}) + response = get(url, user=admin, expect=200) + assert response.data['result_stdout'] == ( + 'Standard Output too large to display ({actual} bytes), only download ' + 'supported for sizes over {max} bytes'.format( + actual=total_bytes, + max=settings.STDOUT_MAX_BYTES_DISPLAY + ) + ) + + @pytest.mark.django_db @pytest.mark.parametrize('Parent, Child, relation, view', [ [Job, JobEvent, 'job', 'api:job_stdout'],