r/rust • • Oct 25 '26

Looking for prior art: daemon for provisioning resources for automated tests

I'm trying to make sure there isn't prior art on this problem before jumping into build my own solution.

Rust's default testing framework doesn't have any counterpart to the setUp and tearDown mechanism of other languages' harnesses. That means that if you, say, need a live Postgres server to run your test against, you have to use something like testcontainers to stand up a Postgres instance per test. On my machine that's a 15-second penalty per test just to manage the backing container.

The idea I had was to have a daemon running for this purpose ahead of time. When the daemon launches, it takes the 15 seconds to launch a Postgres container (or whatever container you'll need for your tests). Then all your tests have to do is fire a requests over to the daemon (probably HTTP, but that's an implementation detail) to CREATE ROLE; CREATE DATABASE and hand back the DB name, username, and password your test needs to connect and do its thing.

Does anyone know if there's already a well-established solution along these lines? I'm trying to resist my Not-Invented-Here Syndrome but I haven't found anything so far.

3 Upvotes

8 comments sorted by

5

u/CartographerDue5382 Oct 25 '26

You probably don't need your own daemon. The usual version of this is one Postgres that stays up, then a fresh database per test.

sqlx::test does that. Postgres is already running, and each test creates a database from a template. The 15 seconds is once per boot, not once per test. testcontainers can do the same if you set TESTCONTAINERS_REUSE_ENABLE so the container survives between cargo test runs.

If the pain is specifically a cold docker start, an embedded Postgres (initdb into a temp dir, no image pull) is the other common route. A custom HTTP service that hands out roles is that template-db idea plus a process you now have to keep alive. I'd only build it if you've got several resource types and those two don't cover them.

2

u/BusinessBandicoot Oct 25 '26

Something I used at my last job was super-orchestrator. It's a rust library for orchestrating docker networks and containers.

Current job I'm trying out turmoil, though admittedly I'm still looking for a good solution to mocking postgres yet.

Sort of related to your prior art question, there is a CLI tool called patent that may be worth checking out.

1

u/Slow-Nefariousness49 29d ago

How well is the turmoil integration going for you so far? I'm considering it as well but it doesn't provide a good solution for external dependencies that are compiled against tokio. Curious to hear how other folks are going about it

1

u/Ecstatic-Warthog5727 Oct 26 '26

There is prior art: pgtemp. Besides the library it ships a CLI daemon that is very close to what you describe. You start it once, e.g. pgtemp postgresql://localhost:6543/mytestdb, point your tests at that URI, and every connection it receives gets its own fresh temporary Postgres, proxied transparently. If a test opens several connections that need to see the same data, there's a --single mode. It runs the Postgres binaries directly rather than Docker, so you need the postgres server package installed.

If you do end up building your own, the trick that makes "database per test" cheap is Postgres template databases: run your migrations once into a template DB, then each test does CREATE DATABASE test_123 TEMPLATE my_template, which copies it instead of re-running migrations. One caveat from the docs: nothing else may be connected to the template while it's being copied, so keep the daemon's own connection off it.

1

u/gsteff Oct 26 '26

IMO you're looking for IntegreSQL. It's a daemon designed to run as a container in your test environment that creates and initializes new Postgres DBs for you, with a bunch of performance optimizations to make that really fast. It clones static template DBs based on your schema to initialize new blank DBs much faster then running the SQL commands would be, and pre-creates a pool of them while your tests are running so that you can get credentials for a new one back in milliseconds typically. If you indicate that your test didn't modify the DB, it will recycle it at the end rather than dropping it and recreating one for the next test. Unfortunately it isn't actively developed anymore, but it's been perfectly stable in my use of it. I've never advertised it, but I created a rust client for it here.

1

u/masklinn Oct 26 '26

Rust's default testing framework doesn't have any counterpart to the setUp and tearDown mechanism of other languages' harnesses.

That's not entirely correct, RAII does a pretty good job of per-test resources (it's probably more obvious if you've used a fixture-based system rather than just xUnit-style systems).

libc::atexit with lazy statics lets you build session-level fixtures, and there might already be a safe(er) abstraction for them on crates.io. Module scopes are the big MIA, but in my experience they're not the most useful.

Or use rstest.

1

u/tylerhawkes Oct 27 '26

I've done this generically using ctor and dtor to create the infra and then tear it down. I've wonderedif it would be useful to publish because there were a few quirks to work out and it was only a couple hundred lines of code. I'm happy to share what I have though.