From 58e504c4b600a3ce70c89168fb55387c16eb7a00 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Sun, 18 Oct 2026 14:52:09 +0530 Subject: [PATCH] fix(keploy-ci): start-docker starts containerd itself and waits on each daemon while it works Enterprise lanes failed before any of their own code ran. start-docker's dockerd quit with "failed to start containerd: timeout waiting for containerd to start": pipeline 11771 (go-docker-timefreeze #214), and four steps of 11931 (194, 369, 402 and 418). - dockerd starts the containerd it manages and gives it a fixed 15 s (moby's startupTimeout, in 29.1.5 daemon/internal/libcontainerd/supervisor/remote_daemon.go). - On those starved runners containerd took longer. In 11771 it was silent for 11.8 s or more after its bolt mount manager loaded. In 11931 it sat 15 s or more in its bolt metadata store's open, logging "waiting for response from boltdb open". - start-docker's own wait, 60 tries a second apart, was a clock too. start-docker now: - starts containerd itself, with the layout dockerd gives the containerd it manages: root and state under /var/lib/docker and /var/run/docker (so on the image's /var/lib/docker volume), the CRI plugin off, its socket at /var/run/docker/containerd/containerd.sock. - containerd runs in a session of its own (setsid), as moby starts it, so it outlives a terminal that ends. - It logs at dockerd's level, as moby gives the containerd it manages: debug with --debug (-D, or any of Go's true spellings), else dockerd's --log-level (-l, either form), info by default. - It is waited for until `ctr version` answers. - then starts dockerd with --containerd on that socket, and waits until `docker info` answers. - waits on each for as long as it is alive and its log grows: - it fails at once, with both daemons' logs, when the daemon exits; - it fails only when the daemon has neither answered nor logged for START_DOCKER_SILENCE_CHECKS looks in a row (600 by default, a hang guard). - A look is a second. The probe is never cut short: one runs in the background at a time, each look sees whether it has finished, and another starts once it has answered no. dockerd listens on its socket before its init and answers only after, and `docker info` asks containerd, the daemon that starves, for its system info, so a probe may take long. A probe killed at a bound would be killed on every look, and a start that would have answered would fail. A slow init that goes on logging is waited for, however long, and only a daemon that stops progressing ends the wait (and then the probe in flight is killed). - A stall is said as it goes on: every START_DOCKER_NOTICE_LOOKS quiet looks (30), with the daemon's last log line, so a step that a CI cap ends still says why. - START_DOCKER_PROBE_SECONDS (10 s) bounds only the one look start-docker takes at a socket no pidfile names, which it has nothing else to watch on. What answers there is reused; a socket that refuses (nothing listens) is taken over; one that listens and does not answer within the bound fails the start, said: its daemon may be slow, not gone, and a second one is not started on the same root. The probes name the socket (`docker -H unix:///var/run/docker.sock`), whatever DOCKER_HOST or the docker context say. - restarts containerd when it exits, as dockerd's own supervisor did: a watcher, in its own session, starts it again and says so in /var/log/containerd.log, and dockerd reconnects. - runs one start at a time (flock on /var/run/start-docker.lock, closed for every process it starts, so a daemon does not hold it); another start waits for it. - run again, reuses what is there: - a dockerd or containerd an earlier start left running (their pidfiles, written as each is started; the watcher keeps containerd's), waited for until it answers however far it got, as after a start killed mid-wait, and not started twice on one root; - a containerd its watcher is starting again, once it has; - a dockerd or containerd that answers but that no pidfile names; - the watcher an earlier start left (its pidfile, checked to name the watcher and not some other sh): one watcher at a time. - refuses DOCKERD_FLAGS, and /etc/docker/daemon.json keys, that move what it fixes (--data-root, --exec-root, --pidfile, --host, --containerd, --config-file, short forms run together as pflag reads them, such as -DH and -Htcp://...), since containerd's root and socket and dockerd's pidfile and socket are fixed here. DOCKERD_FLAGS is split into words, not globbed. - when a start fails, kills what it started, dockerd, the watcher, then containerd, and reaps each, so nothing of it is left, not even a zombie, and the next begins clean. What an earlier start left, and this one only waited for, is not this one's to kill. - The watcher is sent SIGTERM, on which it kills the last containerd it started ($!, as a TERM can come between that start and its pid=$!) and reaps what it started. - With the trap killing $pid, 40 runs in 300 of a watcher-alike TERMed at a random moment under dash left a child; with $!, none in 600. - appends to both logs, not truncating them under a daemon still writing them. With --containerd, dockerd still gives up about a second after containerd stops answering it ("failed to get containerd plugins"); that is not a start's wait, and it is left as it is. Tests: keploy-ci/start-docker-test.sh runs each case in a fresh privileged container of the keploy-ci image, with this checkout's script. In CI (docker-build.yml) that is the image this commit builds, so an engine or containerd bump is tested with start-docker before it is released. Each case's container runs detached, known by its id, and is waited for under a hang guard (CASE_GUARD, 900 s), which ends a case that hangs, fails it and removes its container: a guard around `docker run` would only signal the client, which hands the signal to the container's PID 1 and waits on. The wait runs in the background, so an INT or TERM to the run is acted on at once. A container goes with its volume (the image's /var/lib/docker), and the one running goes too when the run is ended. Stand-ins first on PATH make containerd slow, dead or silent, and dockerd slow, wedged or failing. The cases: - the guard ends a case that hangs and removes its container; - settings that are not whole numbers, 1 or more, and DOCKERD_FLAGS or daemon.json keys that move what start-docker fixes (in each spelling above), are refused (2), and flags that do not (-ldebug, --dns) are not; - it starts both, with DOCKERD_FLAGS reaching dockerd (a registry mirror, as the lanes give it, and --debug). containerd and its watcher each lead a session of their own, and containerd logs at debug. It builds and runs a container (an image of the engine's static docker-init, nothing pulled); - a second start finds the first's dockerd (its pidfile) and returns once it answers, and with --log-level=debug containerd logs at debug; the first runs with DOCKER_HOST pointing elsewhere; - the failure fixed, replicated: dockerd starting a containerd that takes 20 s quits with "timeout waiting for containerd to start"; - a containerd that takes 75 s, logging as it loads, is waited for with 60 looks of silence allowed (more than a real daemon here keeps silent: 15 s on a starved runner): it is the growing log that keeps the wait going; - a containerd silent for 20 s, then up, is waited for within its silence checks; - a containerd that exits fails the start at once (1), with its log; - a containerd that neither answers nor logs fails after its silence checks (1, the start's own status, not the guard's); - a dockerd that exits (a bad flag) fails the start at once. Run under a subreaper that reaps nothing, the failed start leaves nothing behind, running or a zombie. The next start comes up with one containerd, and both logs keep the first start's lines. With -l debug, containerd logs at debug; - a start that fails after its watcher started containerd again leaves nothing either (and with --log-level debug, containerd logs at debug); - a dockerd stopped once its socket is listening (its probes block) that goes on logging for 75 s is waited for with 60 looks of silence allowed. One that logs nothing is said to stall at 2 quiet looks and fails at 4, said ("dockerd has not answered, and its log has not grown in 4 looks"); - a docker that takes 8 s to answer, with 30 looks of silence allowed and the one-shot bound at 2 s, is waited for and its answer taken; - a socket no pidfile names that refuses is taken over, and the start goes on; one that listens and does not answer fails the start, said, and nothing is started over it; - a start killed after it started dockerd, before dockerd wrote its own pidfile, leaves its pid: the next start waits for that dockerd; - a watcher pidfile that names some other sh is no watcher: containerd gets one of its own; - a start while containerd's watcher is starting it again waits for the watcher, and reuses the containerd it starts; - a second start while the first's containerd is still starting waits for the first (the lock), then for its dockerd, and nothing is started twice: one containerd, one watcher; - a start after one killed while containerd was starting waits for that containerd and gives it a watcher; one after a start killed in dockerd's init waits for that dockerd until it answers; - a containerd killed under a running dockerd (--debug=1, containerd at debug too) is started again, said in its log. dockerd says it is back on it (its event stream processed again), and docker runs a container; - a start after dockerd stopped reuses the containerd that answers, starts no second one, and appends to dockerd's log; containerd is at info by default. Each of 33 mutants of start-docker fails the cases, among them: the probe in a wait bounded (a slow docker info cut every look), the one-shot look at an unknown socket unbounded, a mute unknown socket taken over, progress ignored, no lock, dockerd holding the lock, a dockerd or containerd an earlier start left not waited for, no dockerd pid recorded, a restarting containerd not waited for, a second watcher, the watcher known by comm sh, the logs truncated, the watcher in the caller's session, dockerd's level not given to containerd, Go's bool spellings or -l VALUE ignored, the settings, fixed flags, run-together short flags or daemon.json not checked, docker info following DOCKER_HOST, no stall notice, a failed start that does not reap, a failed start that keeps what it started, a watcher killed instead of stopped, and a watcher that leaves its containerd. Signed-off-by: slayerjain --- .github/workflows/docker-build.yml | 31 +++ keploy-ci/start-docker | 381 +++++++++++++++++++++++++++-- keploy-ci/start-docker-test.sh | 377 ++++++++++++++++++++++++++++ 3 files changed, 768 insertions(+), 21 deletions(-) mode change 100644 => 100755 keploy-ci/start-docker create mode 100755 keploy-ci/start-docker-test.sh diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index c7ad8e5..26a9f8b 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -108,6 +108,37 @@ jobs: exit 1 fi + # start-docker (baked into keploy-ci, and so into every image built FROM it) + # starts containerd and then dockerd on it, and waits on each while it is + # alive and its log grows. Its cases run in fresh privileged containers of + # the keploy-ci image this commit builds (its engine and containerd, not + # the last release's), stand-ins making containerd slow, dead or silent and + # dockerd slow or wedged as a starved host does. Each case has a hang guard + # of its own (CASE_GUARD, 900 s), which ends a case that hangs and fails + # it; the job's cap is the last one. + start-docker: + name: start-docker waits on containerd and dockerd + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build this commit's keploy-ci image + uses: docker/build-push-action@v6 + with: + context: ./keploy-ci + platforms: linux/amd64 + load: true + push: false + tags: keploy-ci:start-docker-test + + - name: start-docker's cases, on that image + run: bash keploy-ci/start-docker-test.sh keploy-ci:start-docker-test + # ── Stage 1: base images (no cross-image deps) ───────────────────── base-images: name: "${{ matrix.tag-prefix || 'base' }} (${{ matrix.arch }})" diff --git a/keploy-ci/start-docker b/keploy-ci/start-docker old mode 100644 new mode 100755 index 8203d28..0315678 --- a/keploy-ci/start-docker +++ b/keploy-ci/start-docker @@ -1,31 +1,370 @@ #!/usr/bin/env sh -set -eu +# start-docker: start the Docker daemon in this container (DinD) and return +# once it answers, with DOCKERD_FLAGS for dockerd's own flags. +# +# containerd is started here, before dockerd, and dockerd attaches to it +# (--containerd). Left to start it itself, dockerd gives the containerd it +# manages a fixed 15 s (moby's startupTimeout, in 29.1.5 +# daemon/internal/libcontainerd/supervisor/remote_daemon.go) and quits with +# "failed to start containerd: timeout waiting for containerd to start" when +# a starved host's containerd takes longer: enterprise pipeline 11931 (steps +# 194, 369, 402 and 418) lost four lanes to containerd sitting over 15 s, +# silent, in its bolt metadata store's open. +# +# Each daemon is waited for while it is alive and making progress: until it +# answers, it exits, or its log has not grown for START_DOCKER_SILENCE_CHECKS +# looks in a row (600 by default; a hang guard, not a budget for a start that +# works). A look is a second. The probe (`ctr version`, `docker info`) is +# never cut short: one runs at a time, and each look sees whether it has +# answered. dockerd listens on its socket before its init and answers only +# after, and docker info asks containerd, the daemon that starves, for its +# system info: a probe that takes long is still waited for, and its answer +# taken when it comes. Only a daemon that stops progressing ends the wait, +# and then the probe in flight is killed. A stall is said as it goes on, +# every START_DOCKER_NOTICE_LOOKS quiet looks (30), with the daemon's last +# log line, so a CI step cut by its own cap still says why. +# START_DOCKER_PROBE_SECONDS (10) bounds only the one look start-docker takes +# at a socket no pidfile names, which it has nothing else to watch on: a +# daemon there that answers is reused, a socket that refuses (nothing +# listens) is taken over, and one that listens and does not answer within +# the bound fails the start, said: its daemon may be slow, not gone, and a +# second one on the same root is not started. +# +# containerd runs as dockerd would run it: its root and state under +# /var/lib/docker and /var/run/docker (the image's /var/lib/docker volume), +# its socket where dockerd puts it, in a session of its own (moby starts it +# with setsid, so it outlives the terminal that started it), at debug level +# when dockerd is. And as dockerd would, it is restarted when it exits: a +# watcher, in a session of its own too, starts it again and says so in its +# log (/var/log/containerd.log). +# +# One start runs at a time (a lock); another waits for it. Run again, it +# reuses what is there: a dockerd or a containerd an earlier start left +# running (their pidfiles, written as each is started), waited for until it +# answers, however far it got (an earlier start killed mid-wait); a +# containerd its watcher is starting again; or a dockerd or containerd that +# answers that no pidfile names. A start that fails stops what it started, +# so the next one begins clean. +# +# DOCKERD_FLAGS (and /etc/docker/daemon.json) may not move what start-docker +# puts in place (--data-root, --exec-root, --pidfile, --host, --containerd, +# --config-file, in any spelling): containerd's root, its socket and +# dockerd's pidfile and socket are fixed here. DOCKERD_FLAGS is split into +# words, not globbed. +# +# Runs as a script, without job control: a daemon started in the background +# here is not a process group leader, so setsid execs it in place and $! is +# the daemon's pid. +set -euf -# Make sure runtime dirs exist -mkdir -p /var/run /var/lib/docker +CONTAINERD_ROOT=/var/lib/docker/containerd/daemon +CONTAINERD_STATE=/var/run/docker/containerd/daemon +CONTAINERD_SOCK=/var/run/docker/containerd/containerd.sock +CONTAINERD_CONFIG=/var/run/docker/containerd/containerd.toml +CONTAINERD_PIDFILE=/var/run/docker/containerd/containerd.pid +WATCHER_PIDFILE=/var/run/docker/containerd/watcher.pid +CONTAINERD_LOG=/var/log/containerd.log +DOCKERD_LOG=/var/log/dockerd.log +DOCKERD_PIDFILE=/var/run/docker.pid +DOCKERD_STARTED=/var/run/docker/start-docker-dockerd.pid +DOCKER_SOCK=/var/run/docker.sock +LOCK=/var/run/start-docker.lock +SILENCE="${START_DOCKER_SILENCE_CHECKS:-600}" +PROBE_SECONDS="${START_DOCKER_PROBE_SECONDS:-10}" +NOTICE="${START_DOCKER_NOTICE_LOOKS:-30}" +for n in "$SILENCE" "$PROBE_SECONDS" "$NOTICE"; do + case "$n" in + '' | *[!0-9]* | 0*) echo "start-docker: START_DOCKER_SILENCE_CHECKS, START_DOCKER_PROBE_SECONDS and START_DOCKER_NOTICE_LOOKS are whole numbers, 1 or more (got '$n')" >&2; exit 2 ;; + esac +done +FLAGS="${DOCKERD_FLAGS:-}" +fixed() { + echo "start-docker: $1 sets $2, which start-docker fixes (containerd's root and socket, dockerd's pidfile and socket)" >&2 + exit 2 +} +# shellcheck disable=SC2086 # FLAGS is a list of flags +for f in $FLAGS; do + case "$f" in + --data-root | --data-root=* | --graph | --graph=* | --exec-root | --exec-root=* | --pidfile | --pidfile=* | \ + --host | --host=* | --containerd | --containerd=* | --config-file | --config-file=*) + fixed DOCKERD_FLAGS "${f%%=*}" + ;; + --*) ;; + -?*) + # Short flags, run together as pflag takes them (-DH, -Htcp://x): + # each letter a flag, until one that takes a value (the rest of the + # word is that value). + g=${f#-} + while [ -n "$g" ]; do + c=${g%"${g#?}"} g=${g#?} + case "$c" in + H | p | g) fixed DOCKERD_FLAGS "-$c" ;; + l | G | b | e) break ;; + esac + done + ;; + esac +done +if [ -f /etc/docker/daemon.json ] && grep -qE '"(data-root|graph|exec-root|pidfile|hosts|containerd)"[[:space:]]*:' /etc/docker/daemon.json; then + fixed /etc/docker/daemon.json "$(grep -oE '"(data-root|graph|exec-root|pidfile|hosts|containerd)"' /etc/docker/daemon.json | head -n 1)" +fi + +mkdir -p /var/run /var/lib/docker "$CONTAINERD_ROOT" "$CONTAINERD_STATE" /var/log + +# probe CMD...: CMD answers within PROBE_SECONDS. Only for a daemon no +# pidfile names, which start-docker has nothing else to watch on (a hang +# guard); a daemon it waits on is probed without a bound (await). +probe() { + timeout "$PROBE_SECONDS" "$@" >/dev/null 2>&1 9>&- +} + +# alive PID: the process runs (a zombie, exited but not yet reaped, does not). +alive() { + [ -r "/proc/$1/status" ] && ! grep -q '^State:[[:space:]]*Z' "/proc/$1/status" 2>/dev/null +} + +# named PID COMM: PID runs and is COMM (a pidfile's pid, not one reused). +named() { + alive "$1" && [ "$(cat "/proc/$1/comm" 2>/dev/null)" = "$2" ] +} -# If already running, do nothing -if docker info >/dev/null 2>&1; then - echo "Docker daemon already running" +# is_watcher PID: PID runs and is start-docker's containerd watcher: one of +# the words of its command line (its script's $0) is +# start-docker-containerd-watcher, not any other sh. The words are read +# NUL-separated (grep -z): the script before it is one word of many lines. +is_watcher() { + alive "$1" && grep -qxz start-docker-containerd-watcher "/proc/$1/cmdline" 2>/dev/null +} + +# pidfile_names FILE COMM: FILE names a running COMM, which it prints. +pidfile_names() { + [ -s "$1" ] && named "$(cat "$1")" "$2" && cat "$1" +} + +# containerd_named: containerd's pidfile names a running containerd. +containerd_named() { + pidfile_names "$CONTAINERD_PIDFILE" containerd >/dev/null +} + +# await NAME PID LOG PROBE...: returns 0 once PROBE answers; 1 once PID has +# exited; 2 once LOG has not grown for SILENCE looks in a row. Every NOTICE +# quiet looks before that, it says that the daemon has stalled. PROBE runs +# in the background, one at a time, and is not cut short: each look sees +# whether it has finished, and starts another once it has answered no. +await() { + name=$1 pid=$2 log=$3 + shift 3 + last=-1 quiet=0 pp="" + while :; do + if [ -z "$pp" ]; then + "$@" >/dev/null 2>&1 9>&- & + pp=$! + fi + if ! alive "$pp"; then + if wait "$pp"; then + return 0 + fi + pp="" + fi + if ! alive "$pid"; then + stop_probe + echo "$name exited before it answered" >&2 + return 1 + fi + size=$(wc -c <"$log" 2>/dev/null || echo 0) + if [ "$size" != "$last" ]; then + last=$size quiet=0 + else + quiet=$((quiet + 1)) + fi + if [ "$quiet" -ge "$SILENCE" ]; then + stop_probe + echo "$name has not answered, and its log has not grown in $SILENCE looks" >&2 + return 2 + fi + if [ "$quiet" -gt 0 ] && [ $((quiet % NOTICE)) = 0 ]; then + echo "$name has not answered, and its log has not grown in $quiet looks; still waiting while it runs (it fails after $SILENCE). The end of $log: $(tail -n 1 "$log" 2>/dev/null)" >&2 + fi + sleep 1 9>&- + done +} +# stop_probe: the probe in flight, if any, is killed and reaped. +stop_probe() { + if [ -n "$pp" ]; then + kill -9 "$pp" 2>/dev/null || true + wait "$pp" 2>/dev/null || true + pp="" + fi +} + +# What this run started, killed when it fails (it failed: nothing of it is +# kept): dockerd, then the watcher, then containerd, so that the watcher does +# not start containerd again. Each is this shell's child and is reaped here, +# so none is left when start-docker returns, not even as a zombie. The +# watcher is sent SIGTERM, on which it kills and reaps a containerd it +# started again (not this shell's child), and then exits. What an earlier +# start left, and this one only waited for, is not this one's to kill. +started="" watcher="" +fail_start() { + echo "$1" + echo "--- the end of $DOCKERD_LOG ---" + tail -n 100 "$DOCKERD_LOG" 2>/dev/null || true + echo "--- the end of $CONTAINERD_LOG ---" + tail -n 100 "$CONTAINERD_LOG" 2>/dev/null || true + for p in $started; do + if [ "$p" = "$watcher" ]; then + kill -TERM "$p" 2>/dev/null || true + else + kill -9 "$p" 2>/dev/null || true + fi + wait "$p" 2>/dev/null || true + done + exit 1 +} + +# One start at a time. The lock is this shell's fd 9, closed for every +# process it starts, so that a daemon does not hold it after it returns. +exec 9>"$LOCK" +if ! flock -n 9; then + echo "another start-docker is running; waiting for it" + flock 9 +fi + +# A dockerd an earlier start left (the pidfile start-docker writes as it +# starts it, or the one dockerd writes): waited for until it answers, not +# started twice on one root. +old=$(pidfile_names "$DOCKERD_STARTED" dockerd || pidfile_names "$DOCKERD_PIDFILE" dockerd || true) +if [ -n "$old" ]; then + echo "dockerd ($old) is already running; waiting for it to answer" + if ! await dockerd "$old" "$DOCKERD_LOG" docker -H "unix://$DOCKER_SOCK" info; then + fail_start "Docker daemon failed to start" + fi + echo "Docker daemon is ready" exit 0 fi +# A socket no pidfile names: one look, bounded. What answers is reused; what +# listens and does not answer may be a slow daemon, not a gone one, and is +# not started over; a socket that refuses (nothing listens) is taken over. +if [ -S "$DOCKER_SOCK" ]; then + rc=0 + probe docker -H "unix://$DOCKER_SOCK" info || rc=$? + case $rc in + 0) + echo "Docker daemon already running" + exit 0 + ;; + 124) + echo "start-docker: something no pidfile names listens on $DOCKER_SOCK and did not answer in ${PROBE_SECONDS}s: not starting a second dockerd over it" + exit 1 + ;; + esac +fi -# Default flags -FLAGS="${DOCKERD_FLAGS:-}" +# containerd logs at dockerd's level, as dockerd gives the containerd it +# manages: debug with --debug (-D), else dockerd's --log-level (-l), info by +# default. +log_level() { + _level=info _debug="" + # shellcheck disable=SC2086 # FLAGS is a list of flags + set -- $FLAGS + while [ "$#" -gt 0 ]; do + case "$1" in + --debug | -D) _debug=1 ;; + # Go's bool flag values (strconv.ParseBool), as dockerd takes them. + --debug=* | -D=*) + case "${1#*=}" in + 1 | t | T | true | TRUE | True) _debug=1 ;; + 0 | f | F | false | FALSE | False) _debug="" ;; + esac + ;; + --log-level=* | -l=*) _level=${1#*=} ;; + --log-level | -l) [ "$#" -lt 2 ] || { _level=$2; shift; } ;; + esac + shift + done + if [ -n "$_debug" ]; then echo debug; else echo "$_level"; fi +} +level=$(log_level) -# Start dockerd in background -echo "Starting dockerd ${FLAGS} ..." -dockerd ${FLAGS} >/var/log/dockerd.log 2>&1 & +# containerd: one its watcher is starting again, once it has; one an earlier +# start left (its pidfile), waited for until it answers; else one that +# answers on its socket; else started here. +w=$(cat "$WATCHER_PIDFILE" 2>/dev/null || true) +if [ -n "$w" ] && is_watcher "$w" && ! containerd_named; then + echo "containerd's watcher ($w) is starting it again; waiting for it" + if ! await "containerd's watcher" "$w" "$CONTAINERD_LOG" containerd_named; then + fail_start "containerd failed to start" + fi +fi +containerd_pid="" +rc=1 +if containerd_pid=$(pidfile_names "$CONTAINERD_PIDFILE" containerd); then + echo "containerd ($containerd_pid) is already running; waiting for it to answer" + if ! await containerd "$containerd_pid" "$CONTAINERD_LOG" ctr --address "$CONTAINERD_SOCK" version; then + fail_start "containerd failed to start" + fi + echo "containerd ($containerd_pid) already answers on $CONTAINERD_SOCK" +elif [ -S "$CONTAINERD_SOCK" ] && { rc=0; probe ctr --address "$CONTAINERD_SOCK" version || rc=$?; [ "$rc" = 0 ] || [ "$rc" = 124 ]; }; then + containerd_pid="" + if [ "$rc" = 124 ]; then + echo "start-docker: something no pidfile names listens on $CONTAINERD_SOCK and did not answer in ${PROBE_SECONDS}s: not starting a second containerd over it" + exit 1 + fi + echo "containerd already answers on $CONTAINERD_SOCK (no pidfile names it: it has no watcher)" +else + containerd_pid="" + cat >"$CONTAINERD_CONFIG" </dev/null 2>&1; then - echo "Docker daemon is ready" - exit 0 +[grpc] + address = "$CONTAINERD_SOCK" +EOF + echo "Starting containerd ..." + setsid containerd --config "$CONTAINERD_CONFIG" --log-level "$level" >>"$CONTAINERD_LOG" 2>&1 9>&- & + containerd_pid=$! + started="$containerd_pid" + echo "$containerd_pid" >"$CONTAINERD_PIDFILE" + if ! await containerd "$containerd_pid" "$CONTAINERD_LOG" ctr --address "$CONTAINERD_SOCK" version; then + fail_start "containerd failed to start" fi - sleep 1 -done +fi +# The watcher: when containerd exits, it is started again, as dockerd's own +# supervisor would; dockerd reconnects to it. It keeps containerd's pidfile. +# Sent SIGTERM (a start that fails), it kills the last containerd it started +# ($!, not $pid: a TERM can be taken between that start and pid=$!) and reaps +# what it started, before it exits; a containerd it did not start is not its +# to kill. One watcher at a time: one an earlier start left (its pidfile) +# goes on. +if [ -n "$containerd_pid" ] && ! { [ -s "$WATCHER_PIDFILE" ] && is_watcher "$(cat "$WATCHER_PIDFILE")"; }; then + # shellcheck disable=SC2016 # the watcher's script, expanded by its own sh + setsid sh -c ' + pid=$1 config=$2 level=$3 log=$4 pidfile=$5 + alive() { [ -r "/proc/$1/status" ] && ! grep -q "^State:[[:space:]]*Z" "/proc/$1/status" 2>/dev/null; } + trap "kill -9 \$! 2>/dev/null; wait; exit 0" TERM + while :; do + while alive "$pid"; do sleep 1; done + echo "start-docker: containerd (pid $pid) exited; starting it again" >>"$log" + containerd --config "$config" --log-level "$level" >>"$log" 2>&1 & + pid=$! + echo "$pid" >"$pidfile" + sleep 1 + done' start-docker-containerd-watcher "$containerd_pid" "$CONTAINERD_CONFIG" "$level" "$CONTAINERD_LOG" "$CONTAINERD_PIDFILE" >/dev/null 2>&1 9>&- & + watcher=$! + started="$watcher $started" + echo "$watcher" >"$WATCHER_PIDFILE" + echo "containerd ($containerd_pid) is up; its watcher ($watcher) starts it again should it exit" +fi -echo "Docker daemon failed to start" -tail -n 200 /var/log/dockerd.log || true -exit 1 +echo "Starting dockerd ${FLAGS} ..." +# shellcheck disable=SC2086 # FLAGS is a list of flags +dockerd --containerd="$CONTAINERD_SOCK" ${FLAGS} >>"$DOCKERD_LOG" 2>&1 9>&- & +dockerd_pid=$! +started="$dockerd_pid $started" +echo "$dockerd_pid" >"$DOCKERD_STARTED" +if ! await dockerd "$dockerd_pid" "$DOCKERD_LOG" docker -H "unix://$DOCKER_SOCK" info; then + fail_start "Docker daemon failed to start" +fi +echo "Docker daemon is ready" diff --git a/keploy-ci/start-docker-test.sh b/keploy-ci/start-docker-test.sh new file mode 100755 index 0000000..870bcec --- /dev/null +++ b/keploy-ci/start-docker-test.sh @@ -0,0 +1,377 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2016 # each case is a script for the container's bash, quoted whole +# Tests for start-docker, each case in a fresh privileged container of IMAGE +# with this checkout's start-docker. CI builds the keploy-ci image of the +# commit and runs them on it (docker-build.yml); by default, the published +# base: +# +# bash keploy-ci/start-docker-test.sh [IMAGE] +# +# Stand-ins (shims first on PATH) make containerd slow, dead or silent, and +# dockerd slow or wedged in its init, as a starved host does. +set -u +IMAGE="${1:-ghcr.io/keploy/keploy-ci:1.2.40}" +HERE="$(cd "$(dirname "$0")" && pwd)" +failures=0 + +# run CASE SCRIPT: runs SCRIPT (bash) in a fresh container; its output is in +# $out and its status in $rc. +# A case that hangs is ended after CASE_GUARD seconds (900 by default; a hang +# guard, so that a start that blocks fails its case instead of the job): rc +# is then 124, and its container is removed. The container runs detached and +# is waited for (docker wait), the wait under the guard. A guard around +# `docker run` would signal only the client, which hands the signal on to the +# container's PID 1, a bash that does not die of it, and waits on. +# A container is known by the id docker run gave it, never by its name, and +# is removed with its volume (the image's /var/lib/docker); the one running is +# removed too when the run ends otherwise (EXIT, INT, TERM). The wait runs in +# the background and is waited for with the wait builtin, which a trapped +# signal interrupts at once (a command substitution would hold it until the +# case ended). +cid="" +WAITED=$(mktemp) +drop() { + [ -z "$cid" ] || docker rm -f -v "$cid" >/dev/null 2>&1 || true + cid="" +} +trap 'drop; rm -f "$WAITED"' EXIT +trap 'drop; trap - INT; kill -INT $$' INT +trap 'drop; exit 143' TERM +run() { + name="start-docker-test-$1-$$" + if ! cid=$(docker run -d --privileged --name "$name" \ + -v "$HERE/start-docker:/usr/local/bin/start-docker:ro" \ + "$IMAGE" bash -c "$2"); then + out="the case's container did not start: $cid" rc=125 cid="" + return + fi + timeout -k 10 "${CASE_GUARD:-900}" docker wait "$cid" >"$WAITED" 2>&1 & + wait $! + w=$? + rc=$(cat "$WAITED") + out=$(docker logs "$cid" 2>&1) + case $w in + 0) ;; + 124 | 137) out="$out +the case guard (${CASE_GUARD:-900} s) ended it" rc=124 ;; + *) out="$out +docker wait failed ($w): $rc" rc=125 ;; + esac + drop +} +pass() { echo "ok - $1"; } +# fail prints the case's last 40 lines that are not a daemon's own (level=) +# lines, which would bury what start-docker said. +fail() { echo "FAIL - $1"; echo "$out" | grep -v 'level=' | tail -n 40 | sed 's/^/ /'; failures=$((failures + 1)); } + +# A containerd that loads for N seconds, a line a second, as a starved one +# loads its plugins, before it is the real one. +slow_shim='mkdir -p /opt/shim && cat >/opt/shim/containerd <<"EOF" +#!/bin/sh +for i in $(seq 1 "${SLOW:-20}"); do echo "loading plugin $i (slow host)"; sleep 1; done +exec /usr/bin/containerd "$@" +EOF +chmod +x /opt/shim/containerd' + +# The guard itself: a case that hangs is ended, and its container removed. +CASE_GUARD=5 run "guard" 'sleep 600' +if [ "$rc" = 124 ] && ! docker container inspect "start-docker-test-guard-$$" >/dev/null 2>&1; then + pass "the case guard ends a case that hangs, and removes its container" +else fail "the case guard ends a case that hangs, and removes its container (rc=$rc)"; fi + +# Settings that are not whole numbers, 1 or more, and DOCKERD_FLAGS that move +# what start-docker fixes (its root, socket and pidfile), are refused (2), +# before anything is started. +run "settings" 'for s in START_DOCKER_SILENCE_CHECKS=0 START_DOCKER_PROBE_SECONDS=x START_DOCKER_NOTICE_LOOKS=-1 DOCKERD_FLAGS=--data-root=/x "DOCKERD_FLAGS=--debug -H tcp://0.0.0.0:2375" DOCKERD_FLAGS=-Htcp://0.0.0.0:2375 "DOCKERD_FLAGS=-DH tcp://0.0.0.0:2375" DOCKERD_FLAGS=-p/x.pid DOCKERD_FLAGS=--config-file=/x.json; do + env "$s" start-docker; echo "$s: rc=$?" +done +mkdir -p /etc/docker && printf "{\"data-root\": \"/x\"}\n" >/etc/docker/daemon.json && start-docker; echo "daemon.json: rc=$?"; rm -f /etc/docker/daemon.json +DOCKERD_FLAGS="-ldebug --dns 1.1.1.1" START_DOCKER_SILENCE_CHECKS=1 start-docker >/tmp/fine.out 2>&1; grep -q "sets" /tmp/fine.out && echo "a fine flag refused" +pgrep -x containerd >/dev/null && echo "a start began" || echo "nothing started"; pkill -9 -x dockerd; pkill -9 -x containerd; true' +if grep -q "START_DOCKER_SILENCE_CHECKS=0: rc=2" <<<"$out" && grep -q "START_DOCKER_PROBE_SECONDS=x: rc=2" <<<"$out" \ + && grep -q "START_DOCKER_NOTICE_LOOKS=-1: rc=2" <<<"$out" && grep -q "whole numbers, 1 or more" <<<"$out" \ + && grep -q "DOCKERD_FLAGS=--data-root=/x: rc=2" <<<"$out" && grep -q "DOCKERD_FLAGS sets --data-root, which start-docker fixes" <<<"$out" \ + && grep -q "DOCKERD_FLAGS=--debug -H tcp://0.0.0.0:2375: rc=2" <<<"$out" && grep -q "DOCKERD_FLAGS=-Htcp://0.0.0.0:2375: rc=2" <<<"$out" \ + && grep -q "DOCKERD_FLAGS=-DH tcp://0.0.0.0:2375: rc=2" <<<"$out" && grep -q "DOCKERD_FLAGS=-p/x.pid: rc=2" <<<"$out" \ + && grep -q "DOCKERD_FLAGS=--config-file=/x.json: rc=2" <<<"$out" && grep -q "daemon.json: rc=2" <<<"$out" \ + && grep -q "/etc/docker/daemon.json sets \"data-root\"" <<<"$out" && ! grep -q "a fine flag refused" <<<"$out"; then + pass "settings that are not whole numbers, 1 or more, and DOCKERD_FLAGS that move what start-docker fixes, are refused before anything starts" +else fail "settings that are not whole numbers, 1 or more, and DOCKERD_FLAGS that move what start-docker fixes, are refused before anything starts"; fi + +# Started, it builds and runs a container: an image of the static tini the +# engine ships (/usr/libexec/docker/docker-init), so nothing is pulled. +# DOCKERD_FLAGS reach dockerd (the lanes' registry mirror), containerd and +# its watcher each lead a session of their own, and containerd logs at +# dockerd's level (--debug). +run "starts" 'set -o pipefail; DOCKERD_FLAGS="--registry-mirror=http://127.0.0.1:5999 --debug" start-docker | tee /tmp/start.out && docker info --format "{{.ServerVersion}}" && pgrep -a dockerd && +docker info --format "mirrors {{.RegistryConfig.Mirrors}}" && +c=$(pidof containerd) && [ "$(ps -o sid= -p "$c" | tr -d " ")" = "$c" ] && echo "containerd leads its own session" && +w=$(sed -n "s/.*its watcher (\([0-9]*\)).*/\1/p" /tmp/start.out) && [ -n "$w" ] && [ "$(ps -o sid= -p "$w" | tr -d " ")" = "$w" ] && echo "the watcher leads its own session" && +grep -q "level=debug" /var/log/containerd.log && echo "containerd logs at debug" && +mkdir -p /tmp/img && cp /usr/libexec/docker/docker-init /tmp/img/tini && +printf "FROM scratch\nCOPY tini /tini\nENTRYPOINT [\"/tini\", \"--version\"]\n" >/tmp/img/Dockerfile && +docker build -q -t start-docker-test /tmp/img >/dev/null && docker run --rm start-docker-test' +if [ "$rc" = 0 ] && grep -q -- '--containerd=/var/run/docker/containerd/containerd.sock' <<<"$out" && grep -q "tini version" <<<"$out" \ + && grep -q "mirrors.*127.0.0.1:5999" <<<"$out" && grep -q "containerd leads its own session" <<<"$out" \ + && grep -q "the watcher leads its own session" <<<"$out" && grep -q "containerd logs at debug" <<<"$out"; then + pass "it starts containerd (in its own session, at dockerd's --debug, with a watcher in a session of its own) and then dockerd on it with DOCKERD_FLAGS, returns once docker answers, and builds and runs a container" +else fail "it starts containerd (in its own session, at dockerd's --debug, with a watcher in a session of its own) and then dockerd on it with DOCKERD_FLAGS, returns once docker answers, and builds and runs a container"; fi + +run "already" 'DOCKER_HOST=tcp://127.0.0.1:1 DOCKERD_FLAGS=--log-level=debug start-docker >/dev/null && grep -q "level=debug" /var/log/containerd.log && echo "containerd logs at debug" && start-docker' +if [ "$rc" = 0 ] && grep -q "dockerd ([0-9]*) is already running; waiting for it to answer" <<<"$out" && grep -q "Docker daemon is ready" <<<"$out" \ + && grep -q "containerd logs at debug" <<<"$out"; then + pass "a second start finds the daemon running (and containerd logs at dockerd's --log-level)" +else fail "a second start finds the daemon running (and containerd logs at dockerd's --log-level)"; fi + +# The failure it fixes: dockerd starting its own containerd gives it 15 s. +run "managed" "$slow_shim"' +PATH=/opt/shim:$PATH dockerd >/var/log/d.log 2>&1 & +d=$!; until grep -q "timeout waiting for containerd" /var/log/d.log || docker info >/dev/null 2>&1 || ! kill -0 "$d" 2>/dev/null; do sleep 1; done +cat /var/log/d.log | grep -E "timeout waiting for containerd|API listen" | head -3' +if grep -q "timeout waiting for containerd to start" <<<"$out"; then + pass "(the failure fixed) dockerd quits when the containerd it starts takes over 15 s" +else fail "(the failure fixed) dockerd quits when the containerd it starts takes over 15 s"; fi + +# Slow and logging: waited for on its progress, with 60 looks of silence +# allowed (more than a real daemon here keeps silent, 15 s on a starved +# runner) and 75 s of loading, so it is the growing log that keeps the wait +# going. +run "slow" "$slow_shim"' +SLOW=75 PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=60 start-docker && docker info --format "{{.ServerVersion}}"' +if [ "$rc" = 0 ] && grep -q "Docker daemon is ready" <<<"$out"; then + pass "a containerd that takes 75 s, logging as it loads, is waited for on its progress past 60 looks" +else fail "a containerd that takes 75 s, logging as it loads, is waited for on its progress past 60 looks"; fi + +# Slow and silent, as containerd sat in its bolt store's open (11931): +# waited for while within its silence checks. +run "slow-silent" 'mkdir -p /opt/shim && printf "#!/bin/sh\nsleep 20\nexec /usr/bin/containerd \"\$@\"\n" >/opt/shim/containerd && chmod +x /opt/shim/containerd +PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=60 start-docker' +if [ "$rc" = 0 ] && grep -q "Docker daemon is ready" <<<"$out"; then + pass "a containerd silent for 20 s, then up, is waited for within its silence checks" +else fail "a containerd silent for 20 s, then up, is waited for within its silence checks"; fi + +run "dies" 'mkdir -p /opt/shim && printf "#!/bin/sh\necho cannot start; exit 1\n" >/opt/shim/containerd && chmod +x /opt/shim/containerd +PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=30 start-docker' +if [ "$rc" = 1 ] && grep -q "containerd exited before it answered" <<<"$out" && grep -q "cannot start" <<<"$out"; then + pass "a containerd that exits fails the start at once, with its log" +else fail "a containerd that exits fails the start at once, with its log"; fi + +run "silent" 'mkdir -p /opt/shim && printf "#!/bin/sh\nexec sleep 1000000\n" >/opt/shim/containerd && chmod +x /opt/shim/containerd +PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=3 start-docker' +if [ "$rc" = 1 ] && grep -q "containerd has not answered, and its log has not grown in 3 looks" <<<"$out"; then + pass "a containerd that neither answers nor logs fails the start after its silence checks" +else fail "a containerd that neither answers nor logs fails the start after its silence checks"; fi + +# A start that fails runs under a subreaper that reaps nothing (python3, +# PR_SET_CHILD_SUBREAPER): whatever it leaves, running or a zombie, is then +# the subreaper's child, and is listed. +reaper='mkdir -p /opt && cat >/opt/reaper.py <<"PY" +import ctypes, os, subprocess, sys +if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0: + sys.exit("prctl(PR_SET_CHILD_SUBREAPER): errno %d: nothing left behind could be seen" % ctypes.get_errno()) +print("first rc=%d" % subprocess.run(sys.argv[1:]).returncode, flush=True) +left = [] +for p in filter(str.isdigit, os.listdir("/proc")): + try: + f = dict(l.split(":", 1) for l in open("/proc/%s/status" % p).read().splitlines() if ":" in l) + except OSError: + continue + if f["PPid"].strip() == str(os.getpid()): + left.append("%s %s (%s)" % (p, f["Name"].strip(), f["State"].strip())) +print("left by the failed start: %s" % (", ".join(left) or "nothing"), flush=True) +PY' + +# A dockerd that fails: said at once, what the start began is stopped and +# reaped, and the next start begins clean, with one containerd, both logs +# appended to. +run "dockerd-fails" "$reaper"' +DOCKERD_FLAGS="--no-such-flag -l debug" python3 /opt/reaper.py start-docker +grep -q "level=debug" /var/log/containerd.log && echo "containerd logs at debug (-l debug)" +start-docker && echo "containerds: $(pgrep -cx containerd)" +echo "containerd starts in its log: $(grep -c "starting containerd" /var/log/containerd.log)" +grep -q "no-such-flag" /var/log/dockerd.log && echo "the failed dockerd is still in its log"' +if grep -q "first rc=1" <<<"$out" && grep -q "dockerd exited before it answered" <<<"$out" && grep -q "left by the failed start: nothing" <<<"$out" \ + && grep -q "Docker daemon is ready" <<<"$out" && grep -q "containerds: 1" <<<"$out" \ + && grep -q "containerd starts in its log: 2" <<<"$out" && grep -q "the failed dockerd is still in its log" <<<"$out" \ + && grep -q "containerd logs at debug (-l debug)" <<<"$out"; then + pass "a dockerd that exits fails the start at once, what the start began is stopped and reaped, and the next start begins clean, its logs appended to" +else fail "a dockerd that exits fails the start at once, what the start began is stopped and reaped, and the next start begins clean, its logs appended to"; fi + +# A start that fails after its watcher started containerd again leaves +# nothing either: the watcher stops and reaps the containerd it started. +run "fails-after-a-restart" "$reaper"' +mkdir -p /opt/shim && cat >/opt/shim/dockerd <<"EOF" +#!/bin/sh +kill -9 "$(pidof containerd)" +until grep -q "exited; starting it again" /var/log/containerd.log; do sleep 0.1; done +echo "dockerd stand-in: containerd was started again; exiting" +exit 1 +EOF +chmod +x /opt/shim/dockerd +DOCKERD_FLAGS="--log-level debug" PATH=/opt/shim:$PATH python3 /opt/reaper.py start-docker +grep -q "level=debug" /var/log/containerd.log && echo "containerd logs at debug (--log-level debug)"' +if grep -q "first rc=1" <<<"$out" && grep -q "containerd was started again; exiting" <<<"$out" && grep -q "left by the failed start: nothing" <<<"$out" \ + && grep -q "containerd logs at debug (--log-level debug)" <<<"$out"; then + pass "a start that fails after containerd was started again leaves nothing: the watcher stops and reaps the containerd it started" +else fail "a start that fails after containerd was started again leaves nothing: the watcher stops and reaps the containerd it started"; fi + +# dockerd listens before its init and answers after: a stand-in stops the +# real one once its socket is there, so docker info blocks, as on a slow +# init. Saying what it does (its log grows), it is waited for, a blocked +# probe being no answer that look; saying nothing, it fails, said. +dockerd_shim='mkdir -p /opt/shim && cat >/opt/shim/dockerd <<"EOF" +#!/bin/sh +/usr/bin/dockerd "$@" & d=$! +until [ -S /var/run/docker.sock ]; do sleep 0.05; done +kill -STOP $d +if [ -n "${PROGRESS:-}" ]; then + for i in $(seq 1 "${STEPS:-15}"); do echo "init step $i"; sleep 1; done + kill -CONT $d +fi +wait $d +EOF +chmod +x /opt/shim/dockerd' +run "dockerd-slow-init" "$dockerd_shim"' +PROGRESS=1 STEPS=75 PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=60 start-docker' +if [ "$rc" = 0 ] && grep -q "Docker daemon is ready" <<<"$out"; then + pass "a dockerd whose init is slow but says what it does is waited for, its blocked probes no answer, not the end" +else fail "a dockerd whose init is slow but says what it does is waited for, its blocked probes no answer, not the end"; fi +run "dockerd-wedged" "$dockerd_shim"' +PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=4 START_DOCKER_NOTICE_LOOKS=2 start-docker' +if [ "$rc" = 1 ] && grep -q "dockerd has not answered, and its log has not grown in 2 looks; still waiting" <<<"$out" \ + && grep -q "dockerd has not answered, and its log has not grown in 4 looks$" <<<"$out"; then + pass "a dockerd wedged in its init, its probes blocked and its log still, is said to stall as it goes on, and fails the start, said" +else fail "a dockerd wedged in its init, its probes blocked and its log still, is said to stall as it goes on, and fails the start, said"; fi + +# A probe that takes long is waited for, not cut: a docker that answers in 8 s +# (as docker info does while it waits on a starved containerd), with 30 looks +# of silence allowed and the bound on one-shot probes at 2 s. +run "slow-probe" 'mkdir -p /opt/shim && printf "#!/bin/sh\n[ \"\$1\" != info ] || sleep 8\nexec /usr/bin/docker \"\$@\"\n" >/opt/shim/docker && chmod +x /opt/shim/docker +PATH=/opt/shim:$PATH START_DOCKER_SILENCE_CHECKS=60 START_DOCKER_PROBE_SECONDS=2 start-docker' +if [ "$rc" = 0 ] && grep -q "Docker daemon is ready" <<<"$out"; then + pass "a probe that takes 8 s to answer is waited for, not cut short, and its answer taken" +else fail "a probe that takes 8 s to answer is waited for, not cut short, and its answer taken"; fi + +# A socket no pidfile names gets one look, bounded +# (START_DOCKER_PROBE_SECONDS). One that refuses (nothing listens: a daemon +# gone without removing it) is taken over: the start goes on, and its own +# dockerd binds the socket. One that listens and does not answer (a daemon +# start-docker knows nothing of, slow or wedged) fails the start, said, and +# nothing is started over it. +run "dead-socket" 'python3 -c "import socket; socket.socket(socket.AF_UNIX).bind(\"/var/run/docker.sock\")" +START_DOCKER_PROBE_SECONDS=2 start-docker' +if [ "$rc" = 0 ] && grep -q "Starting containerd" <<<"$out" && grep -q "Docker daemon is ready" <<<"$out"; then + pass "a socket no pidfile names that refuses is taken over, and the start goes on" +else fail "a socket no pidfile names that refuses is taken over, and the start goes on"; fi +run "mute-socket" 'python3 -c "import socket, time; s = socket.socket(socket.AF_UNIX); s.bind(\"/var/run/docker.sock\"); s.listen(); time.sleep(600)" & until [ -S /var/run/docker.sock ]; do sleep 0.05; done +START_DOCKER_PROBE_SECONDS=2 start-docker; echo "rc=$?" +pgrep -x containerd >/dev/null && echo "a containerd was started" || echo "nothing started"' +if grep -q "rc=1" <<<"$out" && grep -q "listens on /var/run/docker.sock and did not answer in 2s: not starting a second dockerd" <<<"$out" \ + && grep -q "nothing started" <<<"$out"; then + pass "a socket no pidfile names that listens and does not answer fails the start, said, and nothing is started over it" +else fail "a socket no pidfile names that listens and does not answer fails the start, said, and nothing is started over it"; fi + +# A start killed after it started dockerd, before dockerd wrote its own +# pidfile (dockerd slow to begin, as a starved one is): the next start finds +# it by the pid start-docker wrote, and waits for it. +run "killed-before-dockerd-pidfile" 'mkdir -p /opt/shim && printf "#!/bin/sh\nsleep 5\nexec /usr/bin/dockerd \"\$@\"\n" >/opt/shim/dockerd && chmod +x /opt/shim/dockerd +PATH=/opt/shim:$PATH start-docker >/dev/null 2>&1 & first=$! +until [ -s /var/run/docker/start-docker-dockerd.pid ]; do sleep 0.05; done +kill -9 "$first"; wait "$first" 2>/dev/null +[ -s /var/run/docker.pid ] || echo "dockerd has written no pidfile yet" +PATH=/opt/shim:$PATH start-docker; echo "second rc=$?"' +if grep -q "dockerd has written no pidfile yet" <<<"$out" && grep -q "dockerd ([0-9]*) is already running; waiting for it to answer" <<<"$out" \ + && grep -q "second rc=0" <<<"$out"; then + pass "a start killed before dockerd wrote its pidfile leaves its pid: the next start waits for that dockerd" +else fail "a start killed before dockerd wrote its pidfile leaves its pid: the next start waits for that dockerd"; fi + +# A watcher pidfile that names some other sh (a pid reused) is no watcher: +# the start gives its containerd one of its own. +run "stale-watcher-pidfile" 'mkdir -p /var/run/docker/containerd && { sh -c "sleep 600; :" & echo "$!" >/var/run/docker/containerd/watcher.pid; } && start-docker' +if [ "$rc" = 0 ] && grep -q "its watcher ([0-9]*) starts it again should it exit" <<<"$out"; then + pass "a watcher pidfile that names another sh is no watcher: the start gives containerd its own" +else fail "a watcher pidfile that names another sh is no watcher: the start gives containerd its own"; fi + +# A start while containerd's watcher is starting it again (held, here, by +# SIGSTOP, between containerd's death and its start) waits for the watcher, +# then reuses the containerd it starts: no second containerd on the root. +run "watcher-restarting" 'start-docker >/tmp/first.out && d=$(pidof dockerd) && kill "$d" && while kill -0 "$d" 2>/dev/null; do sleep 0.2; done && +w=$(cat /var/run/docker/containerd/watcher.pid) && kill -STOP "$w" && kill -9 "$(pidof containerd)" && while pidof containerd >/dev/null; do sleep 0.1; done && +{ start-docker >/tmp/second.out 2>&1 & s=$!; } && +until grep -q "watcher ($w) is starting it again; waiting for it" /tmp/second.out; do sleep 0.1; done && kill -CONT "$w" && +wait "$s"; echo "second rc=$?"; cat /tmp/second.out +echo "containerds: $(pgrep -cx containerd)"' +if grep -q "is starting it again; waiting for it" <<<"$out" && grep -q "second rc=0" <<<"$out" && grep -q "already answers on" <<<"$out" \ + && grep -q "containerds: 1" <<<"$out"; then + pass "a start while containerd's watcher is starting it again waits for the watcher, and reuses what it starts" +else fail "a start while containerd's watcher is starting it again waits for the watcher, and reuses what it starts"; fi + +# A second start while the first's containerd is still starting (a lock): +# it waits for the first, and then finds its dockerd, which it waits on +# until it answers (its pidfile). One containerd, one watcher, both ready. +run "starting-twice" "$slow_shim"' +PATH=/opt/shim:$PATH start-docker >/tmp/first.out 2>&1 & first=$! +until grep -q "loading plugin" /var/log/containerd.log 2>/dev/null; do sleep 0.05; done +PATH=/opt/shim:$PATH start-docker; echo "second rc=$?" +wait "$first"; echo "first rc=$?"; cat /tmp/first.out +echo "containerds: $(pgrep -cx containerd)" +echo "watchers: $(for p in /proc/[0-9]*; do tr "\0" "\n" <"$p/cmdline" 2>/dev/null | grep -qx start-docker-containerd-watcher && echo "$p"; done | wc -l)"' +if grep -q "another start-docker is running; waiting for it" <<<"$out" && grep -q "dockerd ([0-9]*) is already running; waiting for it to answer" <<<"$out" \ + && grep -q "second rc=0" <<<"$out" && grep -q "first rc=0" <<<"$out" && grep -q "containerds: 1" <<<"$out" && grep -q "watchers: 1" <<<"$out"; then + pass "a start while another is still starting waits for it, then for its dockerd, and starts nothing twice" +else fail "a start while another is still starting waits for it, then for its dockerd, and starts nothing twice"; fi + +# A start killed while its containerd or its dockerd is starting leaves them +# running: the next start waits for them (their pidfiles) until they answer, +# and starts no second one on the same root. +run "killed-in-containerd" "$slow_shim"' +PATH=/opt/shim:$PATH start-docker >/dev/null 2>&1 & first=$! +until grep -q "loading plugin" /var/log/containerd.log 2>/dev/null; do sleep 0.05; done +kill -9 "$first"; wait "$first" 2>/dev/null +PATH=/opt/shim:$PATH start-docker; echo "second rc=$?" +echo "containerds: $(pgrep -cx containerd)"' +if grep -q "containerd ([0-9]*) is already running; waiting for it to answer" <<<"$out" && grep -q "second rc=0" <<<"$out" \ + && grep -q "containerds: 1" <<<"$out" && grep -q "its watcher ([0-9]*) starts it again" <<<"$out"; then + pass "a start after one killed while containerd was starting waits for that containerd, and gives it a watcher" +else fail "a start after one killed while containerd was starting waits for that containerd, and gives it a watcher"; fi +run "killed-in-dockerd" "$dockerd_shim"' +PROGRESS=1 PATH=/opt/shim:$PATH start-docker >/dev/null 2>&1 & first=$! +until [ -S /var/run/docker.sock ]; do sleep 0.05; done +kill -9 "$first"; wait "$first" 2>/dev/null +PATH=/opt/shim:$PATH start-docker; echo "second rc=$?"' +if grep -q "dockerd ([0-9]*) is already running; waiting for it to answer" <<<"$out" && grep -q "second rc=0" <<<"$out" \ + && grep -q "Docker daemon is ready" <<<"$out"; then + pass "a start after one killed while dockerd was in its init waits for that dockerd until it answers" +else fail "a start after one killed while dockerd was in its init waits for that dockerd until it answers"; fi + +# containerd killed under a running dockerd is started again, and said; then +# dockerd says it is back on it (at --debug, its event stream processed +# again: its containerd client is serving), and docker runs a container. +events='grep -c "msg=\"processing event stream\" module=libcontainerd namespace=moby$" /var/log/dockerd.log' +run "containerd-restarted" 'DOCKERD_FLAGS=--debug=1 start-docker >/dev/null && grep -q "level=debug" /var/log/containerd.log && echo "containerd logs at debug (--debug=1)" && +mkdir -p /tmp/img && cp /usr/libexec/docker/docker-init /tmp/img/tini && +printf "FROM scratch\nCOPY tini /tini\nENTRYPOINT [\"/tini\", \"--version\"]\n" >/tmp/img/Dockerfile && +docker build -q -t t2 /tmp/img >/dev/null && n=$('"$events"') && +kill -9 "$(pidof containerd)" && +until grep -q "exited; starting it again" /var/log/containerd.log; do sleep 0.2; done && echo "containerd is started again" && +until [ "$('"$events"')" -gt "$n" ]; do sleep 0.2; done && echo "dockerd is back on containerd" && +docker run --rm t2' +if [ "$rc" = 0 ] && grep -q "containerd is started again" <<<"$out" && grep -q "dockerd is back on containerd" <<<"$out" && grep -q "tini version" <<<"$out" \ + && grep -q "containerd logs at debug (--debug=1)" <<<"$out"; then + pass "a containerd that dies under dockerd is started again, said in its log, dockerd goes back on it, and docker runs a container" +else fail "a containerd that dies under dockerd is started again, said in its log, dockerd goes back on it, and docker runs a container"; fi + +# A start after dockerd stopped reuses the containerd that answers, and +# appends to dockerd's log. containerd is at info by default. +run "rerun" 'start-docker >/dev/null && head -n 1 /var/log/dockerd.log >/tmp/first-line && d=$(pidof dockerd) && kill "$d" && while kill -0 "$d" 2>/dev/null; do sleep 0.2; done && +start-docker && echo "containerds: $(pgrep -cx containerd)" +echo "watchers: $(for p in /proc/[0-9]*; do tr "\0" "\n" <"$p/cmdline" 2>/dev/null | grep -qx start-docker-containerd-watcher && echo "$p"; done | wc -l)" +[ "$(head -n 1 /var/log/dockerd.log)" = "$(cat /tmp/first-line)" ] && echo "the first dockerd is still in its log" +grep -q "level=debug" /var/log/containerd.log || echo "containerd logs at info"' +if grep -q "containerd ([0-9]*) already answers on" <<<"$out" && grep -q "containerds: 1" <<<"$out" && grep -q "watchers: 1" <<<"$out" \ + && grep -q "the first dockerd is still in its log" <<<"$out" && grep -q "containerd logs at info" <<<"$out"; then + pass "a start after dockerd stopped reuses the containerd that answers and its watcher, starts no second of either, and appends to dockerd's log" +else fail "a start after dockerd stopped reuses the containerd that answers and its watcher, starts no second of either, and appends to dockerd's log"; fi + +if [ "$failures" -gt 0 ]; then echo "$failures failure(s)"; exit 1; fi +echo "all start-docker cases pass"