From 9f66143b7024f3be37764f918ec39af261bde94a Mon Sep 17 00:00:00 2001 From: Rafal Stefanowski Date: Tue, 16 Aug 2022 15:29:14 +0200 Subject: [PATCH 1/3] test/api: Refactor checkout_cas_version() Signed-off-by: Rafal Stefanowski --- test/functional/api/cas/git.py | 38 +++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/test/functional/api/cas/git.py b/test/functional/api/cas/git.py index f6af519..0a69dd3 100644 --- a/test/functional/api/cas/git.py +++ b/test/functional/api/cas/git.py @@ -1,5 +1,5 @@ # -# Copyright(c) 2019-2021 Intel Corporation +# Copyright(c) 2019-2022 Intel Corporation # SPDX-License-Identifier: BSD-3-Clause # @@ -7,6 +7,7 @@ import os from core.test_run import TestRun from connection.local_executor import LocalExecutor +from test_utils.output import CmdException def get_current_commit_hash(from_dut: bool = False): @@ -25,6 +26,21 @@ def get_current_commit_message(): f'git show HEAD -s --pretty=format:"%B"').stdout +def get_commit_hash(cas_version, from_dut: bool = False): + executor = TestRun.executor if from_dut else LocalExecutor() + repo_path = TestRun.usr.working_dir if from_dut else TestRun.usr.repo_dir + + output = executor.run( + f"cd {repo_path} && " + f"git rev-parse {cas_version}") + if output.exit_code != 0: + raise CmdException(f"Failed to resolve '{cas_version}' to commit hash", output) + + TestRun.LOGGER.info(f"Resolved '{cas_version}' as commit {output.stdout}") + + return output.stdout + + def get_release_tags(): repo_path = os.path.join(TestRun.usr.working_dir, ".git") output = TestRun.executor.run_expect_success(f"git --git-dir={repo_path} tag").stdout @@ -36,29 +52,17 @@ def get_release_tags(): def checkout_cas_version(cas_version): - from api.cas.version import CasVersion - if isinstance(cas_version, CasVersion): - output = TestRun.executor.run( - f"cd {TestRun.usr.working_dir} && " - f"git rev-parse {cas_version}") - if output.exit_code != 0: - raise CmdException(f"Failed to resolve {cas_version} tag to commit hash", output) - TestRun.LOGGER.info(f"Resolved {cas_version} as commit {output.stdout}") - cas_version = output.stdout - - _checkout_cas_commit(cas_version) - - -def _checkout_cas_commit(commit_hash): + commit_hash = get_commit_hash(cas_version) TestRun.LOGGER.info(f"Checkout CAS to {commit_hash}") + output = TestRun.executor.run( f"cd {TestRun.usr.working_dir} && " f"git checkout --force {commit_hash}") if output.exit_code != 0: - raise CmdException(f"Failed to checkout to CAS {commit_hash}", output) + raise CmdException(f"Failed to checkout to {commit_hash}", output) output = TestRun.executor.run( f"cd {TestRun.usr.working_dir} && " - f"git submodule update") + f"git submodule update --force") if output.exit_code != 0: raise CmdException(f"Failed to update submodules", output) From 824e086ffb1f92e8e310f2cd195a9ca29dcf37eb Mon Sep 17 00:00:00 2001 From: Rafal Stefanowski Date: Tue, 16 Aug 2022 15:39:03 +0200 Subject: [PATCH 2/3] test/api: Add functionality to install particular CAS version Signed-off-by: Rafal Stefanowski --- test/functional/api/cas/installer.py | 50 +++++++++++++++++++--------- test/functional/api/cas/version.py | 16 ++++++++- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/test/functional/api/cas/installer.py b/test/functional/api/cas/installer.py index 79093f2..8e11254 100644 --- a/test/functional/api/cas/installer.py +++ b/test/functional/api/cas/installer.py @@ -1,5 +1,5 @@ # -# Copyright(c) 2019-2021 Intel Corporation +# Copyright(c) 2019-2022 Intel Corporation # SPDX-License-Identifier: BSD-3-Clause # @@ -8,8 +8,8 @@ import logging from tests import conftest from core.test_run import TestRun -from api.cas import git -from api.cas import cas_module +from api.cas import cas_module, git +from api.cas.version import get_installed_cas_version from test_utils import os_utils from test_utils.output import CmdException @@ -48,24 +48,27 @@ def install_opencas(): f"cd {TestRun.usr.working_dir} && " f"make install") if output.exit_code != 0: - raise CmdException("Error while installing Open CAS", output) + raise CmdException("Failed to install Open CAS", output) + + output = TestRun.executor.run("rmmod cas_cache cas_disk; modprobe cas_cache") + if output.exit_code != 0: + raise CmdException("Failed to reload modules", output) TestRun.LOGGER.info("Check if casadm is properly installed.") output = TestRun.executor.run("casadm -V") if output.exit_code != 0: raise CmdException("'casadm -V' command returned an error", output) - else: - TestRun.LOGGER.info(output.stdout) + + TestRun.LOGGER.info(output.stdout) -def set_up_opencas(version=None): +def set_up_opencas(version: str = ""): _clean_opencas_repo() if version: git.checkout_cas_version(version) build_opencas() - install_opencas() @@ -82,20 +85,35 @@ def uninstall_opencas(): raise CmdException("There was an error during uninstall process", output) -def reinstall_opencas(version=None): +def reinstall_opencas(version: str = ""): if check_if_installed(): uninstall_opencas() set_up_opencas(version) -def check_if_installed(): - TestRun.LOGGER.info("Check if Open-CAS-Linux is installed") +def check_if_installed(version: str = ""): + TestRun.LOGGER.info("Check if Open CAS Linux is installed") output = TestRun.executor.run("which casadm") modules_loaded = os_utils.is_kernel_module_loaded(cas_module.CasModule.cache.value) - if output.exit_code == 0 and modules_loaded: - TestRun.LOGGER.info("CAS is installed") + if output.exit_code != 0 or not modules_loaded: + TestRun.LOGGER.info("CAS is not installed") + return False - return True - TestRun.LOGGER.info("CAS not installed") - return False + TestRun.LOGGER.info("CAS is installed") + + if version: + TestRun.LOGGER.info(f"Check for requested CAS version: {version}") + cas_commit_expected = git.get_commit_hash(version) + cas_commit_installed = get_installed_cas_version() + + if cas_commit_expected != cas_commit_installed: + TestRun.LOGGER.info( + f"CAS version '{version}' is not installed. " + f"Installed version found: {cas_commit_installed}" + ) + return False + + TestRun.LOGGER.info(f"CAS version '{version}' is installed") + + return True diff --git a/test/functional/api/cas/version.py b/test/functional/api/cas/version.py index 13bb953..9ede4c4 100644 --- a/test/functional/api/cas/version.py +++ b/test/functional/api/cas/version.py @@ -1,11 +1,13 @@ # -# Copyright(c) 2019-2021 Intel Corporation +# Copyright(c) 2019-2022 Intel Corporation # SPDX-License-Identifier: BSD-3-Clause # import re from api.cas import git +from core.test_run import TestRun +from test_utils.output import CmdException class CasVersion: @@ -43,3 +45,15 @@ def get_available_cas_versions(): versions = [CasVersion.from_git_tag(tag) for tag in release_tags] return versions + + +def get_installed_cas_version(): + output = TestRun.executor.run("grep -i '^LAST_COMMIT_HASH=' /var/lib/opencas/cas_version") + if output.exit_code != 0: + raise CmdException( + "Could not find commit hash of installed version. " + "Check if Open CAS Linux is properly installed.", + output, + ) + + return output.stdout.split("=")[1] From 58ba99aa4a88ca26ac2ee42fddfb1f5a6db1b66c Mon Sep 17 00:00:00 2001 From: Rafal Stefanowski Date: Tue, 16 Aug 2022 15:41:11 +0200 Subject: [PATCH 3/3] test/api: Check for particular CAS version before test Signed-off-by: Rafal Stefanowski --- test/functional/config/example_dut_config.yml | 9 +++++++++ test/functional/tests/conftest.py | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/functional/config/example_dut_config.yml b/test/functional/config/example_dut_config.yml index 00180d8..e49f4a2 100644 --- a/test/functional/config/example_dut_config.yml +++ b/test/functional/config/example_dut_config.yml @@ -14,6 +14,15 @@ type: "local" allow_disk_autoselect: False working_dir: "/tmp/open-cas-linux/" +# CAS version to test. +# This version will be installed on DUT before test. Can be any string +# digestible by git-checkout. If not specified, by default current state +# of your controller's repo (HEAD) will be used as version to install. +# NOTE: Make sure to have a local master branch synced with upstream +# as the local repo will be taken for version reference. To do this +# simply run: `git fetch origin master:master` +cas_version: "master" + disks: - path: "/dev/device_name1" # disk device path serial: "ABC" # disk serial number diff --git a/test/functional/tests/conftest.py b/test/functional/tests/conftest.py index ce6a74c..b8f6ed3 100644 --- a/test/functional/tests/conftest.py +++ b/test/functional/tests/conftest.py @@ -275,12 +275,14 @@ def base_prepare(item): disk.remove_partitions() create_partition_table(disk, PartitionTable.gpt) + cas_version = TestRun.config.get("cas_version") or git.get_current_commit_hash() if get_force_param(item) and not TestRun.usr.already_updated: installer.rsync_opencas_sources() - installer.reinstall_opencas() - elif not installer.check_if_installed(): + installer.reinstall_opencas(cas_version) + elif not installer.check_if_installed(cas_version): installer.rsync_opencas_sources() - installer.set_up_opencas() + installer.set_up_opencas(cas_version) + TestRun.usr.already_updated = True TestRun.LOGGER.add_build_info(f'Commit hash:') TestRun.LOGGER.add_build_info(f"{git.get_current_commit_hash()}")