Skip to content

Commit

Permalink
Merge pull request #31 from startersclan/enhancement/tests-use-.-test…
Browse files Browse the repository at this point in the history
…-test.sh-for-tests-instead-of-testdocker-compose.test.yml

Enhancement (tests): Use ./test/test.sh for tests instead of `docker-compose.test.yml`
  • Loading branch information
leojonathanoh committed Nov 11, 2023
2 parents 3c65f50 + 6a54c0e commit 56c2267
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 144 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci-master-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ jobs:
if: matrix.testenv == 'dev'
run: |
set -eux
docker compose -f docker-compose.yml -f docker-compose.build.yml up --build -d
docker compose -f docker-compose.test.yml --profile dev up
./test/test.sh dev 1 1
- name: Integration test (prod)
if: matrix.testenv == 'prod'
run: |
set -eux
docker compose -f docker-compose.example.yml -f docker-compose.example.build.yml up --build -d
docker compose -f docker-compose.test.yml --profile prod up
./test/test.sh prod 1 1
build:
strategy:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ docker exec -it $( docker compose ps -q heatmaps) php /heatmaps/generate.php #--
docker exec -it $( docker compose ps -q db ) sh

# Test
docker compose -f docker-compose.test.yml --profile dev up
./test/test.sh dev 1

# Test production builds locally
docker compose -f docker-compose.example.yml -f docker-compose.example.build.yml up --build
docker compose -f docker-compose.test.yml --profile prod up
./test/test.sh prod 1

# Dump the DB
docker exec $( docker compose ps -q db ) mysqldump -uroot -proot hlstatsxce | gzip > hlstatsxce.sql.gz
Expand Down
137 changes: 0 additions & 137 deletions docker-compose.test.yml

This file was deleted.

1 change: 1 addition & 0 deletions test/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_PROJECT_NAME=hlstatsx-community-edition
33 changes: 33 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '2.2'
services:
test-container-networking:
image: alpine:latest
volumes:
- ./:/test:ro
networks:
- default
stop_signal: SIGKILL
working_dir: /test
entrypoint:
- /bin/sh
command:
- -c
- |
sleep infinity
test-host-networking:
image: alpine:latest
volumes:
- ./:/test:ro
network_mode: host
stop_signal: SIGKILL
working_dir: /test
entrypoint:
- /bin/sh
command:
- -c
- |
sleep infinity
networks:
default:
4 changes: 4 additions & 0 deletions test/test-awards.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu

docker exec -i $( docker compose ps -q awards) sh -c /awards.sh
17 changes: 17 additions & 0 deletions test/test-endpoints.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu

echo "[test-endpoints]"
ENDPOINTS="
web.example.com 200
phpmyadmin.example.com 200
"
command -v curl || apk add --no-cache curl
echo "$ENDPOINTS" | awk NF | while read -r i j; do
if curl --head -skL http://$i --resolve $i:80:127.0.0.1 --resolve $i:443:127.0.0.1 2>&1 | grep "^HTTP/2 $j " > /dev/null; then
echo "PASS: $i"
else
echo "FAIL: $i"
exit 1
fi
done
4 changes: 4 additions & 0 deletions test/test-heatmaps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu

docker exec -i $( docker compose ps -q heatmaps) php /heatmaps/generate.php
18 changes: 18 additions & 0 deletions test/test-ready.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
set -eu

echo
echo "[test-ready] Waiting for stack to be ready"
s=0
while true; do
nc -vz -w 1 web 80 \
&& nc -vz -w 1 web 9000 \
&& nc -vz -w 1 db 3306 \
&& break || true
s=$(( $s + 1 ))
if [ "$s" -eq 600 ]; then
exit 1
fi
echo "Retrying in 3 seconds"
sleep 3
done
31 changes: 31 additions & 0 deletions test/test-routes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
set -eu

echo "[test-routes]"
URLS="
http://web/ 302
http://web/css/spinner.gif 200
http://web/hlstatsimg/ajax.gif 200
http://web/includes/ 401
http://web/pages/ 401
http://web/pages/.htaccess 401
http://web/styles/classic.css 200
http://web/updater/ 401
http://web/autocomplete.php 200
http://web/config.php 401
http://web/hlstats.php 200
http://web/index.php 302
http://web/ingame.php 200
http://web/show_graph.php 200
http://web/sig.php 200
http://web/status.php 200
http://web/trend_graph.php 200
"
echo "$URLS" | awk NF | while read -r i j; do
if wget -q -SO- "$i" 2>&1 | grep "HTTP/1.1 $j " > /dev/null; then
echo "PASS: $i"
else
echo "FAIL: $i"
exit 1
fi
done
79 changes: 79 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/sh
set -eu

TEST=${1:-} # Test environment
UP=${2:-} # Whether to docker compose up and down the test stack
CACHE=${3:-} # Whether to override with docker-compose.build.yml

# Validation and normalization
if ! echo "$TEST" | grep -E '^(dev|prod)$' > /dev/null; then
echo "Specify TEST as the first argument. E.g. 'dev', 'prod'"
exit 1
fi
if [ -n "$CACHE" ]; then
CACHE='-f docker-compose.build.yml'
fi

SCRIPT_DIR=$( cd "$( dirname "$0" )" && pwd )
ERR=
setup_test() {
cd "$SCRIPT_DIR"
docker compose up -d
if [ -n "$UP" ]; then
setup
fi
run
}
cleanup_test() {
ERR=$?
if [ -n "$UP" ]; then
cleanup
fi
docker compose stop
if [ -z "$ERR" ] || [ "$ERR" = 0 ]; then
echo "All tests succeeded"
else
echo "Some tests failed"
echo "Exit code: $ERR"
exit "$ERR"
fi
}
trap cleanup_test INT TERM EXIT

echo "Testing..."
if [ "$TEST" = 'dev' ]; then
setup() {
if [ -n "$CACHE" ]; then
CACHE='-f docker-compose.build.yml'
fi
(cd .. && docker compose -f docker-compose.yml $CACHE up --build -d)
}
run() {
docker exec $( docker compose ps -q test-container-networking ) ./test-ready.sh
docker exec $( docker compose ps -q test-container-networking ) ./test-routes.sh
./test-awards.sh
./test-heatmaps.sh
}
cleanup() {
(cd .. && docker compose -f docker-compose.yml $CACHE stop)
}
fi
if [ "$TEST" = 'prod' ]; then
setup() {
if [ -n "$CACHE" ]; then
CACHE='-f docker-compose.example.build.yml'
fi
(cd .. && docker compose -f docker-compose.example.yml $CACHE up --build -d)
}
run() {
docker exec $( docker compose ps -q test-container-networking ) ./test-ready.sh
docker exec $( docker compose ps -q test-container-networking ) ./test-routes.sh
./test-awards.sh
./test-heatmaps.sh
docker exec $( docker compose ps -q test-host-networking ) ./test-endpoints.sh
}
cleanup() {
(cd .. && docker compose -f docker-compose.example.yml $CACHE stop)
}
fi
setup_test

0 comments on commit 56c2267

Please sign in to comment.