diff --git a/tests/unit/python/utils/update/test_docker_release_groups.py b/tests/unit/python/utils/update/test_docker_release_groups.py new file mode 100644 index 0000000000..11fd767a6a --- /dev/null +++ b/tests/unit/python/utils/update/test_docker_release_groups.py @@ -0,0 +1,113 @@ +"""Several images of one role at one version move together or not at all. + +Upstream does not push a release group atomically. Measured on Docker Hub: +``jitsi/prosody``, ``jitsi/jicofo`` and ``jitsi/jvb`` all publish +``stable-9457`` while ``jitsi/web`` does not, so a bump run landing in that +window would raise three components and leave the fourth behind. +""" + +from __future__ import annotations + +import unittest +from pathlib import Path + +from utils.update.docker import ( + DockerImageVersionEntry, + DockerImageVersionUpdate, + _held_to_release_groups, +) + +JITSI = ("web", "prosody", "jicofo", "jvb") + + +def _entry(role: str, service: str, image: str, version: str): + return DockerImageVersionEntry( + role=role, + service=service, + image=image, + version=version, + config_path=Path("meta/services.yml"), + ) + + +def _jitsi_updates(version: str, latest: str): + return [ + DockerImageVersionUpdate( + entry=_entry("web-app-jitsi", name, f"jitsi/{name}", version), + latest=latest, + ) + for name in JITSI + ] + + +class TestReleaseGroupsMoveTogether(unittest.TestCase): + def test_a_partial_publish_holds_the_whole_group_back(self): + tags = { + "jitsi/web": ["stable-9400", "stable-9646"], + "jitsi/prosody": ["stable-9400", "stable-9646", "stable-9700"], + "jitsi/jicofo": ["stable-9400", "stable-9646", "stable-9700"], + "jitsi/jvb": ["stable-9400", "stable-9646", "stable-9700"], + } + updates = _jitsi_updates("stable-9400", "stable-9700") + + held = _held_to_release_groups(updates, tags) + + self.assertEqual({update.latest for update in held}, {"stable-9646"}) + self.assertEqual(len(held), 4) + + def test_a_group_all_publish_moves_to_the_head(self): + tags = { + f"jitsi/{name}": ["stable-9400", "stable-9700"] for name in JITSI + } + updates = _jitsi_updates("stable-9400", "stable-9700") + + held = _held_to_release_groups(updates, tags) + + self.assertEqual({update.latest for update in held}, {"stable-9700"}) + + def test_a_group_with_nothing_in_common_is_dropped(self): + tags = { + "jitsi/web": ["stable-9400", "stable-9701"], + "jitsi/prosody": ["stable-9400", "stable-9702"], + "jitsi/jicofo": ["stable-9400", "stable-9703"], + "jitsi/jvb": ["stable-9400", "stable-9704"], + } + updates = _jitsi_updates("stable-9400", "stable-9704") + + self.assertEqual(_held_to_release_groups(updates, tags), []) + + def test_a_lone_entry_is_untouched(self): + update = DockerImageVersionUpdate( + entry=_entry("web-app-fider", "fider", "getfider/fider", "v0.35.0"), + latest="v0.36.0", + ) + + held = _held_to_release_groups([update], {"getfider/fider": ["v0.36.0"]}) + + self.assertEqual(held, [update]) + + def test_two_roles_on_one_version_are_not_one_group(self): + tags = { + "jitsi/web": ["stable-9400", "stable-9700"], + "other/thing": ["stable-9400"], + } + updates = [ + DockerImageVersionUpdate( + entry=_entry("web-app-jitsi", "web", "jitsi/web", "stable-9400"), + latest="stable-9700", + ), + DockerImageVersionUpdate( + entry=_entry("web-app-other", "thing", "other/thing", "stable-9400"), + latest="stable-9400", + ), + ] + + held = _held_to_release_groups(updates, tags) + + self.assertEqual( + [update.latest for update in held], ["stable-9700", "stable-9400"] + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/update/docker.py b/utils/update/docker.py index 0c96b20cd4..53bf9561f8 100644 --- a/utils/update/docker.py +++ b/utils/update/docker.py @@ -19,6 +19,7 @@ import re import time import urllib.error import urllib.request +from collections import defaultdict from dataclasses import dataclass from typing import TYPE_CHECKING from urllib.parse import quote, urlencode @@ -342,7 +343,53 @@ def find_outdated_updates(repo_root: Path) -> list[DockerImageVersionUpdate]: if latest and version_key(entry.version) < version_key(latest): updates.append(DockerImageVersionUpdate(entry=entry, latest=latest)) - return updates + return _held_to_release_groups(updates, image_tags) + + +def _held_to_release_groups( + updates: list[DockerImageVersionUpdate], image_tags: dict[str, list[str]] +) -> list[DockerImageVersionUpdate]: + """Hold every member of a release group to the newest tag all of them + publish. + + Several images of one role that currently sit on the same version are one + product released together (the four Jitsi components, the six GitLab CNG + images). Upstream does not push them atomically, so a run that lands in the + window between the first and the last push would raise the ones already + there and leave the rest behind, deploying a mixed set. + + Args: + updates: the per-entry candidates, before grouping. + image_tags: every tag fetched per image, keyed as in the caller. + + Returns: + The updates, each group lowered to its common newest tag, and dropped + entirely where the group shares no newer tag at all. + """ + groups: dict[tuple[str, str], list[DockerImageVersionUpdate]] = defaultdict(list) + for update in updates: + groups[(update.entry.role, update.entry.version)].append(update) + + held: list[DockerImageVersionUpdate] = [] + for group in groups.values(): + if len(group) == 1: + held.extend(group) + continue + shared = set.intersection( + *(set(image_tags.get(update.entry.image, [])) for update in group) + ) + current = group[0].entry.version + common = latest_semver( + sorted(shared), version_depth(current), version_flavor(current) + ) + if not common or version_key(current) >= version_key(common): + continue + held.extend( + DockerImageVersionUpdate(entry=update.entry, latest=common) + for update in group + ) + + return held def update_config_versions(config_path: Path, service_versions: dict[str, str]) -> bool: