Skip to content

Commit 02f4108

Browse files
authored
bugfix: use os_distro and os_version to determine distro and version … (#698)
thanks to @gtema for the suggestion and review and @martinmo for additional review Signed-off-by: Matthias Büchse <[email protected]>
1 parent 663c9d9 commit 02f4108

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

Tests/iaas/entropy/entropy-check.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -402,38 +402,52 @@ def handle(self, record):
402402
self.bylevel[record.levelno] += 1
403403

404404

405-
def _deduce_version(name, ubuntu_ver=re.compile(r"\d\d\.\d\d\Z"), debian_ver=re.compile(r"\d+\Z")):
406-
"""helper for `select_deb_image` to deduce a version even if its only given via codename"""
407-
canonicalized = [part.strip() for part in name.lower().split()]
408-
if "debian" in canonicalized:
409-
# don't even consider "stretch" (9) here
410-
codenames = ("buster", "bullseye", "bookworm")
411-
for idx, name in enumerate(codenames):
412-
if name in canonicalized:
413-
return idx + 10
414-
for part in canonicalized:
415-
if debian_ver.match(part):
416-
return int(part)
417-
elif "ubuntu" in canonicalized:
418-
for part in canonicalized:
419-
if ubuntu_ver.match(part):
420-
return int(part[:2] + part[3:])
421-
return -1
405+
# the following functions are used to map any OpenStack Image to a pair of integers
406+
# used for sorting the images according to fitness for our test
407+
# - debian take precedence over ubuntu
408+
# - higher versions take precedence over lower ones
409+
410+
# only list stable versions here
411+
DEBIAN_CODENAMES = {
412+
"buster": 10,
413+
"bullseye": 11,
414+
"bookworm": 12,
415+
}
416+
417+
418+
def _deduce_sort_debian(os_version, debian_ver=re.compile(r"\d+\Z")):
419+
if debian_ver.match(os_version):
420+
return 2, int(os_version)
421+
return 2, DEBIAN_CODENAMES.get(os_version, 0)
422+
423+
424+
def _deduce_sort_ubuntu(os_version, ubuntu_ver=re.compile(r"\d\d\.\d\d\Z")):
425+
if ubuntu_ver.match(os_version):
426+
return 1, int(os_version.replace(".", ""))
427+
return 1, 0
428+
429+
430+
# map lower-case distro name to version deducing function
431+
DISTROS = {
432+
"ubuntu": _deduce_sort_ubuntu,
433+
"debian": _deduce_sort_debian,
434+
}
435+
436+
437+
def _deduce_sort(img):
438+
# avoid private images here
439+
# (note that with SCS, public images MUST have os_distro and os_version, but we check nonetheless)
440+
if img.visibility != 'public' or not img.os_distro or not img.os_version:
441+
return 0, 0
442+
deducer = DISTROS.get(img.os_distro.strip().lower())
443+
if deducer is None:
444+
return 0, 0
445+
return deducer(img.os_version.strip().lower())
422446

423447

424448
def select_deb_image(images):
425-
"""From a list of OpenStack image objects, select a recent Debian derivative.
426-
427-
Try Debian first, then Ubuntu.
428-
"""
429-
for prefix in ("Debian ", "Ubuntu "):
430-
imgs = sorted(
431-
[img for img in images if img.name.startswith(prefix)],
432-
key=lambda img: _deduce_version(img.name),
433-
)
434-
if imgs:
435-
return imgs[-1]
436-
return None
449+
"""From a list of OpenStack image objects, select a recent Debian derivative."""
450+
return max(images, key=_deduce_sort, default=None)
437451

438452

439453
def print_result(check_id, passed):

0 commit comments

Comments
 (0)