"""Fixed Linux counter fixtures; no host load or privileged runner needed.""" import importlib.util import json import pathlib import subprocess import sys import tempfile import time import unittest spec = importlib.util.spec_from_file_location('ci_load', pathlib.Path(__file__).with_name('ci-load.py')) load = importlib.util.module_from_spec(spec) spec.loader.exec_module(load) class LoadTest(unittest.TestCase): @unittest.skipUnless(sys.platform == 'linux', 'real /proc lifecycle is Linux-only') def test_cli_samples_and_stops_its_own_process(self): with tempfile.TemporaryDirectory() as name: output = pathlib.Path(name)/'load.tsv' command = [sys.executable, '-I', str(pathlib.Path(__file__).with_name('ci-load.py'))] try: subprocess.run(command+['start', str(output), '--duration', '30'], check=True, timeout=10) deadline = time.monotonic()+10 while time.monotonic() < deadline: if output.exists() and len(output.read_text().splitlines()) >= 3: break time.sleep(0.05) finally: subprocess.run(command+['stop', str(output)], check=True, timeout=5) lines = output.read_text().splitlines() self.assertGreaterEqual(len(lines), 3) self.assertTrue(lines[0].startswith('ts_utc\tload1\tsteal_pct')) for line in lines[1:]: fields = line.split('\t') self.assertEqual(len(fields), 10) self.assertTrue(fields[0].endswith('+00:00')) for field in fields[1:]: json.loads(field) # The sampler exited: its pid is gone, and no incomplete note. pid = json.loads(output.with_suffix('.pid.json').read_text())['pid'] self.assertFalse(pathlib.Path(f'/proc/{pid}').exists() and pathlib.Path(f'/proc/{pid}/stat').read_text().split(')')[1].split()[0] != 'Z') self.assertFalse(output.with_suffix('.incomplete').exists()) @unittest.skipUnless(sys.platform == 'linux', 'real /proc lifecycle is Linux-only') def test_stop_records_a_sampler_that_does_not_exit(self): with tempfile.TemporaryDirectory() as name: output = pathlib.Path(name)/'load.tsv' ready = pathlib.Path(name)/'ready' stubborn = subprocess.Popen([sys.executable, '-c', 'import pathlib,signal,sys,time; signal.signal(signal.SIGTERM, signal.SIG_IGN); ' 'pathlib.Path(sys.argv[1]).touch(); time.sleep(30)', str(ready)]) try: deadline = time.monotonic()+10 while not ready.exists(): self.assertIsNone(stubborn.poll(), 'the stand-in exited before it was ready') self.assertLess(time.monotonic(), deadline, 'the stand-in never became ready') time.sleep(0.01) output.with_suffix('.pid.json').write_text(json.dumps(dict(pid=stubborn.pid, start=load.identity(stubborn.pid)))) began = time.monotonic() done = subprocess.run([sys.executable, '-I', str(pathlib.Path(__file__).with_name('ci-load.py')), 'stop', str(output)], capture_output=True, text=True, timeout=10) self.assertEqual(done.returncode, 0) self.assertLess(time.monotonic()-began, 5) self.assertIn('did not exit', output.with_suffix('.incomplete').read_text()) self.assertIn('load evidence incomplete', done.stderr) finally: stubborn.kill() stubborn.wait() def test_cpu_and_disk_interval_not_since_boot(self): # CPU: 100 ticks, 10 iowait, 5 stolen. guest ticks are already in user. before = {'cpu': [100, 0, 20, 800, 20, 0, 0, 10, 3, 0], 'disks': {'nvme0n1': [10, 30]}} after = {'cpu': [130, 0, 25, 850, 30, 0, 0, 15, 7, 0], 'disks': {'nvme0n1': [14, 50], 'new': [1000, 9000]}} cpu, disks = load.interval(before, after) self.assertEqual(cpu, {'steal_pct': 5.0, 'iowait_pct': 10.0}) self.assertEqual(disks, {'nvme0n1': 5.0, 'new': None}) def test_reset_or_no_io_is_unknown_not_zero_latency(self): before = {'cpu': [10]*8, 'disks': {'sda': [10, 100], 'sdb': [20, 200]}} after = {'cpu': [1]*8, 'disks': {'sda': [10, 100], 'sdb': [1, 3]}} cpu, disks = load.interval(before, after) self.assertEqual(cpu, {'steal_pct': None, 'iowait_pct': None}) self.assertEqual(disks, {'sda': None, 'sdb': None}) def test_proc_fields_and_cgroup_ancestor_quota(self): with tempfile.TemporaryDirectory() as name: root = pathlib.Path(name) (root/'pressure').mkdir() (root/'self').mkdir() mount = root/'cg' (mount/'runner/job').mkdir(parents=True) (mount/'cpu.max').write_text('max 100000\n') (mount/'runner/cpu.max').write_text('200000 100000\n') (mount/'runner/job/cpu.max').write_text('max 100000\n') (root/'self/cgroup').write_text('0::/slice/runner/job\n') (root/'self/mountinfo').write_text(f'1 0 0:1 /slice {mount.as_posix()} rw - cgroup2 cgroup rw\n') (root/'loadavg').write_text('3.25 2.00 1.00 1/1 1\n') (root/'stat').write_text('cpu 100 0 20 800 20 0 0 10 3 0\n') (root/'diskstats').write_text('259 0 nvme0n1 2 0 0 6 3 0 0 9 0 0 0\n') for kind in ('cpu', 'io', 'memory'): (root/'pressure'/kind).write_text('some avg10=1.25 avg60=0.50 avg300=0.20 total=12345\n') snapshot = load.read_snapshot(root) self.assertEqual(snapshot['load1'], 3.25) self.assertEqual(snapshot['disks'], {'nvme0n1': [5, 15]}) self.assertEqual(snapshot['psi']['cpu']['some']['total'], 12345) self.assertEqual(snapshot['cpu_max'][str(mount/'runner/cpu.max')], '200000 100000') self.assertEqual(snapshot['errors'], []) (mount/'cpu.max').unlink() snapshot = load.read_snapshot(root) self.assertEqual(snapshot['cpu_max'][str(mount/'runner/cpu.max')], '200000 100000') self.assertIsNone(snapshot['cpu_max'][str(mount/'cpu.max')]) self.assertTrue(snapshot['errors']) def test_missing_inputs_explicitly_reported(self): with tempfile.TemporaryDirectory() as name: snapshot = load.read_snapshot(pathlib.Path(name)) self.assertIsNone(snapshot['load1']) self.assertIsNone(snapshot['cpu']) self.assertTrue(snapshot['errors']) self.assertIsNone(snapshot['psi']['cpu']) self.assertIsNone(snapshot['cpu_max']) if __name__ == '__main__': unittest.main()