|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Automated smoke test for the zoneminder container. |
| 3 | +# Builds the image, starts a mariadb sidecar on a shared docker network, runs |
| 4 | +# zoneminder pointed at it and asserts that apache + the ZoneMinder web console |
| 5 | +# come up and serve without PHP fatal errors. |
| 6 | +# |
| 7 | +# No host bind-mounts: the DB lives inside the sidecar container, and HTTP is |
| 8 | +# spoken from inside the zoneminder container with a bash /dev/tcp helper |
| 9 | +# (the apache2 base image ships no wget/curl). |
| 10 | +set -e |
| 11 | + |
| 12 | +IMG=zoneminder-test |
| 13 | +CN=zoneminder-test-run |
| 14 | +DB=zoneminder-test-db |
| 15 | +NET=zoneminder-test-net |
| 16 | + |
| 17 | +cleanup() { |
| 18 | + docker rm -f "$CN" >/dev/null 2>&1 || true |
| 19 | + docker rm -f "$DB" >/dev/null 2>&1 || true |
| 20 | + docker network rm "$NET" >/dev/null 2>&1 || true |
| 21 | +} |
| 22 | +trap cleanup EXIT |
| 23 | + |
| 24 | +fail() { echo "FAIL: $1"; exit 1; } |
| 25 | + |
| 26 | +# http_get <container> <path> -> full HTTP response (headers + body) via /dev/tcp |
| 27 | +http_get() { |
| 28 | + docker exec "$1" bash -c ' |
| 29 | + exec 3<>/dev/tcp/127.0.0.1/80 || exit 1 |
| 30 | + printf "GET '"$2"' HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n" >&3 |
| 31 | + cat <&3 |
| 32 | + ' |
| 33 | +} |
| 34 | + |
| 35 | +# http_console <container> -> fetch /zm/ and follow ZoneMinder's first-run |
| 36 | +# redirect (e.g. -> ?view=privacy) so we land on the real console HTML. |
| 37 | +http_console() { |
| 38 | + local resp loc |
| 39 | + resp=$(http_get "$1" /zm/) |
| 40 | + loc=$(echo "$resp" | grep -i '^Location:' | head -1 | sed 's/^[Ll]ocation:[[:space:]]*//; s/[[:space:]]*$//' | tr -d '\r') |
| 41 | + if [ -n "$loc" ]; then |
| 42 | + case "$loc" in |
| 43 | + /*) http_get "$1" "$loc" ;; |
| 44 | + *) http_get "$1" "/zm/$loc" ;; |
| 45 | + esac |
| 46 | + else |
| 47 | + echo "$resp" |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +echo ">> building image" |
| 52 | +docker build -t "$IMG" . |
| 53 | + |
| 54 | +echo ">> (re)creating docker network" |
| 55 | +docker network rm "$NET" >/dev/null 2>&1 || true |
| 56 | +docker network create "$NET" >/dev/null |
| 57 | + |
| 58 | +echo ">> starting mariadb sidecar" |
| 59 | +docker rm -f "$DB" >/dev/null 2>&1 || true |
| 60 | +docker run -d --name "$DB" --network "$NET" \ |
| 61 | + -e MYSQL_ROOT_PASSWORD=rootpass \ |
| 62 | + -e MYSQL_DATABASE=zm \ |
| 63 | + -e MYSQL_USER=zmuser \ |
| 64 | + -e MYSQL_PASSWORD=zmpass \ |
| 65 | + mariadb --max-allowed-packet=64MB >/dev/null |
| 66 | + |
| 67 | +echo ">> waiting for mariadb to accept connections (up to 60s)" |
| 68 | +dbup=0 |
| 69 | +for _ in $(seq 1 30); do |
| 70 | + if docker exec "$DB" mariadb-admin ping -uzmuser -pzmpass >/dev/null 2>&1; then dbup=1; break; fi |
| 71 | + sleep 2 |
| 72 | +done |
| 73 | +[ "$dbup" = 1 ] || fail "mariadb sidecar did not become ready in time" |
| 74 | +echo "ok - mariadb ready" |
| 75 | + |
| 76 | +echo ">> starting zoneminder" |
| 77 | +docker rm -f "$CN" >/dev/null 2>&1 || true |
| 78 | +docker run -d --name "$CN" --network "$NET" \ |
| 79 | + --shm-size=2g \ |
| 80 | + -e DISABLE_TLS=disable \ |
| 81 | + -e ZM_DB_HOST="$DB" \ |
| 82 | + -e ZM_DB_NAME=zm \ |
| 83 | + -e ZM_DB_USER=zmuser \ |
| 84 | + -e ZM_DB_PASS=zmpass \ |
| 85 | + "$IMG" >/dev/null |
| 86 | + |
| 87 | +echo ">> waiting for apache to listen on :80 (up to 90s)" |
| 88 | +up=0 |
| 89 | +for _ in $(seq 1 45); do |
| 90 | + if docker exec "$CN" bash -c 'exec 3<>/dev/tcp/127.0.0.1/80' 2>/dev/null; then up=1; break; fi |
| 91 | + sleep 2 |
| 92 | +done |
| 93 | +[ "$up" = 1 ] || fail "apache did not start listening on :80 in time" |
| 94 | +echo "ok - apache listening on :80" |
| 95 | + |
| 96 | +echo ">> assert: container is running" |
| 97 | +[ "$(docker inspect -f '{{.State.Running}}' "$CN")" = true ] || fail "container not running" |
| 98 | +echo "ok - container running" |
| 99 | + |
| 100 | +echo ">> waiting for the ZoneMinder console to serve (db init is slow, up to 120s)" |
| 101 | +zmup=0 |
| 102 | +for _ in $(seq 1 60); do |
| 103 | + if http_console "$CN" 2>/dev/null | grep -q 'ZoneMinder'; then zmup=1; break; fi |
| 104 | + sleep 2 |
| 105 | +done |
| 106 | +[ "$zmup" = 1 ] || fail "ZoneMinder console did not serve HTML in time" |
| 107 | +echo "ok - ZoneMinder console responding" |
| 108 | + |
| 109 | +echo ">> assert: GET / redirects (302) to zm/" |
| 110 | +root=$(http_get "$CN" /) |
| 111 | +echo "$root" | head -1 | grep -q '302' || fail "GET / did not return 302 (got: $(echo "$root" | head -1))" |
| 112 | +echo "$root" | grep -qi '^Location:.*zm/' || fail "GET / 302 has no Location: zm/ header" |
| 113 | +echo "ok - GET / -> 302 Location: zm/" |
| 114 | + |
| 115 | +echo ">> assert: the ZoneMinder web console serves HTML containing ZoneMinder" |
| 116 | +# GET /zm/ lands on the console; on a fresh install it first bounces through |
| 117 | +# ?view=privacy, so follow that one redirect before inspecting the body. |
| 118 | +zm=$(http_console "$CN") |
| 119 | +echo "$zm" | head -1 | grep -q '200' || fail "console did not return 200 (got: $(echo "$zm" | head -1))" |
| 120 | +echo "$zm" | grep -q 'ZoneMinder' || fail "console body does not contain 'ZoneMinder'" |
| 121 | +echo "ok - console serves ZoneMinder HTML ($(echo "$zm" | grep -io '<title>[^<]*</title>' | head -1))" |
| 122 | + |
| 123 | +# The very first requests during db bootstrap can log a transient logger.php |
| 124 | +# fatal (config constants not defined yet); that window is over once the |
| 125 | +# console serves above. Truncate the log, hit the console fresh and assert the |
| 126 | +# steady-state console renders without any PHP fatal. |
| 127 | +echo ">> assert: no PHP Fatal errors serving the console (steady state)" |
| 128 | +docker exec "$CN" sh -c ': > /var/log/apache2/error.log' 2>/dev/null || true |
| 129 | +http_console "$CN" >/dev/null 2>&1 |
| 130 | +http_get "$CN" '/zm/?view=console' >/dev/null 2>&1 |
| 131 | +sleep 2 |
| 132 | +logs=$( { docker exec "$CN" cat /var/log/apache2/error.log 2>/dev/null; docker logs "$CN" 2>&1; } || true ) |
| 133 | +echo "$logs" | grep -i 'PHP Fatal' && fail "PHP Fatal error found while serving the console" || true |
| 134 | +echo "ok - no PHP Fatal errors" |
| 135 | + |
| 136 | +echo "" |
| 137 | +echo "ALL TESTS PASSED" |
0 commit comments