omnet
aien-dev/omnet
Brought by drakestapleton
omnet / cmd / tkad / test / rollover.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | import json |
| 4 | import os |
| 5 | import pathlib |
| 6 | import socket |
| 7 | import struct |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import tempfile |
| 11 | import time |
| 12 | |
| 13 | root, binary = map(pathlib.Path, sys.argv[1:]) |
| 14 | source = pathlib.Path(str(binary) + ".om").read_text() |
| 15 | needle = "fn dsbatch fd int to void\nfor i ulong 100000" |
| 16 | assert source.count(needle) == 1 |
| 17 | omc = subprocess.check_output(["sh", str(root / "tools/omc.sh")], text=True).strip() |
| 18 | built = subprocess.run([omc], input=source.replace(needle, needle.replace("100000", "2")).encode(), capture_output=True) |
| 19 | assert built.returncode == 0, built.stderr.decode() |
| 20 | epoch = root / "build/tkad-epoch2" |
| 21 | epoch.write_bytes(built.stdout); epoch.chmod(0o700) |
| 22 | frame = struct.pack("<4sBBHQI", b"TKA\x01", 11, 1, 0, 0, 2) + b"{}" |
| 23 | |
| 24 | |
| 25 | def exact(s, n): |
| 26 | out = b"" |
| 27 | while len(out) < n: |
| 28 | v = s.recv(n-len(out)) |
| 29 | assert v, "truncated rollover reply" |
| 30 | out += v |
| 31 | return out |
| 32 | |
| 33 | |
| 34 | seen = {} |
| 35 | |
| 36 | |
| 37 | def response(s): |
| 38 | magic, status, reserved, n = struct.unpack("<4sHHI", exact(s, 12)) |
| 39 | assert magic == b"TKA\x01" and status == 200 and reserved == 0 |
| 40 | result = json.loads(exact(s, n)); s.close() |
| 41 | assert (result["TKAInfo"], result["Revision"], result["Nodes"]) == (None, 0, []), result |
| 42 | ident = result["StoreID"] |
| 43 | assert len(ident) == 32, result |
| 44 | # The epoch exec replaces the process image; the store identity must not move. |
| 45 | assert seen.setdefault("id", ident) == ident, "identity changed across the epoch boundary" |
| 46 | |
| 47 | |
| 48 | with tempfile.TemporaryDirectory(prefix="tkad-rollover-") as name: |
| 49 | path = pathlib.Path(name) |
| 50 | p = subprocess.Popen([str(epoch), name, "--init"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 51 | try: |
| 52 | deadline = time.monotonic()+10 |
| 53 | while not (path/"tkad.sock").exists(): |
| 54 | assert p.poll() is None, p.communicate() |
| 55 | assert time.monotonic() < deadline |
| 56 | time.sleep(.01) |
| 57 | pid = p.pid |
| 58 | # Connect both before sending the first request. The next connection is |
| 59 | # queued while the epoch boundary exec replaces the process image. |
| 60 | for _ in range(8): |
| 61 | a = socket.socket(socket.AF_UNIX); a.settimeout(10); a.connect(str(path/"tkad.sock")) |
| 62 | b = socket.socket(socket.AF_UNIX); b.settimeout(10); b.connect(str(path/"tkad.sock")) |
| 63 | a.sendall(frame); b.sendall(frame) |
| 64 | response(a); response(b) |
| 65 | assert p.pid == pid and p.poll() is None |
| 66 | link = os.readlink(f"/proc/{pid}/fd/66") |
| 67 | for _ in range(4): |
| 68 | s = socket.socket(socket.AF_UNIX);s.settimeout(10);s.connect(str(path/"tkad.sock"));s.sendall(frame);response(s) |
| 69 | assert os.readlink(f"/proc/{pid}/fd/66") == link, "listener replaced" |
| 70 | contender = subprocess.run([str(binary),name],capture_output=True,timeout=10) |
| 71 | assert contender.returncode == 73, (contender.returncode,contender.stderr) |
| 72 | finally: |
| 73 | p.kill();p.communicate(timeout=10) |
| 74 | missing = subprocess.run([str(epoch),"--resume"],capture_output=True,timeout=10) |
| 75 | assert missing.returncode == 73 |
| 76 | directory = os.open(name,os.O_RDONLY|os.O_DIRECTORY) |
| 77 | wrong = socket.socket(socket.AF_UNIX);wrong.bind("\0tkad-invalid-resume-"+str(os.getpid())) |
| 78 | def swapped(): |
| 79 | os.dup2(wrong.fileno(),64,inheritable=True) |
| 80 | os.dup2(directory,65,inheritable=True) |
| 81 | bad = subprocess.run([str(epoch),"--resume"],preexec_fn=swapped,close_fds=False,capture_output=True,timeout=10) |
| 82 | assert bad.returncode == 73 |
| 83 | wrong.close();os.close(directory) |
| 84 | print("PASS tkad epoch2 self-exec: queued clients, stable PID/listener, retained exclusion, missing/swapped FD refusal") |