From d91e46ebb85b9f2108230e1b2cff21d12b709f50 Mon Sep 17 00:00:00 2001 From: Rafal Stefanowski Date: Thu, 29 Sep 2022 15:08:51 +0200 Subject: [PATCH 1/2] test/api: Add packaging functionality Signed-off-by: Rafal Stefanowski --- test/functional/api/cas/packaging.py | 133 +++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 test/functional/api/cas/packaging.py diff --git a/test/functional/api/cas/packaging.py b/test/functional/api/cas/packaging.py new file mode 100644 index 0000000..1fe354e --- /dev/null +++ b/test/functional/api/cas/packaging.py @@ -0,0 +1,133 @@ +# +# Copyright(c) 2022 Intel Corporation +# SPDX-License-Identifier: BSD-3-Clause +# + + +import os +import re + +from core.test_run import TestRun +from test_tools.fs_utils import check_if_directory_exists, find_all_files +from test_tools.packaging import DebSet, RpmSet +from test_utils.output import CmdException + + +class Packages: + # This __init__() method will never be triggered since this class + # returns other class in __new__() before __init__() is evaluated. + # It is implemented here only to indicate what arguments can be + # passed into this class and how are they used. + def __init__(self, packages_dir: str = ""): + self.packages_dir = packages_dir + + def __new__(cls, *args, **kwargs): + distro_id_like = TestRun.executor.run_expect_success( + "grep -i ID_LIKE /etc/os-release" + ).stdout + + if re.search("rhel|fedora|suse|sles", distro_id_like): + return _Rpm(*args, **kwargs) + elif re.search("debian", distro_id_like): + return _Deb(*args, **kwargs) + else: + distro_id_like = distro_id_like.split("=")[1].strip("\"'") + raise TypeError(f"{distro_id_like} - not recognized OS distribution") + + +class _Rpm(RpmSet): + def __init__(self, packages_dir: str = ""): + self.packages_dir = packages_dir + self.packages = get_packages_list("rpm", self.packages_dir) + + def create( + self, + sources_dir, + packages_dir: str = "", + debug: bool = False, + arch: str = "", + source: bool = False, + ): + TestRun.LOGGER.info(f"Creating Open CAS RPM packages") + + self.packages_dir = ( + packages_dir or self.packages_dir or os.path.join(sources_dir, "packages") + ) + + self.packages = create_packages( + "rpm", + sources_dir, + self.packages_dir, + debug, + arch, + source, + ) + + +class _Deb(DebSet): + def __init__(self, packages_dir: str = ""): + self.packages_dir = packages_dir + self.packages = get_packages_list("deb", self.packages_dir) + + def create( + self, + sources_dir, + packages_dir: str = "", + debug: bool = False, + arch: str = "", + source: bool = False, + ): + TestRun.LOGGER.info(f"Creating Open CAS DEB packages") + + self.packages_dir = ( + packages_dir or self.packages_dir or os.path.join(sources_dir, "packages") + ) + + self.packages = create_packages( + "deb", + sources_dir, + self.packages_dir, + debug, + arch, + source, + ) + + +def get_packages_list(package_type: str, packages_dir: str): + if not check_if_directory_exists(packages_dir): + return [] + + return [ + package for package in find_all_files(packages_dir, recursive=False) + # include only binary packages (ready to be processed by package manager) + if package.endswith(package_type.lower()) + and not package.endswith("src." + package_type.lower()) + ] + + +def create_packages( + package_type: str, + sources_dir: str, + packages_dir: str, + debug: bool = False, + arch: str = "", + source: bool = False, +): + pckgen = os.path.join(sources_dir, "tools", "pckgen.sh") + + opts = f"{package_type.lower()} --output-dir {packages_dir}" + if debug: + opts += " --debug" + if arch: + opts += f" --arch {arch}" + if source: + opts += f" {'srpm' if package_type.lower() == 'rpm' else 'dsc'}" + + packages_before = get_packages_list(package_type, packages_dir) + TestRun.executor.run_expect_success(f"{pckgen} {opts} {sources_dir}") + packages_after = get_packages_list(package_type, packages_dir) + + new_packages = [file for file in packages_after if file not in packages_before] + packages = new_packages or packages_after + + return packages From 71e2b5f5867daf5b7a615f34c4bd177bf6f74e5b Mon Sep 17 00:00:00 2001 From: Rafal Stefanowski Date: Thu, 29 Sep 2022 15:11:24 +0200 Subject: [PATCH 2/2] test: Adapt weak-modules test to changes in packaging API Signed-off-by: Rafal Stefanowski --- test/functional/tests/misc/test_weak_modules.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/tests/misc/test_weak_modules.py b/test/functional/tests/misc/test_weak_modules.py index 2aadf7c..c4b5c13 100644 --- a/test/functional/tests/misc/test_weak_modules.py +++ b/test/functional/tests/misc/test_weak_modules.py @@ -15,7 +15,7 @@ from test_tools.fs_utils import ( readlink, remove, ) -from test_tools.packaging import Packages +from api.cas.packaging import Packages modules_links_dir = "/lib/modules/$(uname -r)/weak-updates/block/opencas" @@ -54,7 +54,7 @@ def test_weak_modules(): cas_pkg.create(TestRun.usr.working_dir) with TestRun.step("Remove any previous installations and cleanup"): - cas_pkg.uninstall("open-cas-linux") + cas_pkg.uninstall_all_matching("open-cas-linux") remove(modules_links_dir, recursive=True, force=True) with TestRun.step("Install RPM packages and check for module symlinks"):