Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shUnit2 for Unit Tests #751

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ jobs:
name: docker-gen.tar
path: docker-gen.tar

unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Required Dependencies
run: make install-unit-test-deps
- name: Run Unit Tests
run: make unit-test

docker-specs-tests:
needs: companion-build
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ vhost.d/
go/
nginx.tmpl
test/local_test_env.sh
test/tests/docker_api/expected-std-out.txt
test/tests/container_restart/docker_event_out.txt
test/tests/certs_standalone/letsencrypt_user_data
test/integration/docker_api/expected-std-out.txt
test/integration/container_restart/docker_event_out.txt
test/integration/certs_standalone/letsencrypt_user_data
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.DEFAULT_GOAL := help

.PHONY: help
help: ## Show help
# Source: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: install
install: install-unit-test-deps ## Install all dev dependencies

.PHONY: install-unit-test-deps
install-unit-test-deps:
./hack/make-rules/install-unit-test-deps.sh

.PHONY: unit-test
unit-test: ## Run all tests
./hack/make-rules/unit-test.sh

.PHONY: verify
verify: ## Verify that dev dependencies are installed and correctly configured
./hack/make-rules/verify.sh
7 changes: 7 additions & 0 deletions hack/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

# shellcheck disable=SC2034 # Variables sourced in other scripts.

PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)

THIRD_PARTY=$PROJECT_ROOT/third_party
8 changes: 8 additions & 0 deletions hack/make-rules/install-unit-test-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)

# shellcheck disable=SC1090 # Sourced files
source "$PROJECT_ROOT/hack/shunit2.sh"

shunit2::install
28 changes: 28 additions & 0 deletions hack/make-rules/unit-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

# shellcheck disable=SC2034 # Variables sourced in other scripts.

set -e
shopt -s globstar

PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)

UNIT_TEST_DIR=$PROJECT_ROOT/test/unit

source_all_files_recursively_from() {
local directory=$1
for file in "$directory"/**/*.sh; do
# shellcheck disable=SC1090 # Sourced files
source "$file"
done
}

# shellcheck disable=SC1090 # Sourced files
source "$PROJECT_ROOT/hack/shunit2.sh"

suite() {
source_all_files_recursively_from "$UNIT_TEST_DIR"
}

# shellcheck disable=SC1090 # Sourced files
. "$SHUNIT2_BIN"
8 changes: 8 additions & 0 deletions hack/make-rules/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)

# shellcheck disable=SC1090 # Sourced files
source "$PROJECT_ROOT/hack/shunit2.sh"

shunit2::validate
50 changes: 50 additions & 0 deletions hack/shunit2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

# shellcheck disable=SC2034 # Variables sourced in other scripts.

PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)

# shellcheck disable=SC1090 # Sourced files
source "$PROJECT_ROOT/hack/init.sh"

SHUNIT2_VERSION=v2.1.8
SHUNIT2_REPO_URL=https://github.com/kward/shunit2
SHUNIT2_DIR=$THIRD_PARTY/shunit2
SHUNIT2_BIN=$SHUNIT2_DIR/shunit2

shunit2::validate() {
# Validate shUnit2 is installed
if [[ ! -d $SHUNIT2_DIR ]]; then
echo "shUnit2 is not installed."
return 1
fi

# Validate installed version
local expected_release=$SHUNIT2_VERSION
local current_release

current_release=$(cd "$SHUNIT2_DIR" && git describe --tags)
if [[ "$current_release" != "$expected_release" ]]; then
echo "shUnit2 version $expected_release required, the current version" \
"installed is $current_release."
return 1
fi
}

shunit2::install() {
if shunit2::validate; then
echo "shUnit2 ${SHUNIT2_VERSION} already installed."
return 1
else
shunit2::cleanup

git clone --branch "$SHUNIT2_VERSION" "$SHUNIT2_REPO_URL" "$SHUNIT2_DIR"
chmod +x "$SHUNIT2_BIN"
fi
}

shunit2::cleanup() {
if [[ -d "$SHUNIT2_DIR" ]]; then
rm -rf "$SHUNIT2_DIR"
fi
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function cleanup {
trap cleanup EXIT

# Create letsencrypt_user_data with a single domain cert
cat > "${GITHUB_WORKSPACE}/test/tests/certs_standalone/letsencrypt_user_data" <<EOF
cat > "${GITHUB_WORKSPACE}/test/integration/certs_standalone/letsencrypt_user_data" <<EOF
LETSENCRYPT_STANDALONE_CERTS=('single')
LETSENCRYPT_single_HOST=('${domains[0]}')
EOF
Expand All @@ -55,7 +55,7 @@ elif [[ "${DRY_RUN:-}" == 1 ]]; then
fi

run_le_container "${1:?}" "$le_container_name" \
"--volume ${GITHUB_WORKSPACE}/test/tests/certs_standalone/letsencrypt_user_data:/app/letsencrypt_user_data"
"--volume ${GITHUB_WORKSPACE}/test/integration/certs_standalone/letsencrypt_user_data:/app/letsencrypt_user_data"

# Wait for a file at /etc/nginx/conf.d/standalone-cert-${domains[0]}.conf
wait_for_standalone_conf "${domains[0]}" "$le_container_name"
Expand All @@ -78,7 +78,7 @@ docker exec "$le_container_name" bash -c "[[ -f /etc/nginx/conf.d/standalone-cer
&& echo "Standalone configuration for ${domains[0]} wasn't correctly removed."

# Add another (SAN) certificate to letsencrypt_user_data
cat > "${GITHUB_WORKSPACE}/test/tests/certs_standalone/letsencrypt_user_data" <<EOF
cat > "${GITHUB_WORKSPACE}/test/integration/certs_standalone/letsencrypt_user_data" <<EOF
LETSENCRYPT_STANDALONE_CERTS=('single' 'san')
LETSENCRYPT_single_HOST=('${domains[0]}')
LETSENCRYPT_san_HOST=('${domains[1]}' '${domains[2]}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ IFS=',' read -r -a domains <<< "$TEST_DOMAINS"
# Listen for Docker restart events
docker events \
--filter event=restart \
--format 'Container {{.Actor.Attributes.name}} restarted' > "${GITHUB_WORKSPACE}/test/tests/container_restart/docker_event_out.txt" &
--format 'Container {{.Actor.Attributes.name}} restarted' > "${GITHUB_WORKSPACE}/test/integration/container_restart/docker_event_out.txt" &
docker_events_pid=$!

# Cleanup function with EXIT trap
function cleanup {
# Kill the Docker events listener
kill $docker_events_pid && wait $docker_events_pid 2>/dev/null
# Remove temporary files
rm -f "${GITHUB_WORKSPACE}/test/tests/container_restart/docker_event_out.txt"
rm -f "${GITHUB_WORKSPACE}/test/integration/container_restart/docker_event_out.txt"
# Remove any remaining Nginx container(s) silently.
for domain in "${domains[@]}"; do
docker rm --force "$domain" &> /dev/null
Expand All @@ -42,7 +42,7 @@ for domain in "${domains[@]}"; do
# Check if container restarted
timeout="$(date +%s)"
timeout="$((timeout + 60))"
until grep "$domain" "${GITHUB_WORKSPACE}"/test/tests/container_restart/docker_event_out.txt; do
until grep "$domain" "${GITHUB_WORKSPACE}"/test/integration/container_restart/docker_event_out.txt; do
if [[ "$(date +%s)" -gt "$timeout" ]]; then
echo "Container $domain didn't restart in under one minute."
break
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ case $SETUP in
"$1" \
bash -c "$commands" 2>&1

cat > "${GITHUB_WORKSPACE}/test/tests/docker_api/expected-std-out.txt" <<EOF
cat > "${GITHUB_WORKSPACE}/test/integration/docker_api/expected-std-out.txt" <<EOF
Container $nginx_vol received exec_start: sh -c /app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload
$nginx_vol
Container $nginx_env received exec_start: sh -c /app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload
Expand Down Expand Up @@ -220,7 +220,7 @@ EOF
"$1" \
bash -c "$commands" 2>&1

cat > "${GITHUB_WORKSPACE}/test/tests/docker_api/expected-std-out.txt" <<EOF
cat > "${GITHUB_WORKSPACE}/test/integration/docker_api/expected-std-out.txt" <<EOF
Container $docker_gen received signal 1
Container $nginx_vol received signal 1
$docker_gen
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_comment='### This is a test comment'
vhost_path='/etc/nginx/vhost.d'

# Create custom location configuration file to be bind mounted
location_file="${GITHUB_WORKSPACE}/test/tests/location_config/le2.wtf"
location_file="${GITHUB_WORKSPACE}/test/integration/location_config/le2.wtf"
echo "$test_comment" > "$location_file"

# Create le1.wtf configuration file, *.le3.wtf and test.* from inside the nginx container
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ if [[ -z $GITHUB_ACTIONS ]] && [[ -f "$dir/local_test_env.sh" ]]; then
source "$dir/local_test_env.sh"
fi

# shellcheck source=./tests/test-functions.sh
source "$dir/tests/test-functions.sh"
# shellcheck source=./integration/test-functions.sh
source "$dir/integration/test-functions.sh"
## End of additional code

usage() {
Expand Down Expand Up @@ -296,12 +296,12 @@ for conf in "${configs[@]}"; do
for testName in ${globalTests[@]} ${imageTests[@]}; do
[ "${testPaths[$testName]}" ] && continue

if [ -d "$confDir/tests/$testName" ]; then
if [ -d "$confDir/integration/$testName" ]; then
# Test directory found relative to the conf file
testPaths[$testName]="$confDir/tests/$testName"
elif [ -d "$dir/tests/$testName" ]; then
testPaths[$testName]="$confDir/integration/$testName"
elif [ -d "$dir/integration/$testName" ]; then
# Test directory found in the main tests/ directory
testPaths[$testName]="$dir/tests/$testName"
testPaths[$testName]="$dir/integration/$testName"
fi
done
done
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/sh

testEquality()
{
assertEquals 1 1
}

suite_addTest testEquality