Skip to content

Commit ce6bfcb

Browse files
committed
Revert "Delete run-container-test script"
This reverts commit ab7851f.
1 parent 0596588 commit ce6bfcb

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
import textwrap
27+
28+
VERBOSE = False
29+
30+
DISTRO_TO_IMAGE_NAME = {
31+
"ubuntu-22.04": "public.ecr.aws/lts/ubuntu:22.04_stable",
32+
"al2023": "public.ecr.aws/amazonlinux/amazonlinux:2023",
33+
"al2": "public.ecr.aws/amazonlinux/amazonlinux:2"
34+
}
35+
36+
DISTRO_TO_PACKAGES = {
37+
"al2023": [ "libxcrypt-compat" ],
38+
}
39+
40+
DOCKER_PLATFORM_BY_ARCH = {
41+
"x64": "linux/amd64",
42+
"arm64": "linux/arm64"
43+
}
44+
45+
46+
def vprint(message):
47+
global VERBOSE
48+
if VERBOSE:
49+
print(message)
50+
51+
52+
def running_in_github_action():
53+
"""
54+
Test if currently running in a GitHub action or running locally
55+
:return: True if running in GH, False otherwise
56+
"""
57+
return "GITHUB_WORKFLOW" in os.environ
58+
59+
60+
def shell(command, cwd=None, check=True, capture_output=False):
61+
"""
62+
Run a command
63+
:param command: command to run
64+
:param cwd: the current working directory to change to before executing the command
65+
:param check: flag indicating if the status code should be checked. When true an exception will be
66+
thrown if the command exits with a non-zero exit status.
67+
:returns: the subprocess CompletedProcess output
68+
"""
69+
vprint(f"running `{command}`")
70+
return subprocess.run(command, shell=True, check=check, cwd=cwd, capture_output=capture_output)
71+
72+
73+
def oci_executable():
74+
"""
75+
Attempt to find the OCI container executor used to build and run docker containers
76+
"""
77+
oci_exe = os.environ.get('OCI_EXE')
78+
if oci_exe is not None:
79+
return oci_exe
80+
81+
executors = ['finch', 'podman', 'docker']
82+
83+
for exe in executors:
84+
if shutil.which(exe) is not None:
85+
return exe
86+
87+
print("cannot find container executor")
88+
exit(1)
89+
90+
91+
def create_docker_image(opts, oci_exe, base_image_name, packages):
92+
lines = []
93+
94+
# Set base image
95+
lines.append(f"FROM {base_image_name}")
96+
97+
# Install extra packages if necessary
98+
if packages:
99+
cmd = [
100+
"RUN yum -y install",
101+
*packages,
102+
]
103+
lines.append(' '.join(cmd))
104+
105+
# Compile the contents
106+
content = '\n'.join(lines) + '\n'
107+
108+
# Write the Dockerfile
109+
with open("Dockerfile", "w") as f:
110+
f.write(content)
111+
112+
# Build the image
113+
image_name = f"container-test-{opts.distro}:latest"
114+
platform = DOCKER_PLATFORM_BY_ARCH[opts.arch]
115+
cmd = shlex.join([
116+
oci_exe,
117+
"build",
118+
"-t",
119+
image_name,
120+
"--platform",
121+
platform,
122+
".",
123+
])
124+
shell(cmd)
125+
126+
print(f"Created Docker image {image_name}. Listing hosted images to be certain:")
127+
shell("docker image ls --all")
128+
129+
return image_name
130+
131+
132+
def get_docker_image(opts, oci_exe):
133+
base_image_name = DISTRO_TO_IMAGE_NAME[opts.distro]
134+
packages = DISTRO_TO_PACKAGES.get(opts.distro)
135+
if packages:
136+
return create_docker_image(opts, oci_exe, base_image_name, packages)
137+
else:
138+
return base_image_name
139+
140+
141+
def run_docker_test(opts):
142+
"""
143+
Run a docker test for a precompiled Kotlin/Native binary
144+
145+
:param opts: the parsed command line options
146+
"""
147+
platform = DOCKER_PLATFORM_BY_ARCH[opts.arch]
148+
oci_exe = oci_executable()
149+
image_name = get_docker_image(opts, oci_exe)
150+
151+
test_bin_dir = os.path.abspath(opts.test_bin_dir)
152+
path_to_exe = f'./linux{opts.arch.capitalize()}/debugTest/test.kexe'
153+
154+
cmd = [
155+
oci_exe,
156+
'run',
157+
'--rm',
158+
f'-v{test_bin_dir}:/test',
159+
]
160+
if not opts.no_system_certs:
161+
cmd.append(f'-v/etc/ssl:/etc/ssl')
162+
163+
cmd.extend(
164+
[
165+
'-w/test',
166+
'-e DEBIAN_FRONTEND=noninteractive',
167+
'--platform',
168+
platform,
169+
image_name,
170+
path_to_exe,
171+
]
172+
)
173+
174+
cmd = shlex.join(cmd)
175+
print(cmd)
176+
shell(cmd)
177+
178+
179+
def create_cli():
180+
parser = argparse.ArgumentParser(
181+
prog="run-container-test",
182+
description="Run cross platform test binaries in a container",
183+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
184+
)
185+
186+
parser.add_argument("-v", "--verbose", help="enable verbose output", action="store_true")
187+
188+
parser.add_argument("--distro", required=True, choices=DISTRO_TO_IMAGE_NAME.keys(), help="the distribution name to run the task on")
189+
parser.add_argument("--arch", required=True, choices=DOCKER_PLATFORM_BY_ARCH.keys(), help="the architecture to use")
190+
parser.add_argument("--test-bin-dir", required=True, help="the path to the test binary directory root")
191+
parser.add_argument("--no-system-certs", action='store_true', help="disable mounting system certificates into the container")
192+
193+
return parser
194+
195+
196+
def main():
197+
cli = create_cli()
198+
opts = cli.parse_args()
199+
if opts.verbose:
200+
global VERBOSE
201+
VERBOSE = True
202+
203+
run_docker_test(opts)
204+
205+
206+
if __name__ == '__main__':
207+
main()

0 commit comments

Comments
 (0)