Refactor install.sh (#765)

* [wip] get assets based on gh api

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* put install.sh download_asset fn under test

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* put install.sh install_asset fn under test

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* use zip for darwin installs

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* fix install.sh negative test cases

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* allow errors to propagate in install.sh

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* remove exit on error from install.sh tests

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add more docs around install.sh helpers

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add integration tests for install.sh

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add install.sh testing to pipeline

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add install test cache to CI

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* make colors globally available

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* test download against github release

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* always test release-based install against latest release

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* use better install.sh test names

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2022-02-01 16:58:47 -05:00
committed by GitHub
parent ed1cbf50d9
commit f38b0b7256
25 changed files with 1303 additions and 284 deletions
+1
View File
@@ -0,0 +1 @@
**
+1
View File
@@ -0,0 +1 @@
cache/
+40
View File
@@ -0,0 +1,40 @@
. test_harness.sh
# search for an asset in a release checksums file
test_search_for_asset_release() {
fixture=./test-fixtures/syft_0.36.0_checksums.txt
# search_for_asset [checksums-file-path] [name] [os] [arch] [format]
# positive case
actual=$(search_for_asset "${fixture}" "syft" "linux" "amd64" "tar.gz")
assertEquals "syft_0.36.0_linux_amd64.tar.gz" "${actual}" "unable to find release asset"
# negative cases
actual=$(search_for_asset "${fixture}" "syft" "Linux" "amd64" "tar.gz")
assertEquals "" "${actual}" "found a release asset but did not expect to (os)"
actual=$(search_for_asset "${fixture}" "syft" "darwin" "amd64" "rpm")
assertEquals "" "${actual}" "found a release asset but did not expect to (format)"
}
run_test_case test_search_for_asset_release
# search for an asset in a snapshot checksums file
test_search_for_asset_snapshot() {
fixture=./test-fixtures/syft_0.35.1-SNAPSHOT-d461f63_checksums.txt
# search_for_asset [checksums-file-path] [name] [os] [arch] [format]
# positive case
actual=$(search_for_asset "${fixture}" "syft" "linux" "amd64" "rpm")
assertEquals "syft_0.35.1-SNAPSHOT-d461f63_linux_amd64.rpm" "${actual}" "unable to find snapshot asset"
# negative case
actual=$(search_for_asset "${fixture}" "syft" "linux" "amd64" "zip")
assertEquals "" "${actual}" "found a snapshot asset but did not expect to (format)"
}
run_test_case test_search_for_asset_snapshot
+88
View File
@@ -0,0 +1,88 @@
. test_harness.sh
DOWNLOAD_SNAPSHOT_POSITIVE_CASES=0
# helper for asserting test_positive_snapshot_download_asset positive cases
test_positive_snapshot_download_asset() {
os="$1"
arch="$2"
format="$3"
# for troubleshooting
# log_set_priority 10
name=${PROJECT_NAME}
github_download=$(snapshot_download_url)
version=$(snapshot_version)
tmpdir=$(mktemp -d)
actual_filepath=$(download_asset "${github_download}" "${tmpdir}" "${name}" "${os}" "${arch}" "${version}" "${format}" )
assertFileExists "${actual_filepath}" "download_asset os=${os} arch=${arch} format=${format}"
assertFilesEqual \
"$(snapshot_dir)/${name}_${version}_${os}_${arch}.${format}" \
"${actual_filepath}" \
"unable to download os=${os} arch=${arch} format=${format}"
((DOWNLOAD_SNAPSHOT_POSITIVE_CASES++))
rm -rf -- "$tmpdir"
}
test_download_snapshot_asset_exercised_all_assets() {
expected=$(snapshot_assets_count)
assertEquals "${expected}" "${DOWNLOAD_SNAPSHOT_POSITIVE_CASES}" "did not download all possible assets (missing an os/arch/format variant?)"
}
# helper for asserting download_asset negative cases
test_negative_snapshot_download_asset() {
os="$1"
arch="$2"
format="$3"
# for troubleshooting
# log_set_priority 10
name=${PROJECT_NAME}
github_download=$(snapshot_download_url)
version=$(snapshot_version)
tmpdir=$(mktemp -d)
actual_filepath=$(download_asset "${github_download}" "${tmpdir}" "${name}" "${os}" "${arch}" "${version}" "${format}")
assertEquals "" "${actual_filepath}" "unable to download os=${os} arch=${arch} format=${format}"
rm -rf -- "$tmpdir"
}
worker_pid=$(setup_snapshot_server)
trap 'teardown_snapshot_server ${worker_pid}' EXIT
# exercise all possible assets
run_test_case test_positive_snapshot_download_asset "linux" "amd64" "tar.gz"
run_test_case test_positive_snapshot_download_asset "linux" "amd64" "rpm"
run_test_case test_positive_snapshot_download_asset "linux" "amd64" "deb"
run_test_case test_positive_snapshot_download_asset "linux" "arm64" "tar.gz"
run_test_case test_positive_snapshot_download_asset "linux" "arm64" "rpm"
run_test_case test_positive_snapshot_download_asset "linux" "arm64" "deb"
run_test_case test_positive_snapshot_download_asset "darwin" "amd64" "tar.gz"
run_test_case test_positive_snapshot_download_asset "darwin" "amd64" "zip"
run_test_case test_positive_snapshot_download_asset "darwin" "arm64" "tar.gz"
run_test_case test_positive_snapshot_download_asset "darwin" "arm64" "zip"
run_test_case test_positive_snapshot_download_asset "windows" "amd64" "zip"
# note: the mac signing process produces a dmg which is not part of the snapshot process (thus is not exercised here)
# let's make certain we covered all assets that were expected
run_test_case test_download_snapshot_asset_exercised_all_assets
# make certain we handle missing assets alright
run_test_case test_negative_snapshot_download_asset "bogus" "amd64" "zip"
trap - EXIT
teardown_snapshot_server "${worker_pid}"
+43
View File
@@ -0,0 +1,43 @@
. test_harness.sh
test_download_release_asset() {
release="$1"
os="$2"
arch="$3"
format="$4"
expected_mime_type="$5"
# for troubleshooting
# log_set_priority 10
name=${PROJECT_NAME}
version=$(tag_to_version ${release})
github_download="https://github.com/${OWNER}/${REPO}/releases/download/${release}"
tmpdir=$(mktemp -d)
actual_filepath=$(download_asset "${github_download}" "${tmpdir}" "${name}" "${os}" "${arch}" "${version}" "${format}" )
assertFileExists "${actual_filepath}" "download_asset os=${os} arch=${arch} format=${format}"
actual_mime_type=$(file -b --mime-type ${actual_filepath})
assertEquals "${expected_mime_type}" "${actual_mime_type}" "unexpected mimetype for os=${os} arch=${arch} format=${format}"
rm -rf -- "$tmpdir"
}
# always test against the latest release
release=$(get_release_tag "${OWNER}" "${REPO}" "latest" )
# exercise all possible assets against a real github release (based on asset listing from https://github.com/anchore/syft/releases/tag/v0.36.0)
run_test_case test_download_release_asset "${release}" "darwin" "amd64" "tar.gz" "application/gzip"
run_test_case test_download_release_asset "${release}" "darwin" "arm64" "tar.gz" "application/gzip"
run_test_case test_download_release_asset "${release}" "darwin" "arm64" "zip" "application/zip"
run_test_case test_download_release_asset "${release}" "darwin" "amd64" "dmg" "application/zlib"
run_test_case test_download_release_asset "${release}" "linux" "amd64" "tar.gz" "application/gzip"
run_test_case test_download_release_asset "${release}" "linux" "amd64" "rpm" "application/x-rpm"
run_test_case test_download_release_asset "${release}" "linux" "amd64" "deb" "application/vnd.debian.binary-package"
run_test_case test_download_release_asset "${release}" "linux" "arm64" "tar.gz" "application/gzip"
run_test_case test_download_release_asset "${release}" "linux" "arm64" "rpm" "application/x-rpm"
run_test_case test_download_release_asset "${release}" "linux" "arm64" "deb" "application/vnd.debian.binary-package"
+98
View File
@@ -0,0 +1,98 @@
. test_harness.sh
INSTALL_ARCHIVE_POSITIVE_CASES=0
# helper for asserting install_asset positive cases
test_positive_snapshot_install_asset() {
os="$1"
arch="$2"
format="$3"
# for troubleshooting
# log_set_priority 10
name=${PROJECT_NAME}
binary=$(get_binary_name "${os}" "${arch}" "${PROJECT_NAME}")
github_download=$(snapshot_download_url)
version=$(snapshot_version)
download_dir=$(mktemp -d)
install_dir=$(mktemp -d)
download_and_install_asset "${github_download}" "${download_dir}" "${install_dir}" "${name}" "${os}" "${arch}" "${version}" "${format}" "${binary}"
assertEquals "0" "$?" "download/install did not succeed"
expected_path="${install_dir}/${binary}"
assertFileExists "${expected_path}" "install_asset os=${os} arch=${arch} format=${format}"
build_dir_name="${name}"
if [ "$os" == "darwin" ]; then
# TODO: when we simplify the goreleaser build steps, this exception would not longer be expected
build_dir_name="${name}-macos"
fi
assertFilesEqual \
"$(snapshot_dir)/${build_dir_name}_${os}_${arch}/${binary}" \
"${expected_path}" \
"unable to verify installation of os=${os} arch=${arch} format=${format}"
((INSTALL_ARCHIVE_POSITIVE_CASES++))
rm -rf -- "$download_dir"
rm -rf -- "$install_dir"
}
# helper for asserting install_asset negative cases
test_negative_snapshot_install_asset() {
os="$1"
arch="$2"
format="$3"
# for troubleshooting
# log_set_priority 10
name=${PROJECT_NAME}
binary=$(get_binary_name "${os}" "${arch}" "${PROJECT_NAME}")
github_download=$(snapshot_download_url)
version=$(snapshot_version)
download_dir=$(mktemp -d)
install_dir=$(mktemp -d)
download_and_install_asset "${github_download}" "${download_dir}" "${install_dir}" "${name}" "${os}" "${arch}" "${version}" "${format}" "${binary}"
assertNotEquals "0" "$?" "download/install should have failed but did not"
rm -rf -- "$download_dir"
rm -rf -- "$install_dir"
}
test_install_asset_exercised_all_archive_assets() {
expected=$(snapshot_assets_archive_count)
assertEquals "${expected}" "${INSTALL_ARCHIVE_POSITIVE_CASES}" "did not download all possible archive assets (missing an os/arch/format variant?)"
}
worker_pid=$(setup_snapshot_server)
trap 'teardown_snapshot_server ${worker_pid}' EXIT
# exercise all possible archive assets (not rpm/deb/dmg) against a snapshot build
run_test_case test_positive_snapshot_install_asset "linux" "amd64" "tar.gz"
run_test_case test_positive_snapshot_install_asset "linux" "arm64" "tar.gz"
run_test_case test_positive_snapshot_install_asset "darwin" "amd64" "tar.gz"
run_test_case test_positive_snapshot_install_asset "darwin" "amd64" "zip"
run_test_case test_positive_snapshot_install_asset "darwin" "arm64" "tar.gz"
run_test_case test_positive_snapshot_install_asset "darwin" "arm64" "zip"
run_test_case test_positive_snapshot_install_asset "windows" "amd64" "zip"
# let's make certain we covered all assets that were expected
run_test_case test_install_asset_exercised_all_archive_assets
# make certain we handle missing assets alright
run_test_case test_negative_snapshot_install_asset "bogus" "amd64" "zip"
trap - EXIT
teardown_snapshot_server "${worker_pid}"
+103
View File
@@ -0,0 +1,103 @@
NAME=syft
IMAGE_NAME=$(NAME)-install.sh-env
UBUNTU_IMAGE=$(IMAGE_NAME):ubuntu-20.04
ALPINE_IMAGE=$(IMAGE_NAME):alpine-3.6
BUSYBOX_IMAGE=busybox:1.35
ENVS=./environments
DOCKER_RUN=docker run --rm -t -w /project/test/install -v $(shell pwd)/../../:/project
UNIT=make unit-local
# acceptance testing is running the current install.sh against the latest release. Note: this could be a problem down
# the line if there are breaking changes made that don't align with the latest release (but will be OK with the next
# release)
ACCEPTANCE_CMD=sh -c '../../install.sh -b /usr/local/bin && syft version'
# CI cache busting values; change these if you want CI to not use previous stored cache
INSTALL_TEST_CACHE_BUSTER=894d8ca
define title
@printf '\n≡≡≡[ $(1) ]≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\n'
endef
.PHONY: test
test: unit acceptance
.PHONY: ci-test-mac
ci-test-mac: unit-local acceptance-local
# note: do not add acceptance-local to this list
acceptance: acceptance-ubuntu-20.04 acceptance-alpine-3.6 acceptance-busybox-1.35
unit: unit-ubuntu-20.04
unit-local:
$(call title,unit tests)
@for f in $(shell ls *_test.sh); do echo "Running unit test suite '$${f}'"; bash $${f} || exit 1; done
acceptance-local:
$(acceptance)
save: ubuntu-20.04 alpine-3.6 busybox-1.35
@mkdir cache || true
docker image save -o cache/ubuntu-env.tar $(UBUNTU_IMAGE)
docker image save -o cache/alpine-env.tar $(ALPINE_IMAGE)
docker image save -o cache/busybox-env.tar $(BUSYBOX_IMAGE)
load:
docker image load -i cache/ubuntu-env.tar
docker image load -i cache/alpine-env.tar
docker image load -i cache/busybox-env.tar
## UBUNTU #######################################################
acceptance-ubuntu-20.04: ubuntu-20.04
$(call title,ubuntu:20.04 - acceptance)
$(DOCKER_RUN) $(UBUNTU_IMAGE) \
$(ACCEPTANCE_CMD)
unit-ubuntu-20.04: ubuntu-20.04
$(call title,ubuntu:20.04 - unit)
$(DOCKER_RUN) $(UBUNTU_IMAGE) \
$(UNIT)
ubuntu-20.04:
$(call title,ubuntu:20.04 - build environment)
docker build -t $(UBUNTU_IMAGE) -f $(ENVS)/Dockerfile-ubuntu-20.04 .
## ALPINE #######################################################
# note: unit tests cannot be run with sh (alpine dosn't have bash by default)
acceptance-alpine-3.6: alpine-3.6
$(call title,alpine:3.6 - acceptance)
$(DOCKER_RUN) $(ALPINE_IMAGE) \
$(ACCEPTANCE_CMD)
alpine-3.6:
$(call title,alpine:3.6 - build environment)
docker build -t $(ALPINE_IMAGE) -f $(ENVS)/Dockerfile-alpine-3.6 .
## BUSYBOX #######################################################
# note: unit tests cannot be run with sh (busybox dosn't have bash by default)
# note: busybox by default will not have cacerts, so you will get TLS warnings (we want to test under these conditions)
acceptance-busybox-1.35: busybox-1.35
$(call title,busybox-1.35 - acceptance)
$(DOCKER_RUN) $(BUSYBOX_IMAGE) \
$(ACCEPTANCE_CMD)
@echo "\n*** test note: you should see syft spit out a 'x509: certificate signed by unknown authority' error --this is expected ***"
busybox-1.35:
$(call title,busybox-1.35 - build environment)
docker pull $(BUSYBOX_IMAGE)
## For CI ########################################################
.PHONY: cache.fingerprint
cache.fingerprint:
$(call title,Install test fixture fingerprint)
@find ./environments/* -type f -exec md5sum {} + | awk '{print $1}' | sort | tee /dev/stderr | md5sum | tee cache.fingerprint && echo "$(INSTALL_TEST_CACHE_BUSTER)" >> cache.fingerprint
@@ -0,0 +1,2 @@
FROM alpine:3.6
RUN apk update && apk add python3 wget unzip make ca-certificates
@@ -0,0 +1,2 @@
FROM ubuntu:20.04
RUN apt update -y && apt install make python3 curl unzip -y
+68
View File
@@ -0,0 +1,68 @@
. test_harness.sh
# check that we can extract single json values
test_extract_json_value() {
fixture=./test-fixtures/github-api-syft-v0.36.0-release.json
content=$(cat ${fixture})
actual=$(extract_json_value "${content}" "tag_name")
assertEquals "v0.36.0" "${actual}" "unable to find tag_name"
actual=$(extract_json_value "${content}" "id")
assertEquals "57501596" "${actual}" "unable to find tag_name"
}
run_test_case test_extract_json_value
# check that we can extract github release tag from github api json
test_github_release_tag() {
fixture=./test-fixtures/github-api-syft-v0.36.0-release.json
content=$(cat ${fixture})
actual=$(github_release_tag "${content}")
assertEquals "v0.36.0" "${actual}" "unable to find release tag"
}
run_test_case test_github_release_tag
# download a known good github release checksums and compare against a test-fixture
test_download_github_release_checksums() {
tmpdir=$(mktemp -d)
tag=v0.36.0
github_download="https://github.com/anchore/syft/releases/download/${tag}"
name=${PROJECT_NAME}
version=$(tag_to_version "${tag}")
actual_filepath=$(download_github_release_checksums "${github_download}" "${name}" "${version}" "${tmpdir}")
assertFilesEqual \
"./test-fixtures/syft_0.36.0_checksums.txt" \
"${actual_filepath}" \
"unable to find release tag"
rm -rf -- "$tmpdir"
}
run_test_case test_download_github_release_checksums
# download a checksums file from a locally served-up snapshot directory and compare against the file in the snapshot dir
test_download_github_release_checksums_snapshot() {
tmpdir=$(mktemp -d)
github_download=$(snapshot_download_url)
name=${PROJECT_NAME}
version=$(snapshot_version)
actual_filepath=$(download_github_release_checksums "${github_download}" "${name}" "${version}" "${tmpdir}")
assertFilesEqual \
"$(snapshot_checksums_path)" \
"${actual_filepath}" \
"unable to find release tag"
rm -rf -- "$tmpdir"
}
run_test_case_with_snapshot_release test_download_github_release_checksums_snapshot
@@ -0,0 +1 @@
{"id":57501596,"tag_name":"v0.36.0","update_url":"/anchore/syft/releases/tag/v0.36.0","update_authenticity_token":"7XbNZgRHpbHegdv-xRlbe84Y983YgyXa3YKWwv_e0ocqTHagsHq5dxCTQUQnuX3vbsgdWQU3A3__hkVNhKGHSg","delete_url":"/anchore/syft/releases/tag/v0.36.0","delete_authenticity_token":"6tLaRtXKUc-zz4tHIwCbbD7CksxIHK5imZE1gnA39oVCe6fYux5a8cPD9J52kGUzM1Hs9JPBjceG7yyszBk_2A","edit_url":"/anchore/syft/releases/edit/v0.36.0"}
@@ -0,0 +1,11 @@
123745ee29779018ab386223a900f8cc704aa577f57ca43c157147c53c998a77 syft_0.35.1-SNAPSHOT-d461f63_linux_arm64.deb
2083c5ad471028212e5ca72fdd3d60204052dbf3a9148c9579deac6af7865a3a syft_0.35.1-SNAPSHOT-d461f63_linux_arm64.tar.gz
6b95e8b17fdbb5da094c2251c8ee5a8e97e6059b6556308f1ff2b657a6a080bc syft_0.35.1-SNAPSHOT-d461f63_darwin_x86_64.tar.gz
940ea13dceedfcf3cf0ee2be24447123bf7efdb034c9f923e35549537b094aaa syft_0.35.1-SNAPSHOT-d461f63_Windows_x86_64.zip
a96efc4139c79e0ecb526c7ab7c90fc94ee89c871c006c1089eb7c40c345ea65 syft_0.35.1-SNAPSHOT-d461f63_linux_x86_64.tar.gz
b120a661ae5e24edc4b2c7932d5b4c9a54d6a90ceced6ba0acb9984ac45c0a4e syft_0.35.1-SNAPSHOT-d461f63_linux_arm64.rpm
d1bc4ac460d5bd5bc173425e32b974a0a0d06f892bef4ab5b431394063b2963a syft_0.35.1-SNAPSHOT-d461f63_linux_amd64.rpm
d65f963160acdc47a0f037bb42993866696181350e9901b8ad3d79f2dea35939 syft_0.35.1-SNAPSHOT-d461f63_darwin_arm64.zip
d978c2bffaad36ea833203377f808725a4d3f8fd486f15552759f63909b210ba syft_0.35.1-SNAPSHOT-d461f63_darwin_arm64.tar.gz
da76cc564d8e597f9c5b33423d2280eb6bf65ba2f2092d7851cdd67718e0cbe1 syft_0.35.1-SNAPSHOT-d461f63_darwin_amd64.zip
e75f4cbf5c2b05663f49f683f99ed01d8e6ebe0e082631461dceae6641c3103f syft_0.35.1-SNAPSHOT-d461f63_linux_amd64.deb
@@ -0,0 +1,8 @@
16541ac64378d4216ac9fe2065d20e188a89e8c9cce4e637940a559daddb213b syft_0.36.0_linux_amd64.rpm
4596fdbb491251f6d8e3be38b64d361a9a35cf6a88da6bdad62c019511ffb1a5 syft_0.36.0_linux_amd64.deb
5a69df410597d8649071b3419c17829f60d9f6f00edc8856b681842c2151f83c syft_0.36.0_linux_amd64.tar.gz
5c06f09d370740fb017c6a51657911a87860450d929fa28a9eff1cf00faac303 syft_0.36.0_darwin_arm64.tar.gz
71403ffa346612f9203ce7657d2d191794a0e4367da355605377b8b5d457b7ef syft_0.36.0_linux_arm64.tar.gz
75989a83f0d361969b5f88f3ee3807ed9ddbdf9fae13b6edce1839cf5952a36c syft_0.36.0_linux_arm64.deb
860b42c5d7d03e484c87e18031e6a473f48d3b088a6268fcf8f9567d5b5c90d3 syft_0.36.0_linux_arm64.rpm
fba022c6fac6f2d2f648295af78f86e873488565e41a252a97efafe75622ccf6 syft_0.36.0_darwin_amd64.tar.gz
+163
View File
@@ -0,0 +1,163 @@
# disable using the install.sh entrypoint such that we can unit test
# script functions without invoking main()
TEST_INSTALL_SH=true
. ../../install.sh
set -u
assertTrue() {
if eval "$1"; then
echo "assertTrue failed: $2"
exit 2
fi
}
assertFalse() {
if eval "$1"; then
echo "assertFalse failed: $2"
exit 2
fi
}
assertEquals() {
want=$1
got=$2
msg=$3
if [ "$want" != "$got" ]; then
echo "assertEquals failed: want='$want' got='$got' $msg"
exit 2
fi
}
assertFilesDoesNotExist() {
path="$1"
msg=$2
if [ -f "${path}" ]; then
echo "assertFilesDoesNotExist failed: path exists '$path': $msg"
exit 2
fi
}
assertFileExists() {
path="$1"
msg=$2
if [ ! -f "${path}" ]; then
echo "assertFileExists failed: path does not exist '$path': $msg"
exit 2
fi
}
assertFilesEqual() {
want=$1
got=$2
msg=$3
diff "$1" "$2"
if [ $? -ne 0 ]; then
echo "assertFilesEqual failed: $msg"
exit 2
fi
}
assertNotEquals() {
want=$1
got=$2
msg=$3
if [ "$want" = "$got" ]; then
echo "assertNotEquals failed: want='$want' got='$got' $msg"
exit 2
fi
}
log_test_case() {
echo " running $@"
}
run_test_case_with_snapshot_release() {
log_test_case ${@:1}
worker_pid=$(setup_snapshot_server)
trap "teardown_snapshot_server $worker_pid" EXIT
# run test function with all arguments
${@:1}
trap - EXIT
teardown_snapshot_server "${worker_pid}"
}
serve_port=8000
setup_snapshot_server() {
# if you want to see proof in the logs, feel free to adjust the redirection
python3 -m http.server --directory "$(snapshot_dir)" $serve_port &> /dev/null &
worker_pid=$!
# it takes some time for the server to be ready...
sleep 0.5
echo "$worker_pid"
}
teardown_snapshot_server() {
worker_pid="$1"
kill $worker_pid
}
snapshot_version() {
partial=$(ls ../../snapshot/*_checksums.txt | grep -o "_.*_checksums.txt")
partial="${partial%_checksums.txt}"
echo "${partial#_}"
}
snapshot_download_url() {
echo "localhost:${serve_port}"
}
snapshot_dir() {
echo "../../snapshot"
}
snapshot_checksums_path() {
echo "$(ls $(snapshot_dir)/*_checksums.txt)"
}
snapshot_assets_count() {
# example output before wc -l:
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.deb
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.tar.gz
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.rpm
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.tar.gz
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.deb
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.rpm
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.zip
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_windows_amd64.zip
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.zip
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.tar.gz
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.tar.gz
echo "$(find ../../snapshot -maxdepth 1 -type f | grep 'syft_' | grep -v checksums | wc -l | tr -d '[:space:]')"
}
snapshot_assets_archive_count() {
# example output before wc -l:
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.tar.gz
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.tar.gz
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.zip
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_windows_amd64.zip
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.zip
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.tar.gz
# ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.tar.gz
echo "$(find ../../snapshot -maxdepth 1 -type f | grep 'syft_' | grep 'tar\|zip' | wc -l | tr -d '[:space:]')"
}
run_test_case() {
log_test_case ${@:1}
${@:1}
}