Testing
There are three test suites. They are split by what each one needs to run, rather than by a marker, so that the split cannot be defeated by a test that forgets to declare itself:
tests/The holdings-free suite. It needs no database and no PDS files.
pytestwith no arguments runs this one and nothing else, becausetestpathsinpyproject.tomlnames it.import_tests/The import pipeline, end to end, against a few megabytes of real archive metadata checked into the repository. It needs a MySQL server, but no PDS holdings and no database anyone prepared. The Mini-Holdings Import Fixture describes the fixture it runs against.
integration_tests/What OPUS answers, checked against a database a real import has populated. It needs that database, and some of its tests need the PDS holdings behind it, so it runs on a machine at the Ring-Moon Systems Node.
The second and third are asked for by name. That is what keeps the everyday pytest
fast and offline.
Measuring how fast the import and the web application are is not a test suite: it is
the benchmark harness under benchmarks/, which Benchmarks describes.
Its own code is unit-tested under tests/benchmarks/ like everything else.
What every run needs first
Set OPUS_CONFIG before running any of them, including the suites that never
touch a database:
export OPUS_CONFIG=$PWD/tests/fixtures/opus_ci.toml
tests/fixtures/opus_ci.toml is a checked-in dummy configuration: its credentials
connect to nothing and the paths it names are under /tmp. Point the variable at a real
installation’s file only when running the integration suite.
It is needed for every run, and not only for the tests that use Django, because
pyproject.toml names opus_app.settings as the Django settings module for pytest.
pytest-django imports that module once when the session starts, and importing it reads
the OPUS configuration file – so a session that collects nothing but
tests/opus_support still reads it. Without the variable, pytest stops during startup
with ConfigError naming it, before a single test runs.
Running the holdings-free suite
pytest # the whole suite
pytest -n auto --dist loadscope # the same, in parallel, as CI runs it
pytest tests/opus_support/test_units.py # one file
pytest -k parse_form_type # one test by name
It takes seconds, needs nothing installed but the dev extra, and is what to run while
working on anything.
--dist loadscope keeps each test module on one worker, which matters for the modules
that mock time or share a fixture.
Warnings are errors here, so a third-party deprecation cannot rot unnoticed. Admitting one
that cannot be fixed here means adding a narrowly-scoped filterwarnings entry to
pyproject.toml, with a comment saying which package raises it and why.
Coverage is deliberately not measured by this suite; Two coverage configurations
says which run measures what, and why pytest --cov on its own needs
--cov-fail-under=0.
Running the import suite
pytest import_tests needs a MySQL server and nothing else – no holdings, no import,
no prepared database.
It looks for that server in two places. OPUS_TEST_DB_HOST, OPUS_TEST_DB_USER and
OPUS_TEST_DB_PASSWORD name one for the suite alone, and are read as a set: if any of
the three is set, the environment is what describes the server, and the other two default
to root with no password on 127.0.0.1. If none of them is set, the host, user and
password come from the [database] table of the OPUS_CONFIG file – which every run
of the suite already has, since pytest-django configures Django from it at collection
– so a configuration that already names your own MySQL server needs no second set of
variables.
Only the host, user and password are taken from that file; the schema it names is never
touched. Each run creates schemas named opus_import_test_<pid> and drops every one of
them when the session ends, pass or fail, so nothing is left on the server to clean up.
The account therefore needs every privilege on the schemas named
opus_import_test_%: it creates and drops them, and in them creates, alters and drops
tables and views (obs_general is a view), and reads and writes their rows.
There is no third source, and no guessing between the two. With OPUS_CONFIG pointing
at tests/fixtures/opus_ci.toml – which is what scripts/run-all-checks.sh sets
when you have not exported one – the suite tries to connect as that dummy configuration’s
opus_ci_user and fails rather than falling back to root, and a file that cannot be
read at all stops the session outright. Export a real configuration, or the three
variables, to run against something else.
It runs serially: the run is one long import followed by fast assertions, so -n buys
nothing, and a session-scoped fixture under xdist would run the import once per worker
into the same schema.
The import counts the cores of the machine a session runs on, and the main run’s
workers = "max" and index_workers = "max" resolve to the ceiling they give; the
goldens do not depend on the count. A session that asks for a fixed count asks for no
more than a four-core machine’s ceiling, 3, so it is never lowered on a GitHub-hosted
runner. The main run computes its observations in a pool at that ceiling, in tasks of 5
index rows so that the fixture’s small indexes split across several workers, and builds
its keys over as many connections; a session that sets no workers of its own, as the
negative cases do not, computes in one process (The Mini-Holdings Import Fixture).
OPUS_IMPORT_FAULT is set by the suite itself, in the environment of an
opus_import process it starts, to make the import fail on purpose at a named point
and prove it recovers. Its form is <site>:<argument>
(opus_import.import_fault); the import reads it only at the sites that ask for it,
and each site decides what its argument selects. The sites are:
swap_inBetween the fill of a rebuilt table and the rename that swaps it in; its argument is a table.
swap_in:definitionsstops the dictionary’s swap.log_errorInside one bundle’s import; its argument is a bundle.
log_error:COISS_2002logs one error while that bundle is imported and carries on, so the bundle endsfailedin the bundle registry without the import raising (The bundle registry: opus_import.bundle_registry and opus_import.steps.do_register).stall_workerAt the start of every task a pool worker computes; its argument is a number of seconds.
stall_worker:600holds each worker asleep, so a test can find the pool’s processes alive and kill the import under them (opus_import.steps.do_import_pool).fail_workerIn the pool worker task holding one index row; its argument is the row, numbered from zero.
fail_worker:12fails that task; the import’s process meets the failure when it reaches the task in index order, having replayed and loaded the tasks before it, so a test can see that the failed index leaves none of its rows.
The three hold_ sites take a file’s path as their argument. An importer waits at
the site until the file is removed or
HOLD_LIMIT_SECONDS pass, whichever comes first, so
a test holds importers there however slowly the computer runs, as long as it removes the
file within that limit; an importer that reaches the site after the file is gone does not
wait.
hold_after_claimAfter a queue importer’s claim of a bundle commits, before it imports the bundle.
hold_after_claim:<path>lets a test start severalopus_import --joinprocesses and know that they hold bundles at the same time (opus_import.steps.do_import).hold_after_chunkAfter the first chunk of each bundle’s rows commits.
hold_after_chunk:<path>holds an import mid-bundle, so a test can kill an importer or its lock connection there.hold_before_lockBetween an importer’s
import_processinsert and its lock.hold_before_lock:<path>lets a test read the importers’ table while the row is written and not yet committed, for as long as its reads take.
Never set it outside a test.
That plain form is the everyday one – no coverage, with the three executed-functions tests skipping because the report they read is not there. Measuring coverage takes two commands, and under Python 3.12 several times as long; The Mini-Holdings Import Fixture gives both and says when each is wanted.
Running the integration suite
integration_tests/ is what proves that a change did not alter what OPUS returns. It
holds the golden-response API tests, which compare every documented API call’s answer byte
for byte, and the live-database tests of the Django apps.
It needs a database that a real import has populated, and a few of its tests need the
PDS holdings behind that database as well. Populating a database to run it against is
how to get one. With OPUS_CONFIG naming that installation’s configuration file:
pytest integration_tests
It is not generally runnable. A real import reads the PDS3 and PDS4 holdings, which are terabytes on a machine at the Ring-Moon Systems Node; without them there is no database to run against. A developer working elsewhere runs the other two suites, and The Mini-Holdings Import Fixture is what covers the import pipeline for them.
It runs serially, deliberately: every test shares one database, and some of them
empty user_searches and search_results between tests, so -n would have workers
pulling the ground out from under each other.
Three markers describe what a test reaches for, and --strict-markers is on, so every
one of them is declared in pyproject.toml:
integrationApplied by
integration_tests/conftest.pyto everything it collects.holdingsReads a product file out of the holdings tree, not just the database row naming it.
livetestQueries an OPUS server outside this process, rather than the application in it.
Populating a database to run it against
Two ways, and they do the same work: by hand, which is what to use while developing and what shows what each step does, and in one command, which is what the self-hosted CI job runs. Both need the holdings, a MySQL server, and a checkout with the distribution installed.
The scripts divide along the same line, which is why they are in two directories:
scripts/import/ holds the wrappers a person runs and answers prompts from, and
scripts/automated_tests/ holds the unattended chain, which calls those same wrappers
with the prompts answered for it. Nothing is duplicated between them.
By hand
Write a configuration file naming a test schema – a name you will recognize as disposable – the holdings roots, and directories for logs, downloads and site data.
opus_config_templatewrites the file to copy; setOPUS_CONFIGto your copy. The configuration file OPUS reads describes every key.Import the test bundles:
./scripts/import/import_for_tests.sh
It prints the schema it is about to erase and asks for confirmation before doing anything, which is the check that keeps it off a database you meant to keep. It then imports a fixed list of bundle sets – Cassini ISS, UVIS, VIMS and CIRS, Galileo, Voyager, Hubble, New Horizons, and the occultation sets – through
opus_import_all, with[import] importersimporters at once, which finishes the database, and then runsmanage.py migrate. Options ofopus_import_allgiven to the script are passed on:--workers 24 --importers 6divides 24 workers among six importers. ReadERRORS.logafterwards rather than trusting the exit status: several import steps report failure through the log and still exit zero.Run the suite:
pytest integration_tests
To reproduce what CI measures rather than just what it asserts, run it the way
scripts/automated_tests/opus_run_unittests_coverage.shdoes – one serial invocation oftests/opus_support,tests/opus_appandintegration_teststogether, underCOVERAGE_RCFILE=integration_tests/.coveragerc. Two coverage configurations says why all three are in one run.
In one command
./scripts/automated_tests/opus_main_test.sh
./scripts/automated_tests/opus_check_coverage.sh
The first does the whole chain unattended: it makes a per-run directory, writes an
opus.toml naming a schema of its own (opus_test_db_<id>, from a timestamp), runs
the import with the confirmation answered for it and gates on both the exit status and
ERRORS.log, runs the three suites under the integration coverage configuration, then
drops the schema and deletes the directories it made. Nothing it creates outlives it,
which is why it is safe to run on a machine that has a real database on it.
It is not the coverage gate. It exits 0 on a run whose coverage is 99%.
opus_check_coverage.sh is the gate, and the self-hosted workflow runs it as a separate
step so that a coverage failure still reaches codecov first – which is why reproducing CI
locally means running the second command yourself.
Both scripts read their machine’s own settings from ~/opus_runner_secrets, a file that
is not in the repository. It sets OPUS_DB_USER and OPUS_DB_PASSWORD, TEST_ROOT
(where the per-run directories go) and PDS_HOLDINGS_ROOT (the directory holding
holdings and pds4-holdings). Every script that needs one of them exits rather than
continue if it is missing.
The opus.toml the chain writes asks for workers = "max" and
index_workers = "max", so the import computes and builds keys at the ceiling of the
cores it counts on the machine running it.
Two coverage configurations
They measure different things, and each is gated by exactly one command:
[tool.coverage]inpyproject.tomlmeasuresopus_support,opus_config,opus_importandopus_log_analyzer– the Django application is not in it. TheImport Testsjob runspytest import_tests --cov, andfail_underis the floor that one run has to stay at or above. Any other suite measured against this configuration reaches far less of those four packages, which is why a plainpytest --covhas to pass--cov-fail-under=0to mean anything.integration_tests/.coveragercmeasuressrc/opus_app/apps,integration_tests/test_apiandsrc/opus_support, and is gated at 100%. That is whytests/opus_support,tests/opus_appandintegration_testsare run together in one invocation: the gate measures what all three reach between them, so dropping one deflates the figure rather than failing the run.
It is a separate file rather than another section of pyproject.toml because
coverage.py ignores include whenever source is set, so one merged configuration
would silently corrupt one gate or the other. Select it with COVERAGE_RCFILE.
Running every check
scripts/run-all-checks.sh runs everything this repository gates on – the tests and
every other check – in parallel by default. It sets OPUS_CONFIG to the checked-in
dummy configuration itself, so a full run needs nothing but a checkout:
./scripts/run-all-checks.sh # everything
./scripts/run-all-checks.sh -c # only the code checks
./scripts/run-all-checks.sh -d # only Sphinx and PyMarkdown
./scripts/run-all-checks.sh --mypy # one check
./scripts/run-all-checks.sh -s # sequentially, for readable output
./scripts/run-all-checks.sh --import-tests # everything, plus the import suite
What it runs, and what each one is configured by:
Check |
Command |
Configuration |
|---|---|---|
Lint |
|
|
Format |
|
|
Types |
|
|
Tests |
|
|
Packaging |
|
the |
Security |
|
|
Dead code |
|
|
Documentation |
|
|
Markdown |
|
|
The tests it runs are the holdings-free suite alone; every other check reads files. The other two suites need a server or a database, so neither is in a default run.
--import-tests adds the import suite to whatever else is running – on its own, to the
full run; alongside another --* flag, to that selection. It is opt-in because it is the
one check that needs a reachable MySQL server, and because it is the slowest check by
far. It uses the bare form, without
coverage, and finds its server itself, from OPUS_TEST_DB_HOST, OPUS_TEST_DB_USER
and OPUS_TEST_DB_PASSWORD or, with none of those set, from the OPUS_CONFIG file –
which this script points at tests/fixtures/opus_ci.toml unless you exported one, and
that file’s credentials are dummies. A failure fails the script, like any other check.
Each check has an ENABLE_* toggle at the top of the script, so one that is not yet
expected to pass can be switched off in one place rather than deleted.