Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions scripts/record_demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import subprocess
import sys
import tempfile
import time

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEMOS = os.path.join(ROOT, "docs", "demos")
Expand All @@ -39,6 +40,7 @@
# a demo with no bundle -> what its output must say
OUTPUT = {"why": re.compile(r"remove input:vendor:portal/notes\.md .* CAUSAL")}
PYTHONPATH = ROOT + os.pathsep + os.environ.get("PYTHONPATH", "") # this checkout's tracekit, installed or not
TIMEOUT_S = 300 # a demo's, and its dev signer's start


def missing(name):
Expand All @@ -50,6 +52,30 @@ def missing(name):
return "its person approves on a Unix socket: POSIX only" if configured and os.name != "posix" else None


def dev_signer(env):
"""The dev signer of a demo's `env`, started as autospawn starts one, once it serves; None when it exits first or
doesn't serve within TIMEOUT_S. With every demo starting at once (--check), its first start can take longer than the
10 s a client waits for one it starts itself (autospawn.ensure), which then says Hung of a signer still starting."""
run_dir = env["TRACEKIT_RUNTIME_DIR"]
os.makedirs(run_dir, 0o700)
with open(os.path.join(run_dir, "signer.log"), "ab") as log:
p = subprocess.Popen([sys.executable, "-m", "tracekit", "signer", "serve", "--dev"], cwd=run_dir, env=env,
stdin=subprocess.DEVNULL, stdout=log, stderr=log, umask=0o077)
deadline = time.monotonic() + TIMEOUT_S
try:
while not os.path.exists(os.path.join(run_dir, "endpoint.json")): # it publishes this once it serves
if p.poll() is not None or time.monotonic() > deadline:
p.kill()
p.wait()
return None
time.sleep(0.05)
except BaseException: # interrupted while it starts (Ctrl-C): don't leave it running
p.kill()
p.wait()
raise
return p


def run(name):
"""Run demo `name` in a new temp dir: (its transcript, the temp dir holding its bundle). RuntimeError if it
fails."""
Expand All @@ -61,15 +87,23 @@ def run(name):
PYTHONUNBUFFERED="1", PYTHONPATH=PYTHONPATH)
for k in ("TRACEKIT_SIGNER", "TRACEKIT_SIGNER_TOKEN", "TRACEKIT_SIGNER_TOKEN_FILE", "TRACEKIT_EGRESS_PROXY"):
env.pop(k, None)
signer = None
try:
if not SCENARIOS[name][2] and name not in OUTPUT: # a bundle and no signer of its own: the dev signer's
signer = dev_signer(env)
if signer is None:
raise RuntimeError(f"{name}: the dev signer did not start (its files are in {tmp})")
cmd = ["-m", "tracekit"] if argv[0] == "tracekit" else [os.path.join(ROOT, *argv[0].split("/"))]
p = subprocess.run([sys.executable, *cmd, *(a.replace("$TMP", tmp) for a in argv[1:])], cwd=tmp, env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=300)
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=TIMEOUT_S)
except subprocess.TimeoutExpired as e: # a failure like any other: --check reports the other demos too
raise RuntimeError(f"{name}: {' '.join(argv)} ran past {e.timeout:g} s (its files are in {tmp})\n"
f"{e.stdout or ''}") from e
finally: # the dev signer the example may have started
finally: # the dev signer, if the example used one
subprocess.run([sys.executable, "-m", "tracekit", "down"], cwd=tmp, env=env, capture_output=True)
if signer:
signer.kill()
signer.wait()
if p.returncode:
raise RuntimeError(f"{name}: {' '.join(argv)} exited {p.returncode} (its files are in {tmp})\n{p.stdout}")
text = f"$ {'' if argv[0] == 'tracekit' else 'python '}{' '.join(argv)}\n{p.stdout}"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ def test_a_demo_that_hangs_is_a_failure_not_a_traceback(self):
self.assertFalse(ok)
self.assertIn("ran past 300 s", said)

def test_a_demo_of_the_dev_signer_runs_when_its_signer_starts_slower_than_a_client_waits(self):
"""--check starts every demo at once, and under that load on CI's py3.9 a dev signer's first start took longer
than the 10 s a client waits for one it starts (autospawn.Hung, pid None). Here its start takes 11 s more."""
slow = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, slow, True)
with open(os.path.join(slow, "sitecustomize.py"), "w") as f:
f.write("import sys, time\n"
"if sys.argv[1:] == ['signer', 'serve', '--dev']:\n"
" from tracekit.signer import service\n"
" open_dev = service.open_dev\n"
" service.open_dev = lambda: time.sleep(11) or open_dev() # once it holds signer.lock\n")
with mock.patch.object(record_demos, "PYTHONPATH", slow + os.pathsep + record_demos.PYTHONPATH):
ok, said = record_demos.check("completeness")
self.assertTrue(ok, said)


FAIL_AFTER_THE_PROXY = """
import json, runpy, subprocess, sys
Expand Down
14 changes: 11 additions & 3 deletions tests/test_netserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,27 @@ def do_GET(self):

def log_message(self, *a):
pass
port = self.start(netserver.Server(("127.0.0.1", 0), Handler, timeout=2, deadline=0.3))
srv, done = netserver.Server(("127.0.0.1", 0), Handler, timeout=2, deadline=0.3), threading.Semaphore(0)
shutdown = srv.shutdown_request

def ended(request): # after handle_error: whatever the server logs of a connection is written by now
shutdown(request)
done.release()
srv.shutdown_request = ended
port = self.start(srv)
err = io.StringIO()
with contextlib.redirect_stderr(err):
self.drip(port) # cut mid request line: the server's 400 goes into a closed connection
time.sleep(0.3 * SLOWER)
self.assertTrue(done.acquire(timeout=10))
self.assertEqual(err.getvalue(), "")
with contextlib.redirect_stderr(err):
s = socket.create_connection(("127.0.0.1", port))
self.socks.append(s)
s.sendall(b"GET / HTTP/1.1\r\nHost: x\r\n\r\n")
s.settimeout(3)
# the deadline may cut the connection while the server thread still writes the traceback (Windows CI)
self.assertEqual(s.recv(1), b"")
time.sleep(0.3 * SLOWER)
self.assertTrue(done.acquire(timeout=10))
self.assertIn("ValueError: a bug in the handler", err.getvalue())

def test_a_corrupt_tls_record_logs_no_traceback(self):
Expand Down
21 changes: 17 additions & 4 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
from tracekit.verify import v2

SENSOR = CallerIdentity("mtls", "spiffe://acme/tetragon", True)
# protojson Timestamp, nanoseconds; an hour from now: after the runs the tests register (an event goes to the run open
# at its time)
TIME = (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=1)).strftime("%Y-%m-%dT%H:%M:%S.%f") \
+ "456Z"
# protojson Timestamp, nanoseconds; after the runs the tests register, however long the suite takes to get here (an
# event goes to the run open at its time)
TIME = "2099-10-12T10:00:01.615123456Z"
TS = TIME[:26] + "Z" # as the signer takes it: microseconds
EXEC_ID = "a2luZC1jb250cm9sLXBsYW5lOjExNDI4NjE1NjM2OTAxOjUxNTgz"
PARENT_EXEC_ID = "a2luZC1jb250cm9sLXBsYW5lOjkwNzkyMjU2MjMy"
Expand Down Expand Up @@ -266,6 +265,20 @@ def test_an_event_goes_to_the_run_open_at_its_time(self):
(old,) = self.gaps(self.events("tracekit/signer"), "effect_outside_run")
self.assertIn(f"uid:{ME.subject} has no open run registered by 2026-10-05T10:00:01.615123Z", old["reason"])

def test_the_events_go_to_a_run_registered_long_after_this_file_was_imported(self):
"""TIME was an hour from import, and Windows CI reached these tests more than an hour after collecting them:
their runs registered after their events, which went to no run. A run registered two hours from now still comes
before TIME."""
later = (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=2)).strftime(
"%Y-%m-%dT%H:%M:%S.%fZ")
with mock.patch.object(pipeline, "now_ts", lambda: later):
imp = self.importer()
imp.flush()
run = self.register()
imp.add(exec_line())
self.assertTrue(imp.flush())
self.assertEqual(len(self.typed(self.events(run["run_id"]), "effect.observed")), 1)

def test_an_event_a_little_before_its_run_registered_is_the_runs(self):
"""The sensor's clock a little behind the signer's: no older run is open, so the run just registered takes it."""
imp = self.importer()
Expand Down
Loading