midnight-saci

Design: run test plans when code changes

[!NOTE] Implemented (issue #135). The user documentation is in watch repositories.

Status: implemented (#135). The user guide is docs/connect/watch-repositories.md.

Goal

When code is merged into a watched branch on GitHub, Saci runs the related test plans without a person. Two constraints apply:

Overview

A new command, saci watch, runs on the Saci host as a long-running process. It does not change the backend: it is a client of the REST API, like saci run.

flowchart LR
    gh["GitHub repositories"]
    subgraph host["Saci host"]
        watch["saci watch"]
        clones[("Local clones<br/>and state file")]
        backend["saci-backend"]
        worker["saci-worker"]
    end

    watch -- "git ls-remote (poll)" --> gh
    watch -- "git fetch, checkout" --> clones
    watch -- "POST /api/v1/runs, GET run" --> backend
    backend -- launch --> worker

One cycle:

  1. Poll. At each interval, the watcher runs git ls-remote for the branch of each repository and compares the commit with the last commit that it saw.
  2. Wait for quiet. When a repository changed, the watcher waits until no watched repository changed for quiet_period_secs. Related merges in several repositories thus give one cycle.
  3. Snapshot. The watcher fetches every repository and checks out the current commit of its branch. The set of commits is the snapshot. All plans of the cycle are read from the same snapshot.
  4. Run. The watcher reads each plan from the clones, submits it to the backend, and waits until the run ends before it submits the next plan.

Versions under test

The plan states the versions that it tests, for example the image tags or the commands that build or deploy the system under test. The watcher does not build, deploy or give versions to the run. It only decides when to run and gives the newest plans to the backend. A plan that tests the newest code of a branch says so itself, for example with a main image tag or a clone of the branch in the worker.

Thus the backend, the worker and the plan format do not change.

The watcher can also start a local backend when none answers (#141). Then one service runs both. See starting the backend.

Detecting changes

The watcher uses git ls-remote <url> refs/heads/<branch>, not the GitHub REST API:

The cost is one small request for each repository at each interval. The default interval is 60 seconds.

Rejected alternatives:

The clones

The watcher owns its clones in <workdir>/repos/<name>. It creates a clone when it is missing. To update a clone, it runs git fetch and git checkout --force --detach <commit>, then git clean -ffdx. Local changes in a clone are lost. Do not edit the clones by hand.

Clones that the watcher owns give a known state for each cycle. A person’s working copy can have local changes, another branch or build output, and the watcher must not change it.

The clones are for the plans only. Workers mount no host directories (launchers).

Repositories and triggers

The configuration has one list of repositories and one list of triggers. A trigger names the repositories that start it and the plans that it runs. A repository that only supplies plans, for example the test repository, is in the snapshot but does not need to start a trigger.

backend:
  url: http://127.0.0.1:18080       # default: --backend or SACI_BACKEND_URL
  token: { env: SACI_TOKEN }        # optional
workdir: ~/.local/state/saci-watch  # clones and the state file
poll_interval_secs: 60
quiet_period_secs: 180
repositories:
  - name: wanaku
    url: https://github.com/wanaku-ai/wanaku.git
    branch: main
  - name: wanaku-operator
    url: https://github.com/wanaku-ai/wanaku-operator.git
    branch: main
  - name: wanaku-tests
    url: git@github.com:wanaku-ai/wanaku-tests.git
    branch: main
triggers:
  - name: operator
    on: [wanaku, wanaku-operator, wanaku-tests]
    plans:
      - plan: wanaku-tests/plans/operator-basic-functionality.md
        environment: openshift
        policy: keep-going
      - plan: wanaku-tests/plans/operator-upgrade.md
        environment: openshift

One cycle at a time

Test plans of one project often use one shared system under test, for example one OpenShift namespace. Two cycles at the same time can break each other. Thus:

Restarts and duplicates

The watcher keeps its state in <workdir>/state.json: the last snapshot that it saw, the last snapshot that it tested for each trigger with its run IDs, and the run that it waits for. It writes the file to a temporary file and renames it, so a crash cannot leave a partial file.

Command line

saci watch --config watch.yaml [--once] [--run-now]
saci watch status --config watch.yaml

The user guide gives a systemd user unit and a launchd agent to start the watcher with the host.

Where the code goes

saci watch is a module of saci-cli. The CLI already has the REST client, the plan bundle loader and the backend settings. The watcher uses the git program, as investigations do, and no Git library.

Notifications

The watcher does not send notifications. The backend’s outcome actions already report run results, for example by email.

Security

Not in scope

Tickets

  1. Watcher core (#137): configuration, git ls-remote polling, the quiet period, the clones and the snapshot, the state file, submission with idempotency keys, waiting for runs, --once, --run-now and saci watch status. Tests use local bare repositories (file:// URLs) and the saci-testkit backend fixtures.
  2. Docs and examples (#138): a user guide in docs/connect/watch-repositories.md, the command in docs/reference/cli.md, an example configuration for a project in several repositories in examples/, and the systemd and launchd units.

Ticket 2 depends on 1.