From ac79d3d160d0de9d38dec1ff590d483c8a07d52f Mon Sep 17 00:00:00 2001 From: Mahesh Shetty Date: Thu, 18 Jul 2024 13:03:34 +0530 Subject: [PATCH] CNV setup, cleanup. Vm setup fixture Signed-off-by: Mahesh Shetty --- ocs_ci/deployment/cnv.py | 87 ++++++++++++++++++++++++++++++++++++++++ tests/conftest.py | 68 +++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) diff --git a/ocs_ci/deployment/cnv.py b/ocs_ci/deployment/cnv.py index 7b730bc8e373..83308217c637 100644 --- a/ocs_ci/deployment/cnv.py +++ b/ocs_ci/deployment/cnv.py @@ -648,3 +648,90 @@ def disable_multicluster_engine(self): logger.error(f"Failed to disable multicluster engine\n{cmd_res.stderr}") return logger.info(cmd_res.stdout.decode("utf-8").splitlines()) + + def remove_hyperconverged(self): + """ + Remove HyperConverged CR + + """ + hyperconverged_obj = OCP( + kind=constants.HYPERCONVERGED, + resource_name=constants.KUBEVIRT_HYPERCONVERGED, + namespace=self.namespace, + ) + hyperconverged_obj.delete() + logger.info( + f"Deleted {constants.HYPERCONVERGED} {constants.KUBEVIRT_HYPERCONVERGED}" + ) + + def remove_cnv_subscription(self): + """ + Remove CNV subscription + + """ + cnv_sub = OCP( + kind=constants.SUBSCRIPTION, + resource_name=constants.KUBEVIRT_HYPERCONVERGED, + namespace=self.namespace, + ) + cnv_sub.delete() + logger.info(f"Deleted subscription {constants.KUBEVIRT_HYPERCONVERGED}") + + def remove_cnv_csv(self): + """ + Remove CNV ClusterServiceVersion + + """ + cnv_csv = OCP( + kind=constants.CLUSTER_SERVICE_VERSION, + selector=constants.CNV_SELECTOR, + namespace=self.namespace, + ) + cnv_csv.delete() + logger.info(f"Deleted ClusterServiceVersion {constants.CNV_OPERATORNAME}") + + def remove_crds(self): + """ + Remove openshift virtualization CRDs + + """ + OCP().exec_oc_cmd( + command=f"delete crd -n {self.namespace} -l {constants.CNV_SELECTOR}" + ) + logger.info("Deleted all the openshift virtualization CRDs") + + def remove_namespace(self): + """ + Remove openshift virtualization namespace + + """ + cnv_namespace = OCP( + kind=constants.NAMESPACE, resource_name=constants.CNV_NAMESPACE + ) + cnv_namespace.delete() + logger.info(f"Deleted the namespace {constants.CNV_NAMESPACE}") + + def cleanup_cnv(self, check_cnv_installed=False): + """ + Uninstall CNV deployment + + """ + if check_cnv_installed: + if not self.cnv_hyperconverged_installed(): + logger.info("CNV is not installed, skipping the cleanup...") + return + + logger.info("Removing the virtualization hyperconverged") + self.remove_hyperconverged() + + logger.info("Removing the virtualization subscription") + self.remove_cnv_subscription() + + logger.info("Removing the virtualization CSV") + self.remove_cnv_csv() + + logger.info("Removing the namespace") + self.remove_namespace() + + logger.info("Removing the openshift virtualization CRDs") + self.remove_crds() diff --git a/tests/conftest.py b/tests/conftest.py index e550f21dcbb6..e6e084a3310a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,7 @@ import pytest from collections import namedtuple +from ocs_ci.deployment.cnv import CNVInstaller from ocs_ci.deployment import factory as dep_factory from ocs_ci.framework import config as ocsci_config import ocs_ci.framework.pytest_customization.marks @@ -30,6 +31,12 @@ upgrade_marks, ignore_resource_not_found_error_label, ) +from ocs_ci.helpers.cnv_helpers import ( + get_pvc_from_vm, + get_secret_from_vm, + get_volumeimportsource, + create_vm_using_standalone_pvc, +) from ocs_ci.helpers.proxy import update_container_with_proxy_env from ocs_ci.ocs import constants, defaults, fio_artefacts, node, ocp, platform_nodes from ocs_ci.ocs.acm.acm import login_to_acm @@ -7816,3 +7823,64 @@ def finalizer(): request.addfinalizer(finalizer) return factory + + +@pytest.fixture(scope="session") +def setup_cnv(request): + """ + Session scoped fixture to setup and cleanup CNV + based on need of the tests + + """ + cnv_obj = CNVInstaller() + cnv_obj.deploy_cnv(check_cnv_deployed=True, check_cnv_ready=True) + + def finalizer(): + """ + Clean up CNV deployment + + """ + cnv_obj.cleanup_cnv() + + request.addfinalizer(finalizer) + + +@pytest.fixture() +def setup_vms_standalone_pvc(request, project_factory, setup_cnv): + """ + This fixture will setup VM using standalone PVC + + """ + vm_obj = None + + def factory(): + nonlocal vm_obj + project_obj = project_factory() + log.info(f"Created project {project_obj.namespace} for VMs") + vm_obj = create_vm_using_standalone_pvc( + running=True, namespace=project_obj.namespace + ) + return vm_obj + + def finalizer(): + """ + Teardown all the objects created as part of + the setup factory + + """ + pvc_obj = get_pvc_from_vm(vm_obj) + secret_obj = get_secret_from_vm(vm_obj) + volumeimportsource_obj = get_volumeimportsource(pvc_obj=pvc_obj) + vm_obj.delete() + log.info(f"Successfully deleted VM {vm_obj.name}") + pvc_obj.delete() + log.info(f"Successfully deleted PVC {pvc_obj.name}") + secret_obj.delete() + log.info(f"Successfully deleted secret {secret_obj.name}") + volumeimportsource_obj.delete() + log.info( + f"Successfully deleted VolumeImportSource {volumeimportsource_obj.name}" + ) + + request.addfinalizer(finalizer) + return factory