|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +""" |
| 7 | +Run precompiled Kotlin/Native test binaries in a Docker container for a specific Linux distribution and architecture. |
| 8 | +
|
| 9 | +This requires Docker multiarch support, see https://docs.docker.com/build/building/multi-platform/ and https://github.com/multiarch/qemu-user-static |
| 10 | +In GitHub we use a provided action for this: https://github.com/docker/setup-qemu-action |
| 11 | +
|
| 12 | +Locally you would need to run one of: |
| 13 | +
|
| 14 | +`docker run --rm --privileged multiarch/qemu-user-static --reset -p yes --credential yes` |
| 15 | +
|
| 16 | +OR |
| 17 | +
|
| 18 | +`docker run --privileged --rm tonistiigi/binfmt --install all` |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import subprocess |
| 24 | +import shlex |
| 25 | +import shutil |
| 26 | + |
| 27 | +VERBOSE = False |
| 28 | + |
| 29 | +DISTRO_TO_IMAGE_NAME = { |
| 30 | + "ubuntu-22.04": "public.ecr.aws/lts/ubuntu:22.04_stable", |
| 31 | + "al2023": "public.ecr.aws/amazonlinux/amazonlinux:2023", |
| 32 | + "al2": "public.ecr.aws/amazonlinux/amazonlinux:2" |
| 33 | +} |
| 34 | + |
| 35 | +DOCKER_PLATFORM_BY_ARCH = { |
| 36 | + "x64": "linux/amd64", |
| 37 | + "arm64": "linux/arm64" |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +def vprint(message): |
| 42 | + global VERBOSE |
| 43 | + if VERBOSE: |
| 44 | + print(message) |
| 45 | + |
| 46 | + |
| 47 | +def running_in_github_action(): |
| 48 | + """ |
| 49 | + Test if currently running in a GitHub action or running locally |
| 50 | + :return: True if running in GH, False otherwise |
| 51 | + """ |
| 52 | + return "GITHUB_WORKFLOW" in os.environ |
| 53 | + |
| 54 | + |
| 55 | +def shell(command, cwd=None, check=True, capture_output=False): |
| 56 | + """ |
| 57 | + Run a command |
| 58 | + :param command: command to run |
| 59 | + :param cwd: the current working directory to change to before executing the command |
| 60 | + :param check: flag indicating if the status code should be checked. When true an exception will be |
| 61 | + thrown if the command exits with a non-zero exit status. |
| 62 | + :returns: the subprocess CompletedProcess output |
| 63 | + """ |
| 64 | + vprint(f"running `{command}`") |
| 65 | + return subprocess.run(command, shell=True, check=check, cwd=cwd, capture_output=capture_output) |
| 66 | + |
| 67 | + |
| 68 | +def oci_executable(): |
| 69 | + """ |
| 70 | + Attempt to find the OCI container executor used to build and run docker containers |
| 71 | + """ |
| 72 | + oci_exe = os.environ.get('OCI_EXE') |
| 73 | + if oci_exe is not None: |
| 74 | + return oci_exe |
| 75 | + |
| 76 | + executors = ['finch', 'podman', 'docker'] |
| 77 | + |
| 78 | + for exe in executors: |
| 79 | + if shutil.which(exe) is not None: |
| 80 | + return exe |
| 81 | + |
| 82 | + print("cannot find container executor") |
| 83 | + exit(1) |
| 84 | + |
| 85 | + |
| 86 | +def run_docker_test(opts): |
| 87 | + """ |
| 88 | + Run a docker test for a precompiled Kotlin/Native binary |
| 89 | +
|
| 90 | + :param opts: the parsed command line options |
| 91 | + """ |
| 92 | + platform = DOCKER_PLATFORM_BY_ARCH[opts.arch] |
| 93 | + oci_exe = oci_executable() |
| 94 | + |
| 95 | + test_bin_dir = os.path.abspath(opts.test_bin_dir) |
| 96 | + image_name = DISTRO_TO_IMAGE_NAME[opts.distro] |
| 97 | + path_to_exe = f'./linux{opts.arch.capitalize()}/debugTest/test.kexe' |
| 98 | + |
| 99 | + cmd = [ |
| 100 | + oci_exe, |
| 101 | + 'run', |
| 102 | + '--rm', |
| 103 | + f'-v{test_bin_dir}:/test', |
| 104 | + ] |
| 105 | + if not opts.no_system_certs: |
| 106 | + cmd.append(f'-v/etc/ssl:/etc/ssl') |
| 107 | + |
| 108 | + cmd.extend( |
| 109 | + [ |
| 110 | + '-w/test', |
| 111 | + '-e DEBIAN_FRONTEND=noninteractive', |
| 112 | + '--platform', |
| 113 | + platform, |
| 114 | + image_name, |
| 115 | + path_to_exe, |
| 116 | + ] |
| 117 | + ) |
| 118 | + |
| 119 | + cmd = shlex.join(cmd) |
| 120 | + print(cmd) |
| 121 | + shell(cmd) |
| 122 | + |
| 123 | + |
| 124 | +def create_cli(): |
| 125 | + parser = argparse.ArgumentParser( |
| 126 | + prog="run-container-test", |
| 127 | + description="Run cross platform test binaries in a container", |
| 128 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 129 | + ) |
| 130 | + |
| 131 | + parser.add_argument("-v", "--verbose", help="enable verbose output", action="store_true") |
| 132 | + |
| 133 | + parser.add_argument("--distro", required=True, choices=DISTRO_TO_IMAGE_NAME.keys(), help="the distribution name to run the task on") |
| 134 | + parser.add_argument("--arch", required=True, choices=DOCKER_PLATFORM_BY_ARCH.keys(), help="the architecture to use") |
| 135 | + parser.add_argument("--test-bin-dir", required=True, help="the path to the test binary directory root") |
| 136 | + parser.add_argument("--no-system-certs", action='store_true', help="disable mounting system certificates into the container") |
| 137 | + |
| 138 | + return parser |
| 139 | + |
| 140 | + |
| 141 | +def main(): |
| 142 | + cli = create_cli() |
| 143 | + opts = cli.parse_args() |
| 144 | + if opts.verbose: |
| 145 | + global VERBOSE |
| 146 | + VERBOSE = True |
| 147 | + |
| 148 | + run_docker_test(opts) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + main() |
0 commit comments