Skip to content

Commit

Permalink
Add package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbeard committed Feb 22, 2024
1 parent 5836c0a commit 8b5b25a
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ jobs:
NFPM_PASSPHRASE: ${{ secrets.GPG_PASSWORD }}
NFPM_TESTPKG_PASSPHRASE: ${{ secrets.GPG_PASSWORD }}

- name: Test packages
run: ./tests/packages/run-tests.sh

docker-hub-doc:
name: Publish DockerHub Readme
runs-on: ubuntu-latest
Expand Down
18 changes: 18 additions & 0 deletions tests/packages/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)

# This file should only be included, not executed.
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
echo "This script should be included, not executed."
exit 1
fi

export PACKAGE_NAME="web-indexer"
export SOURCE_VERSION=$(git describe --tags --always --dirty)
export EXPECTED_VERSION=$(echo "$SOURCE_VERSION" | sed -E 's/([0-9]+\.[0-9]+\.[0-9]+).*/\1/')

export OS="linux"
export FILENAME_BASE="${PACKAGE_NAME}_${SOURCE_VERSION}_${OS}_amd64"
export DIST_DIR="${script_dir}/../../dist"
export TERM=xterm-256color

49 changes: 49 additions & 0 deletions tests/packages/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)
source $script_dir/common.sh

FAILED=0
FAILED_TESTS=""
PASSED_TESTS=""

green=`tput setaf 2`
red=`tput setaf 1`
reset=`tput sgr0`

tests=$(ls $script_dir/test-*.sh)

if [ -n "$1" ]; then
tests=$(ls $script_dir/test-$1*.sh)
fi

if [ -z "$tests" ]; then
echo "No tests found in $script_dir"
exit 1
fi

for test_script in $tests; do
echo "==============================================================================="
echo "▶ Running $(basename $test_script) for $EXPECTED_VERSION"
echo "==============================================================================="
if ! bash $test_script; then
echo "${red}FAILED: ${test_script}${reset}"
FAILED=1
FAILED_TESTS="$FAILED_TESTS $test_script"
continue
fi

echo "${green}PASSED: $(basename $test_script)${reset}"
PASSED_TESTS="$PASSED_TESTS $(basename $test_script)"
done

echo
echo "==============================================================================="
if [ $FAILED -eq 1 ]; then
echo "${red}Failed:$FAILED_TESTS${reset}"
if [ -n "$PASSED_TESTS" ]; then
echo "${green}Passed:$PASSED_TESTS${reset}"
fi
exit 1
else
echo "${green}All tests passed!${reset}"
fi
36 changes: 36 additions & 0 deletions tests/packages/test-apk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)
source $script_dir/common.sh

docker run -v ${DIST_DIR}:/tmp/dist \
--rm alpine /bin/ash -c "
cp /tmp/dist/${FILENAME_BASE}.apk /tmp;
cd /tmp;
apk add --no-cache --allow-untrusted ${FILENAME_BASE}.apk;
# Verify installation
echo '=== Verifying installation ===';
if ! command -v $PACKAGE_NAME &> /dev/null; then
echo '$PACKAGE_NAME could not be installed.' >&2;
exit 1;
fi;
echo 'ok';
# Check the version
echo '=== Checking executed version ===';
INSTALLED_VERSION=\$($PACKAGE_NAME --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+');
if [ \"\$INSTALLED_VERSION\" != \"$EXPECTED_VERSION\" ]; then
echo 'Version mismatch: expected $EXPECTED_VERSION, got '\"\$INSTALLED_VERSION\"'.' >&2;
exit 1;
fi;
echo 'ok';
echo 'All tests passed!';
"

if [ $? -eq 0 ]; then
echo "Package $PACKAGE_NAME tests passed successfully."
else
echo "Package $PACKAGE_NAME tests failed." >&2
exit 1
fi
37 changes: 37 additions & 0 deletions tests/packages/test-arch-zst.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)
source $script_dir/common.sh

docker run -v ${DIST_DIR}:/tmp/dist \
--rm archlinux /bin/bash -c "
cp /tmp/dist/${FILENAME_BASE}.pkg.tar.zst /tmp;
cd /tmp;
pacman -U ${FILENAME_BASE}.pkg.tar.zst --noconfirm;
# Verify installation
echo '=== Verifying installation ===';
if ! command -v $PACKAGE_NAME &> /dev/null; then
echo '$PACKAGE_NAME could not be installed.' >&2;
exit 1;
fi;
echo 'ok';
# Check the version
echo '=== Checking executed version ===';
INSTALLED_VERSION=\$($PACKAGE_NAME --version | grep -oP '\d+\.\d+\.\d+');
if [ \"\$INSTALLED_VERSION\" != \"$EXPECTED_VERSION\" ]; then
echo 'Version mismatch: expected $EXPECTED_VERSION, got '\"\$INSTALLED_VERSION\"'.' >&2;
exit 1;
fi;
echo 'ok';
echo 'All tests passed!';
"

if [ $? -eq 0 ]; then
echo "Package $PACKAGE_NAME tests passed successfully."
else
echo "Package $PACKAGE_NAME tests failed." >&2
exit 1
fi
37 changes: 37 additions & 0 deletions tests/packages/test-deb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)
source $script_dir/common.sh

docker run -v ${DIST_DIR}:/tmp/dist \
--rm debian /bin/bash -c "
cp /tmp/dist/${FILENAME_BASE}.deb /tmp;
cd /tmp;
dpkg -i ${FILENAME_BASE}.deb;
# Verify installation
echo '=== Verifying installation ===';
if ! command -v $PACKAGE_NAME &> /dev/null; then
echo '$PACKAGE_NAME could not be installed.' >&2;
exit 1;
fi;
echo 'ok';
# Check the version
echo '=== Checking executed version ===';
INSTALLED_VERSION=\$($PACKAGE_NAME --version | grep -oP '\d+\.\d+\.\d+');
if [ \"\$INSTALLED_VERSION\" != \"$EXPECTED_VERSION\" ]; then
echo 'Version mismatch: expected $EXPECTED_VERSION, got '\"\$INSTALLED_VERSION\"'.' >&2;
exit 1;
fi;
echo 'ok';
echo 'All tests passed!';
"

if [ $? -eq 0 ]; then
echo "Package $PACKAGE_NAME tests passed successfully."
else
echo "Package $PACKAGE_NAME tests failed." >&2
exit 1
fi
38 changes: 38 additions & 0 deletions tests/packages/test-pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)
source $script_dir/common.sh

docker run -v ${DIST_DIR}:/tmp/dist \
--rm rockylinux:9 /bin/bash -c "
cp /tmp/dist/${FILENAME_BASE}.tar.gz /tmp;
tar -xzf /tmp/${FILENAME_BASE}.tar.gz -C /tmp;
mv /tmp/${PACKAGE_NAME} /usr/local/bin/${PACKAGE_NAME};
chmod +x /usr/local/bin/${PACKAGE_NAME};
# Verify installation
echo '=== Verifying installation ===';
if ! command -v $PACKAGE_NAME &> /dev/null; then
echo '$PACKAGE_NAME could not be installed.' >&2;
exit 1;
fi;
echo 'ok';
# Check the version
echo '=== Checking executed version ===';
INSTALLED_VERSION=\$($PACKAGE_NAME --version | grep -oP '\d+\.\d+\.\d+');
if [ \"\$INSTALLED_VERSION\" != \"$EXPECTED_VERSION\" ]; then
echo 'Version mismatch: expected $EXPECTED_VERSION, got '\"\$INSTALLED_VERSION\"'.' >&2;
exit 1;
fi;
echo 'ok';
echo 'All tests passed!';
"

if [ $? -eq 0 ]; then
echo "Package $PACKAGE_NAME tests passed successfully."
else
echo "Package $PACKAGE_NAME tests failed." >&2
exit 1
fi
38 changes: 38 additions & 0 deletions tests/packages/test-rpm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
script_dir=$(cd $(dirname $0) && pwd)
source $script_dir/common.sh

docker run -v ${DIST_DIR}:/tmp/dist \
--rm rockylinux:9 /bin/bash -c "
cp /tmp/dist/${FILENAME_BASE}.rpm /tmp;
cd /tmp;
rpm -i ${FILENAME_BASE}.rpm;
# Verify installation
echo '=== Verifying installation ===';
if ! command -v $PACKAGE_NAME &> /dev/null; then
echo '$PACKAGE_NAME could not be installed.' >&2;
exit 1;
fi;
echo 'ok';
# Check the version
echo '=== Checking executed version ===';
INSTALLED_VERSION=\$($PACKAGE_NAME --version | grep -oP '\d+\.\d+\.\d+');
if [ \"\$INSTALLED_VERSION\" != \"$EXPECTED_VERSION\" ]; then
echo 'Version mismatch: expected $EXPECTED_VERSION, got '\"\$INSTALLED_VERSION\"'.' >&2;
exit 1;
fi;
echo 'ok';
echo 'All tests passed!';
"

if [ $? -eq 0 ]; then
echo "Package $PACKAGE_NAME tests passed successfully."
else
echo "Package $PACKAGE_NAME tests failed." >&2
exit 1
fi

0 comments on commit 8b5b25a

Please sign in to comment.