Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions bazel/container_structure_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,41 @@ _attrs = {
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}

CMD = """\
#!/usr/bin/env bash

{BASH_RLOCATION_FUNCTION}
CMD_HEAD = [
"#!/usr/bin/env bash",
"# This script generated by container_structure_test.bzl",
BASH_RLOCATION_FUNCTION,
]

CMD = """\
readonly st=$(rlocation {st_path})
readonly yq=$(rlocation {yq_path})
readonly image=$(rlocation {image_path})

# When the image points to a folder, we can read the index.json file inside
if [[ -d "{image_path}" ]]; then
readonly DIGEST=$("$yq" eval '.manifests[0].digest | sub(":"; "-")' "{image_path}/index.json")
exec "$st" test {fixed_args} --default-image-tag "registry.structure_test.oci.local/image:$DIGEST" $@
if [[ -d "$image" ]]; then
readonly DIGEST=$("$yq" eval '.manifests[0].digest | sub(":"; "-")' "$image/index.json")
exec "$st" test --driver {driver} {fixed_args} --default-image-tag "registry.structure_test.oci.local/image:$DIGEST" $@
else
exec "$st" test {fixed_args} $@
exec "$st" test --driver {driver} {fixed_args} $@
fi
"""

def _structure_test_impl(ctx):
fixed_args = ["--driver", ctx.attr.driver]
fixed_args = []
test_bin = ctx.toolchains["@container_structure_test//bazel:structure_test_toolchain_type"].st_info.binary
yq_bin = ctx.toolchains["@aspect_bazel_lib//lib:yq_toolchain_type"].yqinfo.bin

image_path = ctx.file.image.short_path
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", image_path])
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(["--image-from-oci-layout", image_path])
fixed_args.extend(["--image-from-oci-layout", "$(rlocation %s)" % image_path])
fixed_args.extend(["--default-image-tag", "registry.structure_test.oci.local/image:$DIGEST"])

for arg in ctx.files.configs:
Expand All @@ -58,10 +62,10 @@ def _structure_test_impl(ctx):
bash_launcher = ctx.actions.declare_file("%s.sh" % ctx.label.name)
ctx.actions.write(
bash_launcher,
content = CMD.format(
BASH_RLOCATION_FUNCTION = BASH_RLOCATION_FUNCTION,
content = "\n".join(CMD_HEAD) + CMD.format(
st_path = to_rlocation_path(ctx, test_bin),
yq_path = to_rlocation_path(ctx, yq_bin),
driver = ctx.attr.driver,
image_path = image_path,
fixed_args = " ".join(fixed_args),
),
Expand All @@ -70,11 +74,13 @@ def _structure_test_impl(ctx):

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, yq_bin
]).merge(ctx.attr._runfiles.default_runfiles)
bash_launcher,
test_bin,
yq_bin,
],
).merge(ctx.attr._runfiles.default_runfiles)

return DefaultInfo(runfiles = runfiles, executable = launcher)

Expand Down