-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_docker.py
84 lines (70 loc) · 1.83 KB
/
test_docker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import os
from typing import List, Optional
import fire
import subprocess
def build(
dockerfile: str,
tagname: str,
python_version: str,
workdir: str,
install_cmd: str,
):
cmd = [
"docker",
"build",
"-f",
dockerfile,
"--build-arg",
f"PYTHON_VERSION={python_version}",
"--build-arg",
f"WORKDIR={workdir}",
"--build-arg",
f"INSTALL_CMD={install_cmd}",
"-t",
tagname,
".",
]
cmd_str = " ".join(cmd)
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
raise ValueError(f"{cmd_str} failed.") from e
print("Build Finished: ", cmd_str)
def test(tagname: str, cmd: List[str]):
run_cmd = ["docker", "run"]
run_cmd.append(tagname)
run_cmd.extend(cmd)
subprocess.run(run_cmd, check=True)
def main(
python_version: str,
package: str,
dockerfile_ext: str,
install_cmd: str = "poetry install",
no_build: bool = False,
):
"""Running test with docker.
Examples:
# testing 'camphr' package in python3.8 environment with Dockerfile.base
$ python test_docker.py 3.8 camphr base
"""
dockerfile = f"dockerfiles/Dockerfile.{dockerfile_ext}"
tagname = f"camphr_{python_version}_{package}_{dockerfile_ext}"
test_cmd = ["/root/test_local.sh", package]
workdir = os.path.join("packages", package)
if not os.path.exists(workdir):
raise ValueError(f"{workdir} not exist")
# build
if not no_build:
build(
dockerfile,
tagname,
python_version,
workdir=workdir,
install_cmd=install_cmd,
)
else:
print("Build skipped")
test(tagname, test_cmd)
if __name__ == "__main__":
fire.Fire(main)