From 2be73fedf23de541dbd6b799874c5f6047e303df Mon Sep 17 00:00:00 2001 From: Marius S <39998+winding-lines@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:06:03 -0800 Subject: [PATCH] Unit tests built once per CI shard; customs@v0.3.1 customs 0.3.1's mojo-build-per-test (magmalake/customs#7) reported four tasks: test-executor-core (25 test files built one by one), test-executor-workers (4), test-scheduler-ui (9) and the data environment's test-data-tables (7). Each test file was a binary of its own, so the code they share compiled once per file. scripts/unit-tests.sh builds one binary per shard, build/unit-tests-, through scripts/mojo-build.sh, so the build cache, MOJO_BUILD_CACHE_DIR and the ouromo path keep working. build/, the path each pixi task always built (build/test-steps), is a link to it, and the binary runs the tests of the file its name says. Each test file still runs as its own process, from the same task, under the same wrappers (scripts/with-postgres.sh) and before the same shell tests. Tests are registered by name (census's scripts/test.sh), and `unit-tests.sh run` checks the number that ran against the file's `def test_*`. Task names, shard boundaries and CI are unchanged. Shards: executor-core 21 files, executor-workers 3 (tests/hcloud.sh now runs test-hcloud from it), scheduler-ui 8, data-tables 5, and data-tables-source 2 (test_tables and test_lake_batch with -D MORCA_LAKE_SOURCE=1, as test-tables-source and test-lake-batch-source built them). Built on their own, as before: - tests/test_source.mojo: its -D MORCA_SOURCE and -D MORCA_COMMIT would be every test's source in a shared binary. - tests/test_append_files.mojo: its tests take the dista runtime its main starts before any connection, so they can't be run by name. - tests/test_datacache.mojo: tests/dataview.sh also runs it as a tool (`test-datacache plant ...`). test_formal_pins, test_formal_migrate and test_formal_fence now run their tests with TestSuite instead of calling each one; test_replace, test_formal_retry and test_run_token set MORCA_WAREHOUSE in before_tests(). The order their tests run in is unchanged (the order of the file). The test and example pipelines' multicall binary (scripts/pipelines.sh) is untouched. Their registry hashes are unchanged (sai, `build/

describe`): toy 0xa3bcc571ad7c6939, daily 0x81ead348d0a21c1f, fanout 0x1f0ebf1f2a93d993. Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_017TycsxF3PkSX49fnv7cSZd --- .github/workflows/ci.yml | 2 +- pixi.toml | 76 ++++++------ scripts/unit-tests.sh | 214 +++++++++++++++++++++++++++++++++ tests/hcloud.sh | 6 +- tests/test_formal_fence.mojo | 5 +- tests/test_formal_migrate.mojo | 5 +- tests/test_formal_pins.mojo | 7 +- tests/test_formal_retry.mojo | 27 ++++- tests/test_replace.mojo | 39 +++--- tests/test_run_token.mojo | 31 +++-- 10 files changed, 331 insertions(+), 81 deletions(-) create mode 100755 scripts/unit-tests.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c314c1c4..c32a82e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -900,7 +900,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: magmalake/customs@v0.3.0 + - uses: magmalake/customs@v0.3.1 # And the packages as layers: `_internal` imports no public module # that imports `_internal` back (AGENTS.md). - run: python3 scripts/check_layers.py diff --git a/pixi.toml b/pixi.toml index af9ddf46..4829fb61 100644 --- a/pixi.toml +++ b/pixi.toml @@ -143,30 +143,30 @@ sqlite-mojo = { git = "https://github.com/magmalake/sqlite.mojo", rev = "0d76aa7 [tasks] -test-steps = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_steps.mojo -o build/test-steps && ./build/test-steps" } -test-cron = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_cron.mojo -o build/test-cron && ./build/test-cron" } -test-warehouse = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_warehouse.mojo -o build/test-warehouse && ./build/test-warehouse" } +test-steps = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-steps" } +test-cron = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-cron" } +test-warehouse = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-warehouse" } test-cli = { cmd = "tests/cli.sh" } test-hetzner = { cmd = "tests/hetzner.sh && tests/hetzner-stacks.sh && tests/hetzner-tools.sh && tests/hetzner-version.sh" } test-compile-fail = { cmd = "tests/compile_fail.sh" } # The executor over every store that needs no server (tests/store_suite.mojo), # then over Postgres, with what only Postgres has. -test-store = { cmd = "scripts/mojo-build.sh -I src -I tests $DEP_INC tests/test_store.mojo -o build/test-store && ./build/test-store" } +test-store = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-store" } # The Lean model of pinned reads (formal/Morca/Pins) against Ctx.pin over the # memory and SQLite stores, on the event sequences in formal/difftest/pins.cases. -test-formal-pins = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_formal_pins.mojo -o build/test-formal-pins && ./build/test-formal-pins" } -test-state = { cmd = "scripts/mojo-build.sh -I src -I tests $DEP_INC tests/test_state.mojo -o build/test-state && scripts/with-postgres.sh ./build/test-state" } +test-formal-pins = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-formal-pins" } +test-state = { cmd = "scripts/unit-tests.sh build executor-core && scripts/with-postgres.sh scripts/unit-tests.sh run test-state" } # The Lean model of the schema floor (formal/Morca/Migrate) against # migration_floor, schema_ahead and State.migrate, on formal/difftest/migrate.cases. -test-formal-migrate = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_formal_migrate.mojo -o build/test-formal-migrate && scripts/with-postgres.sh ./build/test-formal-migrate" } +test-formal-migrate = { cmd = "scripts/unit-tests.sh build executor-core && scripts/with-postgres.sh scripts/unit-tests.sh run test-formal-migrate" } # The Lean model of fencing (formal/Morca/Fence) against PgStore's claim, fence # and beat_run, on the event sequences in formal/difftest/fence.cases. -test-formal-fence = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_formal_fence.mojo -o build/test-formal-fence && scripts/with-postgres.sh ./build/test-formal-fence" } +test-formal-fence = { cmd = "scripts/unit-tests.sh build executor-core && scripts/with-postgres.sh scripts/unit-tests.sh run test-formal-fence" } # A crash's stack dump: morca.crash's summary of one, then a run whose # process aborts mid-step under the scheduler. -test-crash = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_crash.mojo -o build/test-crash && ./build/test-crash && scripts/with-postgres.sh tests/crash.sh" } -test-tasks = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_tasks.mojo -o build/test-tasks && ./build/test-tasks" } -test-logs = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_logs.mojo -o build/test-logs && ./build/test-logs && rm -f build/logs.db build/logs.db-wal build/logs.db-shm && MORCA_DATABASE_URL=sqlite:build/logs.db tests/logs.sh" } +test-crash = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-crash && scripts/with-postgres.sh tests/crash.sh" } +test-tasks = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-tasks" } +test-logs = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-logs && rm -f build/logs.db build/logs.db-wal build/logs.db-shm && MORCA_DATABASE_URL=sqlite:build/logs.db tests/logs.sh" } test-scheduler = { cmd = "scripts/with-postgres.sh tests/scheduler.sh" } test-chaos = { cmd = "scripts/with-postgres.sh tests/chaos.sh" } # The same over a SQLite file (morca.sqlite_store), with no server. @@ -181,11 +181,11 @@ test-ui-login = { cmd = "scripts/with-postgres.sh tests/ui_login.sh" } test-ui-drain = { cmd = "scripts/with-postgres.sh tests/ui_drain.sh" } # `morca login` and `--remote` (morca.remote, morca.tokens): tokens, URLs and # the credentials file, then the command against morca ui over HTTP. -test-remote = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_remote.mojo -o build/test-remote && ./build/test-remote && scripts/with-postgres.sh tests/remote.sh" } +test-remote = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-remote && scripts/with-postgres.sh tests/remote.sh" } # The watchdog (morca.watchdog): each rule over made-up rows, then the scheduler # pausing pipelines, against the stand-in EC2 for the launches. test-notify = { cmd = "scripts/with-postgres.sh tests/notify.sh" } -test-watchdog = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_watchdog.mojo -o build/test-watchdog && ./build/test-watchdog && scripts/with-postgres.sh tests/watchdog.sh" } +test-watchdog = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-watchdog && scripts/with-postgres.sh tests/watchdog.sh" } test-pause = { cmd = "scripts/with-postgres.sh tests/pause.sh" } test-older-binary = { cmd = "scripts/with-postgres.sh tests/older_binary.sh" } # The ec2 runner against a stand-in EC2 API; needs python3. @@ -207,11 +207,11 @@ test-hourly = { cmd = "scripts/with-postgres.sh tests/hourly.sh" } test-weekly = { cmd = "scripts/with-postgres.sh tests/weekly.sh" } # morca.toml (morca.toml, morca.config): the parser, what register reads from # the file and records, config show and export, and the env on each runner. -test-toml = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_toml.mojo -o build/test-toml && ./build/test-toml" } -test-upgrade = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_upgrade.mojo -o build/test-upgrade && ./build/test-upgrade" } -test-config = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_config.mojo -o build/test-config && ./build/test-config && scripts/with-postgres.sh tests/config.sh" } +test-toml = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-toml" } +test-upgrade = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-upgrade" } +test-config = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-config && scripts/with-postgres.sh tests/config.sh" } # Fills (morca.fill): a pipeline filling its range by itself, within limits. -test-fill = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_fill.mojo -o build/test-fill && ./build/test-fill && scripts/with-postgres.sh tests/fill.sh" } +test-fill = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-fill && scripts/with-postgres.sh tests/fill.sh" } test-fill-version = { cmd = "scripts/with-postgres.sh tests/fill_version.sh" } test-fill-requests = { cmd = "scripts/with-postgres.sh tests/fill_requests.sh" } # Fills on a worker pool (#177): two `morca worker`s fill a range within @@ -220,13 +220,13 @@ test-pool-fills = { cmd = "scripts/with-postgres.sh tests/pool_fills.sh" } # No Postgres deadlock on orca.runs between ticks and bulk updates (#440). test-run-locks = { cmd = "scripts/with-postgres.sh tests/run_locks.sh" } test-source = { cmd = "scripts/mojo-build.sh -I src $DEP_INC -D MORCA_SOURCE=https://example.com/lake.git -D MORCA_COMMIT=1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a tests/test_source.mojo -o build/test-source && ./build/test-source" } -test-highlight = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_highlight.mojo -o build/test-highlight && ./build/test-highlight" } +test-highlight = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-highlight" } # The step cards' data (morca.step_cards): signatures, docstrings, the JSON. -test-step-cards = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_step_cards.mojo -o build/test-step-cards && ./build/test-step-cards" } -test-graph = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_graph.mojo -o build/test-graph && ./build/test-graph" } +test-step-cards = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-step-cards" } +test-graph = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-graph" } playground = { cmd = "scripts/playground.sh" } -test-locate = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_locate.mojo -o build/test-locate && ./build/test-locate" } -test-timeline = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_timeline.mojo -o build/test-timeline && ./build/test-timeline" } +test-locate = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-locate" } +test-timeline = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-timeline" } test-ec2 = { cmd = "scripts/with-postgres.sh tests/ec2.sh" } test-autoscale = { cmd = "scripts/with-postgres.sh tests/autoscale.sh" } test-no-workers = { cmd = "scripts/with-postgres.sh tests/no_workers.sh" } @@ -247,23 +247,23 @@ test-secrets = { cmd = "scripts/with-postgres.sh tests/secrets.sh" } # limits, beside runs on the stand-in EC2. # A worker's run's state over morca ui's API: its encoding (morca.http_store, # orca.workers); tests/worker.sh (the data environment) runs the rest. -test-worker-api = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_worker.mojo -o build/test-worker-api && ./build/test-worker-api" } -test-placement = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_control.mojo -o build/test-control && ./build/test-control && scripts/with-postgres.sh tests/placement.sh" } +test-worker-api = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-worker-api" } +test-placement = { cmd = "scripts/unit-tests.sh build executor-workers && scripts/unit-tests.sh run test-control && scripts/with-postgres.sh tests/placement.sh" } # What runs use (morca.usage): the meter's sharing out, then a step holding a # known amount on the local runner, the control host and a remote worker. -test-resources = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_resources.mojo -o build/test-resources && ./build/test-resources" } +test-resources = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-resources" } # Requests learned from the measured peaks (morca.recommend), shown only. -test-recommend = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_recommend.mojo -o build/test-recommend && ./build/test-recommend" } +test-recommend = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-recommend" } test-grants = { cmd = "scripts/with-postgres.sh tests/grants.sh" } test-priority = { cmd = "scripts/with-postgres.sh tests/priority.sh" } # Admission by memory pressure (orca.pressure, morca.admission): PSI parsing # and the hold's hysteresis, then runs waiting on a fixture PSI file. -test-pressure = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_pressure.mojo -o build/test-pressure && ./build/test-pressure && scripts/with-postgres.sh tests/pressure.sh" } -test-usage = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_usage.mojo -o build/test-usage && ./build/test-usage && scripts/with-postgres.sh tests/usage.sh" } +test-pressure = { cmd = "scripts/unit-tests.sh build executor-workers && scripts/unit-tests.sh run test-pressure && scripts/with-postgres.sh tests/pressure.sh" } +test-usage = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-usage && scripts/with-postgres.sh tests/usage.sh" } # Containment (morca.cgroup): the arithmetic, then on Linux a run killed for # its memory and retried bigger (skipped, saying why, without cgroups); then a # run at a lower nice with MORCA_CGROUPS=0 (tests/run_nice.sh, #229). -test-cgroups = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_cgroup.mojo -o build/test-cgroup && ./build/test-cgroup && scripts/with-postgres.sh tests/cgroups.sh && scripts/with-postgres.sh tests/run_nice.sh" } +test-cgroups = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-cgroup && scripts/with-postgres.sh tests/cgroups.sh && scripts/with-postgres.sh tests/run_nice.sh" } test-aws = { cmd = "tests/aws.sh && tests/replace_control.sh" } test-lost-workers = { cmd = "scripts/with-postgres.sh tests/lost_workers.sh" } test-datasets = { cmd = "scripts/with-postgres.sh tests/datasets.sh" } @@ -273,9 +273,9 @@ test-db-blip = { cmd = "scripts/with-postgres.sh tests/db_blip.sh" } test-sense-errors = { cmd = "scripts/with-postgres.sh tests/sense_errors.sh" } test-defer = { cmd = "scripts/with-postgres.sh tests/defer.sh" } # The sampler (morca.sample): its parsing, then the binary on this machine. -test-sample = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_sample.mojo -o build/test-sample && ./build/test-sample && tests/sample.sh" } +test-sample = { cmd = "scripts/unit-tests.sh build executor-core && scripts/unit-tests.sh run test-sample && tests/sample.sh" } # A run's machines on its page (morca.metrics): peaks, warnings and charts. -test-metrics = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_metrics.mojo -o build/test-metrics && ./build/test-metrics" } +test-metrics = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-metrics" } # Needs docker, kind and kubectl; builds the image of deploy/Dockerfile. test-kind = { cmd = "tests/kind.sh" } # The binaries most tests share, built at once before the tests that copy @@ -405,29 +405,29 @@ LAKE_LIB = "$PIXI_PROJECT_ROOT/build/liblake.so" build-shared = { cmd = "scripts/build-shared.sh morca", depends-on = ["build-liblake"] } # liblake, which morca.tables loads (#454); every test below needs it. build-liblake = { cmd = "scripts/build-liblake.sh" } -test-tables = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_tables.mojo -o build/test-tables && scripts/with-postgres.sh ./build/test-tables", depends-on = ["build-liblake"] } +test-tables = { cmd = "scripts/unit-tests.sh build data-tables && scripts/with-postgres.sh scripts/unit-tests.sh run test-tables", depends-on = ["build-liblake"] } # Data files written in dista tasks, committed once by the step (append_files). test-append-files = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_append_files.mojo -o build/test-append-files && scripts/with-postgres.sh ./build/test-append-files", depends-on = ["build-liblake"] } # Steps that replace their partitions (replaces=, replace_partitions): reruns, # retries, runs racing over one partition, and stale removals. -test-replace = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_replace.mojo -o build/test-replace && scripts/with-postgres.sh ./build/test-replace", depends-on = ["build-liblake"] } +test-replace = { cmd = "scripts/unit-tests.sh build data-tables && scripts/with-postgres.sh scripts/unit-tests.sh run test-replace", depends-on = ["build-liblake"] } test-replace-concurrent = { cmd = "tests/replace_concurrent.sh", depends-on = ["build-liblake"] } # The Lean model of a retried step (formal/) against morca.tables.append, on # the event sequences in formal/difftest/retry.cases. -test-formal-retry = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_formal_retry.mojo -o build/test-formal-retry && ./build/test-formal-retry", depends-on = ["build-liblake"] } +test-formal-retry = { cmd = "scripts/unit-tests.sh build data-tables && scripts/unit-tests.sh run test-formal-retry", depends-on = ["build-liblake"] } # A run's commits told from those of another run with its number, by the # run's token (#292). -test-run-token = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_run_token.mojo -o build/test-run-token && ./build/test-run-token", depends-on = ["build-liblake"] } +test-run-token = { cmd = "scripts/unit-tests.sh build data-tables && scripts/unit-tests.sh run test-run-token", depends-on = ["build-liblake"] } # liblake itself (#454): ABI and missing-library refusals, an iceberg error # crossing to the step's log as it would compiled in, and its line tables. test-lake = { cmd = "tests/lake.sh", depends-on = ["build-liblake"] } # test-tables with liblake's code compiled in (-D MORCA_LAKE_SOURCE=1), the # fallback: both builds must pass. -test-tables-source = { cmd = "scripts/mojo-build.sh -D MORCA_LAKE_SOURCE=1 -I src $DEP_INC tests/test_tables.mojo -o build/test-tables-source && scripts/with-postgres.sh ./build/test-tables-source" } +test-tables-source = { cmd = "scripts/unit-tests.sh build data-tables-source && scripts/with-postgres.sh scripts/unit-tests.sh run test-tables-source" } # morca.tables' BatchBuilder and column accessors, and a table outside any step. -test-lake-batch = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_lake_batch.mojo -o build/test-lake-batch && ./build/test-lake-batch", depends-on = ["build-liblake"] } +test-lake-batch = { cmd = "scripts/unit-tests.sh build data-tables && scripts/unit-tests.sh run test-lake-batch", depends-on = ["build-liblake"] } # The same with liblake's code compiled in, as test-tables-source. -test-lake-batch-source = { cmd = "scripts/mojo-build.sh -D MORCA_LAKE_SOURCE=1 -I src $DEP_INC tests/test_lake_batch.mojo -o build/test-lake-batch-source && ./build/test-lake-batch-source" } +test-lake-batch-source = { cmd = "scripts/unit-tests.sh build data-tables-source && scripts/unit-tests.sh run test-lake-batch-source" } test-chaos-tables = { cmd = "CHAOS_SRC=tests/chaos_tables.mojo CHAOS_CHECK=1 scripts/with-postgres.sh tests/chaos.sh", depends-on = ["build-liblake"] } test-taxi = { cmd = "scripts/with-postgres.sh tests/taxi.sh", depends-on = ["build-liblake"] } test-lineage = { cmd = "scripts/with-postgres.sh tests/lineage.sh", depends-on = ["build-liblake"] } diff --git a/scripts/unit-tests.sh b/scripts/unit-tests.sh new file mode 100755 index 00000000..0e6c233e --- /dev/null +++ b/scripts/unit-tests.sh @@ -0,0 +1,214 @@ +#!/bin/sh +# The Mojo unit tests (tests/test_*.mojo), built once per CI shard and run +# from that one binary: +# +# scripts/unit-tests.sh build SHARD # build/unit-tests-SHARD and its links +# scripts/unit-tests.sh run NAME # run build/NAME, check its count +# +# Each test file of a shard goes into build/unit-tests-SHARD, and build/NAME +# (the name its pixi task always built, build/test-steps for +# tests/test_steps.mojo) is a link to it. The binary runs the tests of the +# file its name says, `basename "$0"`, so each test file still runs as its +# own process, under its own name, with whatever its task runs around it +# (scripts/with-postgres.sh, the shell test after it). The code the files of +# a shard share (morca, dista, postgres...) compiles once per shard, not once +# per test file. This follows census's scripts/test.sh. +# +# The shards are pixi.toml's: test-executor-core, test-executor-workers and +# test-scheduler-ui (CI runs each on a runner of its own) and the data +# environment's test-data-tables. A test file is in the shard whose task runs +# it, so a CI job builds only its own shard's binary. The build goes through +# scripts/mojo-build.sh like every other: an unchanged binary is a copy from +# build/.mojo-build (or MOJO_BUILD_CACHE_DIR), and under `ouromo` the split +# cache is used. The test and example pipelines' multi-call binary +# (scripts/pipelines.sh) is separate and unchanged. +# +# Not in a shard binary, each built on its own by its task: +# - tests/test_source.mojo: built with its own -D MORCA_SOURCE and +# -D MORCA_COMMIT, which would be the source of every test in the binary. +# - tests/test_append_files.mojo: its tests take the dista runtime its main +# starts before any connection, as an argument, so they can't be run by +# name from another main. +# - tests/test_datacache.mojo: tests/dataview.sh also runs its binary as a +# tool (`test-datacache plant ...`). +# test-tables-source and test-lake-batch-source build test_tables and +# test_lake_batch again with -D MORCA_LAKE_SOURCE=1: they are a shard of +# their own, data-tables-source. +# +# The tests are registered by name, each `def test_...` at column 0, not +# found with `__functions_in_module()`: in a module that is imported, rather +# than compiled as the main file, that list comes back incomplete (Mojo +# 1.2.0), and a test file would pass having run nothing. `run` checks the +# count the binary reports against the functions found here. A test file's +# `main` is therefore not called: it may only run the tests, and what a file +# needs done around them goes in `def before_tests() raises` and +# `def after_tests() raises`, called here and by its main +# (tests/test_replace.mojo). +# +# One file alone, without this: scripts/mojo-build.sh -I src -I tests +# $DEP_INC tests/test_steps.mojo -o build/x && ./build/x +# Run inside `pixi run`. +set -eu +cd "$(dirname "$0")/.." + +# shard:file:name, one test file per line. +all='executor-core:tests/test_sample.mojo:test-sample +executor-core:tests/test_steps.mojo:test-steps +executor-core:tests/test_cron.mojo:test-cron +executor-core:tests/test_warehouse.mojo:test-warehouse +executor-core:tests/test_store.mojo:test-store +executor-core:tests/test_formal_pins.mojo:test-formal-pins +executor-core:tests/test_state.mojo:test-state +executor-core:tests/test_formal_migrate.mojo:test-formal-migrate +executor-core:tests/test_formal_fence.mojo:test-formal-fence +executor-core:tests/test_crash.mojo:test-crash +executor-core:tests/test_tasks.mojo:test-tasks +executor-core:tests/test_logs.mojo:test-logs +executor-core:tests/test_usage.mojo:test-usage +executor-core:tests/test_resources.mojo:test-resources +executor-core:tests/test_recommend.mojo:test-recommend +executor-core:tests/test_cgroup.mojo:test-cgroup +executor-core:tests/test_highlight.mojo:test-highlight +executor-core:tests/test_step_cards.mojo:test-step-cards +executor-core:tests/test_graph.mojo:test-graph +executor-core:tests/test_locate.mojo:test-locate +executor-core:tests/test_timeline.mojo:test-timeline +executor-workers:tests/test_control.mojo:test-control +executor-workers:tests/test_pressure.mojo:test-pressure +executor-workers:tests/test_hcloud.mojo:test-hcloud +scheduler-ui:tests/test_toml.mojo:test-toml +scheduler-ui:tests/test_upgrade.mojo:test-upgrade +scheduler-ui:tests/test_config.mojo:test-config +scheduler-ui:tests/test_fill.mojo:test-fill +scheduler-ui:tests/test_metrics.mojo:test-metrics +scheduler-ui:tests/test_remote.mojo:test-remote +scheduler-ui:tests/test_worker.mojo:test-worker-api +scheduler-ui:tests/test_watchdog.mojo:test-watchdog +data-tables:tests/test_tables.mojo:test-tables +data-tables:tests/test_lake_batch.mojo:test-lake-batch +data-tables:tests/test_replace.mojo:test-replace +data-tables:tests/test_formal_retry.mojo:test-formal-retry +data-tables:tests/test_run_token.mojo:test-run-token +data-tables-source:tests/test_tables.mojo:test-tables-source +data-tables-source:tests/test_lake_batch.mojo:test-lake-batch-source' + +functions() { sed -n 's/^def \(test_[A-Za-z0-9_]*\)(.*/\1/p' "$1"; } +usage() { + echo "usage: unit-tests.sh build SHARD | run NAME" >&2 + exit 2 +} + +build() { + shard=$1 + lines=$(printf '%s\n' "$all" | grep "^$shard:") || { + echo "unit-tests.sh: no shard $shard" >&2 + exit 2 + } + defines= + [ "$shard" != data-tables-source ] || defines="-D MORCA_LAKE_SOURCE=1" + gen=build/unit-tests-gen/$shard + mkdir -p "$gen" + main=$gen/unit_tests_main.mojo + { + echo '"""Generated by scripts/unit-tests.sh: the tests of the file' + echo 'basename(argv[0]) names, of the shard '"$shard"'."""' + echo + echo 'from std.sys import argv' + echo 'from std.testing import TestSuite' + echo + for l in $lines; do + f=${l#*:} + f=${f%%:*} + m=$(basename "$f" .mojo) + for t in $(functions "$f"); do echo "from $m import $t as ${m}__$t"; done + if grep -q '^def before_tests()' "$f"; then + echo "from $m import before_tests as ${m}__before_tests" + echo "from $m import after_tests as ${m}__after_tests" + fi + done + echo + echo + echo 'def main() raises:' + echo ' var me = String(argv()[0])' + echo ' var slash = me.rfind("/")' + echo ' var name = String(me[byte = slash + 1 :]) if slash >= 0 else me' + kw=if + for l in $lines; do + name=${l##*:} + f=${l#*:} + f=${f%%:*} + m=$(basename "$f" .mojo) + list="" + for t in $(functions "$f"); do list="$list${m}__$t, "; done + [ -n "$list" ] || { + echo "unit-tests.sh: $f has no test" >&2 + exit 1 + } + echo " $kw name == \"$name\":" + if grep -q '^def before_tests()' "$f"; then + echo " ${m}__before_tests()" + echo ' var failed = False' + echo ' try:' + echo " TestSuite.discover_tests[($list)]().run()" + echo ' except e:' + echo ' failed = True' + echo ' print(e)' + echo " ${m}__after_tests()" + echo ' if failed:' + echo " raise Error(\"$m failed\")" + else + # Its main is not called, so it may only run the tests. + [ "$(sed -n '/^def main()/,$p' "$f" | grep -vc '^[[:space:]]*$')" = 2 ] || { + echo "unit-tests.sh: $f: main does more than run the tests; move that to before_tests() and after_tests()" >&2 + exit 1 + } + echo " TestSuite.discover_tests[($list)]().run()" + fi + kw=elif + done + echo ' else:' + echo ' raise Error(name, ": not a test of this binary")' + } > "$main.tmp" + # Rewritten only when it changed: scripts/mojo-build.sh keys on it. + if cmp -s "$main.tmp" "$main"; then rm -f "$main.tmp"; else mv -f "$main.tmp" "$main"; fi + bin=unit-tests-$shard + # shellcheck disable=SC2086 + scripts/mojo-build.sh $defines -I src -I tests $DEP_INC "$main" -o "build/$bin" + for l in $lines; do + name=${l##*:} + # A link made beside it and moved into place: a test running + # build/ never sees it missing. + ln -sf "$bin" "build/$name.tmp.$$" + mv -f "build/$name.tmp.$$" "build/$name" + done +} + +run() { + name=$1 + l=$(printf '%s\n' "$all" | grep ":$name\$") || { + echo "unit-tests.sh: no test binary $name" >&2 + exit 2 + } + f=${l#*:} + f=${f%%:*} + want=$(functions "$f" | wc -l | tr -d ' ') + log=$(mktemp "${TMPDIR:-/tmp}/unit-tests.XXXXXX") + if "./build/$name" > "$log" 2>&1; then rc=0; else rc=$?; fi + cat "$log" + ran=$(sed -n 's/.* \([0-9][0-9]*\) tests run:.*/\1/p' "$log" | tail -1) + rm -f "$log" + if [ "$rc" != 0 ]; then + exit "$rc" + fi + if [ "${ran:-0}" != "$want" ]; then + echo "unit-tests.sh: $f has $want tests and build/$name ran ${ran:-0}" >&2 + exit 1 + fi +} + +[ "$#" = 2 ] || usage +case "$1" in +build) build "$2" ;; +run) run "$2" ;; +*) usage ;; +esac diff --git a/tests/hcloud.sh b/tests/hcloud.sh index 1b753b1e..97227527 100755 --- a/tests/hcloud.sh +++ b/tests/hcloud.sh @@ -22,7 +22,7 @@ until curl -s -o /dev/null "$MORCA_HCLOUD_ENDPOINT/server_types"; do i=$((i + 1)); [ "$i" -lt 100 ] || { echo "FAIL: the fake didn't start"; exit 1; } sleep 0.1 done -# shellcheck disable=SC2086 -scripts/mojo-build.sh -I src $DEP_INC tests/test_hcloud.mojo -o build/test-hcloud -./build/test-hcloud +# tests/test_hcloud.mojo, from the executor-workers shard's binary. +scripts/unit-tests.sh build executor-workers +scripts/unit-tests.sh run test-hcloud echo "hcloud: ok" diff --git a/tests/test_formal_fence.mojo b/tests/test_formal_fence.mojo index 578b4edf..73c8879a 100644 --- a/tests/test_formal_fence.mojo +++ b/tests/test_formal_fence.mojo @@ -20,7 +20,7 @@ the repository's root. """ from std.os import getenv, setenv -from std.testing import assert_equal +from std.testing import TestSuite, assert_equal from postgres import Connection, Params @@ -128,5 +128,4 @@ def test_fencing_agrees_with_the_model() raises: def main() raises: - test_fencing_agrees_with_the_model() - print("PASS test_fencing_agrees_with_the_model") + TestSuite.discover_tests[__functions_in_module()]().run() diff --git a/tests/test_formal_migrate.mojo b/tests/test_formal_migrate.mojo index 0fd925af..c46fde52 100644 --- a/tests/test_formal_migrate.mojo +++ b/tests/test_formal_migrate.mojo @@ -18,7 +18,7 @@ Run under scripts/with-postgres.sh, from the repository's root. """ from std.os import getenv, setenv, unsetenv -from std.testing import assert_equal +from std.testing import TestSuite, assert_equal from postgres import Connection @@ -140,5 +140,4 @@ def test_the_floor_agrees_with_the_model() raises: def main() raises: - test_the_floor_agrees_with_the_model() - print("PASS test_the_floor_agrees_with_the_model") + TestSuite.discover_tests[__functions_in_module()]().run() diff --git a/tests/test_formal_pins.mojo b/tests/test_formal_pins.mojo index 59c25a04..ad7ca3a6 100644 --- a/tests/test_formal_pins.mojo +++ b/tests/test_formal_pins.mojo @@ -18,7 +18,7 @@ Run from the repository's root. from std.os import remove from std.os.path import exists -from std.testing import assert_equal +from std.testing import TestSuite, assert_equal from morca import Ctx, MemoryStore, RunStore, SqliteStore @@ -123,7 +123,4 @@ def test_sqlite_pins_agree_with_the_model() raises: def main() raises: - test_memory_pins_agree_with_the_model() - print("PASS test_memory_pins_agree_with_the_model") - test_sqlite_pins_agree_with_the_model() - print("PASS test_sqlite_pins_agree_with_the_model") + TestSuite.discover_tests[__functions_in_module()]().run() diff --git a/tests/test_formal_retry.mojo b/tests/test_formal_retry.mojo index 8208752f..d01344ac 100644 --- a/tests/test_formal_retry.mojo +++ b/tests/test_formal_retry.mojo @@ -16,7 +16,7 @@ environment, from the repository's root. """ from std.os import getenv, setenv -from std.testing import assert_equal +from std.testing import TestSuite, assert_equal from iceberg.catalog.filesystem import FilesystemCatalog from iceberg.schema import Schema as IcebergSchema @@ -131,7 +131,26 @@ def test_append_agrees_with_the_model() raises: assert_equal(cases, 363) -def main() raises: +def before_tests() raises: + """A warehouse of its own for the run, before any test.""" _ = setenv("MORCA_WAREHOUSE", String("build/wh-formal-retry-", now_ms())) - test_append_agrees_with_the_model() - print("PASS test_append_agrees_with_the_model") + + +def after_tests() raises: + """Nothing to undo: the process ends after the tests.""" + pass + + +def main() raises: + # scripts/unit-tests.sh, which runs the tests from one binary, calls + # before_tests and after_tests as this does. + before_tests() + var failed = False + try: + TestSuite.discover_tests[__functions_in_module()]().run() + except e: + failed = True + print(e) + after_tests() + if failed: + raise Error("test_formal_retry failed") diff --git a/tests/test_replace.mojo b/tests/test_replace.mojo index e1a0bdf4..748982a0 100644 --- a/tests/test_replace.mojo +++ b/tests/test_replace.mojo @@ -10,7 +10,7 @@ ids say which run each row came from. from std.os import getenv, setenv from std.os.path import exists -from std.testing import assert_equal, assert_true +from std.testing import TestSuite, assert_equal, assert_true from iceberg.catalog.filesystem import FilesystemCatalog from iceberg.schema import Schema as IcebergSchema @@ -380,20 +380,27 @@ def test_append_to_a_replaced_dataset_replaces() raises: assert_equal(_ids(_table("d")), _expected_n(2000, ROWS)) -def main() raises: +def before_tests() raises: + """A warehouse of its own for the run, before any test.""" var root = String("build/wh-replace-", now_ms()) _ = setenv("MORCA_WAREHOUSE", root) - test_a_rerun_replaces_its_partition() - print("PASS test_a_rerun_replaces_its_partition") - test_append_mode_still_appends() - print("PASS test_append_mode_still_appends") - test_a_retry_within_a_run_is_the_uninterrupted_result() - print("PASS test_a_retry_within_a_run_is_the_uninterrupted_result") - test_two_runs_replacing_one_partition_keep_one_runs_rows() - print("PASS test_two_runs_replacing_one_partition_keep_one_runs_rows") - test_a_commit_refuses_a_stale_removal() - print("PASS test_a_commit_refuses_a_stale_removal") - test_an_unpartitioned_table_is_replaced_whole() - print("PASS test_an_unpartitioned_table_is_replaced_whole") - test_append_to_a_replaced_dataset_replaces() - print("PASS test_append_to_a_replaced_dataset_replaces") + + +def after_tests() raises: + """Nothing to undo: the process ends after the tests.""" + pass + + +def main() raises: + # scripts/unit-tests.sh, which runs the tests from one binary, calls + # before_tests and after_tests as this does. + before_tests() + var failed = False + try: + TestSuite.discover_tests[__functions_in_module()]().run() + except e: + failed = True + print(e) + after_tests() + if failed: + raise Error("test_replace failed") diff --git a/tests/test_run_token.mojo b/tests/test_run_token.mojo index 8a5df8c0..006169cb 100644 --- a/tests/test_run_token.mojo +++ b/tests/test_run_token.mojo @@ -10,7 +10,7 @@ Over a local Iceberg table each, with a `Ctx` per attempt as """ from std.os import getenv, setenv -from std.testing import assert_equal +from std.testing import TestSuite, assert_equal from iceberg.catalog.filesystem import FilesystemCatalog from iceberg.schema import Schema as IcebergSchema @@ -106,11 +106,26 @@ def test_a_run_without_a_token_recognises_its_commit() raises: assert_equal(_snapshots("worker"), 1) -def main() raises: +def before_tests() raises: + """A warehouse of its own for the run, before any test.""" _ = setenv("MORCA_WAREHOUSE", String("build/wh-run-token-", now_ms())) - test_a_run_with_another_token_commits_again() - print("PASS test_a_run_with_another_token_commits_again") - test_a_commit_from_before_tokens_is_still_recognised() - print("PASS test_a_commit_from_before_tokens_is_still_recognised") - test_a_run_without_a_token_recognises_its_commit() - print("PASS test_a_run_without_a_token_recognises_its_commit") + + +def after_tests() raises: + """Nothing to undo: the process ends after the tests.""" + pass + + +def main() raises: + # scripts/unit-tests.sh, which runs the tests from one binary, calls + # before_tests and after_tests as this does. + before_tests() + var failed = False + try: + TestSuite.discover_tests[__functions_in_module()]().run() + except e: + failed = True + print(e) + after_tests() + if failed: + raise Error("test_run_token failed")