From 20c14695952f91320fbde0b8a05549943c1cf6fe Mon Sep 17 00:00:00 2001 From: Harvey Lynden Date: Mon, 2 Dec 2024 11:05:40 +0100 Subject: [PATCH] Avocado Jobs Keys Fix: Handle missing job data in 'avocado jobs list' command - Updated 'handle_list_command' to use `job.get()` for safely accessing job data. - If a key is missing, it now defaults to 'N/A' instead of causing a crash. Reference: https://github.com/avocado-framework/avocado/issues/6067 Signed-off-by: Harvey Lynden --- avocado/plugins/jobs.py | 46 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/avocado/plugins/jobs.py b/avocado/plugins/jobs.py index c1b5fa8381..af18168795 100644 --- a/avocado/plugins/jobs.py +++ b/avocado/plugins/jobs.py @@ -56,14 +56,25 @@ def _print_job_tests(tests): for test in tests: status = test.get("status") decorator = output.TEST_STATUS_DECORATOR_MAPPING.get(status) - # Retrieve "end" for backward compatibility - end = datetime.fromtimestamp(test.get("actual_end", test.get("end"))) + + end_timestamp = test.get("actual_end", test.get("end")) + if end_timestamp is not None: + end = datetime.fromtimestamp(end_timestamp).strftime(date_fmt) + else: + end = "N/A" + + time_taken = test.get("time") + if time_taken is not None: + time_str = f"{float(time_taken):5f}" + else: + time_str = "N/A" + test_matrix.append( ( - test.get("id"), - end.strftime(date_fmt), - f"{float(test.get('time')):5f}", - decorator(status, ""), + test.get("id", "N/A"), + end, + time_str, + decorator(status, "") if decorator else "N/A", ) ) header = ( @@ -116,15 +127,24 @@ def handle_list_command(jobs_results): for filename in jobs_results.values(): with open(filename, "r", encoding="utf-8") as fp: job = json.load(fp) + + job_id = job.get("job_id", "N/A") + start_time = job.get("start", "N/A") + total_tests = job.get("total", "N/A") + passed = job.get("pass", "N/A") + skipped = job.get("skip", "N/A") + errors = job.get("errors", "N/A") + failures = job.get("failures", "N/A") + LOG_UI.info( "%-40s %-26s %3s (%s/%s/%s/%s)", - job["job_id"], - job["start"], - job["total"], - job["pass"], - job["skip"], - job["errors"], - job["failures"], + job_id, + start_time, + total_tests, + passed, + skipped, + errors, + failures, ) return exit_codes.AVOCADO_ALL_OK