Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
| 32 | import optparse |
| 33 | import os |
| 34 | import subprocess |
| 35 | import sys |
| 36 | import time |
| 37 | import utils |
| 38 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 39 | # How often the bot/tester should check state |
| 40 | PULL_DELAY = 30 |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 41 | BUCKET = 'r8-test-results' |
| 42 | TEST_RESULT_DIR = 'internal' |
| 43 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 44 | # Magic files |
| 45 | READY_FOR_TESTING = 'READY_FOR_TESTING' |
| 46 | TESTING = 'TESTING' |
| 47 | TESTING_COMPLETE = 'TESTING_COMPLETE' |
| 48 | |
| 49 | ALL_MAGIC = [READY_FOR_TESTING, TESTING, TESTING_COMPLETE] |
| 50 | |
| 51 | # Log file names |
| 52 | STDERR = 'stderr' |
| 53 | STDOUT = 'stdout' |
| 54 | EXITCODE = 'exitcode' |
| 55 | TIMED_OUT = 'timed_out' |
| 56 | |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 57 | TEST_COMMANDS = [ |
| 58 | ['tools/test.py', '--only_internal'], |
| 59 | ['tools/run_on_app.py', '--ignore-java-version','--run-all', '--out=out'] |
| 60 | ] |
| 61 | |
| 62 | # Command timeout, in seconds. |
Rico Wind | f021d83 | 2018-12-13 11:29:22 +0100 | [diff] [blame] | 63 | RUN_TIMEOUT = 7200 |
| 64 | BOT_RUN_TIMEOUT = RUN_TIMEOUT * len(TEST_COMMANDS) |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 65 | |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 66 | def log(str): |
Rico Wind | ffccab1 | 2018-09-26 12:39:42 +0200 | [diff] [blame] | 67 | print("%s: %s" % (time.strftime("%c"), str)) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 68 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 69 | def ParseOptions(): |
| 70 | result = optparse.OptionParser() |
| 71 | result.add_option('--continuous', |
| 72 | help='Continuously run internal tests and post results to GCS.', |
| 73 | default=False, action='store_true') |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 74 | result.add_option('--print_logs', |
| 75 | help='Fetch logs from gcs and print them, takes the commit to print for.', |
| 76 | default=None) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 77 | result.add_option('--bot', |
| 78 | help='Run in bot mode, i.e., scheduling runs.', |
| 79 | default=False, action='store_true') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 80 | result.add_option('--archive', |
| 81 | help='Post result to GCS, implied by --continuous', |
| 82 | default=False, action='store_true') |
| 83 | return result.parse_args() |
| 84 | |
| 85 | def get_own_file_content(): |
| 86 | with open(sys.argv[0], 'r') as us: |
| 87 | return us.read() |
| 88 | |
| 89 | def restart_if_new_version(original_content): |
| 90 | new_content = get_own_file_content() |
| 91 | if new_content != original_content: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 92 | log('Restarting tools/internal_test.py, content changed') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 93 | os.execv(sys.argv[0], sys.argv) |
| 94 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 95 | def ensure_git_clean(): |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 96 | # Ensure clean git repo. |
| 97 | diff = subprocess.check_output(['git', 'diff']) |
| 98 | if len(diff) > 0: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 99 | log('Local modifications to the git repo, exiting') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 100 | sys.exit(1) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 101 | |
| 102 | def git_pull(): |
| 103 | ensure_git_clean() |
Rico Wind | 2a19d93 | 2018-09-25 16:48:56 +0200 | [diff] [blame] | 104 | subprocess.check_call(['git', 'checkout', 'master']) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 105 | subprocess.check_call(['git', 'pull']) |
| 106 | return utils.get_HEAD_sha1() |
| 107 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 108 | def git_checkout(git_hash): |
| 109 | ensure_git_clean() |
| 110 | # Ensure that we are up to date to get the commit. |
| 111 | git_pull() |
| 112 | subprocess.check_call(['git', 'checkout', git_hash]) |
| 113 | return utils.get_HEAD_sha1() |
| 114 | |
| 115 | def get_test_result_dir(): |
| 116 | return os.path.join(BUCKET, TEST_RESULT_DIR) |
| 117 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 118 | def get_sha_destination(sha): |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 119 | return os.path.join(get_test_result_dir(), sha) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 120 | |
| 121 | def archive_status(failed): |
| 122 | gs_destination = 'gs://%s' % get_sha_destination(utils.get_HEAD_sha1()) |
| 123 | archive_value('status', gs_destination, failed) |
| 124 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 125 | def get_status(sha): |
| 126 | gs_destination = 'gs://%s/status' % get_sha_destination(sha) |
| 127 | return utils.cat_file_on_cloud_storage(gs_destination) |
| 128 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 129 | def archive_file(name, gs_dir, src_file): |
| 130 | gs_file = '%s/%s' % (gs_dir, name) |
| 131 | utils.upload_file_to_cloud_storage(src_file, gs_file, public_read=False) |
| 132 | |
| 133 | def archive_value(name, gs_dir, value): |
| 134 | with utils.TempDir() as temp: |
| 135 | tempfile = os.path.join(temp, name); |
| 136 | with open(tempfile, 'w') as f: |
| 137 | f.write(str(value)) |
| 138 | archive_file(name, gs_dir, tempfile) |
| 139 | |
| 140 | def archive_log(stdout, stderr, exitcode, timed_out, cmd): |
| 141 | sha = utils.get_HEAD_sha1() |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 142 | cmd_dir = cmd.replace(' ', '_').replace('/', '_') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 143 | destination = os.path.join(get_sha_destination(sha), cmd_dir) |
| 144 | gs_destination = 'gs://%s' % destination |
| 145 | url = 'https://storage.cloud.google.com/%s' % destination |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 146 | log('Archiving logs to: %s' % gs_destination) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 147 | archive_value(EXITCODE, gs_destination, exitcode) |
| 148 | archive_value(TIMED_OUT, gs_destination, timed_out) |
| 149 | archive_file(STDOUT, gs_destination, stdout) |
| 150 | archive_file(STDERR, gs_destination, stderr) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 151 | log('Logs available at: %s' % url) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 152 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 153 | def get_magic_file_base_path(): |
| 154 | return 'gs://%s/magic' % get_test_result_dir() |
| 155 | |
| 156 | def get_magic_file_gs_path(name): |
| 157 | return '%s/%s' % (get_magic_file_base_path(), name) |
| 158 | |
| 159 | def get_magic_file_exists(name): |
| 160 | return utils.file_exists_on_cloud_storage(get_magic_file_gs_path(name)) |
| 161 | |
| 162 | def delete_magic_file(name): |
| 163 | utils.delete_file_from_cloud_storage(get_magic_file_gs_path(name)) |
| 164 | |
| 165 | def put_magic_file(name, sha): |
| 166 | archive_value(name, get_magic_file_base_path(), sha) |
| 167 | |
| 168 | def get_magic_file_content(name, ignore_errors=False): |
| 169 | return utils.cat_file_on_cloud_storage(get_magic_file_gs_path(name), |
| 170 | ignore_errors=ignore_errors) |
| 171 | |
| 172 | def print_magic_file_state(): |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 173 | log('Magic file status:') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 174 | for magic in ALL_MAGIC: |
| 175 | if get_magic_file_exists(magic): |
| 176 | content = get_magic_file_content(magic, ignore_errors=True) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 177 | log('%s content: %s' % (magic, content)) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 178 | |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 179 | def fetch_and_print_logs(hash): |
| 180 | gs_base = 'gs://%s' % get_sha_destination(hash) |
| 181 | listing = utils.ls_files_on_cloud_storage(gs_base).strip().split('\n') |
| 182 | for entry in listing: |
| 183 | if not entry.endswith('/status'): # Ignore the overall status file |
| 184 | for to_print in [EXITCODE, TIMED_OUT, STDERR, STDOUT]: |
| 185 | gs_location = '%s%s' % (entry, to_print) |
| 186 | value = utils.cat_file_on_cloud_storage(gs_location) |
| 187 | print('\n\n%s had value:\n%s' % (to_print, value)) |
| 188 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 189 | def run_bot(): |
| 190 | print_magic_file_state() |
| 191 | # Ensure that there is nothing currently scheduled (broken/stopped run) |
| 192 | for magic in ALL_MAGIC: |
| 193 | if get_magic_file_exists(magic): |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 194 | log('ERROR: Synchronizing file %s exists, cleaning up' % magic) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 195 | delete_magic_file(magic) |
| 196 | print_magic_file_state() |
| 197 | assert not get_magic_file_exists(READY_FOR_TESTING) |
| 198 | git_hash = utils.get_HEAD_sha1() |
| 199 | put_magic_file(READY_FOR_TESTING, git_hash) |
| 200 | begin = time.time() |
| 201 | while True: |
| 202 | if time.time() - begin > BOT_RUN_TIMEOUT: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 203 | log('Timeout exceeded: http://go/internal-r8-doc') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 204 | raise Exception('Bot timeout') |
| 205 | if get_magic_file_exists(TESTING_COMPLETE): |
| 206 | if get_magic_file_content(TESTING_COMPLETE) == git_hash: |
| 207 | break |
| 208 | else: |
| 209 | raise Exception('Non matching git hashes %s and %s' % ( |
| 210 | get_magic_file_content(TESTING_COMPLETE), git_hash)) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 211 | log('Still waiting for test result') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 212 | print_magic_file_state() |
| 213 | time.sleep(PULL_DELAY) |
| 214 | total_time = time.time()-begin |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 215 | log('Done running test for %s in %ss' % (git_hash, total_time)) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 216 | test_status = get_status(git_hash) |
| 217 | delete_magic_file(TESTING_COMPLETE) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 218 | log('Test status is: %s' % test_status) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 219 | if test_status != '0': |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 220 | fetch_and_print_logs(git_hash) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 221 | return 1 |
| 222 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 223 | def run_continuously(): |
| 224 | # If this script changes, we will restart ourselves |
| 225 | own_content = get_own_file_content() |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 226 | while True: |
| 227 | restart_if_new_version(own_content) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 228 | print_magic_file_state() |
| 229 | if get_magic_file_exists(READY_FOR_TESTING): |
| 230 | git_hash = get_magic_file_content(READY_FOR_TESTING) |
| 231 | checked_out = git_checkout(git_hash) |
| 232 | # Sanity check, if this does not succeed stop. |
| 233 | if checked_out != git_hash: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 234 | log('Inconsistent state: %s %s' % (git_hash, checked_out)) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 235 | sys.exit(1) |
| 236 | put_magic_file(TESTING, git_hash) |
| 237 | delete_magic_file(READY_FOR_TESTING) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 238 | log('Running with hash: %s' % git_hash) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 239 | exitcode = run_once(archive=True) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 240 | log('Running finished with exit code %s' % exitcode) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 241 | put_magic_file(TESTING_COMPLETE, git_hash) |
| 242 | delete_magic_file(TESTING) |
| 243 | time.sleep(PULL_DELAY) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 244 | |
| 245 | def handle_output(archive, stderr, stdout, exitcode, timed_out, cmd): |
| 246 | if archive: |
| 247 | archive_log(stdout, stderr, exitcode, timed_out, cmd) |
| 248 | else: |
| 249 | print 'Execution of %s resulted in:' % cmd |
| 250 | print 'exit code: %s ' % exitcode |
| 251 | print 'timeout: %s ' % timed_out |
| 252 | with open(stderr, 'r') as f: |
| 253 | print 'stderr: %s' % f.read() |
| 254 | with open(stdout, 'r') as f: |
| 255 | print 'stdout: %s' % f.read() |
| 256 | |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 257 | def execute(cmd, archive, env=None): |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 258 | utils.PrintCmd(cmd) |
| 259 | with utils.TempDir() as temp: |
| 260 | try: |
| 261 | stderr_fd = None |
| 262 | stdout_fd = None |
| 263 | exitcode = 0 |
| 264 | stderr = os.path.join(temp, 'stderr') |
| 265 | stderr_fd = open(stderr, 'w') |
| 266 | stdout = os.path.join(temp, 'stdout') |
| 267 | stdout_fd = open(stdout, 'w') |
| 268 | popen = subprocess.Popen(cmd, |
| 269 | bufsize=1024*1024*10, |
| 270 | stdout=stdout_fd, |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 271 | stderr=stderr_fd, |
| 272 | env=env) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 273 | begin = time.time() |
| 274 | timed_out = False |
| 275 | while popen.poll() == None: |
| 276 | if time.time() - begin > RUN_TIMEOUT: |
| 277 | popen.terminate() |
| 278 | timed_out = True |
| 279 | time.sleep(2) |
| 280 | exitcode = popen.returncode |
| 281 | finally: |
| 282 | if stderr_fd: |
| 283 | stderr_fd.close() |
| 284 | if stdout_fd: |
| 285 | stdout_fd.close() |
| 286 | if exitcode != 0: |
| 287 | handle_output(archive, stderr, stdout, popen.returncode, |
| 288 | timed_out, ' '.join(cmd)) |
| 289 | return exitcode |
| 290 | |
| 291 | def run_once(archive): |
| 292 | failed = False |
| 293 | git_hash = utils.get_HEAD_sha1() |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 294 | log('Running once with hash %s' % git_hash) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 295 | # Run test.py internal testing. |
Morten Krogh-Jespersen | b4f8e16 | 2018-12-21 13:43:43 +0100 | [diff] [blame] | 296 | # TODO(mkrogh) Change this to --r8lib when we have it working with dependencies relocated. |
| 297 | cmd = ['tools/test.py', '--only_internal', '--r8lib_no_deps'] |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 298 | env = os.environ.copy() |
| 299 | # Bot does not have a lot of memory. |
| 300 | env['R8_GRADLE_CORES_PER_FORK'] = '8' |
| 301 | if execute(cmd, archive, env): |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 302 | failed = True |
| 303 | # Ensure that all internal apps compile. |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 304 | cmd = ['tools/run_on_app.py', '--ignore-java-version','--run-all', |
| 305 | '--out=out'] |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 306 | if execute(cmd, archive): |
| 307 | failed = True |
| 308 | archive_status(1 if failed else 0) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 309 | return failed |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 310 | |
| 311 | def Main(): |
| 312 | (options, args) = ParseOptions() |
| 313 | if options.continuous: |
| 314 | run_continuously() |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 315 | elif options.bot: |
| 316 | return run_bot() |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 317 | elif options.print_logs: |
| 318 | return fetch_and_print_logs(options.print_logs) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 319 | else: |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 320 | return run_once(options.archive) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 321 | |
| 322 | if __name__ == '__main__': |
| 323 | sys.exit(Main()) |