blob: 224f97b4e9c0db541f397492586f01bbd3acd7a2 [file] [log] [blame]
Matthew Hartfb6fd362020-03-04 21:03:59 +00001#!/usr/bin/env python3
2
3from __future__ import print_function
4
5__copyright__ = """
6/*
Xinyu Zhang78c146a2022-09-05 19:06:40 +08007 * Copyright (c) 2020-2022, Arm Limited. All rights reserved.
Matthew Hartfb6fd362020-03-04 21:03:59 +00008 *
9 * SPDX-License-Identifier: BSD-3-Clause
10 *
11 */
12 """
13
14"""
15Script for waiting for LAVA jobs and parsing the results
16"""
17
18import os
Matthew Hartfb6fd362020-03-04 21:03:59 +000019import time
20import yaml
21import argparse
Xinyu Zhangc8a670c2021-05-18 20:20:53 +080022import shutil
Paul Sokolovskya95abd92022-12-27 13:48:11 +030023import logging
Matthew Hartfb6fd362020-03-04 21:03:59 +000024from jinja2 import Environment, FileSystemLoader
Matthew Hartfb6fd362020-03-04 21:03:59 +000025from lava_helper import test_lava_dispatch_credentials
Xinyu Zhangc918b6e2022-10-08 17:13:17 +080026from lava_submit_jobs import submit_lava_jobs
Paul Sokolovsky2512ec52022-03-04 00:15:39 +030027import codecov_helper
28
Matthew Hartfb6fd362020-03-04 21:03:59 +000029
Paul Sokolovskya95abd92022-12-27 13:48:11 +030030_log = logging.getLogger("lavaci")
31
32
Matthew Hartfb6fd362020-03-04 21:03:59 +000033def wait_for_jobs(user_args):
34 job_list = user_args.job_ids.split(",")
35 job_list = [int(x) for x in job_list if x != '']
36 lava = test_lava_dispatch_credentials(user_args)
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080037 finished_jobs = get_finished_jobs(job_list, user_args, lava)
Xinyu Zhangc8a670c2021-05-18 20:20:53 +080038 resubmit_jobs = resubmit_failed_jobs(finished_jobs, user_args)
Paul Sokolovskyc87beee2022-04-30 08:50:47 +030039 if resubmit_jobs:
Paul Sokolovskyf3674562022-12-27 22:20:01 +030040 _log.info("Waiting for resubmitted jobs: %s", resubmit_jobs)
Paul Sokolovskyc87beee2022-04-30 08:50:47 +030041 finished_resubmit_jobs = get_finished_jobs(resubmit_jobs, user_args, lava)
42 finished_jobs.update(finished_resubmit_jobs)
Paul Sokolovsky451f67b2022-03-08 19:44:41 +030043 return finished_jobs
44
Paul Sokolovsky451f67b2022-03-08 19:44:41 +030045def process_finished_jobs(finished_jobs, user_args):
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080046 print_lava_urls(finished_jobs, user_args)
Paul Sokolovsky451f67b2022-03-08 19:44:41 +030047 test_report(finished_jobs, user_args)
Xinyu Zhang82dab282022-10-09 16:33:19 +080048 job_links(finished_jobs, user_args)
Paul Sokolovsky2512ec52022-03-04 00:15:39 +030049 codecov_helper.coverage_reports(finished_jobs, user_args)
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080050
51def get_finished_jobs(job_list, user_args, lava):
Paul Sokolovskya95abd92022-12-27 13:48:11 +030052 _log.info("Waiting for %d LAVA jobs", len(job_list))
Paul Sokolovsky697f9552022-05-05 10:44:27 +030053 finished_jobs = lava.block_wait_for_jobs(job_list, user_args.dispatch_timeout, 5)
Matthew Hartfb6fd362020-03-04 21:03:59 +000054 unfinished_jobs = [item for item in job_list if item not in finished_jobs]
55 for job in unfinished_jobs:
Paul Sokolovskyf3674562022-12-27 22:20:01 +030056 _log.info("Cancelling unfinished job %d", job)
Matthew Hartfb6fd362020-03-04 21:03:59 +000057 lava.cancel_job(job)
58 if user_args.artifacts_path:
59 for job, info in finished_jobs.items():
60 info['job_dir'] = os.path.join(user_args.artifacts_path, "{}_{}".format(str(job), info['description']))
61 finished_jobs[job] = info
62 finished_jobs = fetch_artifacts(finished_jobs, user_args, lava)
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080063 return finished_jobs
Matthew Hartfb6fd362020-03-04 21:03:59 +000064
Xinyu Zhangc8a670c2021-05-18 20:20:53 +080065def resubmit_failed_jobs(jobs, user_args):
66 if not jobs:
67 return []
Xinyu Zhang4aca6d02021-05-31 11:43:32 +080068 time.sleep(2) # be friendly to LAVA
Xinyu Zhangc8a670c2021-05-18 20:20:53 +080069 failed_job = []
70 os.makedirs('failed_jobs', exist_ok=True)
71 for job_id, info in jobs.items():
72 if not (info['health'] == "Complete" and info['state'] == "Finished"):
Paul Sokolovskyb7a41a92022-12-28 18:06:45 +030073 _log.warning(
74 "Will resubmit job %d because of its state: %s, health: %s",
Paul Sokolovsky7fa6c9e2022-12-30 15:01:49 +030075 job_id, info["state"], info["health"]
Paul Sokolovskyb7a41a92022-12-28 18:06:45 +030076 )
Xinyu Zhangc8a670c2021-05-18 20:20:53 +080077 job_dir = info['job_dir']
78 def_path = os.path.join(job_dir, 'definition.yaml')
79 os.rename(def_path, 'failed_jobs/{}_definition.yaml'.format(job_id))
80 shutil.rmtree(job_dir)
81 failed_job.append(job_id)
82 for failed_job_id in failed_job:
83 jobs.pop(failed_job_id)
Xinyu Zhangc918b6e2022-10-08 17:13:17 +080084 resubmitted_jobs = submit_lava_jobs(user_args, job_dir='failed_jobs')
Xinyu Zhangc8a670c2021-05-18 20:20:53 +080085 resubmitted_jobs = [int(x) for x in resubmitted_jobs if x != '']
86 return resubmitted_jobs
87
Matthew Hartfb6fd362020-03-04 21:03:59 +000088def fetch_artifacts(jobs, user_args, lava):
89 if not user_args.artifacts_path:
90 return
91 for job_id, info in jobs.items():
92 job_dir = info['job_dir']
Paul Sokolovskydc8281a2022-12-27 21:54:42 +030093 t = time.time()
94 _log.info("Fetching artifacts for job %d to %s", job_id, job_dir)
Paul Sokolovskyce546192023-01-03 21:28:08 +030095
96 for retry in range(3, 0, -1):
97 try:
98 os.makedirs(job_dir, exist_ok=True)
99 def_path = os.path.join(job_dir, 'definition.yaml')
100 target_log = os.path.join(job_dir, 'target_log.txt')
101 config = os.path.join(job_dir, 'config.tar.bz2')
102 results_file = os.path.join(job_dir, 'results.yaml')
103 definition = lava.get_job_definition(job_id, def_path)
104 jobs[job_id]['metadata'] = definition.get('metadata', [])
105 time.sleep(0.2) # be friendly to LAVA
106 lava.get_job_log(job_id, target_log)
107 time.sleep(0.2)
108 lava.get_job_config(job_id, config)
109 time.sleep(0.2)
110 lava.get_job_results(job_id, results_file)
111 break
112 except IOError as e:
113 if retry == 1:
114 raise
115 else:
116 _log.warning("fetch_artifacts: Error %r occurred, retrying", e)
117 time.sleep(2)
118
Paul Sokolovskydc8281a2022-12-27 21:54:42 +0300119 _log.info("Fetched artifacts in %ds", time.time() - t)
Paul Sokolovskyc2d6d882022-02-25 19:11:18 +0300120 codecov_helper.extract_trace_data(target_log, job_dir)
Matthew Hartfb6fd362020-03-04 21:03:59 +0000121 return(jobs)
122
123
124def lava_id_to_url(id, user_args):
125 return "{}/scheduler/job/{}".format(user_args.lava_url, id)
126
Xinyu Zhang97ee3fd2020-12-14 14:45:06 +0800127def job_links(jobs, user_args):
128 job_links = ""
129 for job, info in jobs.items():
Xinyu Zhang82dab282022-10-09 16:33:19 +0800130 job_links += "\nLAVA Test Config:\n"
131 job_links += "Config Name: {}\n".format(info['metadata']['build_name'])
132 job_links += "Test Result: {}\n".format(info['result'])
133 job_links += "Device Type: {}\n".format(info['metadata']['device_type'])
Xinyu Zhang97ee3fd2020-12-14 14:45:06 +0800134 job_links += "Build link: {}\n".format(info['metadata']['build_job_url'])
Xinyu Zhang78c146a2022-09-05 19:06:40 +0800135 job_links += "LAVA link: {}\n".format(lava_id_to_url(job, user_args))
Xinyu Zhang82dab282022-10-09 16:33:19 +0800136 job_links += "TFM LOG: {}artifact/{}/target_log.txt\n".format(os.getenv("BUILD_URL"), info['job_dir'])
Xinyu Zhang97ee3fd2020-12-14 14:45:06 +0800137 print(job_links)
138
Matthew Hartfb6fd362020-03-04 21:03:59 +0000139def remove_lava_dupes(results):
140 for result in results:
141 if result['result'] != 'pass':
142 if result['suite'] == "lava":
143 for other in [x for x in results if x != result]:
144 if other['name'] == result['name']:
145 if other['result'] == 'pass':
146 results.remove(result)
147 return(results)
148
Paul Sokolovsky451f67b2022-03-08 19:44:41 +0300149def test_report(jobs, user_args):
Matthew Hartfb6fd362020-03-04 21:03:59 +0000150 # parsing of test results is WIP
151 fail_j = []
152 jinja_data = []
153 for job, info in jobs.items():
Xinyu Zhang0f78e7a2022-10-17 13:55:52 +0800154 info['result'] = 'SUCCESS'
Xinyu Zhang82dab282022-10-09 16:33:19 +0800155 if info['health'] != 'Complete':
Xinyu Zhang0f78e7a2022-10-17 13:55:52 +0800156 info['result'] = 'FAILURE'
Xinyu Zhang82dab282022-10-09 16:33:19 +0800157 fail_j.append(job)
158 continue
Matthew Hart4a4f1202020-06-12 15:52:46 +0100159 results_file = os.path.join(info['job_dir'], 'results.yaml')
160 if not os.path.exists(results_file) or (os.path.getsize(results_file) == 0):
Xinyu Zhang0f78e7a2022-10-17 13:55:52 +0800161 info['result'] = 'FAILURE'
Matthew Hart4a4f1202020-06-12 15:52:46 +0100162 fail_j.append(job)
163 continue
164 with open(results_file, "r") as F:
165 res_data = F.read()
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300166 results = yaml.safe_load(res_data)
Paul Sokolovsky07f6dfb2022-07-15 12:26:24 +0300167 non_lava_results = [x for x in results if x['suite'] != 'lava' or x['name'] == 'lava-test-monitor']
Matthew Hartfb6fd362020-03-04 21:03:59 +0000168 info['lava_url'] = lava_id_to_url(job, user_args)
Arthur She38d5f5a2022-09-02 17:32:14 -0700169 info['artifacts_dir'] = info['job_dir']
Matthew Hartfb6fd362020-03-04 21:03:59 +0000170 jinja_data.append({job: [info, non_lava_results]})
171 for result in non_lava_results:
Paul Sokolovsky58f00de2022-02-01 00:26:32 +0300172 if result['result'] == 'fail':
Xinyu Zhang0f78e7a2022-10-17 13:55:52 +0800173 info['result'] = 'FAILURE'
Matthew Hartfb6fd362020-03-04 21:03:59 +0000174 fail_j.append(job) if job not in fail_j else fail_j
175 time.sleep(0.5) # be friendly to LAVA
Matthew Hartfb6fd362020-03-04 21:03:59 +0000176 data = {}
177 data['jobs'] = jinja_data
178 render_jinja(data)
179
180def render_jinja(data):
181 work_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "jinja2_templates")
182 template_loader = FileSystemLoader(searchpath=work_dir)
183 template_env = Environment(loader=template_loader)
184 html = template_env.get_template("test_summary.jinja2").render(data)
185 csv = template_env.get_template("test_summary_csv.jinja2").render(data)
186 with open('test_summary.html', "w") as F:
187 F.write(html)
188 with open('test_summary.csv', "w") as F:
189 F.write(csv)
190
191def print_lava_urls(jobs, user_args):
192 output = [lava_id_to_url(x, user_args) for x in jobs]
Xinyu Zhang78c146a2022-09-05 19:06:40 +0800193 info_print("LAVA jobs triggered for this build: {}".format(output))
Matthew Hartfb6fd362020-03-04 21:03:59 +0000194
195
Xinyu Zhang78c146a2022-09-05 19:06:40 +0800196def info_print(line, silent=True):
197 if not silent:
198 print("INFO: {}".format(line))
Matthew Hartfb6fd362020-03-04 21:03:59 +0000199
Paul Sokolovskyde25e1f2023-01-02 14:29:21 +0300200
201# WARNING: Setting this to >1 is a last resort, temporary stop-gap measure,
202# which will overload LAVA and jeopardize stability of the entire TF CI.
203INEFFICIENT_RETRIES = 1
204
205
Matthew Hartfb6fd362020-03-04 21:03:59 +0000206def main(user_args):
207 """ Main logic """
Paul Sokolovskyde25e1f2023-01-02 14:29:21 +0300208 for try_time in range(INEFFICIENT_RETRIES):
Xinyu Zhang3e8f6602021-04-28 10:57:32 +0800209 try:
Paul Sokolovsky451f67b2022-03-08 19:44:41 +0300210 finished_jobs = wait_for_jobs(user_args)
Xinyu Zhang3e8f6602021-04-28 10:57:32 +0800211 break
212 except Exception as e:
Paul Sokolovskyde25e1f2023-01-02 14:29:21 +0300213 if try_time < INEFFICIENT_RETRIES - 1:
Paul Sokolovskyf3674562022-12-27 22:20:01 +0300214 _log.exception("Exception in wait_for_jobs")
215 _log.info("Will try to get LAVA jobs again, this was try: %d", try_time)
Xinyu Zhang3e8f6602021-04-28 10:57:32 +0800216 else:
217 raise e
Paul Sokolovsky451f67b2022-03-08 19:44:41 +0300218 process_finished_jobs(finished_jobs, user_args)
Matthew Hartfb6fd362020-03-04 21:03:59 +0000219
220def get_cmd_args():
221 """ Parse command line arguments """
222
223 # Parse command line arguments to override config
224 parser = argparse.ArgumentParser(description="Lava Wait Jobs")
225 cmdargs = parser.add_argument_group("Lava Wait Jobs")
226
227 # Configuration control
228 cmdargs.add_argument(
229 "--lava-url", dest="lava_url", action="store", help="LAVA lab URL (without RPC2)"
230 )
231 cmdargs.add_argument(
232 "--job-ids", dest="job_ids", action="store", required=True, help="Comma separated list of job IDS"
233 )
234 cmdargs.add_argument(
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +0800235 "--lava-token", dest="lava_token", action="store", help="LAVA auth token"
Matthew Hartfb6fd362020-03-04 21:03:59 +0000236 )
237 cmdargs.add_argument(
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +0800238 "--lava-user", dest="lava_user", action="store", help="LAVA username"
Matthew Hartfb6fd362020-03-04 21:03:59 +0000239 )
240 cmdargs.add_argument(
241 "--use-env", dest="token_from_env", action="store_true", default=False, help="Use LAVA auth info from environment"
242 )
243 cmdargs.add_argument(
244 "--lava-timeout", dest="dispatch_timeout", action="store", type=int, default=3600, help="Time in seconds to wait for all jobs"
245 )
246 cmdargs.add_argument(
247 "--artifacts-path", dest="artifacts_path", action="store", help="Download LAVA artifacts to this directory"
248 )
249 return parser.parse_args()
250
251
252if __name__ == "__main__":
Paul Sokolovskya95abd92022-12-27 13:48:11 +0300253 logging.basicConfig(level=logging.INFO)
Matthew Hartfb6fd362020-03-04 21:03:59 +0000254 main(get_cmd_args())