Foundational Compute

Split a test suite

Run test shards in many sandboxes at once and collect the reports.

Templatebase (2 vCPU, 512 MiB, $0.039 per hour)
Run timeAbout 18 seconds from code, 20 seconds from the CLI script
Cost of a runLess than $0.001. 20 shards of 10 minutes cost $0.13.
CleanupCode kills each sandbox in finally. The script ends with fdtn kill.

How the split works

Each sandbox gets SHARD (0, 1 or 2) and SHARDS (3) at create. This line picks every third test file:

ls colorama/tests/*_test.py | awk -v s=$SHARD -v n=$SHARDS 'NR % n == s'

Replace the colorama clone and the test command with your own.

1From code

The script exits with the highest shard exit code, so one failed test fails the run.

Python · TypeScript

shards.py
import asyncio
from fdtn import AsyncSandbox, CommandExitException

SHARDS = 3
SETUP = "git clone -q --depth 1 https://github.com/tartley/colorama repo && pip install -q pytest"
TEST = ("cd repo && python3 -m pytest -q "
        "$(ls colorama/tests/*_test.py | awk -v s=$SHARD -v n=$SHARDS 'NR % n == s')")

async def shard(i):
    sbx = await AsyncSandbox.create(
        "base",
        timeout=900,                                # 15 minutes at most
        request_timeout=1800,                       # wait in the queue if needed
        envs={"SHARD": str(i), "SHARDS": str(SHARDS)},
        metadata={"suite": "colorama", "shard": str(i)},
    )
    try:
        await sbx.commands.run(SETUP, timeout=300)
        try:
            r = await sbx.commands.run(TEST, timeout=600)
            return i, 0, r.stdout.strip().splitlines()[-1]
        except CommandExitException as e:            # a test failed
            return i, e.exit_code, e.stdout.strip().splitlines()[-1]
    finally:
        await sbx.kill()

async def main():
    results = await asyncio.gather(*(shard(i) for i in range(SHARDS)))
    for i, code, summary in results:
        print(f"shard {i}: exit {code}: {summary}")
    raise SystemExit(max(code for _, code, _ in results))

asyncio.run(main())
$ python shards.py      # or: node shards.mjs
shard 0: exit 0: 1 passed, 10 skipped in 0.04s
shard 1: exit 0: 10 passed in 0.02s
shard 2: exit 0: 27 passed, 4 skipped in 0.08s

2From the CLI

The same run as a script. It also saves a JUnit report from each shard.

shards.sh
#!/bin/sh
set -e
RUN=demo
# 1. Start 3 sandboxes. Each gets its shard number in SHARD.
for i in 0 1 2; do
  fdtn create base -d --name shard-$i --timeout 900 \
    -e SHARD=$i -e SHARDS=3 -m run=$RUN,suite=colorama
done
# 2. Install the code and the test tools in each.
fdtn each -m run=$RUN,suite=colorama -- bash -c \
  'git clone -q --depth 1 https://github.com/tartley/colorama repo && pip install -q pytest 2>/dev/null'
# 3. Each sandbox runs every third test file.
fdtn each -m run=$RUN,suite=colorama -- bash -c \
  'cd repo && files=$(ls colorama/tests/*_test.py | awk -v s=$SHARD -v n=$SHARDS "NR % n == s") &&
   python3 -m pytest -q --junitxml=/home/user/results/junit.xml $files | tail -1'
# 4. Download the test reports.
fdtn collect -m run=$RUN,suite=colorama --path /home/user/results --destination ./results
# 5. Stop the group.
fdtn kill --all -s running,paused -m run=$RUN,suite=colorama
$ sh shards.sh
iv3qur0loppcdwip9tct4f79
ildux9nk6h5h8fk2i3lur
i88zlb40fuac9dz7p6gr4890
[iv3qur0loppcdwip9tct4f79] exit: 0
...
[ildux9nk6h5h8fk2i3lur] 10 passed in 0.03s
[i88zlb40fuac9dz7p6gr4890] 27 passed, 4 skipped in 0.07s
[iv3qur0loppcdwip9tct4f79] 1 passed, 10 skipped in 0.05s
[1/3] Downloading i88zlb40fuac9dz7p6gr4890…
  Downloaded 1 file (4392 bytes).
...
Stopped 3 sandboxes.

./results holds one folder per sandbox with its junit.xml, and a manifest.json. To run shards on each push, see Run CI.

Notes

  • The metadata suite=colorama names the group. fdtn each, collect and kill act on the whole group.
  • request_timeout=1800 lets the SDK wait up to 30 minutes in the queue. The queue
  • For a long install, build a template with the repo and packages. Templates
  • If a run stops early, stop the rest: fdtn kill --all -s running,paused -m suite=colorama.

Next