Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 2 | # Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | # Run all internal tests, archive result to cloud storage. |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 7 | # In the continuous operation flow we have a tester continuously checking |
| 8 | # a specific cloud storage location for a file with a git hash. |
| 9 | # If the file is there, the tester will remove the file, and add another |
| 10 | # file stating that this is now being run. After successfully running, |
| 11 | # the tester will add yet another file, and remove the last one. |
| 12 | # Complete flow with states: |
| 13 | # 1: |
| 14 | # BOT: |
| 15 | # Add file READY_FOR_TESTING (contains git hash) |
| 16 | # Wait until file TESTING_COMPLETE exists (contains git hash) |
| 17 | # Timeout if no progress for RUN_TIMEOUT |
| 18 | # Cleanup READY_FOR_TESTING and TESTING |
| 19 | # 2: |
| 20 | # TESTER: |
| 21 | # Replace file READY_FOR_TESTING by TESTING (contains git hash) |
| 22 | # Run tests for git hash |
| 23 | # Upload commit specific logs if failures |
| 24 | # Upload git specific overall status file (failed or succeeded) |
| 25 | # Replace file TESTING by TESTING_COMPLETE (contains git hash) |
| 26 | # 3: |
| 27 | # BOT: |
| 28 | # Read overall status |
| 29 | # Delete TESTING_COMPLETE |
| 30 | # Exit based on status |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 31 | |
Rico Wind | ba63dc8 | 2019-03-29 14:33:47 +0100 | [diff] [blame] | 32 | import gradle |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 33 | import optparse |
| 34 | import os |
| 35 | import subprocess |
| 36 | import sys |
| 37 | import time |
| 38 | import utils |
Morten Krogh-Jespersen | f241230 | 2019-10-22 10:18:04 +0200 | [diff] [blame] | 39 | import run_on_app |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 40 | |
Christoffer Quist Adamsen | 81d4150 | 2021-06-25 09:33:43 +0200 | [diff] [blame] | 41 | import chrome_data |
Christoffer Quist Adamsen | 4380de3 | 2021-04-23 06:24:31 +0200 | [diff] [blame] | 42 | import youtube_data |
| 43 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 44 | # How often the bot/tester should check state |
| 45 | PULL_DELAY = 30 |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 46 | TEST_RESULT_DIR = 'internal' |
| 47 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 48 | # Magic files |
| 49 | READY_FOR_TESTING = 'READY_FOR_TESTING' |
| 50 | TESTING = 'TESTING' |
| 51 | TESTING_COMPLETE = 'TESTING_COMPLETE' |
| 52 | |
| 53 | ALL_MAGIC = [READY_FOR_TESTING, TESTING, TESTING_COMPLETE] |
| 54 | |
| 55 | # Log file names |
| 56 | STDERR = 'stderr' |
| 57 | STDOUT = 'stdout' |
| 58 | EXITCODE = 'exitcode' |
| 59 | TIMED_OUT = 'timed_out' |
| 60 | |
Christoffer Quist Adamsen | b8ab39d | 2023-08-23 12:22:10 +0200 | [diff] [blame] | 61 | BENCHMARK_APPS = [chrome_data, youtube_data] |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 62 | |
Rico Wind | a7b8fff | 2023-07-03 07:49:17 +0200 | [diff] [blame] | 63 | DEPENDENT_PYTHON_FILES = [gradle, utils, run_on_app] |
| 64 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 65 | |
Christoffer Quist Adamsen | 81d4150 | 2021-06-25 09:33:43 +0200 | [diff] [blame] | 66 | def find_min_xmx_command(app_data): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 67 | record = app_data.GetMemoryData(app_data.GetLatestVersion()) |
| 68 | assert record['find-xmx-min'] < record['find-xmx-max'] |
| 69 | assert record[ |
| 70 | 'find-xmx-range'] < record['find-xmx-max'] - record['find-xmx-min'] |
| 71 | return [ |
| 72 | 'tools/run_on_app.py', '--compiler=r8', '--compiler-build=lib', |
| 73 | '--app=%s' % app_data.GetName(), |
| 74 | '--version=%s' % app_data.GetLatestVersion(), '--no-debug', |
| 75 | '--no-build', '--find-min-xmx', |
| 76 | '--find-min-xmx-min-memory=%s' % record['find-xmx-min'], |
| 77 | '--find-min-xmx-max-memory=%s' % record['find-xmx-max'], |
| 78 | '--find-min-xmx-range-size=%s' % record['find-xmx-range'], |
| 79 | '--find-min-xmx-archive' |
| 80 | ] |
| 81 | |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 82 | |
Christoffer Quist Adamsen | 81d4150 | 2021-06-25 09:33:43 +0200 | [diff] [blame] | 83 | def compile_with_memory_max_command(app_data): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 84 | # TODO(b/152939233): Remove this special handling when fixed. |
| 85 | factor = 1.25 if app_data.GetName() == 'chrome' else 1.15 |
| 86 | record = app_data.GetMemoryData(app_data.GetLatestVersion()) |
| 87 | return [] if 'skip-find-xmx-max' in record else [ |
| 88 | 'tools/run_on_app.py', '--compiler=r8', '--compiler-build=lib', |
| 89 | '--app=%s' % app_data.GetName(), |
| 90 | '--version=%s' % |
| 91 | app_data.GetLatestVersion(), '--no-debug', '--no-build', |
| 92 | '--max-memory=%s' % int(record['oom-threshold'] * factor) |
| 93 | ] |
| 94 | |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 95 | |
Christoffer Quist Adamsen | 81d4150 | 2021-06-25 09:33:43 +0200 | [diff] [blame] | 96 | def compile_with_memory_min_command(app_data): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 97 | record = app_data.GetMemoryData(app_data.GetLatestVersion()) |
| 98 | return [ |
| 99 | 'tools/run_on_app.py', '--compiler=r8', '--compiler-build=lib', |
| 100 | '--app=%s' % app_data.GetName(), |
| 101 | '--version=%s' % app_data.GetLatestVersion(), '--no-debug', |
| 102 | '--no-build', '--expect-oom', |
| 103 | '--max-memory=%s' % int(record['oom-threshold'] * 0.85) |
| 104 | ] |
| 105 | |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 106 | |
Ian Zerny | df1deca | 2023-10-12 10:38:46 +0200 | [diff] [blame] | 107 | CLEAN_COMMANDS = [ |
Rico Wind | 5a360e9 | 2022-03-31 08:49:25 +0200 | [diff] [blame] | 108 | # Make sure we have a clean build to not be polluted by old test files |
Rico Wind | e0c7cdf | 2023-10-31 08:16:03 +0100 | [diff] [blame] | 109 | ['tools/gradle.py', 'clean'], |
Ian Zerny | df1deca | 2023-10-12 10:38:46 +0200 | [diff] [blame] | 110 | ] |
| 111 | |
| 112 | # TODO(b/210982978): Enable testing of min xmx again |
| 113 | TEST_COMMANDS = [ |
Morten Krogh-Jespersen | 2243b16 | 2019-01-14 08:40:53 +0100 | [diff] [blame] | 114 | # Run test.py internal testing. |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 115 | [ |
| 116 | 'tools/test.py', '--only_internal', '--slow_tests', |
| 117 | '--java_max_memory_size=8G' |
| 118 | ], |
Morten Krogh-Jespersen | 2243b16 | 2019-01-14 08:40:53 +0100 | [diff] [blame] | 119 | # Ensure that all internal apps compile. |
Rico Wind | beeaded | 2024-04-11 09:49:37 +0200 | [diff] [blame] | 120 | ['tools/run_on_app.py', '--run-all', '--out=out', '--workers', '3'], |
Morten Krogh-Jespersen | fcb0645 | 2021-12-16 15:19:49 +0100 | [diff] [blame] | 121 | ] |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 122 | |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 123 | # Command timeout, in seconds. |
Rico Wind | 39c73eb | 2023-08-17 08:59:01 +0200 | [diff] [blame] | 124 | RUN_TIMEOUT = 3600 * 7 |
Rico Wind | f021d83 | 2018-12-13 11:29:22 +0100 | [diff] [blame] | 125 | BOT_RUN_TIMEOUT = RUN_TIMEOUT * len(TEST_COMMANDS) |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 126 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 127 | |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 128 | def log(str): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 129 | print("%s: %s" % (time.strftime("%c"), str)) |
| 130 | sys.stdout.flush() |
| 131 | |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 132 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 133 | def ParseOptions(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 134 | result = optparse.OptionParser() |
| 135 | result.add_option( |
| 136 | '--continuous', |
| 137 | help='Continuously run internal tests and post results to GCS.', |
| 138 | default=False, |
| 139 | action='store_true') |
| 140 | result.add_option( |
| 141 | '--print_logs', |
| 142 | help= |
| 143 | 'Fetch logs from gcs and print them, takes the commit to print for.', |
| 144 | default=None) |
| 145 | result.add_option('--bot', |
| 146 | help='Run in bot mode, i.e., scheduling runs.', |
| 147 | default=False, |
| 148 | action='store_true') |
| 149 | result.add_option('--archive', |
| 150 | help='Post result to GCS, implied by --continuous', |
| 151 | default=False, |
| 152 | action='store_true') |
| 153 | return result.parse_args() |
| 154 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 155 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 156 | def ensure_git_clean(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 157 | # Ensure clean git repo. |
| 158 | diff = subprocess.check_output(['git', 'diff']).decode('utf-8') |
| 159 | if len(diff) > 0: |
| 160 | log('Local modifications to the git repo, exiting') |
| 161 | sys.exit(1) |
| 162 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 163 | |
| 164 | def git_pull(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 165 | ensure_git_clean() |
| 166 | subprocess.check_call(['git', 'checkout', 'main']) |
| 167 | subprocess.check_call(['git', 'pull']) |
| 168 | return utils.get_HEAD_sha1() |
| 169 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 170 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 171 | def git_checkout(git_hash): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 172 | ensure_git_clean() |
| 173 | # Ensure that we are up to date to get the commit. |
| 174 | git_pull() |
| 175 | exitcode = subprocess.call(['git', 'checkout', git_hash]) |
| 176 | if exitcode != 0: |
| 177 | return None |
| 178 | return utils.get_HEAD_sha1() |
| 179 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 180 | |
| 181 | def get_test_result_dir(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 182 | return os.path.join(utils.R8_INTERNAL_TEST_RESULTS_BUCKET, TEST_RESULT_DIR) |
| 183 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 184 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 185 | def get_sha_destination(sha): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 186 | return os.path.join(get_test_result_dir(), sha) |
| 187 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 188 | |
| 189 | def archive_status(failed): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 190 | gs_destination = 'gs://%s' % get_sha_destination(utils.get_HEAD_sha1()) |
| 191 | utils.archive_value('status', gs_destination, failed) |
| 192 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 193 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 194 | def get_status(sha): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 195 | gs_destination = 'gs://%s/status' % get_sha_destination(sha) |
| 196 | return utils.cat_file_on_cloud_storage(gs_destination) |
| 197 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 198 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 199 | def archive_log(stdout, stderr, exitcode, timed_out, cmd): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 200 | sha = utils.get_HEAD_sha1() |
| 201 | cmd_dir = cmd.replace(' ', '_').replace('/', '_') |
| 202 | destination = os.path.join(get_sha_destination(sha), cmd_dir) |
| 203 | gs_destination = 'gs://%s' % destination |
| 204 | url = 'https://storage.cloud.google.com/%s' % destination |
| 205 | log('Archiving logs to: %s' % gs_destination) |
| 206 | utils.archive_value(EXITCODE, gs_destination, exitcode) |
| 207 | utils.archive_value(TIMED_OUT, gs_destination, timed_out) |
| 208 | utils.archive_file(STDOUT, gs_destination, stdout) |
| 209 | utils.archive_file(STDERR, gs_destination, stderr) |
| 210 | log('Logs available at: %s' % url) |
| 211 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 212 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 213 | def get_magic_file_base_path(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 214 | return 'gs://%s/magic' % get_test_result_dir() |
| 215 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 216 | |
| 217 | def get_magic_file_gs_path(name): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 218 | return '%s/%s' % (get_magic_file_base_path(), name) |
| 219 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 220 | |
| 221 | def get_magic_file_exists(name): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 222 | return utils.file_exists_on_cloud_storage(get_magic_file_gs_path(name)) |
| 223 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 224 | |
| 225 | def delete_magic_file(name): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 226 | utils.delete_file_from_cloud_storage(get_magic_file_gs_path(name)) |
| 227 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 228 | |
| 229 | def put_magic_file(name, sha): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 230 | utils.archive_value(name, get_magic_file_base_path(), sha) |
| 231 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 232 | |
| 233 | def get_magic_file_content(name, ignore_errors=False): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 234 | return utils.cat_file_on_cloud_storage(get_magic_file_gs_path(name), |
| 235 | ignore_errors=ignore_errors) |
| 236 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 237 | |
| 238 | def print_magic_file_state(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 239 | log('Magic file status:') |
| 240 | for magic in ALL_MAGIC: |
| 241 | if get_magic_file_exists(magic): |
| 242 | content = get_magic_file_content(magic, ignore_errors=True) |
| 243 | log('%s content: %s' % (magic, content)) |
| 244 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 245 | |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 246 | def fetch_and_print_logs(hash): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 247 | gs_base = 'gs://%s' % get_sha_destination(hash) |
| 248 | listing = utils.ls_files_on_cloud_storage(gs_base).strip().split('\n') |
| 249 | for entry in listing: |
| 250 | if not entry.endswith('/status'): # Ignore the overall status file |
| 251 | for to_print in [EXITCODE, TIMED_OUT, STDERR, STDOUT]: |
| 252 | gs_location = '%s%s' % (entry, to_print) |
| 253 | value = utils.cat_file_on_cloud_storage(gs_location) |
| 254 | print('\n\n%s had value:\n%s' % (to_print, value)) |
| 255 | print("\n\nPrinting find-min-xmx ranges for apps") |
| 256 | run_on_app.print_min_xmx_ranges_for_hash(hash, 'r8', 'lib') |
| 257 | |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 258 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 259 | def run_bot(): |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 260 | print_magic_file_state() |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 261 | # Ensure that there is nothing currently scheduled (broken/stopped run) |
| 262 | for magic in ALL_MAGIC: |
| 263 | if get_magic_file_exists(magic): |
| 264 | log('ERROR: Synchronizing file %s exists, cleaning up' % magic) |
| 265 | delete_magic_file(magic) |
| 266 | print_magic_file_state() |
| 267 | assert not get_magic_file_exists(READY_FOR_TESTING) |
| 268 | git_hash = utils.get_HEAD_sha1() |
| 269 | put_magic_file(READY_FOR_TESTING, git_hash) |
| 270 | begin = time.time() |
| 271 | while True: |
| 272 | if time.time() - begin > BOT_RUN_TIMEOUT: |
| 273 | log('Timeout exceeded: http://go/internal-r8-doc') |
| 274 | raise Exception('Bot timeout') |
| 275 | if get_magic_file_exists(TESTING_COMPLETE): |
| 276 | if get_magic_file_content(TESTING_COMPLETE) == git_hash: |
| 277 | break |
| 278 | else: |
| 279 | raise Exception( |
| 280 | 'Non matching git hashes %s and %s' % |
| 281 | (get_magic_file_content(TESTING_COMPLETE), git_hash)) |
| 282 | log('Still waiting for test result') |
| 283 | print_magic_file_state() |
| 284 | time.sleep(PULL_DELAY) |
| 285 | total_time = time.time() - begin |
| 286 | log('Done running test for %s in %ss' % (git_hash, total_time)) |
| 287 | test_status = get_status(git_hash) |
| 288 | delete_magic_file(TESTING_COMPLETE) |
| 289 | log('Test status is: %s' % test_status) |
| 290 | if test_status != '0': |
| 291 | print('Tests failed, you can print the logs by running(googlers only):') |
| 292 | print(' tools/internal_test.py --print_logs %s' % git_hash) |
| 293 | return 1 |
| 294 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 295 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 296 | def run_continuously(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 297 | while True: |
| 298 | print_magic_file_state() |
| 299 | if get_magic_file_exists(READY_FOR_TESTING): |
| 300 | git_hash = get_magic_file_content(READY_FOR_TESTING) |
| 301 | checked_out = git_checkout(git_hash) |
| 302 | if not checked_out: |
| 303 | # Gerrit change, we don't run these on internal. |
| 304 | archive_status(0) |
| 305 | put_magic_file(TESTING_COMPLETE, git_hash) |
| 306 | delete_magic_file(READY_FOR_TESTING) |
| 307 | continue |
| 308 | # Sanity check, if this does not succeed stop. |
| 309 | if checked_out != git_hash: |
| 310 | log('Inconsistent state: %s %s' % (git_hash, checked_out)) |
| 311 | sys.exit(1) |
| 312 | put_magic_file(TESTING, git_hash) |
| 313 | delete_magic_file(READY_FOR_TESTING) |
| 314 | log('Running with hash: %s' % git_hash) |
| 315 | exitcode = run_external() |
| 316 | log('Running finished with exit code %s' % exitcode) |
| 317 | # If the bot timed out or something else triggered the bot to fail, don't |
| 318 | # put up the result (it will not be displayed anywhere, and we can't |
| 319 | # remove the magic file if the bot cleaned up). |
| 320 | if get_magic_file_exists(TESTING): |
| 321 | put_magic_file(TESTING_COMPLETE, git_hash) |
| 322 | # There is still a potential race here (we check, bot deletes, we try to |
| 323 | # delete) - this is unlikely and we ignore it (restart if it happens). |
| 324 | delete_magic_file(TESTING) |
| 325 | time.sleep(PULL_DELAY) |
| 326 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 327 | |
Rico Wind | 62ed83c | 2023-08-25 11:01:53 +0200 | [diff] [blame] | 328 | def run_external(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 329 | return subprocess.call( |
| 330 | [sys.executable, "tools/internal_test.py", "--archive"]) |
| 331 | |
Rico Wind | 62ed83c | 2023-08-25 11:01:53 +0200 | [diff] [blame] | 332 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 333 | def handle_output(archive, stderr, stdout, exitcode, timed_out, cmd): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 334 | if archive: |
| 335 | archive_log(stdout, stderr, exitcode, timed_out, cmd) |
| 336 | else: |
| 337 | print('Execution of %s resulted in:' % cmd) |
| 338 | print('exit code: %s ' % exitcode) |
| 339 | print('timeout: %s ' % timed_out) |
| 340 | with open(stderr, 'r') as f: |
| 341 | print('stderr: %s' % f.read()) |
| 342 | with open(stdout, 'r') as f: |
| 343 | print('stdout: %s' % f.read()) |
| 344 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 345 | |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 346 | def execute(cmd, archive, env=None): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 347 | if cmd == []: |
| 348 | return |
Christoffer Quist Adamsen | 280fae0 | 2021-11-05 13:06:17 +0100 | [diff] [blame] | 349 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 350 | assert (cmd[0].endswith('.py')) |
| 351 | cmd = [sys.executable] + cmd |
Christoffer Quist Adamsen | e2788d2 | 2021-11-05 12:37:39 +0100 | [diff] [blame] | 352 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 353 | utils.PrintCmd(cmd) |
| 354 | with utils.TempDir() as temp: |
| 355 | try: |
| 356 | stderr_fd = None |
| 357 | stdout_fd = None |
| 358 | exitcode = 0 |
| 359 | stderr = os.path.join(temp, 'stderr') |
| 360 | stderr_fd = open(stderr, 'w') |
| 361 | stdout = os.path.join(temp, 'stdout') |
| 362 | stdout_fd = open(stdout, 'w') |
| 363 | popen = subprocess.Popen(cmd, |
| 364 | bufsize=1024 * 1024 * 10, |
| 365 | stdout=stdout_fd, |
| 366 | stderr=stderr_fd, |
| 367 | env=env) |
| 368 | begin = time.time() |
| 369 | timed_out = False |
| 370 | while popen.poll() == None: |
| 371 | if time.time() - begin > RUN_TIMEOUT: |
| 372 | popen.terminate() |
| 373 | timed_out = True |
| 374 | time.sleep(2) |
| 375 | exitcode = popen.returncode |
| 376 | finally: |
| 377 | if stderr_fd: |
| 378 | stderr_fd.close() |
| 379 | if stdout_fd: |
| 380 | stdout_fd.close() |
| 381 | if exitcode != 0: |
| 382 | handle_output(archive, stderr, stdout, popen.returncode, |
| 383 | timed_out, ' '.join(cmd)) |
| 384 | return exitcode |
Morten Krogh-Jespersen | b042c97 | 2019-11-04 08:57:59 +0100 | [diff] [blame] | 385 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 386 | |
| 387 | def run_once(archive): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 388 | git_hash = utils.get_HEAD_sha1() |
| 389 | log('Running once with hash %s' % git_hash) |
| 390 | env = os.environ.copy() |
| 391 | # Bot does not have a lot of memory. |
| 392 | env['R8_GRADLE_CORES_PER_FORK'] = '5' |
| 393 | if archive: |
| 394 | [execute(cmd, archive, env) for cmd in CLEAN_COMMANDS] |
| 395 | failed = any([execute(cmd, archive, env) for cmd in TEST_COMMANDS]) |
| 396 | # Gradle daemon occasionally leaks memory, stop it. |
| 397 | gradle.RunGradle(['--stop']) |
| 398 | archive_status(1 if failed else 0) |
| 399 | return failed |
| 400 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 401 | |
| 402 | def Main(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 403 | (options, args) = ParseOptions() |
| 404 | if options.continuous: |
| 405 | run_continuously() |
| 406 | elif options.bot: |
| 407 | return run_bot() |
| 408 | elif options.print_logs: |
| 409 | return fetch_and_print_logs(options.print_logs) |
| 410 | else: |
| 411 | return run_once(options.archive) |
| 412 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 413 | |
| 414 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 415 | sys.exit(Main()) |