import copy import importlib.util from pathlib import Path import unittest spec = importlib.util.spec_from_file_location("capture", Path(__file__).with_name("capture.py")) capture = importlib.util.module_from_spec(spec) spec.loader.exec_module(capture) def fixture(): return { "rows": [{"cluster_bytes": c, "logical_bytes": s, "blockcount_512": ((s+c-1)//c)*c//512, "allocated_bytes": ((s+c-1)//c)*c, "content_matches": True} for c in capture.CLUSTERS for s in capture.SIZES], "checks": [{"cluster_bytes": c, "returncode": 0} for c in capture.CLUSTERS], "rejections": [{"case": name, "returncode": 1} for name in ("cluster_without_bigalloc", "bigalloc_without_extents")] } class CaptureTests(unittest.TestCase): def test_complete(self): capture.validate(fixture()) def test_missing_and_duplicate_rows(self): for rows in (fixture()["rows"][:-1], fixture()["rows"][1:] + fixture()["rows"][1:2]): data = fixture() data["rows"] = rows with self.assertRaises(ValueError): capture.validate(data) def test_mutations_fail_closed(self): for key, value in (("allocated_bytes", 1), ("blockcount_512", 1), ("content_matches", False), ("logical_bytes", 2)): data = copy.deepcopy(fixture()) data["rows"][0][key] = value with self.assertRaises(ValueError): capture.validate(data) def test_missing_or_failed_checks(self): for checks in ([], [{"cluster_bytes": c, "returncode": 1} for c in capture.CLUSTERS], [fixture()["checks"][0]] * 3): data = fixture() data["checks"] = checks with self.assertRaises(ValueError): capture.validate(data) def test_rejections(self): for bad in ([], [fixture()["rejections"][0]] * 2, [{"case": r["case"], "returncode": 0} for r in fixture()["rejections"]]): data = fixture() data["rejections"] = bad with self.assertRaises(ValueError): capture.validate(data) def test_stat_parser(self): self.assertEqual(capture.stat_fields("Size: 4097\nBlockcount: 128"), (4097, 128)) for text in ("", "Size: 1", "Blockcount: 128"): with self.assertRaises(ValueError): capture.stat_fields(text) if __name__ == "__main__": unittest.main()