From d0249740266842e4cc4febb3c47243c0b95f8f39 Mon Sep 17 00:00:00 2001 From: "michal.jagiello@t-mobile.pl" Date: Thu, 4 Apr 2024 10:47:55 +0000 Subject: Refactor code a bit to make status test able running on multiple namespaces It's possible to run status on all tests and also exclude some namespaces from check Issue-ID: TEST-404 Change-Id: I33c54624f60b1c4db82a252d97a26e18464ed07f Signed-off-by: Michal Jagiello --- src/onaptests/configuration/settings.py | 4 ++-- src/onaptests/configuration/status_settings.py | 2 ++ src/onaptests/steps/cloud/check_status.py | 19 ++++++++++++------- src/onaptests/steps/cloud/expose_service_node_port.py | 8 ++++---- src/onaptests/steps/instantiate/sdnc_service.py | 2 +- src/onaptests/steps/onboard/cps.py | 4 ++-- .../steps/simulator/pnf_simulator_cnf/pnf_register.py | 2 +- src/onaptests/utils/kubernetes.py | 2 +- 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/onaptests/configuration/settings.py b/src/onaptests/configuration/settings.py index a9e60b0..a3aa3b9 100644 --- a/src/onaptests/configuration/settings.py +++ b/src/onaptests/configuration/settings.py @@ -50,8 +50,8 @@ JSON_REPORTING_FILE_NAME = "reporting.json" K8S_REGION_TYPE = "k8s" TILLER_HOST = "localhost" K8S_CONFIG = None # None means it will use default config (~/.kube/config) -K8S_ONAP_NAMESPACE = "onap" # ONAP Kubernetes namespace -K8S_ADDITIONAL_RESOURCES_NAMESPACE = K8S_ONAP_NAMESPACE # Resources created on tests namespace +K8S_TESTS_NAMESPACE = "onap" # ONAP Kubernetes namespace +K8S_ADDITIONAL_RESOURCES_NAMESPACE = K8S_TESTS_NAMESPACE # Resources created on tests namespace MSB_K8S_OVERRIDE_VALUES = None # SOCK_HTTP = "socks5h://127.0.0.1:8091" diff --git a/src/onaptests/configuration/status_settings.py b/src/onaptests/configuration/status_settings.py index 691c747..a291c21 100644 --- a/src/onaptests/configuration/status_settings.py +++ b/src/onaptests/configuration/status_settings.py @@ -8,7 +8,9 @@ CHECK_POD_VERSIONS = True IGNORE_EMPTY_REPLICAS = False STATUS_DETAILS_JSON = "status-details.json" INCLUDE_ALL_RES_IN_DETAILS = True +CHECK_ALL_NAMESPACES = False EXTRA_NAMESPACE_LIST = [] +EXCLUDE_NAMESPACE_LIST = [] FULL_LOGS_CONTAINERS = [ 'dcae-bootstrap', 'dcae-cloudify-manager', 'aai-resources', diff --git a/src/onaptests/steps/cloud/check_status.py b/src/onaptests/steps/cloud/check_status.py index 359e830..5a4b962 100644 --- a/src/onaptests/steps/cloud/check_status.py +++ b/src/onaptests/steps/cloud/check_status.py @@ -66,7 +66,7 @@ class CheckK8sResourcesStep(BaseStep): @property def is_primary(self) -> bool: """Does step analyses primary namespace.""" - return self.namespace == settings.K8S_ONAP_NAMESPACE + return self.namespace == settings.K8S_TESTS_NAMESPACE def _init_resources(self): if self.resource_type != "": @@ -772,7 +772,7 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): def __init__(self): """Init CheckNamespaceStatusStep.""" - super().__init__(namespace=settings.K8S_ONAP_NAMESPACE, resource_type="") + super().__init__(namespace=settings.K8S_TESTS_NAMESPACE, resource_type="") self.__logger.debug("K8s namespaces status test init started") self.job_list_step = None @@ -792,7 +792,13 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): config.load_incluster_config() else: config.load_kube_config(config_file=settings.K8S_CONFIG) - for namespace in ([self.namespace] + settings.EXTRA_NAMESPACE_LIST): + if settings.CHECK_ALL_NAMESPACES or settings.EXCLUDE_NAMESPACE_LIST: + self.namespaces_to_check_set = {namespace.metadata.name for namespace in + client.CoreV1Api().list_namespace().items} - set( + settings.EXCLUDE_NAMESPACE_LIST) + else: + self.namespaces_to_check_set = set([self.namespace] + settings.EXTRA_NAMESPACE_LIST) + for namespace in self.namespaces_to_check_set: self._init_namespace_steps(namespace) self.pods = [] self.services = [] @@ -827,7 +833,7 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): ingress_list_step = CheckK8sIngressesStep(namespace) pvc_list_step = CheckK8sPvcsStep(namespace) node_list_step = CheckK8sNodesStep(namespace) - if namespace == settings.K8S_ONAP_NAMESPACE: + if namespace == settings.K8S_TESTS_NAMESPACE: self.job_list_step = job_list_step self.pod_list_step = pod_list_step self.service_list_step = service_list_step @@ -868,11 +874,10 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): """Check status of all k8s resources in the selected namespaces. Use settings values: - - K8S_ONAP_NAMESPACE + - K8S_TESTS_NAMESPACE - STATUS_RESULTS_DIRECTORY - STORE_ARTIFACTS - CHECK_POD_VERSIONS - - EXTRA_NAMESPACE_LIST - IGNORE_EMPTY_REPLICAS - INCLUDE_ALL_RES_IN_DETAILS """ @@ -905,7 +910,7 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): self.res_dir)) details = {"namespace": { - "all": settings.EXTRA_NAMESPACE_LIST, + "all": list(self.namespaces_to_check_set - set([self.namespace])), "resources": {} }} diff --git a/src/onaptests/steps/cloud/expose_service_node_port.py b/src/onaptests/steps/cloud/expose_service_node_port.py index 763d0b9..db5b646 100644 --- a/src/onaptests/steps/cloud/expose_service_node_port.py +++ b/src/onaptests/steps/cloud/expose_service_node_port.py @@ -46,7 +46,7 @@ class ExposeServiceNodePortStep(BaseStep): try: service_data: Dict[str, Any] = self.k8s_client.read_namespaced_service( self.service_name, - settings.K8S_ONAP_NAMESPACE + settings.K8S_TESTS_NAMESPACE ) return service_data.spec.type == "NodePort" except ApiException as exc: @@ -59,7 +59,7 @@ class ExposeServiceNodePortStep(BaseStep): Use settings values: - K8S_CONFIG, - - K8S_ONAP_NAMESPACE. + - K8S_TESTS_NAMESPACE. - EXPOSE_SERVICES_NODE_PORTS """ @@ -73,7 +73,7 @@ class ExposeServiceNodePortStep(BaseStep): try: self.k8s_client.patch_namespaced_service( self.service_name, - settings.K8S_ONAP_NAMESPACE, + settings.K8S_TESTS_NAMESPACE, {"spec": {"ports": [{"port": self.port, "nodePort": self.node_port}], "type": "NodePort"}} @@ -98,7 +98,7 @@ class ExposeServiceNodePortStep(BaseStep): try: self.k8s_client.patch_namespaced_service( self.service_name, - settings.K8S_ONAP_NAMESPACE, + settings.K8S_TESTS_NAMESPACE, [ { "op": "remove", diff --git a/src/onaptests/steps/instantiate/sdnc_service.py b/src/onaptests/steps/instantiate/sdnc_service.py index 7896d98..6aea501 100644 --- a/src/onaptests/steps/instantiate/sdnc_service.py +++ b/src/onaptests/steps/instantiate/sdnc_service.py @@ -69,7 +69,7 @@ class CheckSdncDbStep(BaseSdncStep): api_instance = client.CoreV1Api() try: secret = api_instance.read_namespaced_secret( - settings.SDNC_SECRET_NAME, settings.K8S_ONAP_NAMESPACE) + settings.SDNC_SECRET_NAME, settings.K8S_TESTS_NAMESPACE) if secret.data: if (self.SDNC_DB_LOGIN in secret.data and self.SDNC_DB_PASSWORD in secret.data): login_base64 = secret.data[self.SDNC_DB_LOGIN] diff --git a/src/onaptests/steps/onboard/cps.py b/src/onaptests/steps/onboard/cps.py index 8f2525c..280082b 100644 --- a/src/onaptests/steps/onboard/cps.py +++ b/src/onaptests/steps/onboard/cps.py @@ -274,7 +274,7 @@ class CheckPostgressDataBaseConnectionStep(CpsBaseStep): api_instance = client.CoreV1Api() try: secret = api_instance.read_namespaced_secret( - settings.SECRET_NAME, settings.K8S_ONAP_NAMESPACE) + settings.SECRET_NAME, settings.K8S_TESTS_NAMESPACE) if secret.data: if settings.DB_LOGIN in secret.data and settings.DB_PASSWORD in secret.data: login_base64 = secret.data[settings.DB_LOGIN] @@ -328,7 +328,7 @@ class CheckPostgressDataBaseConnectionStep(CpsBaseStep): - DB_PRIMARY_HOST, - DATABASE, - DB_PORT, - - K8S_ONAP_NAMESPACE, + - K8S_TESTS_NAMESPACE, - SECRET_NAME, - DB_LOGIN, - DB_PASSWORD. diff --git a/src/onaptests/steps/simulator/pnf_simulator_cnf/pnf_register.py b/src/onaptests/steps/simulator/pnf_simulator_cnf/pnf_register.py index acfa990..4ef92ef 100644 --- a/src/onaptests/steps/simulator/pnf_simulator_cnf/pnf_register.py +++ b/src/onaptests/steps/simulator/pnf_simulator_cnf/pnf_register.py @@ -86,7 +86,7 @@ class PnfSimulatorCnfRegisterStep(BaseStep): k8s_client: "client.CoreV1Api" = client.CoreV1Api() try: for service in k8s_client.list_namespaced_service( - namespace=settings.K8S_ONAP_NAMESPACE).items: + namespace=settings.K8S_TESTS_NAMESPACE).items: if service.metadata.name == settings.DCAE_VES_COLLECTOR_POD_NAME: proto = "http" if "443" in str(service.spec.ports[0].port): diff --git a/src/onaptests/utils/kubernetes.py b/src/onaptests/utils/kubernetes.py index 05728ca..b86326d 100644 --- a/src/onaptests/utils/kubernetes.py +++ b/src/onaptests/utils/kubernetes.py @@ -22,7 +22,7 @@ class KubernetesHelper: secret_name: str, login_key: str, password_key: str, - namespace: str = settings.K8S_ONAP_NAMESPACE): + namespace: str = settings.K8S_TESTS_NAMESPACE): """Resolve SDNC datbase credentials from k8s secret. Args: -- cgit 1.2.3-korg