Ensure deterministic ordering of web health expectations and add unit tests

This update sorts all expectation keys alphabetically to guarantee idempotent
Ansible deployments and stable systemd unit generation. Added two unit tests to
validate proper key ordering for canonical domains, aliases, redirects, and WWW
mappings.

Reference: https://chatgpt.com/share/692ae99b-dd88-800f-8fad-2ff62666e295
This commit is contained in:
2025-11-29 13:40:31 +01:00
parent 1129b943fc
commit 03bea763f1
2 changed files with 27 additions and 1 deletions

View File

@@ -397,5 +397,30 @@ class TestWebHealthExpectationsFilter(unittest.TestCase):
self.assertEqual(out["v1.l11.example.org"], [301, 307]) # per-key list
self.assertEqual(out["v2.l11.example.org"], [301, 307])
def test_expectations_keys_are_sorted_alphabetically (self ):
apps ={"app-sort":{}}
self ._configure_returns ({
("app-sort","server.domains.canonical"):["b.example.org","a.example.org","c.example.org"],
("app-sort","server.domains.aliases"):["alias.example.org"],
("app-sort","server.status_codes"):{"default":200 },
})
out =self .mod .web_health_expectations (apps ,group_names =["app-sort"])
keys =list (out .keys ())
self .assertEqual (keys ,sorted (keys ))
def test_expectations_keys_sorted_with_redirects_and_www (self ):
apps ={}
out =self .mod .web_health_expectations (
apps ,
group_names =["dummy-app"],
www_enabled =True ,
redirect_maps =[{"source":"redir-b.example.org"},{"source":"redir-a.example.org"}],
)
keys =list (out .keys ())
self .assertEqual (keys ,sorted (keys ))
if __name__ == "__main__":
unittest.main()