Tests of the Python layer

The Python layer has two things to prove: that it speaks the Protocol to the core and that it reports the core status to the client. Both are tested against the real server and the real core, because a mock would prove neither. The test file starts the service as a subprocess on a free port, waits for /health, runs six cases and stops it.

The test file

#!/usr/bin/env python3 """Tests of the HTTP layer, with the real server and the real COBOL core.""" import json import socket import subprocess import sys import time import unittest import urllib.error import urllib.request REQUEST_OK = b"C00001WIDGET0100002000123456N1" REQUEST_REJECTED = b"C00002WIDGET0200000000100000N1" def free_port(): with socket.socket() as sock: sock.bind(("127.0.0.1", 0)) return sock.getsockname()[1] class ServiceTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.port = free_port() cls.base = f"http://127.0.0.1:{cls.port}" cls.server = subprocess.Popen( [sys.executable, "-I", "server.py", str(cls.port)], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, ) deadline = time.time() + 5 while time.time() < deadline: try: urllib.request.urlopen(cls.base + "/health", timeout=1).read() return except OSError: time.sleep(0.05) raise RuntimeError("the service did not start") @classmethod def tearDownClass(cls): cls.server.terminate() cls.server.wait(timeout=5) if cls.server.stdout is not None: cls.server.stdout.close() def post(self, path, body, content_type="text/plain"): request = urllib.request.Request( self.base + path, data=body, headers={"Content-Type": content_type}, method="POST", ) try: with urllib.request.urlopen(request, timeout=10) as response: return response.status, response.read() except urllib.error.HTTPError as error: body = error.read() error.close() return error.code, body def test_health(self): with urllib.request.urlopen(self.base + "/health", timeout=5) as response: document = json.loads(response.read()) self.assertEqual(response.status, 200) self.assertEqual(document["status"], "ok") self.assertTrue(document["core_exists"]) def test_price_ok(self): status, body = self.post("/price", REQUEST_OK) line = body.decode("ascii") self.assertEqual(status, 200) self.assertEqual(len(line.rstrip("\n")), 50) self.assertEqual(line[6:8], "00") self.assertEqual(int(line[8:19]), 246912) def test_price_rejected(self): status, body = self.post("/price", REQUEST_REJECTED) self.assertEqual(status, 422) self.assertEqual(body.decode("ascii")[6:8], "04") def test_short_request(self): status, body = self.post("/price", REQUEST_OK[:-1]) document = json.loads(body) self.assertEqual(status, 400) self.assertEqual(document["got"], 29) def test_json_ok(self): body = json.dumps({"order_id": "C00007", "sku": "WIDGET07", "qty": 4, "price_cents": 2500, "region": "S2"}).encode() status, answer = self.post("/price/json", body, "application/json") document = json.loads(answer) self.assertEqual(status, 200) self.assertEqual(document["net_cents"], 10000) self.assertEqual(document["tax_cents"], 1300) def test_json_bad_region(self): body = json.dumps({"order_id": "C00009", "sku": "WIDGET09", "qty": 1, "price_cents": 100, "region": "Q7"}).encode() status, answer = self.post("/price/json", body, "application/json") self.assertEqual(status, 400) self.assertIn("unknown region", json.loads(answer)["error"]) if __name__ == "__main__": unittest.main()

The setup is the part worth copying. The port comes from the operating system through a socket bound to port zero, so two runs cannot collide and the test does not need a fixed number. The wait loop polls /health instead of sleeping, so a fast machine runs the tests in a tenth of a second instead of a fixed two. The teardown terminates the process and closes its pipe, which is what stops a suite from leaving a server behind.

The run

python3 -I -m py_compile server.py test_service.py
python3 -I test_service.py -v
echo $?

test_health (__main__.ServiceTest.test_health) ... ok
test_json_bad_region (__main__.ServiceTest.test_json_bad_region) ... ok
test_json_ok (__main__.ServiceTest.test_json_ok) ... ok
test_price_ok (__main__.ServiceTest.test_price_ok) ... ok
test_price_rejected (__main__.ServiceTest.test_price_rejected) ... ok
test_short_request (__main__.ServiceTest.test_short_request) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.185s

OK
0

Six tests in a hundred and fifty milliseconds, because each one is a request to a process that is already running. The price of that speed is that the tests are integration tests: they need the core binary, they start a server, and a bug in the COBOL makes them fail. That is the right level for a wrapper whose whole job is to call another program.

What the tests cover

TestWhat it provesLayer
healththe service answers and the core file existsPython
price ok200, a fifty-byte answer, status 00, net 246912both
price rejected422 and status 04 from the coreboth
short request400 with the length, the core never startedPython
JSON okthe conversion in both directions, 10000 net centsPython
JSON bad region400 before the core, with the reasonPython

The three failures of the error page that are not here are the ones that need a different server: a missing core, a slow core and a wrong path each start their own service with another environment. They are worth a test each, and they need a helper that starts a server with a modified environment rather than the one setUpClass builds.

The COBOL side of the tests

The core has its own test driver on the core page: five cases in a COBOL table, a pass or fail line each, and a status of 1 when anything fails. The two suites overlap on purpose. The COBOL driver tests the rules without the service, the Python tests check that the rules reach the client through the protocol, and a failure in only one of them says which layer broke.

The next page calls the same core without a process at all, through a shared module and ctypes, and measures what the process costs.

The COBOL was built with cobc (GnuCOBOL) 3.2.0 with -O2, the tests run on Python 3.14.10 with the standard library only, and both ran on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4.

Article author: Arthur Isaev