new file mode 100644
@@ -0,0 +1,218 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Unit tests for modules/state.py and modules/statistics.py.
+# Run from the top of the tree with:
+# python3 -m unittest discover -s tests -v
+
+import json
+import os
+import sys
+import tempfile
+import time
+import unittest
+
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
+
+from modules import state as state_mod
+from modules.state import State, STATE_FILENAME, SECONDS_PER_DAY
+from modules.statistics import Statistics
+
+
+def pkg(pn, pv="1.0", npv="1.1", maintainer="a@b.c"):
+ return {"PN": pn, "PV": pv, "NPV": npv, "MAINTAINER": maintainer}
+
+
+def group(*pkgs, error=None):
+ return {"name": ",".join(p["PN"] for p in pkgs), "pkgs": list(pkgs),
+ "error": error}
+
+
+class StateTests(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.dir = self.tmp.name
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def age(self, st, pn, days):
+ """Pretend the last attempt on pn was 'days' days ago."""
+ st.data[pn]["timestamp"] = time.time() - days * SECONDS_PER_DAY
+
+ def test_no_state_file(self):
+ self.assertEqual(State(self.dir).data, {})
+
+ def test_v1_state_is_ignored(self):
+ with open(os.path.join(self.dir, STATE_FILENAME), "w") as f:
+ json.dump({"version": 1, "recipes": {
+ "a": {"1.1": {"timestamp": time.time(), "result": "success"}}}}, f)
+ self.assertEqual(State(self.dir).data, {})
+
+ def test_corrupt_state_is_ignored(self):
+ with open(os.path.join(self.dir, STATE_FILENAME), "w") as f:
+ f.write("{not json")
+ self.assertEqual(State(self.dir).data, {})
+
+ def test_save_and_reload(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_SUCCESS)
+ st.save()
+ self.assertEqual(State(self.dir).data, st.data)
+
+ def test_save_keeps_entries_not_in_current_run(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_SUCCESS)
+ st.record("b", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ st.save()
+ st = State(self.dir) # a later run that only touches 'a'
+ st.record("a", "1.0", "1.1", state_mod.RESULT_SUCCESS)
+ st.save()
+ self.assertEqual(set(State(self.dir).data), {"a", "b"})
+
+ def test_record_keeps_firstseen_when_versions_unchanged(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ st.data["a"]["oldversion_firstseen"] -= 10 * SECONDS_PER_DAY
+ st.data["a"]["newversion_firstseen"] -= 20 * SECONDS_PER_DAY
+ old, new = (st.data["a"]["oldversion_firstseen"],
+ st.data["a"]["newversion_firstseen"])
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ self.assertEqual(st.data["a"]["oldversion_firstseen"], old)
+ self.assertEqual(st.data["a"]["newversion_firstseen"], new)
+
+ def test_record_resets_only_the_changed_side(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ st.data["a"]["oldversion_firstseen"] -= 10 * SECONDS_PER_DAY
+ st.data["a"]["newversion_firstseen"] -= 10 * SECONDS_PER_DAY
+ old, new = (st.data["a"]["oldversion_firstseen"],
+ st.data["a"]["newversion_firstseen"])
+ st.record("a", "1.0", "1.2", state_mod.RESULT_FAILURE) # new changed
+ self.assertEqual(st.data["a"]["oldversion_firstseen"], old)
+ self.assertGreater(st.data["a"]["newversion_firstseen"], new)
+ old = st.data["a"]["oldversion_firstseen"]
+ st.data["a"]["oldversion_firstseen"] -= 5 * SECONDS_PER_DAY
+ old = st.data["a"]["oldversion_firstseen"]
+ st.record("a", "1.0.1", "1.2", state_mod.RESULT_FAILURE) # old changed
+ self.assertGreater(st.data["a"]["oldversion_firstseen"], old)
+
+ def test_check_skips_same_pair_within_window(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ self.assertTrue(st.check("a", "1.0", "1.1")["skip"])
+
+ def test_check_unknown_recipe(self):
+ self.assertFalse(State(self.dir).check("a", "1.0", "1.1")["skip"])
+
+ def test_check_rerolls_when_old_version_changed(self):
+ # Layer moved the recipe to an intermediate version after our attempt.
+ st = State(self.dir)
+ st.record("a", "1.0", "1.3", state_mod.RESULT_SUCCESS)
+ self.assertFalse(st.check("a", "1.1", "1.3")["skip"])
+
+ def test_check_retries_when_new_version_changed(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ self.assertFalse(st.check("a", "1.0", "1.2")["skip"])
+
+ def test_check_windows_expire(self):
+ st = State(self.dir, retry_same_version_days=30, success_max_age_days=10)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ st.record("b", "1.0", "1.1", state_mod.RESULT_SUCCESS)
+ self.age(st, "a", 20)
+ self.age(st, "b", 20)
+ self.assertTrue(st.check("a", "1.0", "1.1")["skip"]) # 20 < 30
+ self.assertFalse(st.check("b", "1.0", "1.1")["skip"]) # 20 > 10
+
+ def test_check_any_version_recipe_ignores_versions(self):
+ st = State(self.dir, retry_any_version_days=7,
+ retry_any_version_recipes=["a"])
+ st.record("a", "1.0", "1.1", state_mod.RESULT_SUCCESS)
+ self.assertTrue(st.check("a", "1.5", "1.6")["skip"])
+ self.age(st, "a", 8)
+ self.assertFalse(st.check("a", "1.5", "1.6")["skip"])
+
+ def test_firstseen_days(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ st.data["a"]["oldversion_firstseen"] -= 3 * SECONDS_PER_DAY + 60
+ st.data["a"]["newversion_firstseen"] -= 1 * SECONDS_PER_DAY + 60
+ self.assertEqual(st.get_oldver_firstseen_days("a", "1.0"), 3)
+ self.assertEqual(st.get_newver_firstseen_days("a", "1.1"), 1)
+
+ def test_firstseen_days_missing_or_mismatched(self):
+ st = State(self.dir)
+ st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ self.assertEqual(st.get_oldver_firstseen_days("nope", "1.0"), 0)
+ self.assertEqual(st.get_newver_firstseen_days("nope", "1.1"), 0)
+ self.assertEqual(st.get_oldver_firstseen_days("a", "9.9"), 0)
+ self.assertEqual(st.get_newver_firstseen_days("a", "9.9"), 0)
+
+
+class StatisticsTests(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.st = State(self.tmp.name)
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def test_total_attempted(self):
+ s = Statistics()
+ self.assertFalse(s.total_attempted) # used by send_status_mail()
+ s.update(group(pkg("a"), pkg("b")))
+ s.update(group(pkg("c"), error=Exception("boom")))
+ self.assertEqual(s.total_attempted, 3)
+
+ def test_summary_without_state(self):
+ # state is None unless --incremental is given
+ s = Statistics()
+ s.update(group(pkg("a")))
+ s.update(group(pkg("b"), error=Exception("boom")))
+ out = s.get_summary("", "w", None)
+ self.assertIn("a, 1.0 -> 1.1, a@b.c", out)
+ self.assertNotIn("first seen", out)
+ self.assertIn("attempted=2 succeeded=1(50.00%) failed=1(50.00%)", out)
+
+ def test_summary_with_state(self):
+ self.st.record("a", "1.0", "1.1", state_mod.RESULT_SUCCESS)
+ self.st.data["a"]["oldversion_firstseen"] -= 5 * SECONDS_PER_DAY + 60
+ s = Statistics()
+ s.update(group(pkg("a")))
+ out = s.get_summary("", "w", self.st)
+ self.assertIn("a, 1.0 (first seen 5 day(s) ago) -> "
+ "1.1 (first seen 0 day(s) ago), a@b.c", out)
+
+ def test_skipped_sorted_longest_outstanding_first(self):
+ for pn, days in (("young", 1), ("old", 40), ("mid", 10)):
+ self.st.record(pn, "1.0", "1.1", state_mod.RESULT_FAILURE)
+ self.st.data[pn]["newversion_firstseen"] -= days * SECONDS_PER_DAY + 60
+ s = Statistics()
+ for pn in ("young", "old", "mid"):
+ s.skip(group(pkg(pn)), "reason")
+ out = s.get_summary("", "w", self.st)
+ self.assertLess(out.index("old,"), out.index("mid,"))
+ self.assertLess(out.index("mid,"), out.index("young,"))
+ self.assertIn("* Skipped: 3", out)
+
+ def test_skipped_group_member_without_state_entry(self):
+ # Group membership changed since the last run: only 'a' has an entry.
+ self.st.record("a", "1.0", "1.1", state_mod.RESULT_FAILURE)
+ s = Statistics()
+ s.skip(group(pkg("a"), pkg("c")), "reason")
+ out = s.get_summary("", "w", self.st)
+ self.assertIn("c, 1.0 (first seen 0 day(s) ago)", out)
+
+ def test_empty_maintainer(self):
+ s = Statistics()
+ s.update(group(pkg("a", maintainer="")))
+ out = s.get_summary("", "w", None)
+ self.assertIn("a, 1.0 -> 1.1\n", out)
+
+ def test_nothing_attempted(self):
+ out = Statistics().get_summary("", "w", None)
+ self.assertIn("attempted=0 succeeded=0(0.00%) failed=0(0.00%)", out)
+
+
+if __name__ == "__main__":
+ unittest.main()
AUH has no built-in tests, and corner cases are only hit in production autobuilder runs. State and statistics are pure logic, so cover the version-change, expiry, state-less (non --incremental) and partial-run cases with stdlib unittest. Run from the top of the tree: python3 -m unittest discover -s tests -v AI-Generated: Claude Sonnet 5.5 Signed-off-by: Alexander Kanavin <alex@linutronix.de> --- tests/test_state_statistics.py | 218 +++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 tests/test_state_statistics.py