blob: 7ee07b827deeb6061b6ff113b3d95012d2f9e250 [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
36 if group.startswith("scp-"):
37 # SCP configs would be specified in the following format:
Zelalem219df412020-05-17 19:21:20 -050038 # scp_config, tf_config, tftf_config, scp_tools
Fathi Boudra422bf772019-12-02 11:10:16 +020039 # Reshuffle them into the canonical format
Zelalem219df412020-05-17 19:21:20 -050040 config_list = [config_list[1], config_list[2], config_list[0], config_list[3]]
Fathi Boudra422bf772019-12-02 11:10:16 +020041
Olivier Deprez0a9a3482019-12-16 14:10:31 +010042 if group.startswith("spm-"):
43 # SPM configs would be specified in the following format:
44 # spm_config, tf_config, tftf_config, scp_config, scp_tools
45 # Reshuffle them into the canonical format
46 config_list = [config_list[1], config_list[2], config_list[3], config_list[4], config_list[0]]
47
Fathi Boudra422bf772019-12-02 11:10:16 +020048 return config_list
49
50
51def gen_desc(group, test):
52 global num_spawn
53
54 build_config, run_config = test.split(":")
55
56 # Test descriptors are always generated in the following order:
Olivier Deprez0a9a3482019-12-16 14:10:31 +010057 # tf_config, tftf_config, scp_config, scp_tools, spm_config
Fathi Boudra422bf772019-12-02 11:10:16 +020058 # Fill missing configs to the right with "nil".
Olivier Deprez0a9a3482019-12-16 14:10:31 +010059 config_list = (build_config.split(",") + ["nil"] * 5)[:5]
Fathi Boudra422bf772019-12-02 11:10:16 +020060
61 # Perform any group-specific translation on the config
62 config_list = translate_build_config(group, config_list)
63
64 test_config = ",".join(config_list) + ":" + run_config
65
66 # Create descriptor. Write the name of the original test config as its
67 # content.
Paul Sokolovsky3cb66032021-11-03 13:36:17 +030068 desc_base = "%".join([str(num_spawn).zfill(4), os.path.basename(group),
69 test_config + TEST_SUFFIX])
70 desc = os.path.join(workspace, desc_base)
Fathi Boudra422bf772019-12-02 11:10:16 +020071 with open(desc, "wt") as fd:
72 print(test, file=fd)
Paul Sokolovsky3cb66032021-11-03 13:36:17 +030073 # Create .testprop file for smoother integration with Jenkins
74 # (allows to pass test config as a normal string param instead
75 # of binary file which takes extra clicks to view).
76 with open(desc + "prop", "wt") as fd:
77 print("TEST_CONFIG={}".format(test), file=fd)
78 print("TEST_DESC={}".format(desc_base), file=fd)
Fathi Boudra422bf772019-12-02 11:10:16 +020079
80 num_spawn += 1
81
82
83def process_item(item):
84 # If an item starts with @, then it's deemed to be an indirection--a file
85 # from which test groups are to be read.
86 if item.startswith("@"):
87 with open(item[1:]) as fd:
88 for line in fd:
89 line = line.strip()
90 if not line:
91 continue
92 process_item(line)
93
94 return
95
96 item_loc = os.path.join(group_dir, item)
97
98 if os.path.isfile(item_loc):
99 gen_desc(*item_loc.split(os.sep)[-2:])
100 elif os.path.isdir(item_loc):
101 # If it's a directory, select all files inside it
102 for a_file in next(os.walk(item_loc))[2]:
103 gen_desc(item, a_file)
104 else:
105 # The item doesn't exist
106 if ":" in item:
107 # A non-existent test config is specified
108 if "/" in item:
109 # The test config doesn't exist, and a group is also specified.
110 # This is not allowed.
111 raise Exception("'{}' doesn't exist.".format(item))
112 else:
113 # The user probably intended to create one on the fly; so create
114 # one in the superficial 'GENERATED' group.
115 print("note: '{}' doesn't exist; generated.".format(item))
116 touch(os.path.join(generated_dir, item))
117 gen_desc(os.path.basename(generated_dir), item)
118 else:
119 raise Exception("'{}' is not valid for test generation!".format(item))
120
121
122ci_root = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir))
123group_dir = os.path.join(ci_root, "group")
124num_spawn = 0
125
126# Obtain variables from environment
127test_groups = get_env("test_groups")
128workspace = get_env("workspace")
129
130# Remove all test files, if any
131_, _, files = next(os.walk(workspace))
132for test_file in files:
133 if test_file.endswith(TEST_SUFFIX):
134 os.remove(os.path.join(workspace, test_file))
135
136generated_dir = os.path.join(group_dir, "GENERATED")
137os.makedirs(generated_dir, exist_ok=True)
138
139for item in test_groups.split():
140 process_item(item)
141
142print()
Paul Sokolovskye2538732021-11-03 11:59:54 +0300143print("{} test configurations to be built...".format(num_spawn))
Fathi Boudra422bf772019-12-02 11:10:16 +0200144print()