diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aafae61..ba14465 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,6 +83,10 @@ jobs: persist-credentials: false - name: Each unit the README enables has an [Install] section run: sh test/units/install-sections.sh + - name: CI runs every install command of the README + # The README marks its install blocks; the install-loop job runs + # them as written. An install command outside a marked block fails. + run: sh test/units/readme-block.sh --check - name: Check the example scripts and units run: | set -eu @@ -128,6 +132,26 @@ jobs: # units as the README says, and each must be enabled (issue #20). run: sudo sh test/units/first-run.sh "${RUNNER_TEMP}/pullgate" + install-loop: + name: README install and a full loop under the units + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: go.mod + - name: Build + run: CGO_ENABLED=0 go build -trimpath -o "${RUNNER_TEMP}/pullgate" ./cmd/pullgate + - name: The README install, then a good commit and a failed one + # The install commands come from README.md itself. Root and the + # listener are different users, under the real units. A tap on a + # fake Telegram applies a commit; a failing commit rolls back + # (DESIGN.md section 14, issue #27). + run: sudo sh test/units/full-loop.sh "${RUNNER_TEMP}/pullgate" + lint: runs-on: ubuntu-latest steps: @@ -150,7 +174,7 @@ jobs: echo "${SHELLCHECK_SHA256} $dir/shellcheck.tar.xz" | sha256sum --strict -c - tar -xJf "$dir/shellcheck.tar.xz" -C "$dir" "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --version - "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh install.sh test/units/first-run.sh test/units/install-sections.sh + "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh install.sh test/units/*.sh - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: go.mod diff --git a/.github/workflows/install-release.yml b/.github/workflows/install-release.yml new file mode 100644 index 0000000..103b789 --- /dev/null +++ b/.github/workflows/install-release.yml @@ -0,0 +1,74 @@ +name: Install a release + +# Installs a published release with its own install.sh, as a user does, +# and runs the README install and a full loop under the units of that +# release (test/units/full-loop.sh, DESIGN.md section 14, issue #27). +# +# The Release workflow calls this after it publishes. A release that the +# Release workflow publishes with GITHUB_TOKEN starts no "release" event, +# so the call is the path that runs. "release: published" covers a release +# published by hand. Run it by hand for any release from v0.3.0 on (older +# tags have no test/units/full-loop.sh). + +on: + release: + types: [published] + workflow_call: + inputs: + version: + description: "The version to install, X.Y.Z" + required: true + type: string + workflow_dispatch: + inputs: + version: + description: "The version to install, X.Y.Z" + required: true + type: string + +permissions: + contents: read + +jobs: + install: + name: install ${{ inputs.version || github.event.release.tag_name }} and a full loop + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + env: + # Through env, never put into the script text. + INPUT_VERSION: ${{ inputs.version }} + RELEASE_TAG: ${{ github.event.release.tag_name }} + steps: + - name: The version + id: version + shell: bash + run: | + set -eu + v="${INPUT_VERSION:-${RELEASE_TAG#v}}" + # The whole string, so a value with a newline does not pass. + if ! [[ "$v" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "::error::The version is not X.Y.Z. Give the version without the v, for example 0.3.0." + exit 1 + fi + echo "version=$v" >>"$GITHUB_OUTPUT" + # The tag of the release: its install.sh, README and units, as a user + # gets them. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/tags/v${{ steps.version.outputs.version }} + persist-credentials: false + - name: install.sh --version, as the README says + # install.sh checks the SHA-256 and the build attestation, and + # installs nothing when a check fails. It needs no GitHub login. + env: + VERSION: ${{ steps.version.outputs.version }} + run: | + set -eu + sudo env -i PATH=/usr/sbin:/usr/bin:/sbin:/bin sh ./install.sh --version "$VERSION" + /usr/local/bin/pullgate --version + - name: The README install and a full loop under the units + env: + VERSION: ${{ steps.version.outputs.version }} + run: sudo sh test/units/full-loop.sh --installed "$VERSION" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ebf4d40..cc7265f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -177,3 +177,15 @@ jobs: env: GH_TOKEN: ${{ github.token }} run: gh release edit "$TAG" --draft=false --repo "$GITHUB_REPOSITORY" + + # Installs the published release with its install.sh and runs a full + # loop (install-release.yml). A failure here does not undo the release: + # it means the release needs a fix release (docs/releasing.md). + install-test: + needs: [release-please, publish] + if: needs.release-please.outputs.release_created == 'true' + permissions: + contents: read + uses: ./.github/workflows/install-release.yml + with: + version: ${{ needs.release-please.outputs.version }} diff --git a/DESIGN.md b/DESIGN.md index 5c2b37b..4aa531b 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -752,6 +752,42 @@ that cannot be enabled, fails CI. Without root, CI also runs checks that each unit the README tells you to enable has an `[Install]` section with `WantedBy=`. +`test/units/full-loop.sh` (CI job `install-loop`, on each pull request +and on `main`) is the path a user takes, as root on the throwaway runner: + +1. It runs the install commands **from README.md itself**: each install + block has a comment line `` (`binary`, + `setup`, `start`) before its `sh` block, and the script runs the block + as written, with `NAME` set to `config-repo`. + `test/units/readme-block.sh --check` fails when a block is missing, or + when an install command (`useradd`, `groupadd`, `install -X`, `chown`, + `chmod`, `cp contrib/systemd`, `systemctl enable`) is in an unmarked + fenced block. So + the README cannot drift from what CI runs. +2. A local bare repo is the remote. A fake Telegram on `127.0.0.1` + (`test/units/faketg.py --tap`) records each message and taps Apply on + each question as the owner. +3. A good commit: `pullgate run` under `pullgate-run@.service` asks (with + `check` and `summary` as the check user), the listener (user + `pullgate`) sends the question and writes the approval, + `pullgate-apply@.service` applies it, and `pullgate status` shows it as + applied. +4. It restarts the listener and checks that it polls again. +5. A commit whose apply fails: the rollback runs, and its notice reaches + the fake Telegram. +6. Each unit of the README `start` block is `enabled` and `active`. + +After each loop, the outbox must be empty and the listener must have +logged no error and no warning. So a message that root writes and the +listener cannot read fails the job. + +The workflow **Install a release** (`install-release.yml`) runs the same +script against a published release: it installs the release with the +tag's own `install.sh --version X.Y.Z`, then runs +`full-loop.sh --installed X.Y.Z` with the README and units of the tag. +The Release workflow calls it after it publishes; it also runs on +`release: published` and by hand. + ## 15. Not in version 0.1 - Self-update of pullgate. `install.sh` updates it only when root runs diff --git a/README.md b/README.md index 16487dc..4141884 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,12 @@ what it trusts: [DESIGN.md](DESIGN.md) section 16 and **By hand.** Download a release and check it as [docs/releasing.md](docs/releasing.md) says (section "Verify a download"). -Then, as root: `install -m 0755 pullgate /usr/local/bin/pullgate`. +Then, as root: + + +```sh +install -m 0755 pullgate /usr/local/bin/pullgate +``` **From the source**, with Go 1.26 or newer. Then install it as above: @@ -83,6 +88,7 @@ CGO_ENABLED=0 go build -trimpath -o pullgate ./cmd/pullgate Then, as root, make the users and the directories, and copy the units from [contrib/systemd/](contrib/systemd/): + ```sh useradd --system --no-create-home --shell /usr/sbin/nologin pullgate # One check user per instance, never the listener user: @@ -106,6 +112,7 @@ cp contrib/systemd/* /etc/systemd/system/ the examples below. 6. Start it: + ```sh systemctl daemon-reload systemctl enable --now pullgate-listen.service @@ -305,6 +312,11 @@ golangci-lint run --build-tags e2e go test -race -tags e2e ./test/e2e/... ``` +CI runs the install commands of this README as written: each install +block above has an `` comment before it, and +`test/units/full-loop.sh` runs those blocks as root, then a full loop +under the units. Keep that comment when you change a block. + The rules for changes are in [AGENTS.md](AGENTS.md). ## License diff --git a/docs/releasing.md b/docs/releasing.md index 05d47c3..2106ed1 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -39,6 +39,16 @@ workflow checks it. build provenance attestation (`pullgate_.sigstore.json`), checks the tarballs with it, uploads everything to the draft release and publishes it. The release is never public without its files. +6. The `install-test` job runs the **Install a release** workflow + (`install-release.yml`): on a fresh runner, it installs the release + with its own `install.sh --version`, runs the README install commands + of the tag, and a full loop under the units (`test/units/full-loop.sh`). + A failure here does not undo the release: fix it in a `fix:` pull + request and make a new release. You can also start this workflow by + hand, with a version from 0.3.0 on. The Release run keeps its + concurrency group until this job ends (up to 20 minutes): do not merge + other pull requests in that time, so no Release run waits and gets + cancelled. If `build` or `publish` fails, the release stays a draft. Fix the problem in a new `fix:` pull request and merge the next release pull request. diff --git a/test/units/faketg.py b/test/units/faketg.py index fbe1039..a91acfa 100644 --- a/test/units/faketg.py +++ b/test/units/faketg.py @@ -1,38 +1,96 @@ #!/usr/bin/env python3 -"""A tiny fake Telegram Bot API for test/units/first-run.sh. +"""A tiny fake Telegram Bot API for the tests in test/units/. -It listens on 127.0.0.1:PORT. It answers every method with "ok", gives no -updates, and appends the name of each method it gets to LOG, one per line. -It never prints a request, so the test token never shows. +It listens on 127.0.0.1:PORT. It appends the name of each method it gets +to LOG, one per line. It never prints a request path, so the test token +never shows. -Usage: python3 -I faketg.py PORT LOG +Without --tap it answers every method with "ok" and gives no updates +(first-run.sh). + +With --tap TEXTS (full-loop.sh) it also: +- appends the text of each sendMessage to TEXTS, as one JSON string per + line, so a test can wait for "Applied" or "Rolled back"; +- taps the Apply button of each question it gets, two seconds later, as + the owner (user 4242) in the chat 4242, through getUpdates. + +Usage: python3 -I faketg.py PORT LOG [--tap TEXTS] """ import json import sys +import threading import time from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer PORT = int(sys.argv[1]) LOG = sys.argv[2] +TEXTS = None +if len(sys.argv) == 5 and sys.argv[3] == "--tap": + TEXTS = sys.argv[4] +elif len(sys.argv) != 3: + sys.exit("usage: faketg.py PORT LOG [--tap TEXTS]") CHAT = 4242 +OWNER = 4242 +# The listener saves a question after sendMessage returns. A tap that came +# before that would find no question, so the tap waits a little. +TAP_DELAY = 2.0 +lock = threading.Lock() +state = {"next_msg": 1, "next_update": 1, "updates": []} -class Handler(BaseHTTPRequestHandler): - next_id = 1 +def apply_button(req): + """The callback data of the Apply button of a question, or None.""" + rows = (req.get("reply_markup") or {}).get("inline_keyboard") or [] + for row in rows: + for b in row: + data = b.get("callback_data", "") + if data.startswith("a:"): + return data + return None + + +class Handler(BaseHTTPRequestHandler): def do_POST(self): length = int(self.headers.get("Content-Length") or 0) - self.rfile.read(length) + raw = self.rfile.read(length) + try: + req = json.loads(raw or b"{}") + except ValueError: + req = {} method = self.path.rsplit("/", 1)[-1] - with open(LOG, "a", encoding="utf-8") as f: - f.write(method + "\n") + with lock: + with open(LOG, "a", encoding="utf-8") as f: + f.write(method + "\n") if method == "getUpdates": - time.sleep(1) - result = [] + result = self.updates(int(req.get("offset") or 0)) elif method == "sendMessage": - result = {"message_id": Handler.next_id, "chat": {"id": CHAT}, "text": ""} - Handler.next_id += 1 + text = str(req.get("text", "")) + with lock: + mid = state["next_msg"] + state["next_msg"] += 1 + if TEXTS is not None: + with open(TEXTS, "a", encoding="utf-8") as f: + f.write(json.dumps(text, ensure_ascii=False) + "\n") + data = apply_button(req) + if data is not None: + state["updates"].append({ + "at": time.monotonic() + TAP_DELAY, + "update": { + "update_id": state["next_update"], + "callback_query": { + "id": "tap-%d" % mid, + "from": {"id": OWNER}, + # The text that this fake gives back, so + # the listener's text hash matches. + "message": {"message_id": mid, "chat": {"id": CHAT}, "text": text}, + "data": data, + }, + }, + }) + state["next_update"] += 1 + result = {"message_id": mid, "chat": {"id": CHAT}, "text": text if TEXTS is not None else ""} else: result = True body = json.dumps({"ok": True, "result": result}).encode() @@ -42,6 +100,18 @@ def do_POST(self): self.end_headers() self.wfile.write(body) + def updates(self, offset): + """The updates from offset on that are due. Waits up to a second.""" + end = time.monotonic() + 1 + while True: + now = time.monotonic() + with lock: + state["updates"] = [u for u in state["updates"] if u["update"]["update_id"] >= offset] + due = [u["update"] for u in state["updates"] if u["at"] <= now] + if due or now >= end: + return due + time.sleep(0.1) + def log_message(self, *args): pass diff --git a/test/units/full-loop.sh b/test/units/full-loop.sh new file mode 100644 index 0000000..7633a1e --- /dev/null +++ b/test/units/full-loop.sh @@ -0,0 +1,349 @@ +#!/bin/sh +# The README install as written, and one full loop under the real units +# (issue #27, DESIGN.md section 14). +# +# 1. It installs the binary with the README "install-test:binary" block, +# or checks the binary that install.sh installed (--installed). +# 2. It runs the README "install-test:setup" block: the users, the +# directories and the units of contrib/systemd/. +# 3. It writes the configs, a local bare repo as the remote, and a fake +# Telegram on 127.0.0.1 that taps Apply on every question. +# 4. It runs the README "install-test:start" block, with NAME=config-repo. +# 5. A good commit: the run asks (as root, check and summary as the check +# user), the fake taps Apply, the listener (user pullgate) writes the +# approval, pullgate-apply@ applies it, and `pullgate status` shows it. +# 6. It restarts the listener and checks that it comes back. +# 7. A commit whose apply fails: the rollback, and its notice reaches the +# fake Telegram. +# 8. Every unit the README enables is enabled and active. +# After each loop, every message root wrote must have been read and sent +# by the listener: the outbox is empty, and the listener logged no error +# and no warning. +# +# It changes the system: it installs /usr/local/bin/pullgate, adds users, +# and writes to /etc, /usr/local/lib, /srv and /var. Run it only as root, +# on a machine you throw away, such as a CI runner. +# +# Usage: sudo sh test/units/full-loop.sh PATH-TO-PULLGATE-BINARY +# sudo sh test/units/full-loop.sh --installed X.Y.Z +set -eu + +if [ "$(id -u)" -ne 0 ]; then + echo "full-loop.sh: run it as root, on a machine you throw away" >&2 + exit 1 +fi +usage() { + echo "usage: full-loop.sh PATH-TO-PULLGATE-BINARY | full-loop.sh --installed X.Y.Z" >&2 + exit 64 +} +installed="" +binary="" +case "${1:-}" in +--installed) + [ $# -eq 2 ] || usage + installed="$2" + ;; +"") usage ;; +*) + [ $# -eq 1 ] && [ -f "$1" ] || usage + binary="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" + ;; +esac + +here="$(cd "$(dirname "$0")" && pwd)" +top="$(cd "$here/../.." && pwd)" +name=config-repo +port=18444 +fail=0 +spool="/var/spool/pullgate/$name" +lib=/usr/local/lib/pullgate-loop +out=/var/lib/pullgate-loop +src=/srv/pullgate-loop + +err() { + echo "FAIL: $*" >&2 + fail=1 +} +die() { + echo "FAIL: $*" >&2 + exit 1 +} +step() { + echo + echo "=== $*" +} + +# readme NAME: run the README block NAME as written, with NAME=config-repo. +readme() { + cmds="$(sh "$here/readme-block.sh" "$1" "$top/README.md")" || die "README.md has no install-test:$1 block" + printf '%s\n' "$cmds" | sed 's/^/README> /' + printf '%s\n' "$cmds" | sed "s/NAME/$name/g" | sh -eu || die "the README block install-test:$1 failed (see the output above)" +} + +# wait_text TEXT SECONDS: wait until the fake Telegram got a message with TEXT. +wait_text() { + i=0 + while [ "$i" -lt "$2" ]; do + if grep -q -F -- "$1" "$texts" 2>/dev/null; then + return 0 + fi + i=$((i + 1)) + sleep 1 + done + return 1 +} + +# outbox_empty: wait until the listener took every file from the outbox. +outbox_empty() { + i=0 + while [ "$i" -lt 30 ]; do + if [ -z "$(ls -A "$spool/outbox")" ]; then + return 0 + fi + i=$((i + 1)) + sleep 1 + done + return 1 +} + +# unit_quiet UNIT: the unit logged no error and no warning. +unit_quiet() { + journalctl --sync || true + # No log line at all would make the check below pass without checking. + if ! journalctl --no-pager -o cat -u "$1" | grep -q 'level=INFO'; then + err "$1: no log line found in the journal, so its errors cannot be checked" + fi + if journalctl --no-pager -o cat -u "$1" | grep -E 'level=(ERROR|WARN)'; then + err "$1 logged an error or a warning (above)" + fi +} + +# listener_read_all WHEN: every message root wrote was read and sent. +listener_read_all() { + if ! outbox_empty; then + ls -la "$spool/outbox" >&2 + err "$1: the listener did not take every message from the outbox" + fi + unit_quiet pullgate-listen.service + if grep -q -F 'could not be read' "$texts"; then + err "$1: the listener could not read a message that root wrote" + fi +} + +# run_until_tip SHA: start runs until one has fetched SHA. A run that the +# timer started before the push is joined by `systemctl start` and does +# not see the new commit, so one start is not enough. +run_until_tip() { + i=0 + while [ "$i" -lt 10 ]; do + systemctl start "pullgate-run@$name.service" || err "pullgate-run@$name.service failed" + if pullgate status "$name" | grep -q "^last tip: *$1\$"; then + return 0 + fi + i=$((i + 1)) + sleep 1 + done + die "after 10 runs, pullgate status does not show the last tip $1" +} + +# applied_sha: the applied SHA that `pullgate status` shows. +applied_sha() { + pullgate status "$name" | sed -n 's/^applied: *//p' +} + +cd "$top" + +step "1. The binary" +if [ -n "$binary" ]; then + bdir="$(mktemp -d)" + cp "$binary" "$bdir/pullgate" + (cd "$bdir" && readme binary) + rm -rf "$bdir" +else + got="$(/usr/local/bin/pullgate --version || true)" + if [ "$got" != "pullgate $installed" ]; then + die "/usr/local/bin/pullgate --version says '$got', want 'pullgate $installed'" + fi +fi +pullgate --version + +step "2. The README setup: users, directories, units" +readme setup +want() { + got="$(stat -c '%U %G %a' "$1" 2>/dev/null || echo missing)" + if [ "$got" != "$2" ]; then + err "$1: got '$got', want '$2'" + fi +} +want /usr/local/bin/pullgate "root root 755" +want /etc/pullgate "root root 755" +want /var/spool/pullgate "root root 755" +want /var/lib/pullgate "root root 711" +check_user="pullgate-check-$name" +id pullgate >/dev/null || die "the README setup made no user pullgate" +id "$check_user" >/dev/null || die "the README setup made no user $check_user" + +step "3. The configs, the remote and the fake Telegram" +# A local bare repo, owned by root, with one commit. +rm -rf "$src" +install -d -m 0755 -o root -g root "$src" +git init -q --bare "$src/$name.git" +git -C "$src/$name.git" symbolic-ref HEAD refs/heads/main +work="$(mktemp -d)" +git -C "$work" init -q +git -C "$work" checkout -q -b main +commit() { + printf '%s\n' "$1" >"$work/policy.toml" + git -C "$work" add policy.toml + git -C "$work" -c user.name=test -c user.email=test@example.com commit -q -m "$2" + git -C "$work" push -q "$src/$name.git" main + git -C "$work" rev-parse HEAD +} +sha1="$(commit 'a = 1' 'a good commit')" + +# The commands. check and summary run as the check user; apply as root. +install -d -m 0755 -o root -g root "$lib" +install -d -m 0755 -o root -g root "$out" +cat >"$lib/check.sh" <<'EOF' +#!/bin/sh +# Runs as the check user: it must be able to read the tree. +set -eu +test -f "$PULLGATE_TREE/policy.toml" +test "$(id -un)" != root +EOF +cat >"$lib/summary.sh" <<'EOF' +#!/bin/sh +set -eu +echo "loop-summary by $(id -un)" +EOF +cat >"$lib/apply.sh" <<'EOF' +#!/bin/sh +# The apply fails for a policy that starts with "fail". +set -eu +if grep -q '^fail' "$PULLGATE_TREE/policy.toml"; then + echo "apply.sh: this policy fails on purpose" >&2 + exit 1 +fi +install -m 0644 "$PULLGATE_TREE/policy.toml" /var/lib/pullgate-loop/policy.toml +printf '%s\n' "$PULLGATE_SHA" >/var/lib/pullgate-loop/applied-sha +EOF +chown root:root "$lib"/*.sh +chmod 0755 "$lib"/*.sh + +cat >"/etc/pullgate/$name.toml" </etc/pullgate/bot-token +chown root:root /etc/pullgate/bot-token +chmod 0600 /etc/pullgate/bot-token +cat >/etc/pullgate/listener.toml </dev/null || true; rm -rf "$work"' EXIT +i=0 +until python3 -I -c 'import socket, sys; socket.create_connection(("127.0.0.1", int(sys.argv[1])), 1)' "$port" 2>/dev/null; do + i=$((i + 1)) + [ "$i" -lt 20 ] || die "the fake Telegram did not start on 127.0.0.1:$port" + sleep 0.5 +done + +step "4. The README start: enable --now" +readme start + +step "5. A good commit: ask, tap, apply" +# The timer may have started a run already; start waits for it. +run_until_tip "$sha1" +if ! wait_text "✅ Applied" 90; then + journalctl --no-pager -o cat -u "pullgate-run@$name.service" -u pullgate-listen.service -u "pullgate-apply@$name.service" || true + die "no 'Applied' message reached the fake Telegram" +fi +grep -q -F "loop-summary by $check_user" "$texts" || err "the question has no summary from the check user $check_user" +journalctl --sync || true +journalctl --no-pager -o cat -u "pullgate-run@$name.service" | grep -q 'msg=asked ' || err "the run did not log 'asked'" +journalctl --no-pager -o cat -u "pullgate-apply@$name.service" | grep -q 'msg=applied ' || err "pullgate-apply@$name.service did not log 'applied'" +[ "$(cat "$out/applied-sha" 2>/dev/null)" = "$sha1" ] || err "apply.sh did not apply $sha1" +[ "$(applied_sha)" = "$sha1" ] || err "pullgate status shows applied '$(applied_sha)', want $sha1" +pullgate status "$name" +want "$spool/outbox" "root pullgate 770" +want "$spool/inbox" "pullgate pullgate 700" +listener_read_all "after the good commit" + +step "6. Restart the listener" +before="$(grep -c -x getUpdates "$tglog" || true)" +systemctl restart pullgate-listen.service || err "systemctl restart pullgate-listen.service failed" +i=0 +while [ "$i" -lt 30 ]; do + now="$(grep -c -x getUpdates "$tglog" || true)" + if systemctl is-active --quiet pullgate-listen.service && [ "$now" -gt $((before + 1)) ]; then + break + fi + i=$((i + 1)) + sleep 1 +done +systemctl is-active --quiet pullgate-listen.service || err "the listener is not active after a restart" +[ "$now" -gt $((before + 1)) ] || err "the listener does not poll Telegram after a restart" + +step "7. A commit whose apply fails: rollback" +sha2="$(commit 'fail = true' 'a commit whose apply fails')" +run_until_tip "$sha2" +if ! wait_text "Rolled back to" 90; then + journalctl --no-pager -o cat -u "pullgate-run@$name.service" -u pullgate-listen.service -u "pullgate-apply@$name.service" || true + die "no 'Rolled back' notice reached the fake Telegram" +fi +[ "$(cat "$out/policy.toml" 2>/dev/null)" = "a = 1" ] || err "the rollback did not put back the policy of $sha1" +[ "$(applied_sha)" = "$sha1" ] || err "after the rollback, pullgate status shows applied '$(applied_sha)', want $sha1" +pullgate status "$name" | grep -q "^tip mark: *failed" || err "pullgate status does not show the tip $sha2 as failed" +pullgate status "$name" +listener_read_all "after the rollback" +unit_quiet "pullgate-run@$name.service" + +step "8. Every unit the README enables" +units="$(sh "$here/readme-block.sh" start "$top/README.md" | sed -e 's/#.*//' -e "s/NAME/$name/g" | + tr -s ' \t' '\n' | grep -E '^pullgate-[A-Za-z0-9@._-]+\.(service|path|timer)$' | sort -u)" +n=0 +for u in $units; do + n=$((n + 1)) + state="$(systemctl is-enabled "$u" 2>/dev/null || true)" + [ "$state" = enabled ] || err "$u: systemctl is-enabled says '$state', want 'enabled'" + state="$(systemctl is-active "$u" 2>/dev/null || true)" + [ "$state" = active ] || err "$u: systemctl is-active says '$state', want 'active'" + echo "$u: enabled, active" +done +[ "$n" -ge 4 ] || err "found $n units in the README start block, want at least 4" + +if [ "$fail" -ne 0 ]; then + exit 1 +fi +echo +echo "ok: the README install and a full loop work under the units of contrib/systemd/" diff --git a/test/units/readme-block.sh b/test/units/readme-block.sh new file mode 100644 index 0000000..3b95566 --- /dev/null +++ b/test/units/readme-block.sh @@ -0,0 +1,84 @@ +#!/bin/sh +# The install commands of README.md, so CI runs them as written (issue #27). +# +# The README marks each install block with a comment line +# "" just before its ```sh fence: +# binary install the binary (by hand) +# setup the users, the directories and the units +# start systemctl daemon-reload and enable --now +# +# Usage: +# sh test/units/readme-block.sh NAME [README] +# Print the commands of block NAME. Fails when the marker is missing, +# is there twice, or is not followed by a non-empty ```sh block. +# sh test/units/readme-block.sh --check [README] +# Check that all three blocks are there, and that no install command +# (useradd, install -d, cp contrib/systemd, systemctl enable) of a +# ```sh block is outside a marked block. Such a command would not +# be run by CI. +set -eu + +blocks="binary setup start" + +usage() { + echo "usage: readme-block.sh NAME [README] | readme-block.sh --check [README]" >&2 + exit 64 +} + +# block NAME FILE: print the lines of block NAME. +block() { + awk -v name="$1" ' + BEGIN { marker = "" } + $0 == marker { n++; want = 1; next } + want == 1 { + if ($0 != "```sh") { bad = "the line after the marker is not ```sh"; exit } + want = 2; next + } + want == 2 { + if ($0 == "```") { want = 0; next } + if (n == 1) { print; lines++ } + } + END { + if (bad != "") { print "FAIL: " marker ": " bad > "/dev/stderr"; exit 1 } + if (n != 1) { print "FAIL: found " marker " " n+0 " times, want 1" > "/dev/stderr"; exit 1 } + if (want != 0) { print "FAIL: " marker ": the ```sh block has no end" > "/dev/stderr"; exit 1 } + if (lines == 0) { print "FAIL: " marker ": the block is empty" > "/dev/stderr"; exit 1 } + } + ' "$2" +} + +[ $# -ge 1 ] || usage +if [ "$1" = --check ]; then + readme="${2:-README.md}" + for b in $blocks; do + if ! block "$b" "$readme" >/dev/null; then + echo "FAIL: $readme: the install-test:$b block is missing or broken. Fix: put \"\" on the line before its \`\`\`sh block" >&2 + exit 1 + fi + done + # Install commands in ```sh blocks that are not marked: CI would not + # run them. The "Update from 0.1.0" steps use "reenable", which is + # not matched here. + # Every fenced block counts, indented or not, in any language. + out="$(awk ' + /^$/ { marked = 1; next } + /^[[:space:]]*```/ { + if (inblock) { inblock = 0; marked = 0 } else { inblock = 1 } + next + } + inblock && !marked && /(^|[[:space:]])(useradd|groupadd|install -[a-zA-Z]|chown|chmod|cp contrib\/systemd|systemctl( --?[A-Za-z-]+)* enable)([[:space:]]|$)/ { print FILENAME ":" NR ": " $0 } + ' "$readme")" + if [ -n "$out" ]; then + echo "$out" >&2 + echo "FAIL: these install commands are in a \`\`\`sh block without an install-test marker, so CI does not run them. Fix: move them into a marked block (see test/units/readme-block.sh)" >&2 + exit 1 + fi + echo "ok: $readme has the blocks $blocks, and no unmarked install command" + exit 0 +fi + +case " $blocks " in +*" $1 "*) ;; +*) usage ;; +esac +block "$1" "${2:-README.md}"