From 02f41081d7789688ec8e22b8a9606b72fadb3e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=BCchse?= Date: Fri, 16 Aug 2024 14:07:59 +0200 Subject: [PATCH] =?UTF-8?q?bugfix:=20use=20os=5Fdistro=20and=20os=5Fversio?= =?UTF-8?q?n=20to=20determine=20distro=20and=20version=20=E2=80=A6=20(#698?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit thanks to @gtema for the suggestion and review and @martinmo for additional review Signed-off-by: Matthias Büchse --- Tests/iaas/entropy/entropy-check.py | 72 +++++++++++++++++------------ 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/Tests/iaas/entropy/entropy-check.py b/Tests/iaas/entropy/entropy-check.py index 91f53fd93..da9f6dfa0 100755 --- a/Tests/iaas/entropy/entropy-check.py +++ b/Tests/iaas/entropy/entropy-check.py @@ -402,38 +402,52 @@ def handle(self, record): self.bylevel[record.levelno] += 1 -def _deduce_version(name, ubuntu_ver=re.compile(r"\d\d\.\d\d\Z"), debian_ver=re.compile(r"\d+\Z")): - """helper for `select_deb_image` to deduce a version even if its only given via codename""" - canonicalized = [part.strip() for part in name.lower().split()] - if "debian" in canonicalized: - # don't even consider "stretch" (9) here - codenames = ("buster", "bullseye", "bookworm") - for idx, name in enumerate(codenames): - if name in canonicalized: - return idx + 10 - for part in canonicalized: - if debian_ver.match(part): - return int(part) - elif "ubuntu" in canonicalized: - for part in canonicalized: - if ubuntu_ver.match(part): - return int(part[:2] + part[3:]) - return -1 +# the following functions are used to map any OpenStack Image to a pair of integers +# used for sorting the images according to fitness for our test +# - debian take precedence over ubuntu +# - higher versions take precedence over lower ones + +# only list stable versions here +DEBIAN_CODENAMES = { + "buster": 10, + "bullseye": 11, + "bookworm": 12, +} + + +def _deduce_sort_debian(os_version, debian_ver=re.compile(r"\d+\Z")): + if debian_ver.match(os_version): + return 2, int(os_version) + return 2, DEBIAN_CODENAMES.get(os_version, 0) + + +def _deduce_sort_ubuntu(os_version, ubuntu_ver=re.compile(r"\d\d\.\d\d\Z")): + if ubuntu_ver.match(os_version): + return 1, int(os_version.replace(".", "")) + return 1, 0 + + +# map lower-case distro name to version deducing function +DISTROS = { + "ubuntu": _deduce_sort_ubuntu, + "debian": _deduce_sort_debian, +} + + +def _deduce_sort(img): + # avoid private images here + # (note that with SCS, public images MUST have os_distro and os_version, but we check nonetheless) + if img.visibility != 'public' or not img.os_distro or not img.os_version: + return 0, 0 + deducer = DISTROS.get(img.os_distro.strip().lower()) + if deducer is None: + return 0, 0 + return deducer(img.os_version.strip().lower()) def select_deb_image(images): - """From a list of OpenStack image objects, select a recent Debian derivative. - - Try Debian first, then Ubuntu. - """ - for prefix in ("Debian ", "Ubuntu "): - imgs = sorted( - [img for img in images if img.name.startswith(prefix)], - key=lambda img: _deduce_version(img.name), - ) - if imgs: - return imgs[-1] - return None + """From a list of OpenStack image objects, select a recent Debian derivative.""" + return max(images, key=_deduce_sort, default=None) def print_result(check_id, passed):