diff --git a/tests/conftest.py b/tests/conftest.py index f5384db57032..80265e488674 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,12 +15,15 @@ from functools import partial import boto3 +import yaml from botocore.exceptions import ClientError import pytest from collections import namedtuple from ocs_ci.deployment import factory as dep_factory -from ocs_ci.framework import config as ocsci_config +from ocs_ci.deployment.helpers.hypershift_base import HyperShiftBase +from ocs_ci.deployment.hosted_cluster import HostedClients +from ocs_ci.framework import config as ocsci_config, Config import ocs_ci.framework.pytest_customization.marks from ocs_ci.framework.pytest_customization.marks import ( deployment, @@ -38,6 +41,7 @@ craft_s3_command, put_bucket_policy, ) +from ocs_ci.ocs.constants import FUSION_CONF_DIR from ocs_ci.ocs.dr.dr_workload import BusyBox, BusyBox_AppSet, CnvWorkload from ocs_ci.ocs.exceptions import ( CommandFailed, @@ -141,6 +145,7 @@ skipif_ui_not_support, run_cmd, ceph_health_check_multi_storagecluster_external, + CustomJSONEncoder, ) from ocs_ci.helpers import helpers, dr_helpers from ocs_ci.helpers.helpers import ( @@ -7560,3 +7565,127 @@ def update_current_active_test_marks_global(request): """ marks = [mark.name for mark in request.node.iter_markers()] ocs_ci.framework.pytest_customization.marks.current_test_marks = marks + + +@pytest.fixture() +def create_hypershift_clusters(): + """ + Create hosted hyperhift clusters. + + Here we create cluster deployment configuration that was set in the Test. With this configuration we + create a hosted cluster. After successful creation of the hosted cluster, we update the Multicluster Config, + adding the new cluster configuration to the list of the clusters. Now we can operate with new and old clusters + switching the context of Multicluster Config + + Following arguments are necessary to build the hosted cluster configuration: + ENV_DATA: + clusters: + : + hosted_cluster_path: + ocp_version: + cpu_cores_per_hosted_cluster: + memory_per_hosted_cluster: + hosted_odf_registry: + hosted_odf_version: + setup_storage_client: + nodepool_replicas: + + """ + + def factory( + cluster_names, ocp_version, odf_version, setup_storage_client, nodepool_replicas + ): + """ + Factory function implementing the fixture + + Args: + cluster_names (list): List of cluster names + ocp_version (str): OCP version + odf_version (str): ODF version + setup_storage_client (bool): Setup storage client + nodepool_replicas (int): Nodepool replicas; supported values are 2,3 + + """ + hosted_cluster_conf_on_provider = {"ENV_DATA": {"clusters": {}}} + + for cluster_name in cluster_names: + hosted_cluster_conf_on_provider["ENV_DATA"]["clusters"][cluster_name] = { + "hosted_cluster_path": f"~/clusters/{cluster_name}/openshift-cluster-dir", + "ocp_version": ocp_version, + "cpu_cores_per_hosted_cluster": 8, + "memory_per_hosted_cluster": "12Gi", + "hosted_odf_registry": "quay.io/rhceph-dev/ocs-registry", + "hosted_odf_version": odf_version, + "setup_storage_client": setup_storage_client, + "nodepool_replicas": nodepool_replicas, + } + + log.info( + "Creating a hosted clusters with following deployment config: \n%s", + json.dumps( + hosted_cluster_conf_on_provider, indent=4, cls=CustomJSONEncoder + ), + ) + ocsci_config.update(hosted_cluster_conf_on_provider) + + # During the initial deployment phase, we always deploy Hosting and specific Hosted clusters. + # To distinguish between clusters intended for deployment on deployment CI stage and those intended for + # deployment on the Test stage, we pass the names of the clusters to be deployed to the + # HostedClients().do_deploy() method. + hosted_clients_obj = HostedClients() + deployed_hosted_cluster_objects = hosted_clients_obj.do_deploy(cluster_names) + deployed_clusters = [obj.name for obj in deployed_hosted_cluster_objects] + + for cluster_name in deployed_clusters: + + client_conf_default_dir = os.path.join( + FUSION_CONF_DIR, f"hypershift_client_bm_{nodepool_replicas}w.yaml" + ) + if not os.path.exists(client_conf_default_dir): + raise FileNotFoundError(f"File {client_conf_default_dir} not found") + with open(client_conf_default_dir) as file_stream: + def_client_config_dict = { + k: (v if v is not None else {}) + for (k, v) in yaml.safe_load(file_stream).items() + } + def_client_config_dict.get("ENV_DATA").update( + {"cluster_name": cluster_name} + ) + kubeconfig_path = hosted_clients_obj.get_kubeconfig_path(cluster_name) + log.info(f"Kubeconfig path: {kubeconfig_path}") + def_client_config_dict.get("RUN").update( + {"kubeconfig": kubeconfig_path} + ) + cluster_config = Config() + cluster_config.update(def_client_config_dict) + + log.debug( + "Inserting new hosted cluster config to Multicluster Config " + f"\n{json.dumps(vars(cluster_config), indent=4, cls=CustomJSONEncoder)}" + ) + ocsci_config.insert_cluster_config( + ocsci_config.nclusters, cluster_config + ) + + return factory + + +@pytest.fixture() +def destroy_hosted_cluster(): + def factory(cluster_name): + ocsci_config.switch_to_provider() + log.info("Destroying hosted cluster. OCS related leftovers are expected") + hypershift_base_obj = HyperShiftBase() + + if not hypershift_base_obj.hcp_binary_exists(): + hypershift_base_obj.update_hcp_binary() + + destroy_res = HyperShiftBase().destroy_kubevirt_cluster(cluster_name) + + if destroy_res: + log.info("Removing cluster from Multicluster Config") + ocsci_config.remove_cluster_by_name(cluster_name) + + return destroy_res + + return factory diff --git a/tests/cross_functional/conftest.py b/tests/cross_functional/conftest.py index 15213f7f1f1b..4c6c4593f3cc 100644 --- a/tests/cross_functional/conftest.py +++ b/tests/cross_functional/conftest.py @@ -1,5 +1,4 @@ import os -import json import logging import boto3 import pytest @@ -7,14 +6,9 @@ from concurrent.futures import ThreadPoolExecutor from threading import Event -import yaml - -from ocs_ci.deployment.helpers.hypershift_base import HyperShiftBase -from ocs_ci.deployment.hosted_cluster import HostedClients -from ocs_ci.ocs.constants import FUSION_CONF_DIR from ocs_ci.utility import version from ocs_ci.utility.retry import retry -from ocs_ci.framework import config, Config +from ocs_ci.framework import config from ocs_ci.helpers.e2e_helpers import ( create_muliple_types_provider_obcs, validate_mcg_bucket_replicaton, @@ -58,7 +52,7 @@ from ocs_ci.utility.kms import is_kms_enabled -from ocs_ci.utility.utils import clone_notify, CustomJSONEncoder +from ocs_ci.utility.utils import clone_notify logger = logging.getLogger(__name__) @@ -1354,125 +1348,3 @@ def factory( return feature_setup_map return factory - - -@pytest.fixture() -def create_hypershift_clusters(): - """ - Create hosted hyperhift clusters. - - Here we create cluster deployment configuration that was set in the Test. With this configuration we - create a hosted cluster. After successful creation of the hosted cluster, we update the Multicluster Config, - adding the new cluster configuration to the list of the clusters. Now we can operate with new and old clusters - switching the context of Multicluster Config - - Following arguments are necessary to build the hosted cluster configuration: - ENV_DATA: - clusters: - : - hosted_cluster_path: - ocp_version: - cpu_cores_per_hosted_cluster: - memory_per_hosted_cluster: - hosted_odf_registry: - hosted_odf_version: - setup_storage_client: - nodepool_replicas: - - """ - - def factory( - cluster_names, ocp_version, odf_version, setup_storage_client, nodepool_replicas - ): - """ - Factory function implementing the fixture - - Args: - cluster_names (list): List of cluster names - ocp_version (str): OCP version - odf_version (str): ODF version - setup_storage_client (bool): Setup storage client - nodepool_replicas (int): Nodepool replicas; supported values are 2,3 - - """ - hosted_cluster_conf_on_provider = {"ENV_DATA": {"clusters": {}}} - - for cluster_name in cluster_names: - hosted_cluster_conf_on_provider["ENV_DATA"]["clusters"][cluster_name] = { - "hosted_cluster_path": f"~/clusters/{cluster_name}/openshift-cluster-dir", - "ocp_version": ocp_version, - "cpu_cores_per_hosted_cluster": 8, - "memory_per_hosted_cluster": "12Gi", - "hosted_odf_registry": "quay.io/rhceph-dev/ocs-registry", - "hosted_odf_version": odf_version, - "setup_storage_client": setup_storage_client, - "nodepool_replicas": nodepool_replicas, - } - - logger.info( - "Creating a hosted clusters with following deployment config: \n%s", - json.dumps( - hosted_cluster_conf_on_provider, indent=4, cls=CustomJSONEncoder - ), - ) - config.update(hosted_cluster_conf_on_provider) - - # During the initial deployment phase, we always deploy Hosting and specific Hosted clusters. - # To distinguish between clusters intended for deployment on deployment CI stage and those intended for - # deployment on the Test stage, we pass the names of the clusters to be deployed to the - # HostedClients().do_deploy() method. - hosted_clients_obj = HostedClients() - deployed_hosted_cluster_objects = hosted_clients_obj.do_deploy(cluster_names) - deployed_clusters = [obj.name for obj in deployed_hosted_cluster_objects] - - for cluster_name in deployed_clusters: - - client_conf_default_dir = os.path.join( - FUSION_CONF_DIR, f"hypershift_client_bm_{nodepool_replicas}w.yaml" - ) - if not os.path.exists(client_conf_default_dir): - raise FileNotFoundError(f"File {client_conf_default_dir} not found") - with open(client_conf_default_dir) as file_stream: - def_client_config_dict = { - k: (v if v is not None else {}) - for (k, v) in yaml.safe_load(file_stream).items() - } - def_client_config_dict.get("ENV_DATA").update( - {"cluster_name": cluster_name} - ) - kubeconfig_path = hosted_clients_obj.get_kubeconfig_path(cluster_name) - logger.info(f"Kubeconfig path: {kubeconfig_path}") - def_client_config_dict.get("RUN").update( - {"kubeconfig": kubeconfig_path} - ) - cluster_config = Config() - cluster_config.update(def_client_config_dict) - - logger.debug( - "Inserting new hosted cluster config to Multicluster Config " - f"\n{json.dumps(vars(cluster_config), indent=4, cls=CustomJSONEncoder)}" - ) - config.insert_cluster_config(config.nclusters, cluster_config) - - return factory - - -@pytest.fixture() -def destroy_hosted_cluster(): - def factory(cluster_name): - config.switch_to_provider() - logger.info("Destroying hosted cluster. OCS related leftovers are expected") - hypershift_base_obj = HyperShiftBase() - - if not hypershift_base_obj.hcp_binary_exists(): - hypershift_base_obj.update_hcp_binary() - - destroy_res = HyperShiftBase().destroy_kubevirt_cluster(cluster_name) - - if destroy_res: - logger.info("Removing cluster from Multicluster Config") - config.remove_cluster_by_name(cluster_name) - - return destroy_res - - return factory diff --git a/tests/libtest/test_provider_create_hosted_cluster.py b/tests/libtest/test_provider_create_hosted_cluster.py index cffe0fcedf08..c94d1ec62856 100644 --- a/tests/libtest/test_provider_create_hosted_cluster.py +++ b/tests/libtest/test_provider_create_hosted_cluster.py @@ -22,10 +22,7 @@ from ocs_ci.utility.utils import get_latest_release_version from ocs_ci.utility.version import get_ocs_version_from_csv from ocs_ci.framework import config as ocsci_config -from tests.cross_functional.conftest import ( - create_hypershift_clusters, - destroy_hosted_cluster, -) + logger = logging.getLogger(__name__) @@ -133,7 +130,9 @@ def test_storage_client_connected(self): @runs_on_provider @hci_provider_required - def test_create_hosted_cluster_with_fixture(self): + def test_create_hosted_cluster_with_fixture( + self, create_hypershift_clusters, destroy_hosted_cluster + ): """ Test create hosted cluster with fixture """