Skip to content

Commit ba77e16

Browse files
committed
add tests
1 parent 494873d commit ba77e16

File tree

5 files changed

+61
-28
lines changed

5 files changed

+61
-28
lines changed

generate.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
parser.add_argument("-o", "--out", "--output", default="Dockerfile",
2323
help="output file name to write Dockerfile to, defaults to 'Dockerfile'")
2424
parser.add_argument("-j", action="store_true", help="Also output a json with information about the image.")
25+
parser.add_argument("--tests", action="store_true", help="Also generate test.sh that will run tests for the image")
2526

2627

2728
args = parser.parse_args()
@@ -37,11 +38,11 @@
3738
context = config.default_env.load(args.config)
3839
else:
3940
package_info = package_filename.parse(args.package_url.rsplit("/", 1)[-1])
40-
package_version = package_info.get("version")
41-
package_version_extra = package_info.get("version_extra")
42-
if args.wheels_url and package_version and package_version_extra:
43-
# concatenating without the last part
44-
package_version += "." + package_version_extra.rsplit(".", 1)[0]
41+
package_version = package_info["version"]
42+
wheels_version = package_version
43+
if args.wheels_url:
44+
# this is a pre-release then add extra version
45+
package_version += "." + package_info["version_extra"]
4546
config_data = {
4647
"_based_on": package_info["os"],
4748
"_template": "Dockerfile_default.j2",
@@ -50,7 +51,7 @@
5051
"version": package_version,
5152
"wheels": {
5253
"url": args.wheels_url,
53-
# "version": None
54+
"version": wheels_version
5455
}
5556
}
5657
}
@@ -72,13 +73,31 @@
7273
with open(args.out, "w") as outfile:
7374
outfile.write(dockerfile)
7475

76+
image_name = f"{context['os_id']}_{args.preset}:{context['package']['version']}"
77+
7578
if args.j:
7679
with open("image_data.json", "w") as file:
7780
json.dump({
78-
"image_name": f"{context['os_id']}_{args.preset}:{context['package']['version']}",
81+
"image_name": image_name,
7982
"base_image": context["base_image"],
8083
"product_version": context["package"]["version"],
8184
"wheels_version": None,
8285
"distribution": args.preset,
8386
"os": context["os_id"]
8487
}, file)
88+
89+
if args.tests:
90+
testfile = open("test.sh", "w")
91+
testfile.write("set -e\n")
92+
testfile.write(f"IMAGE_NAME={image_name}\n")
93+
testfile.write("TESTS_VOLUME=./refactor/tests/in_container:/tests\n")
94+
testfile.write('crun() { docker run --rm -it -v $TESTS_VOLUME $IMAGE_NAME bash -c "$1"; }\n')
95+
testfile.write('run_test() { echo TEST $1 $2; crun "/tests/$1 $2 2>&1 >/dev/null && echo SUCCESS || echo ERROR"; }\n')
96+
testfile.write("docker build -t $IMAGE_NAME .\n")
97+
for test in context["tests"]:
98+
arg = None
99+
try:
100+
test, arg = test.split("@", 1)
101+
except ValueError:
102+
pass
103+
testfile.write(f"run_test {test} {arg}\n")

refactor/tests/test_configs.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
import json
3+
import os
4+
5+
from urllib.request import Request, urlopen
6+
7+
8+
def parametrize_configs(path):
9+
to_scan = [path]
10+
while to_scan:
11+
for item in os.scandir(to_scan.pop()):
12+
if item.is_dir():
13+
to_scan.append(item.path)
14+
continue
15+
if item.name.endswith(".json"):
16+
yield item.path
17+
18+
19+
@pytest.mark.parametrize('path', parametrize_configs("configs"))
20+
def test_valid_json(path):
21+
json.load(open(path))
22+
23+
24+
@pytest.mark.parametrize('path', parametrize_configs("configs/releases"))
25+
def test_package_url_exists(path):
26+
data = json.load(open(path))
27+
package = data.get("package")
28+
if not package:
29+
pytest.skip("no package defined")
30+
package_url = package.get("url")
31+
if not package_url:
32+
raise Exception("package defined, but no url")
33+
urlopen(Request(package_url, method="HEAD"))

refactor/tests/test_imagepy.py renamed to refactor/tests/test_entrypoint.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_runs_without_virtualenv():
1717
if "VIRTUAL_ENV" in environ:
1818
del environ["VIRTUAL_ENV"]
1919
subprocess.check_call(
20-
args=[os.path.realpath(sys.executable), "image.py", "--help"],
20+
args=[os.path.realpath(sys.executable), "generate.py", "--help"],
2121
env=environ
2222
)
2323

@@ -56,22 +56,3 @@ def parametrize_config_preset():
5656
def test_dockerfile_generate(config, preset, temp_file):
5757
dockerfile = temp_file("Dockerfile")
5858
subprocess.check_call([sys.executable, "image.py", config, "-p", preset, "-o", dockerfile])
59-
60-
61-
@pytest.mark.parametrize('config,preset', parametrize_config_preset())
62-
def test_dockerfile_build_internal(config, preset, temp_file):
63-
dockerfile = temp_file("Dockerfile")
64-
subprocess.check_call([sys.executable, "image.py", config, "-p", preset, "-o", dockerfile, "--build"])
65-
66-
67-
@pytest.mark.parametrize('config,preset', parametrize_config_preset())
68-
def test_dockerfile_build_manually(config, preset, temp_file):
69-
dockerfile = temp_file("Dockerfile")
70-
subprocess.check_call([sys.executable, "image.py", config, "-p", preset, "-o", dockerfile])
71-
subprocess.check_call(["docker", "build", "-f", dockerfile])
72-
73-
74-
@pytest.mark.parametrize('config,preset', parametrize_config_preset())
75-
def test_dockerfile_test_internal(config, preset, temp_file):
76-
dockerfile = temp_file("Dockerfile")
77-
subprocess.check_call([sys.executable, "image.py", config, "-p", preset, "-o", dockerfile, "--build", "--test"])

refactor/tests/test_oldentrypoint.py

Whitespace-only changes.

templates/Dockerfile_default.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ RUN mkdir -p $INTEL_OPENVINO_DIR && \
5555
chmod a=rwx $INTEL_OPENVINO_DIR && \
5656
cd $INTEL_OPENVINO_DIR && \
5757
curl "{{ package.url }}" \
58-
| tar -xz --strip-components=1
58+
| tar -xz --strip-components=1 --no-same-owner --owner=openvino --group=openvino
5959

6060
# Package env setup
6161
ENV PKG_CONFIG_PATH=$INTEL_OPENVINO_DIR/runtime/lib/intel64/pkgconfig

0 commit comments

Comments
 (0)