From patchwork Thu Dec 3 21:20:50 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Kanavin X-Patchwork-Id: 107229 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.3752.1796332865504429075 for ; Thu, 03 Dec 2026 13:21:05 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=Ltssp98J; spf=pass (domain: gmail.com, ip: 209.85.221.51, mailfrom: alex.kanavin@gmail.com) Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-490d59c3edaso159611f8f.1 for ; Thu, 03 Dec 2026 13:21:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796332864; x=1796937664; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PqOxCHfLw0wOuqDeJZ2dxMAtu1ZyptOGEF5/jTb+FEc=; b=Ltssp98JxykFDMMWbm0Bii8oJcITBqM6tllrmB9EEfYaBd5fIUwU/oxDaQ6zHQXZaF ioDpqbQtD0N8NbBqrg8Rg8h32q3rrbKbo1++rg3wr1oE9bgtkHTnAfrVRwuRq7/oQbaa pjPDPYvggy/Ipup2AQPf+H1eaMtPyuDNc1GDgoxMwDfzs0wq+vB/K0iDkg67vPhaHBlv hi4kOcuTSRCIUydtbfaZ+EsBGfUNjKcr04GJWHhl3XIS5MpWOnqVdADIx5k78baCIF6H mYgEuKxkrpPuPn2DrAtH+rL5WHTIBCnIylo8X4ctiPvyT7mf4b4wkZX+n1fsWunCrjR7 0deg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796332864; x=1796937664; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=PqOxCHfLw0wOuqDeJZ2dxMAtu1ZyptOGEF5/jTb+FEc=; b=LH2VZBAZFk+6ONvyyCiL9iSpoiK8SQWjkJm7YMBUN3wuDYTVxpSu47oPNrcl09oQRd omfrI+m8NTzWUpLuXTNSjxcNHdRRz7uqtGBY9eOYrv14ODX55vRxaJzBa2QM0Cblk2jl vvBypsnrnjjr86ntCdfV+D3ePGgUhrQ/N7jWgulSHNzNCn3pCwdVvEBnlTUroW353WI3 8C1B/XebVMXtph7c7ydkRs2j7vO+UMT2FYIq6g/g/n6FH2IQM11kAUEgdnsdfGQY1DLi J0HUHGOf/BYvafULlqZBg7vLYQCqkvyq6PrDhyPHywARNVmpq6GG3frPp4l5FkA+wekp by3A== X-Gm-Message-State: APuU4Z9N5PkqeBizdESczBJ5xqblAtBfmjH99Upf3oCbh0sl5cxx39ea 5Qf92QR+ccWXfOgzkrK32I9ZIMsLAk92wLwzp1Va5l4I+j3MqUFOUJ5fj1y/FA== X-Gm-Gg: AZFdTZcMAm6OkZt2n3/Ii0SafIuPpyEihmp42xfqx8pXQBfYYJRbgnUfGilpzeCRtBD RKWh24e3uQAbD7xvq6KNYx4PsxSN7g2Yjuro0m4qunVkOaay/AfeugjGRxTmSiTQ5RIXJvaFpjQ ZVxo3nYUdjZ+TK4Wx4M7b12E0AbMhNz4HXp3ul5SA2augMyqEs2U+Cw2AAID1OFi7t8IwLHjIKF Nv1+Qc6/kDB1Q82k+3v4f0VKmadWE50jtum7B2+8ydLXpjD/e7THYukvxzfOVr0stypq4bSk0wp kCsxUX15U6ZM3qBtfB2rHeQWowsEo4+QVkgYJflNX/xS6I7pf9I8XOD7PwPxA1YDvpgPdHZ1jwk axpbzycUETotOLZ5MtLP0+kFazsk9LpcoA8rBJ0nLHxHYc2yijuqcp8LkyxfygaZbr0oTLdnol1 q/IgFuRFdlmTNJhwp+W0LVlgnGtOHnBx3MXacQlP8e52dkcDxPh/T12tIjaw6wnlMX6L/wrKluk di2tKrWOTFPmFtaFAVxU592Ey6t+cuWMHjMGWY8EpC9FR06xjn9HorrHJ+9K5e5eX5BcAWl02kn VjW6Vu+Re49nbPn0TNQO7O6d/tmfLjbg8pk1QolyQgbvd6HnjU0IfkGo5Ci4eVcT X-Received: by 2002:a05:6000:2909:b0:492:2b36:a149 with SMTP id ffacd0b85a97d-4cf4bb393b1mr7093593f8f.25.1796332863809; Thu, 03 Dec 2026 13:21:03 -0800 (PST) Return-Path: Received: from Zen2.lab.linutronix.de. (drugstore.linutronix.de. [80.153.143.164]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d1724ad3a5sm1860126f8f.9.2026.12.03.13.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 13:21:03 -0800 (PST) From: Alexander Kanavin X-Google-Original-From: Alexander Kanavin To: yocto-patches@lists.yoctoproject.org Cc: daniel.turull@ericsson.com, Alexander Kanavin Subject: [auh][PATCH v2 3/3] tests: add unit tests for modules/state.py and modules/statistics.py Date: Thu, 3 Dec 2026 22:20:50 +0100 Message-ID: <20261203212050.799982-3-alex@linutronix.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203212050.799982-1-alex@linutronix.de> References: <20261203212050.799982-1-alex@linutronix.de> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Thu, 03 Dec 2026 21:21:06 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/5666 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 --- tests/test_state_statistics.py | 218 +++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 tests/test_state_statistics.py diff --git a/tests/test_state_statistics.py b/tests/test_state_statistics.py new file mode 100644 index 0000000..73a3517 --- /dev/null +++ b/tests/test_state_statistics.py @@ -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()