-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathcontainer_structure_test.bzl
96 lines (82 loc) · 3.85 KB
/
container_structure_test.bzl
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
85
86
87
88
89
90
91
92
93
94
95
96
"Implementation details for container_structure_test rule."
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
load("@aspect_bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher_script")
_attrs = {
"image": attr.label(
allow_single_file = True,
doc = "Label of an oci_image or oci_tarball target.",
),
"configs": attr.label_list(allow_files = True, mandatory = True),
"driver": attr.string(
default = "docker",
# https://github.com/GoogleContainerTools/container-structure-test/blob/5e347b66fcd06325e3caac75ef7dc999f1a9b614/pkg/drivers/driver.go#L26-L28
values = ["docker", "tar", "host"],
doc = "See https://github.com/GoogleContainerTools/container-structure-test#running-file-tests-without-docker",
),
"platform": attr.string(
default = "linux/amd64",
doc = "Set platform if host is multi-platform capable (default \"linux/amd64\")",
),
"_runfiles": attr.label(default = "@bazel_tools//tools/bash/runfiles"),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
CMD_HEAD = [
"#!/usr/bin/env bash",
"# This script generated by container_structure_test.bzl",
BASH_RLOCATION_FUNCTION,
]
CMD = """\
readonly st=$(rlocation {st_path})
readonly jq=$(rlocation {jq_path})
readonly image=$(rlocation {image_path})
# When the image points to a folder, we can read the index.json file inside
if [[ -d "$image" ]]; then
readonly DIGEST=$("$jq" -r '.manifests[0].digest | sub(":"; "-")' "$image/index.json")
exec "$st" test --driver {driver} {fixed_args} --default-image-tag "cst.oci.local/$DIGEST:$DIGEST" $@
else
exec "$st" test --driver {driver} {fixed_args} $@
fi
"""
def _structure_test_impl(ctx):
fixed_args = []
test_bin = ctx.toolchains["@container_structure_test//bazel:structure_test_toolchain_type"].st_info.binary
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin
image_path = to_rlocation_path(ctx, ctx.file.image)
# Prefer to use a tarball if we are given one, as it works with more 'driver' types.
if image_path.endswith(".tar"):
fixed_args.extend(["--image", "$(rlocation %s)" % image_path])
else:
# https://github.com/GoogleContainerTools/container-structure-test/blob/5e347b66fcd06325e3caac75ef7dc999f1a9b614/cmd/container-structure-test/app/cmd/test.go#L110
if ctx.attr.driver != "docker":
fail("when the 'driver' attribute is not 'docker', then the image must be a .tar file")
fixed_args.extend(["--ignore-ref-annotation", "--image-from-oci-layout", "$(rlocation %s)" % image_path])
for arg in ctx.files.configs:
fixed_args.extend(["--config", "$(rlocation %s)" % to_rlocation_path(ctx, arg)])
if ctx.attr.platform:
fixed_args.extend(["--platform", ctx.attr.platform])
bash_launcher = ctx.actions.declare_file("%s.sh" % ctx.label.name)
ctx.actions.write(
bash_launcher,
content = "\n".join(CMD_HEAD) + CMD.format(
st_path = to_rlocation_path(ctx, test_bin),
jq_path = to_rlocation_path(ctx, jq_bin),
driver = ctx.attr.driver,
image_path = image_path,
fixed_args = " ".join(fixed_args),
),
is_executable = True,
)
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher
runfiles = ctx.runfiles(
files = ctx.files.image + ctx.files.configs + [
bash_launcher,
test_bin,
jq_bin,
],
).merge(ctx.attr._runfiles.default_runfiles)
return DefaultInfo(runfiles = runfiles, executable = launcher)
lib = struct(
attrs = _attrs,
implementation = _structure_test_impl,
)