#!/usr/bin/env bash # Test of the image just built, on the runner's Docker daemon. Every container runs WITHOUT NETWORK (--network none): # Febe cannot reach any site, let alone Instagram (rule 1), and the health check goes through the container's # internal network. # Every container carries the label febe.ci.prova=, set here with «docker run --label» and never in an # image: it is the mark by which pulizia-docker.sh recognises a test container, together with the name # febe-prova-. The febe.ci.* labels of the image are not enough: Docker copies them into every container, # even into a real Febe started from a published image. # # .gitea/scripts/prova-immagine.sh [esecuzione] # # 1. the distribution's Chromium answers and the Febe policies (no saved passwords) are in place; # 2. the it_IT.UTF-8 locale is generated; # 3. the container starts as the guide says (Febe's seccomp profile, interface password, default PUID/PGID), the # image's health check becomes «healthy» and «febe --controllo-salute» answers; # 4. process 1 is Febe (no tini) and no orphan processes are left in the container to be reaped; # 5. on stop it stops by itself, within the 30 seconds the guide allows. # The password is random, lives only in the Docker client's environment and is never printed. set -euo pipefail immagine="${1:?Indica quale immagine provare.}" esecuzione="${2:-${ESECUZIONE:-locale}}" radice="$(git rev-parse --show-toplevel)" etichette=(--label "febe.ci.prova=$esecuzione") nome="febe-prova-$esecuzione" # Removes the test container of this run (also the one of an earlier attempt), but only if it carries the test mark: # a container with the same name and without the mark stays, and «docker run --name» stops. togli() { local segno segno=$(docker container inspect -f '{{index .Config.Labels "febe.ci.prova"}}' "$nome" 2> /dev/null) || return 0 if [[ "$segno" == "$esecuzione" ]]; then docker rm -f -v "$nome" > /dev/null 2>&1 || true fi } trap togli EXIT togli echo "== Chromium e criteri di Febe" docker run --rm --network none "${etichette[@]}" --entrypoint /usr/bin/chromium "$immagine" --version docker run --rm --network none "${etichette[@]}" --entrypoint sh "$immagine" -c \ 'test -s /etc/chromium/policies/managed/febe.json && test -s /usr/share/febe/seccomp.json && echo "criteri di Chromium e profilo seccomp presenti"' echo "== Lingua italiana" docker run --rm --network none "${etichette[@]}" --entrypoint sh "$immagine" -c 'locale -a | grep -i "^it_IT\.utf8$"' echo "== Avvio, salute e arresto" FEBE_PASSWORD="$(head -c 32 /dev/urandom | base64 | tr -dc 'A-Za-z0-9')" export FEBE_PASSWORD docker run -d --name "$nome" --network none "${etichette[@]}" \ --security-opt "seccomp=$radice/docker/chromium/seccomp.json" \ --stop-timeout 30 -e FEBE_PASSWORD "$immagine" > /dev/null unset FEBE_PASSWORD # The image's first health check comes 30 seconds after the start (HEALTHCHECK --interval=30s): up to 3 minutes. stato="" for _ in $(seq 1 36); do if [[ "$(docker inspect -f '{{.State.Running}}' "$nome")" != "true" ]]; then echo "Il contenitore si è fermato da solo (codice $(docker inspect -f '{{.State.ExitCode}}' "$nome")). Registro:" >&2 docker logs --tail 80 "$nome" >&2 || true exit 1 fi stato="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' "$nome")" [[ "$stato" == "healthy" || "$stato" == "unhealthy" ]] && break sleep 5 done if [[ "$stato" != "healthy" ]]; then echo "Il controllo di salute dice «${stato:-niente}» invece di «healthy». Registro:" >&2 docker logs --tail 80 "$nome" >&2 || true exit 1 fi if ! docker exec "$nome" /app/febe --controllo-salute > /dev/null; then echo "febe --controllo-salute non risponde «ok» dentro il contenitore. Registro:" >&2 docker logs --tail 80 "$nome" >&2 || true exit 1 fi echo "salute: healthy, e febe --controllo-salute risponde" echo "== Processo 1 e processi orfani" # Without tini process 1 is Febe, and the .NET runtime as process 1 also reaps the orphaned children of Chromium. # A process that has just ended may stay a «zombie» for an instant: only one that stays so for 10 seconds counts. primo="$(docker exec "$nome" cat /proc/1/comm)" if [[ "$primo" != "febe" ]]; then echo "Il processo 1 del contenitore è «$primo» invece di febe. Registro:" >&2 docker logs --tail 80 "$nome" >&2 || true exit 1 fi # The ids of the zombie processes that are children of process 1, sorted: the ones it has to reap (orphans end up # there). One that vanishes while being read does not count. orfani_zombie='for f in /proc/[0-9]*/status; do awk '\''$1 == "State:" { z = $2 } $1 == "Pid:" { p = $2 } $1 == "PPid:" { q = $2 } END { if (z == "Z" && q == 1) print p }'\'' "$f" 2> /dev/null done' zombie() { { docker exec "$nome" sh -c "$orfani_zombie" || true; } | sort } rimasti="$(zombie)" for _ in 1 2 3 4 5; do [[ -z "$rimasti" ]] && break sleep 2 rimasti="$(comm -12 <(printf '%s\n' "$rimasti") <(zombie))" done if [[ -n "$rimasti" ]]; then echo "Processi zombie che nessuno raccoglie (numeri: $(tr '\n' ' ' <<< "$rimasti")). Registro:" >&2 docker logs --tail 80 "$nome" >&2 || true exit 1 fi echo "processo 1: febe, nessun processo orfano da raccogliere" inizio=$(date +%s) docker stop -t 30 "$nome" > /dev/null durata=$(($(date +%s) - inizio)) codice="$(docker inspect -f '{{.State.ExitCode}}' "$nome")" echo "arresto in ${durata} s (codice di uscita $codice)" if ((durata >= 30)); then echo "Febe non si è fermato da solo entro 30 secondi: Docker l'ha dovuto uccidere. Registro:" >&2 docker logs --tail 80 "$nome" >&2 || true exit 1 fi