#!/usr/bin/env python3 """Negative controls for verify_route117_stepcheck.py (job #4434). The checker is run in a scratch copy of the package; each control corrupts exactly one value and the checker must exit non-zero. Scratch directories live under ``outputs/job4434/_controls/`` (no system temp, which the sandbox denies here). python3 test_verify_route117_stepcheck.py """ import json import os import shutil import subprocess import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) CHECKER = "verify_route117_stepcheck.py" CTL = os.path.join(HERE, "_controls") NEEDED = ["stepcheck_facts.json", "dgate30.json", "dgate_saving.json", CHECKER] SERVED = ["f1833_dbound2612.json", "f1812_sweep2775b.json"] def run(d): """Run the checker inside ``d``; return its exit code (no pipes: DEVNULL only).""" p = subprocess.run([sys.executable, CHECKER, "--dir", "."], cwd=d, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) return p.returncode def make_copy(name): d = os.path.join(CTL, name) if os.path.isdir(d): shutil.rmtree(d) os.makedirs(os.path.join(d, "served")) for f in NEEDED: shutil.copy2(os.path.join(HERE, f), os.path.join(d, f)) for f in SERVED: shutil.copy2(os.path.join(HERE, "served", f), os.path.join(d, "served", f)) return d def edit_json(path, fn): with open(path, encoding="utf-8") as fh: obj = json.load(fh) fn(obj) with open(path, "w", encoding="utf-8") as fh: json.dump(obj, fh, indent=1, sort_keys=True) class TestControls(unittest.TestCase): @classmethod def tearDownClass(cls): if os.path.isdir(CTL): shutil.rmtree(CTL, ignore_errors=True) def test_00_checker_passes_on_the_published_package(self): d = make_copy("clean") self.assertEqual(run(d), 0) def test_01_corrupt_R7_min_M(self): d = make_copy("c1") edit_json(os.path.join(d, "stepcheck_facts.json"), lambda o: o["step_population"].__setitem__("R7_min_M", 6469693231)) self.assertNotEqual(run(d), 0) def test_02_corrupt_gate_threshold_p11_drop3(self): d = make_copy("c2") edit_json(os.path.join(d, "dgate30.json"), lambda o: o["thresholds"]["11"].__setitem__("min_A_drop3_permitted", 18)) self.assertNotEqual(run(d), 0) def test_03_corrupt_gate_saving_count(self): d = make_copy("c3") edit_json(os.path.join(d, "dgate_saving.json"), lambda o: o.__setitem__("gate_D_ge_2_kept", 808)) self.assertNotEqual(run(d), 0) def test_04_corrupt_step_population_sum_Mp(self): d = make_copy("c4") edit_json(os.path.join(d, "stepcheck_facts.json"), lambda o: o["step_population"].__setitem__("sum_Mp_all", 15474105210719941)) self.assertNotEqual(run(d), 0) def test_05_corrupt_served_sweep_min_rise(self): d = make_copy("c5") edit_json(os.path.join(d, "served", "f1812_sweep2775b.json"), lambda o: o.__setitem__("min_rise", -2)) self.assertNotEqual(run(d), 0) def test_06_corrupt_served_D_table_entry(self): d = make_copy("c6") p = os.path.join(d, "served", "f1833_dbound2612.json") with open(p, encoding="utf-8") as fh: db = json.load(fh) for row in db["strict"]: # D(30,7,11)=2 -> 1 if row[0] == 30 and row[1] == 7 and row[2] == 11: row[4] = 1 with open(p, "w", encoding="utf-8") as fh: json.dump(db, fh, indent=1) self.assertNotEqual(run(d), 0) def test_07_missing_required_file(self): d = make_copy("c7") os.remove(os.path.join(d, "dgate_saving.json")) self.assertNotEqual(run(d), 0) def test_08_missing_served_input(self): d = make_copy("c8") os.remove(os.path.join(d, "served", "f1812_sweep2775b.json")) self.assertNotEqual(run(d), 0) def test_09_corrupt_price_rate(self): d = make_copy("c9") edit_json(os.path.join(d, "stepcheck_facts.json"), lambda o: (o["price"].__setitem__("cpu_hours_all", 10320.0), o["price"].__setitem__("slots_per_sec", 4.17e9))) self.assertNotEqual(run(d), 0) if __name__ == "__main__": os.makedirs(CTL, exist_ok=True) unittest.main(verbosity=2)