blob: cfee0ed03c165a70e1a63c33f72608a434588bb5 [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 Zhang28d61b42022-03-21 16:46:35 +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"""
Fathi Boudra13b7eba2020-11-26 10:29:53 +010015Script to create LAVA definitions from a single tf-m-build-config Jenkins Job
Matthew Hartfb6fd362020-03-04 21:03:59 +000016"""
17
18import os
19import sys
Matthew Hartfb6fd362020-03-04 21:03:59 +000020import argparse
Matthew Hartfb6fd362020-03-04 21:03:59 +000021from jinja2 import Environment, FileSystemLoader
22from lava_helper_configs import *
23
Matthew Hartfb6fd362020-03-04 21:03:59 +000024
Matthew Hartfb6fd362020-03-04 21:03:59 +000025def get_recovery_url(recovery_store_url, recovery):
Dean Birch5d2dc572020-05-29 13:15:59 +010026 return "{}/{}".format(recovery_store_url.rstrip('/'), recovery)
Matthew Hartfb6fd362020-03-04 21:03:59 +000027
28
Xinyu Zhangc918b6e2022-10-08 17:13:17 +080029def load_config_overrides(user_args, config_key):
30 """Load a configuration from multiple locations and override it with user provided
31 arguments"""
32
33 print("Using built-in config: %s" % config_key)
34 try:
35 config = lava_gen_config_map[config_key]
36 except KeyError:
37 print("No template found for config: %s" % config_key)
38 sys.exit(1)
39
Xinyu Zhangc918b6e2022-10-08 17:13:17 +080040 # Add the template folder
41 config["templ"] = os.path.join(user_args.template_dir, config["templ"])
42 return config
43
44
Matthew Hartfb6fd362020-03-04 21:03:59 +000045def generate_test_definitions(config, work_dir, user_args):
Fathi Boudra13b7eba2020-11-26 10:29:53 +010046 """Get a dictionary configuration and an existing jinja2 template to generate
47 a LAVA compatible yaml definition"""
Matthew Hartfb6fd362020-03-04 21:03:59 +000048
49 template_loader = FileSystemLoader(searchpath=work_dir)
50 template_env = Environment(loader=template_loader)
Dean Birch5d2dc572020-05-29 13:15:59 +010051 recovery_store_url = config.get('recovery_store_url', '')
Matthew Hartfb6fd362020-03-04 21:03:59 +000052 template_file = config.pop("templ")
53
54 definitions = {}
55
56 for platform, recovery in config["platforms"].items():
Xinyu Zhang22a12752022-10-10 17:21:21 +080057 if platform != os.getenv('TFM_PLATFORM'):
Matthew Hartfb6fd362020-03-04 21:03:59 +000058 continue
59 recovery_image_url = get_recovery_url(recovery_store_url, recovery)
Xinyu Zhang22a12752022-10-10 17:21:21 +080060 if os.getenv("TEST_REGRESSION") == "True":
61 monitor_name = "reg_tests"
62 elif os.getenv("TEST_PSA_API") != "OFF":
63 monitor_name = "arch_tests"
64 else:
65 monitor_name = "no_reg_tests"
66 params = {
67 "job_name": "{}_{}_{}".format(os.getenv('CONFIG_NAME'), os.getenv("BUILD_NUMBER"), config["device_type"]),
68 "build_name": os.getenv('CONFIG_NAME'),
69 "device_type": config["device_type"],
70 "job_timeout": config["job_timeout"],
71 "action_timeout": config.get("action_timeout", ''),
72 "monitor_timeout": config.get("monitor_timeout", ''),
73 "poweroff_timeout": config.get("poweroff_timeout", ''),
74 "build_no": os.getenv("BUILD_NUMBER"),
75 "name": monitor_name,
76 "monitors": config['monitors'].get(monitor_name, []),
77 "platform": platform,
78 "recovery_image_url": recovery_image_url,
79 "data_bin_offset": config.get('data_bin_offset', ''),
80 "docker_prefix": vars(user_args).get('docker_prefix', ''),
81 "license_variable": vars(user_args).get('license_variable', ''),
82 "enable_code_coverage": user_args.enable_code_coverage == "TRUE",
83 "coverage_trace_plugin": coverage_trace_plugin,
84 "build_job_url": os.getenv("BUILD_URL"),
85 "cpu0_baseline": config.get("cpu0_baseline", 0),
86 "cpu0_initvtor_s": config.get("cpu0_initvtor_s", "0x10000000"),
87 "psa_api_suite": os.getenv("TEST_PSA_API") if os.getenv("TEST_PSA_API") == "IPC" else "",
88 }
89 for binary_type, binary_name in config["binaries"].items():
90 params.update(
91 {
92 "{}_url".format(binary_type): "{}/artifact/trusted-firmware-m/build/bin/{}".format(params["build_job_url"], binary_name)
93 }
94 )
Matthew Hartfb6fd362020-03-04 21:03:59 +000095
Xinyu Zhang22a12752022-10-10 17:21:21 +080096 if len(params["monitors"]) == 0:
97 break
98
99 definition = template_env.get_template(template_file).render(
100 params
101 )
102 definitions.update({params["job_name"]: definition})
103
Matthew Hartfb6fd362020-03-04 21:03:59 +0000104 return definitions
105
106
107def generate_lava_job_defs(user_args, config):
Fathi Boudra13b7eba2020-11-26 10:29:53 +0100108 """Create a LAVA test job definition file"""
Matthew Hartfb6fd362020-03-04 21:03:59 +0000109
110 # Evaluate current directory
111 work_dir = os.path.abspath(os.path.dirname(__file__))
112
Xinyu Zhang22a12752022-10-10 17:21:21 +0800113 # If platform exists in the LAVA platforms
114 if os.getenv("TFM_PLATFORM") in config["platforms"]:
Matthew Hartfb6fd362020-03-04 21:03:59 +0000115 # Only test this platform
Xinyu Zhang22a12752022-10-10 17:21:21 +0800116 platform = os.getenv("TFM_PLATFORM")
Matthew Hartfb6fd362020-03-04 21:03:59 +0000117 config["platforms"] = {platform: config["platforms"][platform]}
Fathi Boudra13b7eba2020-11-26 10:29:53 +0100118 # Generate the output definition
Matthew Hartfb6fd362020-03-04 21:03:59 +0000119 definitions = generate_test_definitions(config, work_dir, user_args)
Matthew Hartfb6fd362020-03-04 21:03:59 +0000120 # Write it into a file
121 out_dir = os.path.abspath(user_args.lava_def_output)
122 os.makedirs(out_dir, exist_ok=True)
123 for name, definition in definitions.items():
124 out_file = os.path.join(out_dir, "{}{}".format(name, ".yaml"))
125 with open(out_file, "w") as F:
126 F.write(definition)
127 print("Definition created at %s" % out_file)
128
129
130def main(user_args):
131 user_args.template_dir = "jinja2_templates"
Xinyu Zhangbe224f62021-02-03 17:57:38 +0800132 config_keys = list(lava_gen_config_map.keys())
133 if user_args.fvp_only:
Xinyu Zhangd7616fc2022-07-06 16:14:36 +0800134 config_keys = [key for key in config_keys if "fvp" in key]
Xinyu Zhang302b74d2021-11-03 14:53:44 +0800135 if user_args.physical_board_only:
Xinyu Zhangd7616fc2022-07-06 16:14:36 +0800136 config_keys = [key for key in config_keys
137 if "fvp" not in key and "qemu" not in key]
Matthew Hartfb6fd362020-03-04 21:03:59 +0000138 if user_args.config_key:
139 config_keys = [user_args.config_key]
140 for config_key in config_keys:
141 config = load_config_overrides(user_args, config_key)
142 generate_lava_job_defs(user_args, config)
143
144
145def get_cmd_args():
Fathi Boudra13b7eba2020-11-26 10:29:53 +0100146 """Parse command line arguments"""
Matthew Hartfb6fd362020-03-04 21:03:59 +0000147
148 # Parse command line arguments to override config
149 parser = argparse.ArgumentParser(description="Lava Create Jobs")
150 cmdargs = parser.add_argument_group("Create LAVA Jobs")
Xinyu Zhang302b74d2021-11-03 14:53:44 +0800151 device_type = parser.add_mutually_exclusive_group()
Matthew Hartfb6fd362020-03-04 21:03:59 +0000152
153 # Configuration control
154 cmdargs.add_argument(
155 "--config-name",
156 dest="config_key",
157 action="store",
158 help="Select built-in configuration by name",
159 )
160 cmdargs.add_argument(
Matthew Hartfb6fd362020-03-04 21:03:59 +0000161 "--output-dir",
162 dest="lava_def_output",
163 action="store",
164 default="job_results",
165 help="Set LAVA compatible .yaml output file",
166 )
167 cmdargs.add_argument(
Matthew Hartfb6fd362020-03-04 21:03:59 +0000168 "--jenkins-build-url",
169 dest="jenkins_build_url",
170 action="store",
171 help="Set the Jenkins URL",
172 )
173 cmdargs.add_argument(
174 "--jenkins-job",
175 dest="jenkins_job",
176 action="store",
177 default="tf-m-build-config",
178 help="Set the jenkins job name",
179 )
180 cmdargs.add_argument(
Matthew Hartfb6fd362020-03-04 21:03:59 +0000181 "--docker-prefix", dest="docker_prefix", action="store", help="Prefix string for the FVP docker registry location"
182 )
183 cmdargs.add_argument(
184 "--license-variable", dest="license_variable", action="store", help="License string for Fastmodels"
185 )
Leonardo Sandoval66386a22021-04-15 14:35:08 -0500186 cmdargs.add_argument(
187 "--enable-code-coverage", dest="enable_code_coverage", action="store", default="FALSE", help="Enable trace-base code coverage"
188 )
Matthew Hartfb6fd362020-03-04 21:03:59 +0000189
Xinyu Zhang302b74d2021-11-03 14:53:44 +0800190 device_type.add_argument(
191 "--fvp-only",
192 dest="fvp_only",
193 action="store_true",
194 help="Run test cases on FVP only",
195 )
196 device_type.add_argument(
197 "--physical-board-only",
198 dest="physical_board_only",
199 action="store_true",
200 help="Run test cases on physical boards only",
201 )
202
203 return parser.parse_args()
Matthew Hartfb6fd362020-03-04 21:03:59 +0000204
205if __name__ == "__main__":
206 main(get_cmd_args())