Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 1 | # Copyright 2024, The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 14 | """Test discovery agent that uses TradeFed to discover test artifacts.""" |
| 15 | import glob |
| 16 | import json |
| 17 | import logging |
| 18 | import os |
| 19 | import subprocess |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 20 | |
| 21 | |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 22 | class TestDiscoveryAgent: |
| 23 | """Test discovery agent.""" |
| 24 | |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 25 | _TRADEFED_PREBUILT_JAR_RELATIVE_PATH = ( |
| 26 | "vendor/google_tradefederation/prebuilts/filegroups/google-tradefed/" |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 27 | ) |
| 28 | |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 29 | _TRADEFED_NO_POSSIBLE_TEST_DISCOVERY_KEY = "NoPossibleTestDiscovery" |
| 30 | |
| 31 | _TRADEFED_TEST_ZIP_REGEXES_LIST_KEY = "TestZipRegexes" |
| 32 | |
Mingjun Yang | 3db3790 | 2025-01-26 23:41:37 +0000 | [diff] [blame] | 33 | _TRADEFED_TEST_MODULES_LIST_KEY = "TestModules" |
| 34 | |
| 35 | _TRADEFED_TEST_DEPENDENCIES_LIST_KEY = "TestDependencies" |
| 36 | |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 37 | _TRADEFED_DISCOVERY_OUTPUT_FILE_NAME = "test_discovery_agent.txt" |
| 38 | |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 39 | def __init__( |
| 40 | self, |
| 41 | tradefed_args: list[str], |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 42 | test_mapping_zip_path: str = "", |
| 43 | tradefed_jar_revelant_files_path: str = _TRADEFED_PREBUILT_JAR_RELATIVE_PATH, |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 44 | ): |
| 45 | self.tradefed_args = tradefed_args |
| 46 | self.test_mapping_zip_path = test_mapping_zip_path |
| 47 | self.tradefed_jar_relevant_files_path = tradefed_jar_revelant_files_path |
| 48 | |
| 49 | def discover_test_zip_regexes(self) -> list[str]: |
| 50 | """Discover test zip regexes from TradeFed. |
| 51 | |
| 52 | Returns: |
| 53 | A list of test zip regexes that TF is going to try to pull files from. |
| 54 | """ |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 55 | test_discovery_output_file_name = os.path.join( |
Mingjun Yang | 3db3790 | 2025-01-26 23:41:37 +0000 | [diff] [blame] | 56 | os.environ.get("TOP"), "out", self._TRADEFED_DISCOVERY_OUTPUT_FILE_NAME |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 57 | ) |
| 58 | with open( |
| 59 | test_discovery_output_file_name, mode="w+t" |
| 60 | ) as test_discovery_output_file: |
| 61 | java_args = [] |
| 62 | java_args.append("prebuilts/jdk/jdk21/linux-x86/bin/java") |
| 63 | java_args.append("-cp") |
| 64 | java_args.append( |
| 65 | self.create_classpath(self.tradefed_jar_relevant_files_path) |
| 66 | ) |
| 67 | java_args.append( |
| 68 | "com.android.tradefed.observatory.TestZipDiscoveryExecutor" |
| 69 | ) |
| 70 | java_args.extend(self.tradefed_args) |
| 71 | env = os.environ.copy() |
| 72 | env.update({"DISCOVERY_OUTPUT_FILE": test_discovery_output_file.name}) |
| 73 | logging.info(f"Calling test discovery with args: {java_args}") |
| 74 | try: |
| 75 | result = subprocess.run(args=java_args, env=env, text=True, check=True) |
| 76 | logging.info(f"Test zip discovery output: {result.stdout}") |
| 77 | except subprocess.CalledProcessError as e: |
| 78 | raise TestDiscoveryError( |
| 79 | f"Failed to run test discovery, strout: {e.stdout}, strerr:" |
| 80 | f" {e.stderr}, returncode: {e.returncode}" |
| 81 | ) |
| 82 | data = json.loads(test_discovery_output_file.read()) |
| 83 | logging.info(f"Test discovery result file content: {data}") |
| 84 | if ( |
| 85 | self._TRADEFED_NO_POSSIBLE_TEST_DISCOVERY_KEY in data |
| 86 | and data[self._TRADEFED_NO_POSSIBLE_TEST_DISCOVERY_KEY] |
| 87 | ): |
| 88 | raise TestDiscoveryError("No possible test discovery") |
| 89 | if ( |
| 90 | data[self._TRADEFED_TEST_ZIP_REGEXES_LIST_KEY] is None |
| 91 | or data[self._TRADEFED_TEST_ZIP_REGEXES_LIST_KEY] is [] |
| 92 | ): |
| 93 | raise TestDiscoveryError("No test zip regexes returned") |
| 94 | return data[self._TRADEFED_TEST_ZIP_REGEXES_LIST_KEY] |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 95 | |
Mingjun Yang | 3db3790 | 2025-01-26 23:41:37 +0000 | [diff] [blame] | 96 | def discover_test_mapping_test_modules(self) -> (list[str], list[str]): |
| 97 | """Discover test mapping test modules and dependencies from TradeFed. |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 98 | |
| 99 | Returns: |
Mingjun Yang | 3db3790 | 2025-01-26 23:41:37 +0000 | [diff] [blame] | 100 | A tuple that contains a list of test modules and a list of test |
| 101 | dependencies that TradeFed is going to execute based on the |
Mingjun Yang | d448be2 | 2024-10-15 05:37:00 +0000 | [diff] [blame] | 102 | TradeFed test args. |
| 103 | """ |
Mingjun Yang | 3db3790 | 2025-01-26 23:41:37 +0000 | [diff] [blame] | 104 | test_discovery_output_file_name = os.path.join( |
| 105 | os.environ.get("TOP"), "out", self._TRADEFED_DISCOVERY_OUTPUT_FILE_NAME |
| 106 | ) |
| 107 | with open( |
| 108 | test_discovery_output_file_name, mode="w+t" |
| 109 | ) as test_discovery_output_file: |
| 110 | java_args = [] |
| 111 | java_args.append("prebuilts/jdk/jdk21/linux-x86/bin/java") |
| 112 | java_args.append("-cp") |
| 113 | java_args.append( |
| 114 | self.create_classpath(self.tradefed_jar_relevant_files_path) |
| 115 | ) |
| 116 | java_args.append( |
| 117 | "com.android.tradefed.observatory.TestMappingDiscoveryAgent" |
| 118 | ) |
| 119 | java_args.extend(self.tradefed_args) |
| 120 | env = os.environ.copy() |
| 121 | env.update({"TF_TEST_MAPPING_ZIP_FILE": self.test_mapping_zip_path}) |
| 122 | env.update({"DISCOVERY_OUTPUT_FILE": test_discovery_output_file.name}) |
| 123 | logging.info(f"Calling test discovery with args: {java_args}") |
| 124 | try: |
| 125 | result = subprocess.run(args=java_args, env=env, text=True, check=True) |
| 126 | logging.info(f"Test discovery agent output: {result.stdout}") |
| 127 | except subprocess.CalledProcessError as e: |
| 128 | raise TestDiscoveryError( |
| 129 | f"Failed to run test discovery, strout: {e.stdout}, strerr:" |
| 130 | f" {e.stderr}, returncode: {e.returncode}" |
| 131 | ) |
| 132 | data = json.loads(test_discovery_output_file.read()) |
| 133 | logging.info(f"Test discovery result file content: {data}") |
| 134 | if ( |
| 135 | self._TRADEFED_NO_POSSIBLE_TEST_DISCOVERY_KEY in data |
| 136 | and data[self._TRADEFED_NO_POSSIBLE_TEST_DISCOVERY_KEY] |
| 137 | ): |
| 138 | raise TestDiscoveryError("No possible test discovery") |
| 139 | if ( |
| 140 | data[self._TRADEFED_TEST_MODULES_LIST_KEY] is None |
| 141 | or data[self._TRADEFED_TEST_MODULES_LIST_KEY] is [] |
| 142 | ): |
| 143 | raise TestDiscoveryError("No test modules returned") |
| 144 | return ( |
| 145 | data[self._TRADEFED_TEST_MODULES_LIST_KEY], |
| 146 | data[self._TRADEFED_TEST_DEPENDENCIES_LIST_KEY], |
| 147 | ) |
Mingjun Yang | d2cc6a6 | 2024-11-05 23:44:38 +0000 | [diff] [blame] | 148 | |
| 149 | def create_classpath(self, directory): |
| 150 | """Creates a classpath string from all .jar files in the given directory. |
| 151 | |
| 152 | Args: |
| 153 | directory: The directory to search for .jar files. |
| 154 | |
| 155 | Returns: |
| 156 | A string representing the classpath, with jar files separated by the |
| 157 | OS-specific path separator (e.g., ':' on Linux/macOS, ';' on Windows). |
| 158 | """ |
| 159 | jar_files = glob.glob(os.path.join(directory, "*.jar")) |
| 160 | return os.pathsep.join(jar_files) |
| 161 | |
| 162 | |
| 163 | class TestDiscoveryError(Exception): |
| 164 | """A TestDiscoveryErrorclass.""" |
| 165 | |
| 166 | def __init__(self, message): |
| 167 | super().__init__(message) |
| 168 | self.message = message |