blob: 5939fca6e5ee10bad95a2117b70bc7f37d49dd8a [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
Leonardo Sandoval579c7372020-10-23 15:23:32 -05003# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#
8# Generate .test files in $workspace based on the $TEST_GROUPS parameter. Test
9# files are prefixed with a zero-padded number for a predictable ordering
10# amongst them.
11
12import os
13
14TEST_SUFFIX = ".test"
15
16
17def touch(a_file):
18 with open(a_file, "w"):
19 pass
20
21
22# Obtain the value of either $variable or $VARIABLE.
23def get_env(variable):
24 var_list = [variable, variable.upper()]
25 for v in var_list:
26 value = os.environ.get(v)
27 if value:
28 return value
29 else:
30 raise Exception("couldn't find {} in env".format(" or ".join(var_list)))
31
32
33# Perform group-specific translation on the build config
34def translate_build_config(group, config_list):
35 # config_list contains build configs as read from the test config
Olivier Deprez0a9a3482019-12-16 14:10:31 +010036 if group.startswith("spm-"):
37 # SPM configs would be specified in the following format:
Chris Kay19f8b362025-08-04 19:56:35 +010038 # spm_config, tf_config, tftf_config
Olivier Deprez0a9a3482019-12-16 14:10:31 +010039 # Reshuffle them into the canonical format
Chris Kay19f8b362025-08-04 19:56:35 +010040 config_list = [config_list[1], config_list[2], config_list[0]]
Manish Pandey1a56ddb2024-03-22 17:09:21 +000041
Fathi Boudra422bf772019-12-02 11:10:16 +020042 return config_list
43
44
45def gen_desc(group, test):
46 global num_spawn
47
48 build_config, run_config = test.split(":")
49
50 # Test descriptors are always generated in the following order:
Chris Kay19f8b362025-08-04 19:56:35 +010051 # tf_config, tftf_config, spm_config
Fathi Boudra422bf772019-12-02 11:10:16 +020052 # Fill missing configs to the right with "nil".
Chris Kay19f8b362025-08-04 19:56:35 +010053 config_list = (build_config.split(",") + ["nil"] * 4)[:4]
Fathi Boudra422bf772019-12-02 11:10:16 +020054
55 # Perform any group-specific translation on the config
56 config_list = translate_build_config(group, config_list)
57
58 test_config = ",".join(config_list) + ":" + run_config
59
60 # Create descriptor. Write the name of the original test config as its
61 # content.
Paul Sokolovsky3cb66032021-11-03 13:36:17 +030062 desc_base = "%".join([str(num_spawn).zfill(4), os.path.basename(group),
63 test_config + TEST_SUFFIX])
64 desc = os.path.join(workspace, desc_base)
Fathi Boudra422bf772019-12-02 11:10:16 +020065 with open(desc, "wt") as fd:
66 print(test, file=fd)
Paul Sokolovsky3cb66032021-11-03 13:36:17 +030067 # Create .testprop file for smoother integration with Jenkins
68 # (allows to pass test config as a normal string param instead
69 # of binary file which takes extra clicks to view).
70 with open(desc + "prop", "wt") as fd:
71 print("TEST_CONFIG={}".format(test), file=fd)
72 print("TEST_DESC={}".format(desc_base), file=fd)
Fathi Boudra422bf772019-12-02 11:10:16 +020073
74 num_spawn += 1
75
76
77def process_item(item):
78 # If an item starts with @, then it's deemed to be an indirection--a file
79 # from which test groups are to be read.
80 if item.startswith("@"):
81 with open(item[1:]) as fd:
82 for line in fd:
83 line = line.strip()
84 if not line:
85 continue
86 process_item(line)
87
88 return
89
90 item_loc = os.path.join(group_dir, item)
91
92 if os.path.isfile(item_loc):
93 gen_desc(*item_loc.split(os.sep)[-2:])
94 elif os.path.isdir(item_loc):
95 # If it's a directory, select all files inside it
96 for a_file in next(os.walk(item_loc))[2]:
Paul Sokolovskya99999c2023-06-14 14:11:40 +030097 if a_file.endswith(".inactive"):
98 continue
Fathi Boudra422bf772019-12-02 11:10:16 +020099 gen_desc(item, a_file)
100 else:
101 # The item doesn't exist
102 if ":" in item:
103 # A non-existent test config is specified
104 if "/" in item:
105 # The test config doesn't exist, and a group is also specified.
106 # This is not allowed.
107 raise Exception("'{}' doesn't exist.".format(item))
108 else:
109 # The user probably intended to create one on the fly; so create
110 # one in the superficial 'GENERATED' group.
111 print("note: '{}' doesn't exist; generated.".format(item))
112 touch(os.path.join(generated_dir, item))
113 gen_desc(os.path.basename(generated_dir), item)
114 else:
115 raise Exception("'{}' is not valid for test generation!".format(item))
116
117
118ci_root = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir))
119group_dir = os.path.join(ci_root, "group")
120num_spawn = 0
121
122# Obtain variables from environment
123test_groups = get_env("test_groups")
124workspace = get_env("workspace")
125
126# Remove all test files, if any
127_, _, files = next(os.walk(workspace))
128for test_file in files:
129 if test_file.endswith(TEST_SUFFIX):
130 os.remove(os.path.join(workspace, test_file))
131
132generated_dir = os.path.join(group_dir, "GENERATED")
133os.makedirs(generated_dir, exist_ok=True)
134
135for item in test_groups.split():
136 process_item(item)
137
138print()
Paul Sokolovskye2538732021-11-03 11:59:54 +0300139print("{} test configurations to be built...".format(num_spawn))
Fathi Boudra422bf772019-12-02 11:10:16 +0200140print()