Skip to content

Commit

Permalink
Improve bazel based testing, by using a run action.
Browse files Browse the repository at this point in the history
We had trouble with the tar file moving somewhere else on cloud build,
maybe because of different sandboxing or something. By using a run
action directly (instead of a script embedding a path), we avoid the path
problem.

It is also cleaner.
  • Loading branch information
jarondl committed Oct 27, 2024
1 parent 8be9bcb commit 7d7cf4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
14 changes: 5 additions & 9 deletions example_bazel/diff_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@
set -o nounset

diff_test () {
local cmd="$1"
local result="$1"
local want="$2"
local got
got="$(set -o pipefail; "$cmd" | sed '1,/===marker===/ d')"
if [[ "$?" -ne 0 ]]; then
echo "${cmd}: run failed" >&2
return 1
fi

got="$(cat "$result" | sed '1,/===marker===/ d')"

if ! diff <( echo "$want") <(echo "$got"); then
echo "${cmd}: diff failed or differences found" >&2
echo "diff failed or differences found" >&2
return 1
fi
return 0
Expand All @@ -37,6 +33,6 @@ read -r -d '' GOLDEN_STR << EOF
{GOLDEN}
EOF

diff_test "{CMD}" "${GOLDEN_STR}"
diff_test "{RESULT}" "${GOLDEN_STR}"

exit $?
4 changes: 3 additions & 1 deletion example_bazel/docker_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ docker_run () {
"${image}" \
bash -c "tar -C root -xvf /rpms.tar && ${cmd}"
}
OUT="$1"
shift

docker_run "{CMD}" "{TAR}" "{IMAGE}"
docker_run "$@" > $OUT

exit $?
31 changes: 17 additions & 14 deletions example_bazel/testing.bzl
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@

def _docker_run_impl(ctx):
ctx.actions.expand_template(
template = ctx.file._template,
output = ctx.outputs.file,
is_executable = True,
substitutions = {
"{CMD}": ctx.attr.cmd,
"{TAR}": ctx.file.tar.short_path,
"{IMAGE}": ctx.attr.image,
},
out = ctx.actions.declare_file(ctx.label.name)
args = ctx.actions.args()
args.add(out)
args.add(ctx.attr.cmd)
args.add(ctx.file.tar)
args.add(ctx.attr.image)
ctx.actions.run(
outputs = [out],
inputs = [ctx.file.tar],
executable = ctx.file._script,
arguments = [args],
)
return DefaultInfo(files = depset([out]))

docker_run = rule(
attrs = {
Expand All @@ -23,12 +26,11 @@ docker_run = rule(
"cmd": attr.string(
mandatory = True,
),
"_template": attr.label(
"_script": attr.label(
default = "//:docker_run.sh",
allow_single_file = True,
),
},
outputs = {"file": "%{name}.sh"},
implementation = _docker_run_impl,
)

Expand All @@ -37,14 +39,15 @@ def _diff_test_impl(ctx):
template = ctx.file._template,
output = ctx.outputs.file,
substitutions = {
"{CMD}": ctx.file.cmd.short_path,
"{RESULT}": ctx.file.result.short_path,
"{GOLDEN}": ctx.attr.golden,
},
)
return DefaultInfo(runfiles = ctx.runfiles(files = ctx.files.result))

diff_test_expand = rule(
attrs = {
"cmd": attr.label(
"result": attr.label(
mandatory = True,
allow_single_file = True,
),
Expand All @@ -70,7 +73,7 @@ def docker_diff(name,cmd, golden, tar=":rpms", image="", base=""):
)
diff_test_expand(
name = name + "_diff",
cmd = ":%s_run" % name,
result = ":%s_run" % name,
golden = golden,
testonly = True,
)
Expand Down

0 comments on commit 7d7cf4d

Please sign in to comment.