[linux] Installer (one copied line) and the container test of the Linux target with real systemd
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# The Linux target's integration test: Debian with real systemd as PID 1.
|
||||
FROM docker.io/library/debian:trixie-slim
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends systemd systemd-sysv python3 curl ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& systemctl mask systemd-logind.service getty.target console-getty.service
|
||||
STOPSIGNAL SIGRTMIN+3
|
||||
CMD ["/sbin/init"]
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/bin/sh
|
||||
# SPDX-License-Identifier: MPL-2.0
|
||||
# The Linux target against real systemd, in a container (Docker or Podman).
|
||||
# tests/linux/run.sh # from the repo root
|
||||
# Builds the test-only `deploy` example in a Rust container, starts Debian with systemd as PID 1,
|
||||
# and runs scenario.sh in it. On Mark's PC the container engine keeps its data on D:.
|
||||
set -eu
|
||||
|
||||
ENGINE=${ENGINE:-$(command -v podman || command -v docker || true)}
|
||||
[ -n "$ENGINE" ] || { echo "run.sh: needs podman or docker" >&2; exit 2; }
|
||||
REPO=$(cd "$(dirname "$0")/../.." && pwd)
|
||||
NAME=legatus-linux-it
|
||||
RUST_IMAGE=${RUST_IMAGE:-docker.io/library/rust:1.89-slim-trixie}
|
||||
|
||||
cleanup() { "$ENGINE" rm -f "$NAME" >/dev/null 2>&1 || true; }
|
||||
trap cleanup EXIT
|
||||
|
||||
echo "== building the test driver"
|
||||
"$ENGINE" volume create legatus-it-cargo >/dev/null
|
||||
"$ENGINE" run --rm -v "$REPO:/src:ro" -v legatus-it-cargo:/cargo -e CARGO_HOME=/cargo/home \
|
||||
-e CARGO_TARGET_DIR=/cargo/target -w /src "$RUST_IMAGE" \
|
||||
sh -c 'cargo build --locked --example deploy -p legatus-linux && cp /cargo/target/debug/examples/deploy /cargo/legatus-deploy-test'
|
||||
|
||||
echo "== starting systemd"
|
||||
"$ENGINE" build -q -t legatus-systemd "$REPO/tests/linux" >/dev/null
|
||||
cleanup
|
||||
"$ENGINE" run -d --name "$NAME" --privileged --cgroupns=host \
|
||||
--tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:rw \
|
||||
-v legatus-it-cargo:/cargo:ro legatus-systemd >/dev/null
|
||||
"$ENGINE" exec "$NAME" install -m 755 /cargo/legatus-deploy-test /usr/local/bin/legatus-deploy-test
|
||||
"$ENGINE" cp "$REPO/tests/linux/scenario.sh" "$NAME:/scenario.sh"
|
||||
|
||||
echo "== scenario"
|
||||
"$ENGINE" exec "$NAME" sh /scenario.sh
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/bin/sh
|
||||
# SPDX-License-Identifier: MPL-2.0
|
||||
# Runs inside the systemd container: four releases of a small web app through the Linux target.
|
||||
# 1.0.0 healthy, 2.0.0 healthy, 3.0.0 crashes at start, 4.0.0 runs but its health URL fails.
|
||||
# Expected: 1 and 2 succeed; 3 and 4 are rolled back to 2.0.0, which keeps running.
|
||||
set -eu
|
||||
|
||||
ROOT=/var/lib/legatus/apps
|
||||
DEPLOY=/usr/local/bin/legatus-deploy-test
|
||||
HEALTH=http://127.0.0.1:8080/health
|
||||
fail() { echo "FAIL: $*"; exit 1; }
|
||||
|
||||
# Wait for systemd to be up.
|
||||
for _ in $(seq 1 60); do
|
||||
s=$(systemctl is-system-running 2>/dev/null || true)
|
||||
[ "$s" = running ] || [ "$s" = degraded ] && break
|
||||
sleep 1
|
||||
done
|
||||
|
||||
cat > /etc/systemd/system/demo.service <<'EOF'
|
||||
[Unit]
|
||||
Description=Legatus demo app
|
||||
[Service]
|
||||
ExecStart=/var/lib/legatus/apps/demo/current/run.sh
|
||||
Restart=no
|
||||
EOF
|
||||
systemctl daemon-reload
|
||||
|
||||
work=$(mktemp -d)
|
||||
release() { # version kind
|
||||
d="$work/$1"; mkdir -p "$d/www"
|
||||
case "$2" in
|
||||
good) echo "ok $1" > "$d/www/health"
|
||||
printf '#!/bin/sh\nexec python3 -m http.server 8080 --bind 127.0.0.1 --directory "$(dirname "$(readlink -f "$0")")/www"\n' > "$d/run.sh" ;;
|
||||
crash) printf '#!/bin/sh\necho "cannot start" >&2\nexit 1\n' > "$d/run.sh" ;;
|
||||
unhealthy) printf '#!/bin/sh\nexec python3 -m http.server 8080 --bind 127.0.0.1 --directory "$(dirname "$(readlink -f "$0")")/www"\n' > "$d/run.sh" ;;
|
||||
esac
|
||||
chmod 755 "$d/run.sh"
|
||||
tar -C "$d" -czf "$work/$1.tar.gz" .
|
||||
}
|
||||
release 1.0.0 good; release 2.0.0 good; release 3.0.0 crash; release 4.0.0 unhealthy
|
||||
|
||||
deploy() { "$DEPLOY" "$ROOT" demo "$1" demo.service "$HEALTH" "$work/$1.tar.gz"; }
|
||||
current() { readlink "$ROOT/demo/current"; }
|
||||
|
||||
out=$(deploy 1.0.0); echo "1.0.0: $out"
|
||||
case "$out" in "succeeded previous=-") ;; *) fail "1.0.0: $out";; esac
|
||||
[ "$(current)" = releases/1.0.0 ] || fail "current is $(current)"
|
||||
[ "$(curl -fsS $HEALTH)" = "ok 1.0.0" ] || fail "1.0.0 does not answer"
|
||||
|
||||
out=$(deploy 2.0.0); echo "2.0.0: $out"
|
||||
case "$out" in "succeeded previous=1.0.0") ;; *) fail "2.0.0: $out";; esac
|
||||
[ "$(curl -fsS $HEALTH)" = "ok 2.0.0" ] || fail "2.0.0 does not answer"
|
||||
|
||||
out=$(deploy 3.0.0); echo "3.0.0: $out"
|
||||
case "$out" in "rolled_back to=2.0.0 healthy=true"*) ;; *) fail "3.0.0: $out";; esac
|
||||
[ "$(current)" = releases/2.0.0 ] || fail "after 3.0.0 current is $(current)"
|
||||
systemctl is-active --quiet demo.service || fail "demo is not active after the rollback"
|
||||
[ "$(curl -fsS $HEALTH)" = "ok 2.0.0" ] || fail "2.0.0 does not answer after the rollback"
|
||||
|
||||
out=$(deploy 4.0.0); echo "4.0.0: $out"
|
||||
case "$out" in "rolled_back to=2.0.0 healthy=true"*) ;; *) fail "4.0.0: $out";; esac
|
||||
[ "$(curl -fsS $HEALTH)" = "ok 2.0.0" ] || fail "2.0.0 does not answer after the second rollback"
|
||||
|
||||
echo "PASS: atomic switch, systemd restart, health check and auto rollback with real systemd"
|
||||
Reference in New Issue
Block a user