Skip to content

Commit

Permalink
make containers.py and install-cmk.py look for an available package t…
Browse files Browse the repository at this point in the history
…o use instead of downloading one

Change-Id: I52e9a29cb93b2707f8b8859c0438dd3380ab1cac
  • Loading branch information
Frans Fürst committed Nov 20, 2023
1 parent 4c6bef8 commit fe17be2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
7 changes: 6 additions & 1 deletion tests/scripts/install-cmk.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ def _is_debuntu(cls) -> bool:
def install(self, version: str, edition: Edition) -> None:
package_name = self._package_name(edition, version)
build_system_path = self._build_system_package_path(version, package_name)
packages_dir = Path(__file__).parent.parent.parent / "package_download"
if (package_path := packages_dir / package_name).exists():
logger.info("Install from locally available package %s", package_path)
self._write_package_hash(version, edition, package_path)
self._install_package(package_path)

if build_system_path.exists():
elif build_system_path.exists():
logger.info("Install from build system package (%s)", build_system_path)
self._write_package_hash(version, edition, build_system_path)
self._install_package(build_system_path)
Expand Down
77 changes: 59 additions & 18 deletions tests/testlib/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import logging
import os
import re
import subprocess
import sys
import tarfile
Expand Down Expand Up @@ -214,6 +215,36 @@ def _handle_api_error(e: docker.errors.APIError) -> None:
raise e


def check_for_local_package(version: CMKVersion) -> bool:
"""Checks package_download folder for a Checkmk package and returns True if
exactly one package is available and meets some requirements. If there are
invalid packages, the application terminates."""
packages_dir = testlib.repo_path() / "package_download"
if available_packages := [
p for p in packages_dir.glob("*") if re.match("^.*check-mk.*(rpm|deb)$", p.name)
]:
if len(available_packages) > 1:
logger.error(
"Error: There must be exactly one Checkmk package in %s, but there are %d:",
packages_dir,
len(available_packages),
)
for path in available_packages:
logger.error("Error: %s", path)
raise SystemExit(1)

package_name = available_packages[0].name
package_pattern = rf"check-mk-{version.edition.long}-{version.version}.*\.(deb|rpm)"
if not re.match(f"^{package_pattern}$", package_name):
logger.error("Error: '%s' does not match version=%s", package_name, version)
logger.error("Error: (must be '%s')", package_pattern)
raise SystemExit(1)

logger.info("found %s", available_packages[0])
return True
return False


def _create_cmk_image(
client: docker.DockerClient, base_image_name: str, docker_tag: str, version: CMKVersion
) -> str:
Expand All @@ -232,17 +263,26 @@ def _create_cmk_image(
f"{base_image_name}-{version.edition.short}-{version.version}:{docker_tag}"
)

logger.info("Check for available test-image [%s]", image_name_with_tag)
# First try to get the pre-built image from the local or remote registry
if (
(image := _get_or_load_image(client, image_name_with_tag))
and _is_based_on_current_base_image(image, base_image)
and _is_using_current_cmk_package(image, version)
):
# We found something locally or remote and ensured it's available locally.
# Only use it when it's based on the latest available base image. Otherwise
# skip it. The following code will re-build one based on the current base image
return image_name_with_tag # already found, nothing to do.
if use_local_package := check_for_local_package(version):
logger.info("+====================================================================+")
logger.info("| Use locally available package (i.e. don't try to fetch test-image) |")
logger.info("+====================================================================+")
else:
logger.info("+====================================+")
logger.info("| No locally available package found |")
logger.info("+====================================+")

logger.info("Check for available test-image [%s]", image_name_with_tag)
# First try to get the pre-built image from the local or remote registry
if (
(image := _get_or_load_image(client, image_name_with_tag))
and _is_based_on_current_base_image(image, base_image)
and _is_using_current_cmk_package(image, version)
):
# We found something locally or remote and ensured it's available locally.
# Only use it when it's based on the latest available base image. Otherwise
# skip it. The following code will re-build one based on the current base image
return image_name_with_tag # already found, nothing to do.

logger.info("Build test image [%s] from [%s]", image_name_with_tag, base_image_name_with_tag)
with _start(
Expand Down Expand Up @@ -314,13 +354,14 @@ def _create_cmk_image(

logger.info("Commited image [%s] (%s)", image_name_with_tag, image.short_id)

try:
logger.info("Uploading [%s] to registry (%s)", image_name_with_tag, image.short_id)
client.images.push(image_name_with_tag)
logger.info(" Upload complete")
except docker.errors.APIError as e:
logger.warning(" An error occurred")
_handle_api_error(e)
if not use_local_package:
try:
logger.info("Uploading [%s] to registry (%s)", image_name_with_tag, image.short_id)
client.images.push(image_name_with_tag)
logger.info(" Upload complete")
except docker.errors.APIError as e:
logger.warning(" An error occurred")
_handle_api_error(e)

return image_name_with_tag

Expand Down

0 comments on commit fe17be2

Please sign in to comment.