Skip to content

Commit

Permalink
CNV setup, cleanup. Vm setup fixture
Browse files Browse the repository at this point in the history
Signed-off-by: Mahesh Shetty <[email protected]>
  • Loading branch information
mashetty330 committed Jul 18, 2024
1 parent ede3505 commit ac79d3d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
87 changes: 87 additions & 0 deletions ocs_ci/deployment/cnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
68 changes: 68 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit ac79d3d

Please sign in to comment.