Erstes Commit
This commit is contained in:
120
tests/test-docker-build.sh
Executable file
120
tests/test-docker-build.sh
Executable file
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
readonly CONTAINER_NAME='umrechner-test'
|
||||
readonly IMAGE_TAG='umrechner-test:latest'
|
||||
readonly TEST_PORT='8888'
|
||||
readonly TMP_DIR="/tmp/umrechner-test-$$"
|
||||
readonly HTML_DIR="$TMP_DIR/html"
|
||||
|
||||
print_ok() {
|
||||
printf ' [OK] %s\n' "$1"
|
||||
}
|
||||
|
||||
print_fail() {
|
||||
printf ' [FAIL] %s\n' "$1" >&2
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
echo '[cleanup] removing test resources...'
|
||||
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
|
||||
docker rmi -f "$IMAGE_TAG" >/dev/null 2>&1 || true
|
||||
# HTML_DIR is kept for inspection
|
||||
# shellcheck disable=SC2115
|
||||
rm -rf "$TMP_DIR"
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2317
|
||||
cleanup_containers() {
|
||||
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
|
||||
docker rmi -f "$IMAGE_TAG" >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
trap cleanup_containers EXIT
|
||||
|
||||
echo '=== Stage 1: Docker Build ==='
|
||||
docker build -t "$IMAGE_TAG" .
|
||||
print_ok 'docker build completed'
|
||||
|
||||
echo ''
|
||||
echo '=== Stage 2: Artefact Validation ==='
|
||||
mkdir -p "$TMP_DIR"
|
||||
docker create --name "$CONTAINER_NAME" "$IMAGE_TAG" >/dev/null
|
||||
docker cp "$CONTAINER_NAME:/usr/share/nginx/html" "$HTML_DIR"
|
||||
docker rm "$CONTAINER_NAME" >/dev/null
|
||||
|
||||
required_files=(
|
||||
'index.html'
|
||||
'css/main.min.css'
|
||||
'robots.txt'
|
||||
)
|
||||
|
||||
for file in "${required_files[@]}"; do
|
||||
path="$HTML_DIR/$file"
|
||||
if [ ! -f "$path" ]; then
|
||||
print_fail "missing required file: $file"
|
||||
exit 1
|
||||
fi
|
||||
size=$(wc -c <"$path" | tr -d ' ')
|
||||
print_ok "$file exists ($size bytes)"
|
||||
done
|
||||
|
||||
sample_page=$(find "$HTML_DIR" -mindepth 2 -name 'index.html' -print -quit)
|
||||
if [ -z "$sample_page" ]; then
|
||||
print_fail 'no conversion pages found'
|
||||
exit 1
|
||||
fi
|
||||
sample_dir=$(dirname "$sample_page")
|
||||
sample_path="${sample_dir#$HTML_DIR}/"
|
||||
print_ok "conversion page found: $sample_path"
|
||||
|
||||
echo ''
|
||||
echo '=== Stage 3: HTTP Smoke Test ==='
|
||||
docker run -d --name "$CONTAINER_NAME" \
|
||||
-p "$TEST_PORT:80" "$IMAGE_TAG" >/dev/null
|
||||
|
||||
for i in $(seq 1 10); do
|
||||
if curl -sf "http://localhost:$TEST_PORT/" >/dev/null; then
|
||||
break
|
||||
fi
|
||||
if [ "$i" -eq 10 ]; then
|
||||
print_fail 'server did not respond in time'
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
print_ok 'server is responding'
|
||||
|
||||
endpoints=(
|
||||
'/'
|
||||
'/css/main.min.css'
|
||||
'/robots.txt'
|
||||
"$sample_path"
|
||||
)
|
||||
|
||||
for endpoint in "${endpoints[@]}"; do
|
||||
status=$(curl -s -o /dev/null -w '%{http_code}' \
|
||||
"http://localhost:$TEST_PORT$endpoint")
|
||||
if [ "$status" != '200' ]; then
|
||||
print_fail "$endpoint returned HTTP $status"
|
||||
exit 1
|
||||
fi
|
||||
print_ok "$endpoint returned HTTP 200"
|
||||
done
|
||||
|
||||
title='Umrechnung von Maßeinheiten'
|
||||
if ! curl -sf "http://localhost:$TEST_PORT/" | grep -q "$title"; then
|
||||
print_fail 'index page missing expected title'
|
||||
exit 1
|
||||
fi
|
||||
print_ok 'index page contains expected title'
|
||||
|
||||
echo ''
|
||||
echo 'All tests passed!'
|
||||
echo ''
|
||||
echo "=== Generated artefacts (kept for inspection) ==="
|
||||
printf ' Path: %s\n' "$HTML_DIR"
|
||||
printf ' Files: %s\n' "$(find "$HTML_DIR" -type f | wc -l)"
|
||||
printf ' Sample links from index.html:\n'
|
||||
grep -oE 'href=[^[:space:]>]+' "$HTML_DIR/index.html" | sed 's/href=//' | sort -u | head -20 | sed 's/^/ /'
|
||||
Reference in New Issue
Block a user