test: scaffold isolated grpc data docker fixtures
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""Static standalone rig contract checks; no Compose invocation or network I/O."""
|
||||
from pathlib import Path
|
||||
import re
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMPOSE = ROOT / "compose.yml"
|
||||
DOCKERFILE = ROOT / "Dockerfile"
|
||||
|
||||
|
||||
def service_block(document, service):
|
||||
match = re.search(rf"(?ms)^ {re.escape(service)}:\s*\n(.*?)(?=^ [a-z][a-z0-9-]*:|\Z)", document)
|
||||
if match is None:
|
||||
raise AssertionError(f"missing service {service}")
|
||||
return match.group(1)
|
||||
|
||||
|
||||
def extension_block(document, name):
|
||||
match = re.search(rf"(?ms)^{re.escape(name)}:\s*&[a-z-]+\s*\n(.*?)(?=^[a-z][a-z0-9-]*:|\Z)", document)
|
||||
if match is None:
|
||||
raise AssertionError(f"missing extension {name}")
|
||||
return match.group(1)
|
||||
|
||||
|
||||
class StandaloneComposeContractTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.compose = COMPOSE.read_text(encoding="utf-8")
|
||||
cls.dockerfile = DOCKERFILE.read_text(encoding="utf-8")
|
||||
|
||||
def test_run_scoped_project_revision_and_no_fixed_container_names_or_shared_hosts(self):
|
||||
self.assertRegex(self.compose, r"(?m)^name:\s*\$\{GRPC_DATA_PROJECT:\?[^}]+\}")
|
||||
self.assertIn("SOURCE_REVISION", self.compose)
|
||||
self.assertNotIn("container_name:", self.compose)
|
||||
self.assertNotIn("10.100.0.35", self.compose)
|
||||
self.assertNotIn("host.docker.internal", self.compose)
|
||||
self.assertNotIn("docker-dev/docker-compose.yml", self.compose)
|
||||
self.assertIn("SourceRevisionId", self.dockerfile)
|
||||
|
||||
def test_three_independent_pairs_with_self_first_seed_nodes(self):
|
||||
common = extension_block(self.compose, "x-common-env")
|
||||
control = extension_block(self.compose, "x-control-env")
|
||||
data = extension_block(self.compose, "x-data-env")
|
||||
self.assertIn("MeshTransport__Mode: Grpc", common)
|
||||
self.assertIn("Telemetry__Mode: Grpc", common)
|
||||
self.assertIn("ConfigSource__Mode: Direct", control)
|
||||
self.assertIn("ConfigSource__Mode: FetchAndCache", data)
|
||||
self.assertIn("cluster-CONTROL", control)
|
||||
for pair in ("control", "source", "consumer"):
|
||||
for suffix, partner in (("1", "2"), ("2", "1")):
|
||||
node = f"{pair}-{suffix}"
|
||||
block = service_block(self.compose, node)
|
||||
self.assertIn("<<: *host", block)
|
||||
expected_env = "control-env" if pair == "control" else "data-env"
|
||||
self.assertIn(f"<<: *{expected_env}", block)
|
||||
if pair != "control":
|
||||
self.assertIn(f"cluster-{pair.upper()}", block)
|
||||
self.assertIn(f"Cluster__SeedNodes__0: akka.tcp://otopcua@{node}:4053", block)
|
||||
self.assertIn(f"Cluster__SeedNodes__1: akka.tcp://otopcua@{pair}-{partner}:4053", block)
|
||||
for other in ({"control", "source", "consumer"} - {pair}):
|
||||
self.assertNotIn(f"akka.tcp://otopcua@{other}-", block)
|
||||
self.assertIn("/app/data", block)
|
||||
|
||||
def test_local_sql_device_and_generated_tls_mounts(self):
|
||||
self.assertIn(" sql:", self.compose)
|
||||
self.assertIn(" device:", self.compose)
|
||||
for node in ("source-1", "source-2", "consumer-1", "consumer-2"):
|
||||
block = service_block(self.compose, node)
|
||||
self.assertIn("GRPC_DATA_CERT_DIR", block)
|
||||
self.assertIn("/run/grpc-data/tls", block)
|
||||
self.assertIn("P07_TLS_BINDING_PENDING", self.compose)
|
||||
self.assertIn("device:5020", self.compose)
|
||||
self.assertRegex(self.compose, r"127\.0\.0\.1::(?:4840|9000)")
|
||||
self.assertNotRegex(self.compose, r"(?m)^\s+- ['\"]?0\.0\.0\.0:")
|
||||
|
||||
def test_no_tracked_secrets_or_private_keys(self):
|
||||
for line in self.compose.splitlines():
|
||||
match = re.match(r"^\s*(?:SA_PASSWORD|ZB_SECRETS_MASTER_KEY|Security__ApiKeys__Pepper):\s*(.*)$", line)
|
||||
if match:
|
||||
self.assertTrue(match.group(1).startswith("${"), line)
|
||||
self.assertNotIn("BEGIN PRIVATE KEY", self.compose + self.dockerfile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,78 @@
|
||||
"""Pure Modbus fixture tests; no sockets, Docker, or production process starts."""
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "device"))
|
||||
from server import DeviceState, apply_control, handle_modbus_pdu
|
||||
|
||||
|
||||
def request(function, address, value):
|
||||
return bytes([function]) + struct.pack(">HH", address, value)
|
||||
|
||||
|
||||
def words(response):
|
||||
return struct.unpack(f">{response[1] // 2}H", response[2:])
|
||||
|
||||
|
||||
class ModbusFixtureTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.state = DeviceState()
|
||||
|
||||
def test_counter_advances_only_by_explicit_bounded_control(self):
|
||||
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (0,))
|
||||
apply_control(self.state, {"action": "advance", "steps": 3})
|
||||
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (3,))
|
||||
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (3,))
|
||||
with self.assertRaises(ValueError):
|
||||
apply_control(self.state, {"action": "advance", "steps": 1001})
|
||||
self.assertEqual(self.state.snapshot()["counter"], 3)
|
||||
|
||||
def test_scalar_and_array_registers_have_deterministic_readback(self):
|
||||
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 10, 3))), (7, 11, 13))
|
||||
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 20, 42)), request(6, 20, 42))
|
||||
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 20, 1))), (42,))
|
||||
payload = bytes([16]) + struct.pack(">HHBHH", 30, 2, 4, 0x1234, 0x00FF)
|
||||
self.assertEqual(handle_modbus_pdu(self.state, 1, payload), request(16, 30, 2))
|
||||
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 30, 2))), (0x1234, 0x00FF))
|
||||
|
||||
def test_each_committed_write_has_one_effect_and_repeated_value_is_a_new_request(self):
|
||||
write = request(6, 20, 42)
|
||||
handle_modbus_pdu(self.state, 1, write)
|
||||
handle_modbus_pdu(self.state, 1, write)
|
||||
ledger = self.state.snapshot()["effects"]
|
||||
self.assertEqual([item["sequence"] for item in ledger], [1, 2])
|
||||
self.assertEqual([item["values"] for item in ledger], [[42], [42]])
|
||||
self.assertEqual(self.state.snapshot()["effect_total"], 2)
|
||||
|
||||
def test_response_loss_is_armed_for_one_write_after_effect(self):
|
||||
apply_control(self.state, {"action": "drop_next_write_response"})
|
||||
self.assertIsNone(handle_modbus_pdu(self.state, 1, request(6, 20, 42)))
|
||||
snapshot = self.state.snapshot()
|
||||
self.assertEqual(snapshot["effect_total"], 1)
|
||||
self.assertEqual(snapshot["effects"][0]["values"], [42])
|
||||
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 20, 43)), request(6, 20, 43))
|
||||
self.assertEqual(self.state.snapshot()["effect_total"], 2)
|
||||
|
||||
def test_malformed_and_out_of_range_requests_never_mutate(self):
|
||||
invalid = [b"", b"\x06\x00", request(3, 0, 0), request(3, 0, 126),
|
||||
request(6, 999, 1), bytes([16]) + struct.pack(">HHB", 30, 2, 2) + b"\x00\x01"]
|
||||
before = self.state.snapshot()
|
||||
for payload in invalid:
|
||||
with self.subTest(payload=payload), self.assertRaises(ValueError):
|
||||
handle_modbus_pdu(self.state, 1, payload)
|
||||
self.assertEqual(self.state.snapshot(), before)
|
||||
|
||||
def test_control_rejects_unknown_actions_and_unbounded_input(self):
|
||||
for command in ({"action": "set", "address": 20, "value": -1},
|
||||
{"action": "set", "address": 999, "value": 3},
|
||||
{"action": "advance", "steps": True},
|
||||
{"action": "unknown"}):
|
||||
with self.subTest(command=command), self.assertRaises(ValueError):
|
||||
apply_control(self.state, command)
|
||||
self.assertEqual(self.state.snapshot()["effect_total"], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user