Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Leonardo Sandoval | 579c737 | 2020-10-23 15:23:32 -0500 | [diff] [blame] | 3 | # Copyright (c) 2019-2020 Arm Limited. All rights reserved. |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 4 | # |
| 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 | |
| 12 | import os |
| 13 | |
| 14 | TEST_SUFFIX = ".test" |
| 15 | |
| 16 | |
| 17 | def touch(a_file): |
| 18 | with open(a_file, "w"): |
| 19 | pass |
| 20 | |
| 21 | |
| 22 | # Obtain the value of either $variable or $VARIABLE. |
| 23 | def 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 |
| 34 | def 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: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 38 | # scp_config, tf_config, tftf_config, scp_tools |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 39 | # Reshuffle them into the canonical format |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 40 | config_list = [config_list[1], config_list[2], config_list[0], config_list[3]] |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 41 | |
Olivier Deprez | 0a9a348 | 2019-12-16 14:10:31 +0100 | [diff] [blame] | 42 | 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 | |
Manish V Badarkhe | c36c4bf | 2025-03-12 14:56:54 +0000 | [diff] [blame^] | 48 | if group.startswith("rmm-"): |
| 49 | # RMM configs would be specified in the following format: |
| 50 | # rmm_config, tf_config, tftf_config, spm_config, scp_config, scp_tools |
| 51 | # Reshuffle them into the canonical format |
| 52 | config_list = [config_list[1], config_list[2], config_list[4], config_list[5], config_list[3], config_list[0]] |
| 53 | |
Manish Pandey | 1a56ddb | 2024-03-22 17:09:21 +0000 | [diff] [blame] | 54 | if group.startswith("tf-l3-code-coverage"): |
| 55 | # coverage configs would be specified in the following format: |
| 56 | # tf_config, tftf_config, spm_config, scp_config, scp_tools |
| 57 | # Reshuffle them into the canonical format |
| 58 | config_list = [config_list[0], config_list[1], config_list[3], config_list[4], config_list[2]] |
| 59 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 60 | return config_list |
| 61 | |
| 62 | |
| 63 | def gen_desc(group, test): |
| 64 | global num_spawn |
| 65 | |
| 66 | build_config, run_config = test.split(":") |
| 67 | |
| 68 | # Test descriptors are always generated in the following order: |
Manish V Badarkhe | c36c4bf | 2025-03-12 14:56:54 +0000 | [diff] [blame^] | 69 | # tf_config, tftf_config, scp_config, scp_tools, spm_config, rmm_config |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 70 | # Fill missing configs to the right with "nil". |
Manish V Badarkhe | c36c4bf | 2025-03-12 14:56:54 +0000 | [diff] [blame^] | 71 | config_list = (build_config.split(",") + ["nil"] * 6)[:6] |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 72 | |
| 73 | # Perform any group-specific translation on the config |
| 74 | config_list = translate_build_config(group, config_list) |
| 75 | |
| 76 | test_config = ",".join(config_list) + ":" + run_config |
| 77 | |
| 78 | # Create descriptor. Write the name of the original test config as its |
| 79 | # content. |
Paul Sokolovsky | 3cb6603 | 2021-11-03 13:36:17 +0300 | [diff] [blame] | 80 | desc_base = "%".join([str(num_spawn).zfill(4), os.path.basename(group), |
| 81 | test_config + TEST_SUFFIX]) |
| 82 | desc = os.path.join(workspace, desc_base) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 83 | with open(desc, "wt") as fd: |
| 84 | print(test, file=fd) |
Paul Sokolovsky | 3cb6603 | 2021-11-03 13:36:17 +0300 | [diff] [blame] | 85 | # Create .testprop file for smoother integration with Jenkins |
| 86 | # (allows to pass test config as a normal string param instead |
| 87 | # of binary file which takes extra clicks to view). |
| 88 | with open(desc + "prop", "wt") as fd: |
| 89 | print("TEST_CONFIG={}".format(test), file=fd) |
| 90 | print("TEST_DESC={}".format(desc_base), file=fd) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 91 | |
| 92 | num_spawn += 1 |
| 93 | |
| 94 | |
| 95 | def process_item(item): |
| 96 | # If an item starts with @, then it's deemed to be an indirection--a file |
| 97 | # from which test groups are to be read. |
| 98 | if item.startswith("@"): |
| 99 | with open(item[1:]) as fd: |
| 100 | for line in fd: |
| 101 | line = line.strip() |
| 102 | if not line: |
| 103 | continue |
| 104 | process_item(line) |
| 105 | |
| 106 | return |
| 107 | |
| 108 | item_loc = os.path.join(group_dir, item) |
| 109 | |
| 110 | if os.path.isfile(item_loc): |
| 111 | gen_desc(*item_loc.split(os.sep)[-2:]) |
| 112 | elif os.path.isdir(item_loc): |
| 113 | # If it's a directory, select all files inside it |
| 114 | for a_file in next(os.walk(item_loc))[2]: |
Paul Sokolovsky | a99999c | 2023-06-14 14:11:40 +0300 | [diff] [blame] | 115 | if a_file.endswith(".inactive"): |
| 116 | continue |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 117 | gen_desc(item, a_file) |
| 118 | else: |
| 119 | # The item doesn't exist |
| 120 | if ":" in item: |
| 121 | # A non-existent test config is specified |
| 122 | if "/" in item: |
| 123 | # The test config doesn't exist, and a group is also specified. |
| 124 | # This is not allowed. |
| 125 | raise Exception("'{}' doesn't exist.".format(item)) |
| 126 | else: |
| 127 | # The user probably intended to create one on the fly; so create |
| 128 | # one in the superficial 'GENERATED' group. |
| 129 | print("note: '{}' doesn't exist; generated.".format(item)) |
| 130 | touch(os.path.join(generated_dir, item)) |
| 131 | gen_desc(os.path.basename(generated_dir), item) |
| 132 | else: |
| 133 | raise Exception("'{}' is not valid for test generation!".format(item)) |
| 134 | |
| 135 | |
| 136 | ci_root = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir)) |
| 137 | group_dir = os.path.join(ci_root, "group") |
| 138 | num_spawn = 0 |
| 139 | |
| 140 | # Obtain variables from environment |
| 141 | test_groups = get_env("test_groups") |
| 142 | workspace = get_env("workspace") |
| 143 | |
| 144 | # Remove all test files, if any |
| 145 | _, _, files = next(os.walk(workspace)) |
| 146 | for test_file in files: |
| 147 | if test_file.endswith(TEST_SUFFIX): |
| 148 | os.remove(os.path.join(workspace, test_file)) |
| 149 | |
| 150 | generated_dir = os.path.join(group_dir, "GENERATED") |
| 151 | os.makedirs(generated_dir, exist_ok=True) |
| 152 | |
| 153 | for item in test_groups.split(): |
| 154 | process_item(item) |
| 155 | |
| 156 | print() |
Paul Sokolovsky | e253873 | 2021-11-03 11:59:54 +0300 | [diff] [blame] | 157 | print("{} test configurations to be built...".format(num_spawn)) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 158 | print() |