Skip to content

Commit

Permalink
bats tests - parallelize
Browse files Browse the repository at this point in the history
All bats tests run with custom root/runroot, so it should be
possible to parallelize them.

Signed-off-by: Ed Santiago <[email protected]>
  • Loading branch information
edsantiago committed May 30, 2024
1 parent 0d7eb12 commit d118eb9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
44 changes: 27 additions & 17 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ function normalize_image_name() {

function _prefetch() {
if [ -z "${_BUILDAH_IMAGE_CACHEDIR}" ]; then
_pgid=$(sed -ne 's/^NSpgid:\s*//p' /proc/$$/status)
export _BUILDAH_IMAGE_CACHEDIR=${BATS_TMPDIR}/buildah-image-cache.$_pgid
export _BUILDAH_IMAGE_CACHEDIR=${BATS_SUITE_TMPDIR}/buildah-image-cache
mkdir -p ${_BUILDAH_IMAGE_CACHEDIR}

# It's 700 by default; this prevents 'unshare' from reading cached images
chmod 755 ${BATS_SUITE_TMPDIR}
fi

local storage=
Expand All @@ -156,24 +158,32 @@ function _prefetch() {
img=$(normalize_image_name "$img")
echo "# [checking for: $img]" >&2
fname=$(tr -c a-zA-Z0-9.- - <<< "$img")
if [ -d $_BUILDAH_IMAGE_CACHEDIR/$fname ]; then
echo "# [restoring from cache: $_BUILDAH_IMAGE_CACHEDIR / $img]" >&2
copy dir:$_BUILDAH_IMAGE_CACHEDIR/$fname containers-storage:"$storage""$img"
else
rm -fr $_BUILDAH_IMAGE_CACHEDIR/$fname
echo "# [copy docker://$img dir:$_BUILDAH_IMAGE_CACHEDIR/$fname]" >&2
for attempt in $(seq 3) ; do
if copy $COPY_REGISTRY_OPTS docker://"$img" dir:$_BUILDAH_IMAGE_CACHEDIR/$fname ; then
break
fi
sleep 5
done
echo "# [copy dir:$_BUILDAH_IMAGE_CACHEDIR/$fname containers-storage:$storage$img]" >&2
copy dir:$_BUILDAH_IMAGE_CACHEDIR/$fname containers-storage:"$storage""$img"
fi
( flock --timeout 300 9 || die "Could not flock"; _prefetch_locksafe $img $fname ) 9> $_BUILDAH_IMAGE_CACHEDIR/$fname.lock
done
}

# DO NOT CALL THIS. EVER. This must only be called from _prefetch().
function _prefetch_locksafe() {
local img="$1"
local fname="$2"

if [ -d $_BUILDAH_IMAGE_CACHEDIR/$fname ]; then
echo "# [restoring from cache: $_BUILDAH_IMAGE_CACHEDIR / $img]" >&2
copy dir:$_BUILDAH_IMAGE_CACHEDIR/$fname containers-storage:"$storage""$img"
else
rm -fr $_BUILDAH_IMAGE_CACHEDIR/$fname
echo "# [copy docker://$img dir:$_BUILDAH_IMAGE_CACHEDIR/$fname]" >&2
for attempt in $(seq 3) ; do
if copy $COPY_REGISTRY_OPTS docker://"$img" dir:$_BUILDAH_IMAGE_CACHEDIR/$fname ; then
break
fi
sleep 5
done
echo "# [copy dir:$_BUILDAH_IMAGE_CACHEDIR/$fname containers-storage:$storage$img]" >&2
copy dir:$_BUILDAH_IMAGE_CACHEDIR/$fname containers-storage:"$storage""$img"
fi
}

function createrandom() {
dd if=/dev/urandom bs=1 count=${2:-256} of=${1:-${BATS_TMPDIR}/randomfile} status=none
}
Expand Down
73 changes: 51 additions & 22 deletions tests/sbom.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ load helpers
# clear out one file that we might need to overwrite, but leave the other to
# ensure that we don't accidentally append content to files that are already
# present
rm -f localpurl.json
rm -f ${TEST_SCRATCH_DIR}/localpurl.json
# write to both the image and the local filesystem
run_buildah commit $WITH_POLICY_JSON --sbom ${sbomtype} --sbom-output=localsbom.json --sbom-purl-output=localpurl.json --sbom-image-output=/root/sbom.json --sbom-image-purl-output=/root/purl.json $squash $cid alpine-derived-image
run_buildah commit $WITH_POLICY_JSON --sbom ${sbomtype} \
--sbom-output=${TEST_SCRATCH_DIR}/localsbom.json \
--sbom-purl-output=${TEST_SCRATCH_DIR}/localpurl.json \
--sbom-image-output=/root/sbom.json \
--sbom-image-purl-output=/root/purl.json \
$squash $cid alpine-derived-image
# both files should exist now, and neither should be empty
test -s localsbom.json
test -s localpurl.json
test -s ${TEST_SCRATCH_DIR}/localsbom.json
test -s ${TEST_SCRATCH_DIR}/localpurl.json
# compare them to their equivalents in the image
run_buildah from --quiet --pull=false $WITH_POLICY_JSON alpine-derived-image
dcid=$output
run_buildah mount $dcid
mountpoint=$output
cmp $mountpoint/root/purl.json localpurl.json
cmp $mountpoint/root/sbom.json localsbom.json
cmp $mountpoint/root/purl.json ${TEST_SCRATCH_DIR}/localpurl.json
cmp $mountpoint/root/sbom.json ${TEST_SCRATCH_DIR}/localsbom.json
done
done
}
Expand All @@ -37,19 +42,24 @@ load helpers
# clear out one file that we might need to overwrite, but leave the other to
# ensure that we don't accidentally append content to files that are already
# present
rm -f localpurl.json
rm -f ${TEST_SCRATCH_DIR}/localpurl.json
# write to both the image and the local filesystem
run_buildah build $WITH_POLICY_JSON --sbom ${sbomtype} --sbom-output=localsbom.json --sbom-purl-output=localpurl.json --sbom-image-output=/root/sbom.json --sbom-image-purl-output=/root/purl.json $layers -t alpine-derived-image $BUDFILES/simple-multi-step
run_buildah build $WITH_POLICY_JSON --sbom ${sbomtype} \
--sbom-output=${TEST_SCRATCH_DIR}/localsbom.json \
--sbom-purl-output=${TEST_SCRATCH_DIR}/localpurl.json \
--sbom-image-output=/root/sbom.json \
--sbom-image-purl-output=/root/purl.json \
$layers -t alpine-derived-image $BUDFILES/simple-multi-step
# both files should exist now, and neither should be empty
test -s localsbom.json
test -s localpurl.json
test -s ${TEST_SCRATCH_DIR}/localsbom.json
test -s ${TEST_SCRATCH_DIR}/localpurl.json
# compare them to their equivalents in the image
run_buildah from --quiet --pull=false $WITH_POLICY_JSON alpine-derived-image
dcid=$output
run_buildah mount $dcid
mountpoint=$output
cmp $mountpoint/root/purl.json localpurl.json
cmp $mountpoint/root/sbom.json localsbom.json
cmp $mountpoint/root/purl.json ${TEST_SCRATCH_DIR}/localpurl.json
cmp $mountpoint/root/sbom.json ${TEST_SCRATCH_DIR}/localsbom.json
done
done
}
Expand All @@ -58,10 +68,15 @@ load helpers
_prefetch alpine ghcr.io/anchore/syft ghcr.io/aquasecurity/trivy
for sbomtype in syft syft-cyclonedx syft-spdx trivy trivy-cyclonedx trivy-spdx; do
echo "[sbom type $sbomtype with $layers]"
run_buildah build $WITH_POLICY_JSON --sbom ${sbomtype} --sbom-output=localsbom.json --sbom-purl-output=localpurl.json --sbom-image-output=/root/sbom.json --sbom-image-purl-output=/root/purl.json -t busybox-derived-image $BUDFILES/pull
run_buildah build $WITH_POLICY_JSON --sbom ${sbomtype} \
--sbom-output=${TEST_SCRATCH_DIR}/localsbom.json \
--sbom-purl-output=${TEST_SCRATCH_DIR}/localpurl.json \
--sbom-image-output=/root/sbom.json \
--sbom-image-purl-output=/root/purl.json \
-t busybox-derived-image $BUDFILES/pull
# both files should exist now, and neither should be empty
test -s localsbom.json
test -s localpurl.json
test -s ${TEST_SCRATCH_DIR}/localsbom.json
test -s ${TEST_SCRATCH_DIR}/localpurl.json
done
}

Expand All @@ -73,19 +88,33 @@ load helpers
# clear out one file that we might need to overwrite, but leave the other to
# ensure that we don't accidentally append content to files that are already
# present
rm -f localpurl.json
run_buildah build $WITH_POLICY_JSON --sbom ${sbomtype} --sbom-output=localsbom.json --sbom-purl-output=localpurl.json --sbom-image-output=/root/sbom.json --sbom-image-purl-output=/root/purl.json $layers -t alpine-derived-image -f $BUDFILES/env/Dockerfile.check-env $BUDFILES/env
rm -f ${TEST_SCRATCH_DIR}/localpurl.json
run_buildah build $WITH_POLICY_JSON --sbom ${sbomtype} \
--sbom-output=${TEST_SCRATCH_DIR}/localsbom.json \
--sbom-purl-output=${TEST_SCRATCH_DIR}/localpurl.json \
--sbom-image-output=/root/sbom.json \
--sbom-image-purl-output=/root/purl.json \
$layers -t alpine-derived-image -f $BUDFILES/env/Dockerfile.check-env $BUDFILES/env
# both files should exist now, and neither should be empty
test -s localsbom.json
test -s localpurl.json
test -s ${TEST_SCRATCH_DIR}/localsbom.json
test -s ${TEST_SCRATCH_DIR}/localpurl.json
done
done
}

@test "bud-sbom-with-non-presets" {
_prefetch alpine busybox
run_buildah build --debug $WITH_POLICY_JSON --sbom-output=localsbom.txt --sbom-purl-output=localpurl.txt --sbom-image-output=/root/sbom.txt --sbom-image-purl-output=/root/purl.txt --sbom-scanner-image=alpine --sbom-scanner-command='echo SCANNED ROOT {ROOTFS} > {OUTPUT}' --sbom-scanner-command='echo SCANNED BUILD CONTEXT {CONTEXT} > {OUTPUT}' --sbom-merge-strategy=cat -t busybox-derived-image $BUDFILES/pull
run_buildah build --debug $WITH_POLICY_JSON \
--sbom-output=${TEST_SCRATCH_DIR}/localsbom.txt \
--sbom-purl-output=${TEST_SCRATCH_DIR}/localpurl.txt \
--sbom-image-output=/root/sbom.txt \
--sbom-image-purl-output=/root/purl.txt \
--sbom-scanner-image=alpine \
--sbom-scanner-command='echo SCANNED ROOT {ROOTFS} > {OUTPUT}' \
--sbom-scanner-command='echo SCANNED BUILD CONTEXT {CONTEXT} > {OUTPUT}' \
--sbom-merge-strategy=cat \
-t busybox-derived-image $BUDFILES/pull
# both files should exist now, and neither should be empty
test -s localsbom.json
test -s localpurl.json
test -s ${TEST_SCRATCH_DIR}/localsbom.txt
test -s ${TEST_SCRATCH_DIR}/localpurl.txt
}
2 changes: 1 addition & 1 deletion tests/test_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ function execute() {
TESTS=${@:-.}

# Run the tests.
execute time bats --tap $TESTS
execute time bats -j 4 --tap $TESTS

0 comments on commit d118eb9

Please sign in to comment.