Application testing
How to test a service that runs on OpenShift: unit tests on the laptop, integration tests against a real database, and a smoke test through the Route after every deploy.
A service on OpenShift has two failure modes a laptop test will not see. The container starts and the process listens, but the Ready probe fails because the database secret is missing. Or the probe passes and the first real request returns 500 because the SQL only works with the migration that production has not applied. Application testing is the set of checks that catch those, plus the ordinary bugs, before a user does.
The platform underneath — operators, storage, failover — is Platform testing. This page is the service itself. The .NET shop in the microservices lab is a concrete place to hang the same checks. The pipeline that runs them is OpenShift Pipelines.
Four layers, in this order
| Layer | Runs where | Proves |
|---|---|---|
| Unit | Laptop, and CI, no cluster | One class does what the test says. Fast, no network. |
| Integration | CI, with a real dependency | The SQL, the Hot Rod call, or the Kafka client works against a real server. |
| Contract | CI | The JSON you send is the JSON the other service accepts. |
| Smoke | The cluster, after deploy | The Route returns the right body, and a row landed. |
Skip a layer and you will try to make the next one do its job. An end-to-end test is a bad unit test: slow, flaky, and vague when it fails. A unit test is a bad smoke test: it never opened the Route.
Unit tests
Keep them in the repository, next to the code, and run them before the image builds. JUnit and pytest are enough. A test that needs a database is not a unit test. Move it down a row. The useful ones here are the ones around money and parsing: a tax calculation, a date in both calendars you accept, a JSON mapper that drops a field.
Run them in the pipeline on every merge. A test that only the author runs locally will be red on the next person's machine, and then it will be deleted.
Integration tests
Use the real database, not a mock of the driver. Testcontainers can start PostgreSQL for a CI job. On a cluster that already has Crunchy, a namespace per pipeline run is the other honest option: apply a small PostgresCluster, migrate, test, delete the namespace.
oc new-project payments-ci-$RUN
oc apply -n payments-ci-$RUN -f postgres-ci.yaml
oc wait -n payments-ci-$RUN pod \
-l postgres-operator.crunchydata.com/role=master \
--for=condition=Ready --timeout=600s
Point the test at the Service DNS and the password from the generated secret. Assert on the row, not on a log line.
SELECT count(*) FROM probe WHERE id = 1;
The count is 1 after the test's POST, and 1 again after the test's POST is repeated. That second assert is the idempotency check. A handler that inserts twice will pass a single "did it return 200" test and double-charge on a retry. The service mesh lab is where those retries appear. The handler has to be safe before you turn them on.
Kafka producers belong in this layer too: produce one record to a topic in the CI namespace, consume it, assert the payload. A mock producer proves you called the mock.
Contract tests
When Orders calls Catalog, a test in the Orders repo should fail if Catalog changes the field price to unitPrice without a version. Keep a recorded example of the response, or a schema, and check the live service against it in CI. This is cheaper than discovering the break in the smoke test, and much cheaper than discovering it on the Route.
A smoke test after deploy
The deploy is not done when the pod is Running. It is done when the request you care about succeeds. A script in the repository, called by the pipeline against the new namespace:
BASE="https://orders-payments.apps.example.com"
curl -fsS "$BASE/health/ready"
code=$(curl -s -o /tmp/order.json -w '%{http_code}' \
-H 'content-type: application/json' \
-d '{"productId":2,"quantity":1}' \
"$BASE/orders")
test "$code" = "201"
python -c 'import json; assert json.load(open("/tmp/order.json"))["quantity"]==1'
curl -fsS fails the script on a 500. The Python line fails it on a body that is a 201 with the wrong shape. Replace the host with the Route oc get route printed. Use a test account and a test product, never a customer's.
Wait on the condition instead of sleeping. oc wait --for=condition=Ready on the Deployment, then one retry loop around the curl if the Route is a second slower than the pod. A sleep 30 is green on a quiet day and red on a busy one, and people learn to click rerun.
Run the smoke test against the namespace you just deployed, not against production, unless the change is a production deploy and the test is safe to run there. A smoke test that creates an order in production needs a path that cancels it.
What the suite is for
A pipeline that is red for a reason the author can explain is healthy. A pipeline that is red because a shared row collided, or because a model endpoint in another country was slow, is a pipeline people will bypass. Keep integration tests on dependencies you run. Keep the external ones, if you truly need them, in a separate job that does not block a hotfix of a payment calculation.
Name the test after the behaviour (order_is_idempotent_when_the_same_key_is_posted_twice), not after the method (testOrder3). The failure in the pipeline log is the only sentence a reviewer reads. When that sentence is the behaviour, the fix starts immediately. When it is testOrder3, the reviewer opens the file, loses the thread, and reruns.
What the test must not do
- Depend on the order of tests in the file. Each one sets up the row it needs.
- Call a public model API with a real customer record. Use the fixture from the benchmarks lab and a model endpoint you are allowed to hit.
- Share one database row across two pipeline runs. The key includes the run id.
- Treat a green pipeline as a reason to skip the smoke test "this once".
A failing test is a blocked deploy. If the team has learned to ignore the pipeline, the tests are either flaky or not theirs. Fix the flaky ones by removing sleeps and shared state, not by adding a retry around the whole suite.
What's next
Platform testing is the drill for the cluster itself: kill a pod, restore a backup, record how long it took. Tracing a smoke-test request across Orders and Catalog is Tracing.