From 6ca89967bc10e265408811f26ed172638d795cbd Mon Sep 17 00:00:00 2001 From: Daniel Osypenko Date: Wed, 11 Dec 2024 15:19:46 +0200 Subject: [PATCH] fix error in test 0.2 (#10910) Signed-off-by: Daniel Osypenko --- .../utility/deployment_openshift_logging.py | 54 ++++++++++++++----- tests/conftest.py | 22 ++++---- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/ocs_ci/utility/deployment_openshift_logging.py b/ocs_ci/utility/deployment_openshift_logging.py index a51068c5a15..768de134fbe 100644 --- a/ocs_ci/utility/deployment_openshift_logging.py +++ b/ocs_ci/utility/deployment_openshift_logging.py @@ -21,7 +21,7 @@ logger = logging.getLogger(__name__) -def create_namespace(yaml_file): +def create_namespace(yaml_file, skip_resource_exists=False): """ Creation of namespace "openshift-operators-redhat" for Elasticsearch-operator and "openshift-logging" @@ -29,6 +29,7 @@ def create_namespace(yaml_file): Args: yaml_file (str): Path to yaml file to create namespace + skip_resource_exists: Skip the namespace creation if it already exists Example: create_namespace(yaml_file=constants.EO_NAMESPACE_YAML) @@ -38,11 +39,20 @@ def create_namespace(yaml_file): namespaces = ocp.OCP(kind=constants.NAMESPACES) logger.info("Creating Namespace.........") - assert namespaces.create(yaml_file=yaml_file), "Failed to create namespace" + try: + assert namespaces.create(yaml_file=yaml_file), "Failed to create namespace" + except CommandFailed as e: + if "AlreadyExists" in str(e) and skip_resource_exists: + # on Rosa HCP the ns created from the deployment + logger.warning("Namespace already exists") + else: + raise logger.info("Successfully created Namespace") -def create_elasticsearch_operator_group(yaml_file, resource_name): +def create_elasticsearch_operator_group( + yaml_file, resource_name, skip_resource_exists=False +): """ Creation of operator-group for Elastic-search operator @@ -51,6 +61,7 @@ def create_elasticsearch_operator_group(yaml_file, resource_name): elastic-search resource_name (str): Name of the operator group to create for elastic-search + skip_resource_exists: Skip the resource creation if it already exists Returns: bool: True if operator group for elastic search is created @@ -69,7 +80,14 @@ def create_elasticsearch_operator_group(yaml_file, resource_name): namespace=constants.OPENSHIFT_OPERATORS_REDHAT_NAMESPACE, ) - es_operator_group.create(yaml_file=yaml_file) + try: + es_operator_group.create(yaml_file=yaml_file) + except CommandFailed as e: + if "AlreadyExists" in str(e) and skip_resource_exists: + logger.warning("Operator group already exists") + return True + else: + raise try: es_operator_group.get(resource_name, out_yaml_format=True) logger.info("The Operator group is created successfully") @@ -79,7 +97,7 @@ def create_elasticsearch_operator_group(yaml_file, resource_name): return True -def set_rbac(yaml_file, resource_name): +def set_rbac(yaml_file, resource_name, skip_resource_exists=False): """ Setting Role Based Access Control to grant Prometheus permission to access the openshift-operators-redhat namespace @@ -89,7 +107,7 @@ def set_rbac(yaml_file, resource_name): (ROLE BASED ACCESS CONTROL) resource_name (str): Name of the resource for which we give RBAC permissions - + skip_resource_exists: Skip the resource creation if it already exists Returns: bool: True if RBAC is set successfully, false otherwise @@ -107,7 +125,14 @@ def set_rbac(yaml_file, resource_name): namespace=constants.OPENSHIFT_OPERATORS_REDHAT_NAMESPACE, ) - rbac_role.create(yaml_file=yaml_file, out_yaml_format=False) + try: + rbac_role.create(yaml_file=yaml_file, out_yaml_format=False) + except CommandFailed as e: + if "AlreadyExists" in str(e) and skip_resource_exists: + logger.warning("RBAC role already exists") + return True + else: + raise try: rbac_role.get(resource_name, out_yaml_format=True) rbac_rolebinding.get(resource_name, out_yaml_format=True) @@ -151,7 +176,7 @@ def get_elasticsearch_subscription(): return bool(es_sub) -def create_clusterlogging_operator_group(yaml_file): +def create_clusterlogging_operator_group(yaml_file, skip_resource_exists=False): """ Creation of operator-group for clusterlogging operator. @@ -159,8 +184,7 @@ def create_clusterlogging_operator_group(yaml_file): Args: yaml_file (str): Path to yaml file to create operator group for cluster-logging operator - resource_name (str): Name of the operator group to create for - cluster-logging operator + skip_resource_exists: Skip the resource creation if it already exists Returns: bool: True if operator group for cluster-logging is created @@ -174,8 +198,14 @@ def create_clusterlogging_operator_group(yaml_file): operator_group = ocp.OCP( kind=constants.OPERATOR_GROUP, namespace=constants.OPENSHIFT_LOGGING_NAMESPACE ) - - operator_group.create(yaml_file=yaml_file) + try: + operator_group.create(yaml_file=yaml_file) + except CommandFailed as e: + if "AlreadyExists" in str(e) and skip_resource_exists: + logger.warning("Operator group already exists") + return True + else: + raise try: operator_group.get(out_yaml_format=True) logger.info("The Operator group is created successfully") diff --git a/tests/conftest.py b/tests/conftest.py index 8820bac797b..3e9585d9676 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3268,6 +3268,7 @@ def install_logging(request): * The teardown will uninstall cluster-logging from the cluster """ + rosa_hcp_depl = config.ENV_DATA.get("platform") == constants.ROSA_HCP_PLATFORM def finalizer(): uninstall_cluster_logging() @@ -3291,18 +3292,15 @@ def finalizer(): logging_channel = "stable" if ocp_version >= version.VERSION_4_7 else ocp_version # Creates namespace openshift-operators-redhat - try: - ocp_logging_obj.create_namespace(yaml_file=constants.EO_NAMESPACE_YAML) - except CommandFailed as e: - if "AlreadyExists" in str(e): - # on Rosa HCP the ns created from the deployment - log.info("Namespace openshift-operators-redhat already exists") - else: - raise + ocp_logging_obj.create_namespace( + yaml_file=constants.EO_NAMESPACE_YAML, skip_resource_exists=rosa_hcp_depl + ) # Creates an operator-group for elasticsearch assert ocp_logging_obj.create_elasticsearch_operator_group( - yaml_file=constants.EO_OG_YAML, resource_name="openshift-operators-redhat" + yaml_file=constants.EO_OG_YAML, + resource_name="openshift-operators-redhat", + skip_resource_exists=rosa_hcp_depl, ) # Set RBAC policy on the project @@ -3325,11 +3323,13 @@ def finalizer(): ) # Creates a namespace openshift-logging - ocp_logging_obj.create_namespace(yaml_file=constants.CL_NAMESPACE_YAML) + ocp_logging_obj.create_namespace( + yaml_file=constants.CL_NAMESPACE_YAML, skip_resource_exists=rosa_hcp_depl + ) # Creates an operator-group for cluster-logging assert ocp_logging_obj.create_clusterlogging_operator_group( - yaml_file=constants.CL_OG_YAML + yaml_file=constants.CL_OG_YAML, skip_resource_exists=rosa_hcp_depl ) # Creates subscription for cluster-logging