Skip to content

@plexus-ms/test-postgres: a throwaway PostgreSQL from the pinned binary - #142

Merged
davidjalbers merged 43 commits into
mainfrom
davidjalbers/plx-177-plexus-mstest-postgres-a-throwaway
Nov 30, 2026
Merged

davidjalbers merged 43 commits into
mainfrom
davidjalbers/plx-177-plexus-mstest-postgres-a-throwaway

Conversation

@davidjalbers

@davidjalbers davidjalbers commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A new package, @plexus-ms/test-postgres: a throwaway PostgreSQL server for tests, started from the binary on the PATH and removed afterwards (Decisions § 2.1, § 2.5).
It installs nothing; the server comes from the repo's conda:postgresql pin.

const server = await startPostgres({ major: 18, settings?: { max_connections: 400 } }); // { url, stop() }
const url = await createDatabase(server.url);                      // an empty database of its own
const copy = await createDatabase(server.url, { template: name }); // a copy of a migrated template
await dropDatabase(copy);
await server.stop();                                               // server down, directory removed
startPostgres (caller)
  refuse inside a supervisor, and when this file is not the package's own (bundled)
  check settings: not port, listen_addresses, unix_socket_directories; strings or numbers
  find initdb on PATH, pg_ctl, postgres, psql beside it   -> else: error naming the mise pin
  postgres --version (as nobody when root)                -> older than 17, or other major: error
  spawn detached supervisor (this file, marked by PLEXUS_TEST_POSTGRES_SUPERVISOR), holding its stdin
    mkdtemp, chown to nobody when root
    initdb (UTF-8, builtin C.UTF-8, trust), postgresql.conf (fsync=off, settings, port, 127.0.0.1, socket)
    pg_ctl start -> report URL, or the error with the end of server.log (directory removed)
    a stop asked during the start: stop the cluster, report an error naming the signal
    wait for EOF on stdin or SIGTERM/SIGINT/SIGHUP   <- stop(), exit, or SIGKILL of the caller
    pg_ctl -m immediate stop, rm -rf

How it works, what it refuses to do and what no test covers is in the header of packages/test-postgres/src/index.ts.

Shared files touched: the conda:postgresql = "18.6" pin and PLX_POSTGRES_MAJOR = "18" in the root mise.toml, the lockfile, and a package-level packages/test-postgres/turbo.json that declares PLX_POSTGRES_MAJOR as an input of this package's test (§ 2.4, § 1.12).
Nothing in test needs Docker.

Decisions the issue did not make

  • Interface: startPostgres({ major, settings }), createDatabase(url, { template }) with the template as a database name (PostgreSQL's own term), and dropDatabase(url), which decodes the name in the URL's path. The server URL names the postgres database as the postgres role.
  • Killed caller: the cluster belongs to a supervisor, the package's own file run as a detached Node process in a session of its own. It stops the server when its stdin reaches EOF, which the kernel delivers on SIGKILL of the caller too, and on SIGTERM, SIGINT or SIGHUP of its own. A stop asked while the cluster starts fails the start with an error that names the signal, once the cluster is up and gone again, so the caller never gets the URL of a server that is going. A caller that exits without stop() leaves nothing behind. A SIGKILL of the supervisor itself cannot be handled and leaves the server running. The session of its own is what keeps a SIGKILL to the caller's process group from reaching the supervisor; the test of that SIGKILL is what holds the header's sentence, since the supervisor's own SIGINT handler would survive a Ctrl-C without the session.
  • Bundling: the package must stay a file of its own. Bundled into the caller's file, the supervisor would run the caller again, and each would start the next. startPostgres refuses unless its file's nearest package.json is this package's, the supervisor is marked by an environment variable rather than its command line, and a marked process refuses to start another server. A marker alone was not enough: by hand, the bundle's supervisor began its cluster directory before the bundle's caller code failed. A bundle placed inside the package's own directory does get past the own-file check and starts one supervisor; that supervisor lets the bundled caller run first, the caller's refusal is reported to the supervisor's caller with the bundled message, and nothing is created, also when the bundled caller catches the refusal and lives on.
  • Root: the server always runs as nobody (not a postgres user where one exists), switched to by Node's spawn uid/gid, so runuser, setpriv and su are not needed. An install nobody cannot run (one under /root) fails with a message that says so; the package never changes its permissions. No test covers this path, by the maintainer's decision; src/fixtures/as-root.ts is a script to run by hand as root, its header says how.
  • Cluster: UTF-8 with --locale=C --locale-provider=builtin --builtin-locale=C.UTF-8, so text sorts and cases alike on macOS and Linux. This needs PostgreSQL 17 or later (17.12 and 18.6 tried), and an older server is refused before initdb runs. fsync=off. Port, address and socket go into postgresql.conf, not through pg_ctl -o, so no shell sees a value.
  • settings: an option for server parameters, because the code this ports from runs with max_connections=400. The package's own three settings and values that are not a string or a number on one line are refused.
  • Install lookup: the first initdb on the PATH, with pg_ctl, postgres and psql required in the same directory.
  • No driver: createDatabase and dropDatabase use the psql of that install, with the environment's PG* variables removed.
  • No retry of a template copy: measured with 18.6, eight copies at once from one template all succeed, and PostgreSQL itself waits about five seconds for a session on the template to leave before it fails with 55006, which the call passes on.
  • A failed start carries the end of server.log in its error and leaves no directory.
  • Stop: pg_ctl -m immediate, since the directory is removed right after.
  • Major: the tests ask for PLX_POSTGRES_MAJOR (the name the repo-turbo template sets), set beside the pin in mise.toml. These are two literal lines, guarded by the wrong-major failure. mise can derive one from the other through [vars] (tried), but I assumed the update bot cannot bump a templated version, and did not check that.
  • Pin: 18.6, the version the trial proved on both platforms.
  • Release: a minor changeset, so the first version is 0.1.0.

Not covered by a test

  • The root path (above), by decision.
  • That the supervisor's entry guard reads the environment marker rather than the command line: with the own-file check in place, only a bundle inside the package's own directory starts a supervisor, and there both would fire, so no test tells the two apart.
  • A signal during the start is covered (a test sends SIGTERM while initdb runs); a SIGKILL of the supervisor is not, since it cannot be handled.
  • A free port taken between the probe and the server's bind: no retry; the error then carries the server's log.
  • macOS arm64: not run, this machine is Linux x64. The command for the Mac, from a checkout of libraries:
gh pr checkout 142 && mise install && mise run test test-postgres

Evidence

Each behaviour was committed as a failing test, then as the code that passes it; the (red) commits fail for the reason named.
The tests added for conditions that already held were each seen to fail with their condition broken in a copy of the source.

  • Before: the package bundled into its caller's file started a supervisor per level without end; a SIGTERM to the supervisor left the server running; a failed start said only "Examine the log output".
    After: mise run verify passes, 31 of 31 tests in this package.
  • Outside the suite, with pnpm pack and the tarball installed into an empty project whose PATH has conda:postgresql 18.6 from a scratch MISE_DATA_DIR, a script started a server, made two databases and a copy of one, queried each, dropped one and stopped it; the directory was gone afterwards.
    By hand: no binaries on the PATH, a wrong major, and kill -9 of the caller each fail cleanly or leave nothing.
    The packed dist/index.js and a caller concatenated into one file fail with the message to keep the package external, and start no supervisor, cluster or directory.
    A real bundler (esbuild) was not run here; the reviewer's esbuild bundle is what found the problem, and the test reproduces it without one.

Merge Danger

Door: two-way until the first publish, then one-way: a published npm version cannot be taken back.

Blast Radius: contained. No consumer exists yet, and std and config are untouched.

  • Whether trusted publishing can publish a package name that does not exist on npm yet is not verified here.

🤖 Generated with Claude Code

davidjalbers and others added 16 commits November 30, 2026 14:27
An empty @plexus-ms/test-postgres, with the conda:postgresql pin its own
tests need and POSTGRES_MAJOR, the major they ask for, as an input of its
test task.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…s caller

The supervisor waits for the end of its stdin, which the caller's stop()
closes and the kernel closes when the caller dies, SIGKILL included.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The root fixture reads the postmaster's pid from postmaster.pid: the
backend of the query that asked for it has gone by the time ps looks.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@davidjalbers davidjalbers self-assigned this Nov 30, 2026
davidjalbers and others added 13 commits November 30, 2026 15:25
…by hand

The maintainer's decision: nothing in test may need Docker. The root path
stays and the header says that no test covers it; the Docker variables
leave the package's turbo.json.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Measured with 18.6 and the loop removed: eight copies at once from one
template all succeed (25 ms), and a session on the template is waited for
about five seconds before the copy fails with 55006 (a 3 s session: the
copy succeeds after 2.7 s; a 9 s session: it fails after 5.0 s).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…red)

The package's source and a caller in one file, as a bundler makes it. The
caller logs every process that runs the file and stops three levels down,
so that the chain this reproduces stays small; the test removes what it
left.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…vironment

The supervisor is the package's own file run on its own. Bundled into the
caller's file, that file is the caller's program, so each supervisor ran the
caller again and started the next. startPostgres now refuses unless its file
is the package's own (the nearest package.json names it), and the entry
guard tests an environment variable the package sets on the supervisor, not
the command line.

A marker alone was not enough: tried by hand, the bundle's supervisor began
its cluster directory before the bundle's caller code failed, and the
directory was left.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
fixtures/psql.ts becomes fixtures/support.ts, with startThatFails(): the
start under a TMPDIR of its own, its error and what it left there.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A supervisor whose start failed also lets go of its stdin, so that it exits
rather than wait for a caller that will not close it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
davidjalbers and others added 14 commits November 30, 2026 15:35
…es (red)

The stand-ins on the PATH now answer postgres --version with a version of
the test's choosing, and initdb leaves a file when it runs.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…types

The settings are checked in the caller's process, before anything starts.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The server listens on 127.0.0.1 only with its socket in its own directory;
a caller that ends without stop() is not held up and leaves nothing; a
SIGINT or a SIGKILL to the caller's process group leaves nothing; the drop
test's session outlasts the five seconds PostgreSQL waits; the environment's
PG variables do not change where the calls connect; an install without psql
fails with the pin; a lower and a higher major both fail.

These pass as they are; each was seen to fail with its condition broken.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…re ended

The failed-start test now also asserts that the supervisor exits, and the
test of a caller ending without stop() ends a caller that is held up, so
that both fail cleanly with their condition broken instead of hanging the
run. The process list moves into the shared fixtures.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The name the repo-turbo template sets, by the maintainer's decision.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A signal that reaches the supervisor while the cluster starts used to give
the caller the URL of a server that was already going. The supervisor now
stops the cluster once it is up, reports an error that names the signal,
and lets go of its stdin so that it exits. The test also asserts that the
supervisor exits, so that a supervisor left waiting fails it rather than
hanging the run.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The bundled test runs twice: in an app of its own, and in a directory whose
package.json names this package, where the own-file check passes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…eaves nothing

The own-file check is not widened. The supervisor lets a caller bundled into
its file run first; that caller's startPostgres() writes the bundled message
to the supervisor's stdout before it throws, and the supervisor, seeing the
refusal, creates nothing.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…s own is for

The supervisor's own session keeps a SIGKILL to the caller's process group
from reaching it; a Ctrl-C would not take it down without the session, since
it handles SIGINT itself.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
With the refusal check in the supervisor removed, the suite passed: a
caller that lets the error end its process ends the supervisor before the
check matters. A caller that catches it and lives on is where the check
counts; the test watches the cluster directory while the bundle runs.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@davidjalbers
davidjalbers merged commit d28b7a5 into main Nov 30, 2026
3 checks passed
@davidjalbers
davidjalbers deleted the davidjalbers/plx-177-plexus-mstest-postgres-a-throwaway branch November 30, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant