Skip to content

Commit

Permalink
bugfix: use os_distro and os_version to determine distro and version … (
Browse files Browse the repository at this point in the history
#698)

thanks to @gtema for the suggestion and review and @martinmo for additional review

Signed-off-by: Matthias Büchse <[email protected]>
  • Loading branch information
mbuechse authored Aug 16, 2024
1 parent 663c9d9 commit 02f4108
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions Tests/iaas/entropy/entropy-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 02f4108

Please sign in to comment.