Skip to content

Commit

Permalink
test: add shellcheck to bats tests, fix lint
Browse files Browse the repository at this point in the history
A previous test bug would have been caught by linting the bats test
code, so let's do that now.

That error was using an undefined variable in a test. ($last_layer_hash
in atomfs.bats) It had been defined in a different test in the same
file, but was undefined in the test in question. Unfortunately because
of the way bats does bash, shellcheck only identifies that as an INFO
level possible error SC2031.

So this adds a new `lintbats` target that first checks for JUST SC2031,
then checks separately for only error level issues.

This commit also fixes a few error level issues in other files:
- asterisk and whiteout : use glob instead of looping over ls. /shrug
- bom: [ -n "${ZOT_HOST}:${ZOT_PORT}" ] will always be true because of
the colon in there, so this commit eliminates the colon.
- config.bats had a complaint about the indentation of the EOF because
it's in yaml in another here doc? OK then.

Also add the shellcheck package to the build deps.

Signed-off-by: Michael McCracken <[email protected]>
  • Loading branch information
mikemccracken committed Jan 10, 2025
1 parent 2446144 commit 15b4b28
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ PRIVILEGE_LEVEL?=
check: lint test go-test

.PHONY: test
test: stacker download-tools
test: stacker download-tools lintbats
sudo -E PATH="$(PATH)" \
STACKER_BUILD_ALPINE_IMAGE=$(STACKER_BUILD_ALPINE_IMAGE) \
STACKER_BUILD_BUSYBOX_IMAGE=$(STACKER_BUILD_BUSYBOX_IMAGE) \
Expand All @@ -179,6 +179,13 @@ test: stacker download-tools
$(shell [ -z $(PRIVILEGE_LEVEL) ] || echo --privilege-level=$(PRIVILEGE_LEVEL)) \
$(patsubst %,test/%.bats,$(TEST))

.PHONY: lintbats
lintbats:
# check only SC2031 which finds undefined variables in bats tests but is only an INFO
shellcheck -i SC2031 $(patsubst %,test/%.bats,$(TEST))
# check all error level issues
shellcheck -S error $(patsubst %,test/%.bats,$(TEST))

.PHONY: check-cov
check-cov: lint test-cov

Expand Down
1 change: 1 addition & 0 deletions install-build-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ installdeps_ubuntu() {
squashfs-tools
squashfuse
libarchive-tools
shellcheck
)

case "$VERSION_ID" in
Expand Down
2 changes: 1 addition & 1 deletion test/asterisk.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ EOF
[ "$status" -eq 0 ]


for i in $(ls dest/rootfs/bin); do
for i in dest/rootfs/bin/*; do
stat dest/rootfs/mybin/$i
done
}
Expand Down
14 changes: 7 additions & 7 deletions test/bom.bats
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
load helpers

function setup_file() {
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
zot_setup
fi
}

function teardown_file() {
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
zot_teardown
fi
}
Expand Down Expand Up @@ -154,7 +154,7 @@ EOF
[ -f .stacker/artifacts/second/inventory.json ]
# sbom for this image
[ -f .stacker/artifacts/second/second.json ]
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
zot_setup
stacker publish --skip-tls --url docker://${ZOT_HOST}:${ZOT_PORT} --tag latest --substitute CENTOS_OCI=${CENTOS_OCI}
refs=$(regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/first:latest --format "{{json .}}" | jq '.referrer | length')
Expand Down Expand Up @@ -204,7 +204,7 @@ EOF
[ -f .stacker/artifacts/bom-alpine/inventory.json ]
# sbom for this image
[ -f .stacker/artifacts/bom-alpine/bom-alpine.json ]
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
zot_setup
stacker publish --skip-tls --url docker://${ZOT_HOST}:${ZOT_PORT} --tag latest --substitute ALPINE_OCI=${ALPINE_OCI}
refs=$(regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/bom-alpine:latest --format "{{json .}}" | jq '.referrer | length')
Expand Down Expand Up @@ -258,7 +258,7 @@ EOF
[ -f .stacker/artifacts/parent/inventory.json ]
# sbom for this image
[ -f .stacker/artifacts/parent/parent.json ]
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
stacker publish --skip-tls --url docker://${ZOT_HOST}:${ZOT_PORT} --tag latest
refs=$(regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/parent:latest --format "{{json .}}" | jq '.referrer | length')
[ $refs -eq 2 ]
Expand Down Expand Up @@ -296,7 +296,7 @@ EOF
[ -f .stacker/artifacts/child/inventory.json ]
# sbom for this image
[ -f .stacker/artifacts/child/child.json ]
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
stacker publish --skip-tls --url docker://${ZOT_HOST}:${ZOT_PORT} --tag latest
refs=$(regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/child:latest --format "{{json .}}" | jq '.referrer | length')
[ $refs -eq 2 ]
Expand Down Expand Up @@ -450,7 +450,7 @@ EOF
[ -f .stacker/artifacts/second/inventory.json ]
# sbom for this image
[ -f .stacker/artifacts/second/second.json ]
if [ -n "${ZOT_HOST}:${ZOT_PORT}" ]; then
if [ -n "${ZOT_HOST}${ZOT_PORT}" ]; then
zot_setup
stacker publish --skip-tls --url docker://${ZOT_HOST}:${ZOT_PORT} --tag latest --substitute CENTOS_OCI=${CENTOS_OCI}
refs=$(regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/second:latest --format "{{json .}}" | jq '.referrer | length')
Expand Down
1 change: 1 addition & 0 deletions test/config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ EOF
"found rootfs=$rdir" "found oci=$odir" "found stacker=$sdir" \
"found name=my-build" > "$expected"

# shellcheck disable=SC1039
cat > "$stacker_yaml" <<"EOF"
my-build:
build_only: true
Expand Down
2 changes: 1 addition & 1 deletion test/whiteout.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ EOF

stacker build
echo "checking"
for f in $(ls oci/blobs/sha256/); do
for f in oci/blobs/sha256/*; do
file oci/blobs/sha256/$f | grep "gzip" || {
echo "skipping $f"
continue
Expand Down

0 comments on commit 15b4b28

Please sign in to comment.