diff options
Diffstat (limited to 'src/onaptests/steps')
28 files changed, 232 insertions, 228 deletions
diff --git a/src/onaptests/steps/base.py b/src/onaptests/steps/base.py index 4fca3e7..918bc6d 100644 --- a/src/onaptests/steps/base.py +++ b/src/onaptests/steps/base.py @@ -16,14 +16,16 @@ from onaptests.utils.exceptions import (OnapTestException, OnapTestExceptionGroup, SkipExecutionException, SubstepExecutionException, - SubstepExecutionExceptionGroup) + SubstepExecutionExceptionGroup, + TestConfigurationException) -IF_VALIDATION = "PYTHON_SDK_TESTS_VALIDATION" +# pylint: disable=protected-access IF_FORCE_CLEANUP = "PYTHON_SDK_TESTS_FORCE_CLEANUP" class StoreStateHandler(ABC): + """Decorator for storing the state of executed test step.""" @classmethod def store_state(cls, fun=None, *, cleanup=False): # noqa @@ -117,7 +119,7 @@ class StoreStateHandler(ABC): class BaseStep(StoreStateHandler, ABC): """Base step class.""" - """Indicates that Step has no dedicated cleanup method""" + # Indicates that Step has no dedicated cleanup method HAS_NO_CLEANUP = False _logger: logging.Logger = logging.getLogger("") @@ -159,7 +161,7 @@ class BaseStep(StoreStateHandler, ABC): self._state_clean: bool = False self._nesting_level: int = 0 self._break_on_error: bool = break_on_error - self._is_validation_only = os.environ.get(IF_VALIDATION) is not None + self._is_validation_only = settings.IF_VALIDATION self._is_force_cleanup = os.environ.get(IF_FORCE_CLEANUP) is not None def add_step(self, step: "BaseStep") -> None: @@ -228,7 +230,7 @@ class BaseStep(StoreStateHandler, ABC): if not self.is_root: return self.parent.reports_collection if not self._reports_collection: - self._reports_collection = ReportsCollection() + self._reports_collection = ReportsCollection(self._component_list()) for step_report in itertools.chain(self.execution_reports, self.cleanup_reports): self._reports_collection.put(step_report) return self._reports_collection @@ -294,6 +296,16 @@ class BaseStep(StoreStateHandler, ABC): """ + def _component_list(self, components: dict = None): + if not components: + components = {} + for step in self._steps: + components[step.component] = step.component + step._component_list(components) + if not self.is_root or not components: + components[self.component] = self.component + return list(components) + def _step_title(self, cleanup=False): cleanup_label = " Cleanup:" if cleanup else ":" return f"[{self.component}] {self.name}{cleanup_label} {self.description}" @@ -332,8 +344,7 @@ class BaseStep(StoreStateHandler, ABC): substep_error = True if step._break_on_error: raise SubstepExecutionException from substep_err - else: - self._logger.exception(substep_err) + self._logger.exception(substep_err) if self._steps: if substep_error and self._break_on_error: raise SubstepExecutionException("Cannot continue due to failed substeps") @@ -364,8 +375,7 @@ class BaseStep(StoreStateHandler, ABC): if len(exceptions_to_raise) > 0: if len(exceptions_to_raise) == 1: raise exceptions_to_raise[0] - else: - raise SubstepExecutionExceptionGroup("Substep Exceptions", exceptions_to_raise) + raise SubstepExecutionExceptionGroup("Substep Exceptions", exceptions_to_raise) def cleanup(self) -> None: """Step's cleanup. @@ -391,14 +401,29 @@ class BaseStep(StoreStateHandler, ABC): Customer.set_proxy(onap_proxy) def validate_execution(self): + """Validate if each step was executed by decorator.""" + if self._is_validation_only: - self._log_execution_state(f"VALIDATE [{self._executed}, {self._cleanup}]") - if self._executed and self._cleanup and not self._cleaned_up: - self._logger.error(f"{self._step_title()} Cleanup Not Executed") - assert self._cleaned_up - for step in reversed(self._steps): + self._log_execution_state(f"VALIDATE EXECUTION [{self._state_execute}]") + if not self._state_execute: + raise TestConfigurationException( + f"{self._step_title()} - Execute decorator was not called") + for step in self._steps: step.validate_execution() + def validate_cleanup(self): + """Validate if each step was cleaned by decorator.""" + + if self._is_validation_only: + for step in reversed(self._steps): + step.validate_cleanup() + if self._cleanup: + self._log_execution_state( + f"VALIDATE CLEANUP [{self._state_clean}, {self._cleanup}]") + if not self._state_clean: + raise TestConfigurationException( + f"{self._step_title()} - Cleanup decorator was not called") + class YamlTemplateBaseStep(BaseStep, ABC): """Base YAML template step.""" diff --git a/src/onaptests/steps/cloud/check_status.py b/src/onaptests/steps/cloud/check_status.py index cbefa18..b3fab03 100644 --- a/src/onaptests/steps/cloud/check_status.py +++ b/src/onaptests/steps/cloud/check_status.py @@ -24,6 +24,7 @@ from .resources import (ConfigMap, Container, DaemonSet, Deployment, Ingress, class CheckK8sResourcesStep(BaseStep): + """Base step for check of k8s resources in the selected namespace.""" __logger = logging.getLogger(__name__) @@ -110,6 +111,7 @@ class CheckK8sResourcesStep(BaseStep): class CheckBasicK8sResourcesStep(CheckK8sResourcesStep): + """Basic check of k8s resources in the selected namespace.""" def __init__(self, namespace: str, resource_type: str, k8s_res_class): """Init CheckBasicK8sResourcesStep.""" @@ -129,6 +131,7 @@ class CheckBasicK8sResourcesStep(CheckK8sResourcesStep): class CheckK8sConfigMapsStep(CheckBasicK8sResourcesStep): + """Check of k8s configmap in the selected namespace.""" def __init__(self, namespace: str): """Init CheckK8sConfigMapsStep.""" @@ -140,6 +143,7 @@ class CheckK8sConfigMapsStep(CheckBasicK8sResourcesStep): class CheckK8sSecretsStep(CheckBasicK8sResourcesStep): + """Check of k8s secrets in the selected namespace.""" def __init__(self, namespace: str): """Init CheckK8sSecretsStep.""" @@ -151,6 +155,7 @@ class CheckK8sSecretsStep(CheckBasicK8sResourcesStep): class CheckK8sIngressesStep(CheckBasicK8sResourcesStep): + """Check of k8s ingress in the selected namespace.""" def __init__(self, namespace: str): """Init CheckK8sIngressesStep.""" @@ -162,6 +167,7 @@ class CheckK8sIngressesStep(CheckBasicK8sResourcesStep): class CheckK8sPvcsStep(CheckK8sResourcesStep): + """Check of k8s pvcs in the selected namespace.""" def __init__(self, namespace: str): """Init CheckK8sPvcsStep.""" @@ -194,6 +200,7 @@ class CheckK8sPvcsStep(CheckK8sResourcesStep): class CheckK8sResourcesUsingPodsStep(CheckK8sResourcesStep): + """Check of k8s respurces with pods in the selected namespace.""" def __init__(self, namespace: str, resource_type: str, pods_source): """Init CheckK8sResourcesUsingPodsStep.""" @@ -231,6 +238,7 @@ class CheckK8sResourcesUsingPodsStep(CheckK8sResourcesStep): class CheckK8sJobsStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s jobs in the selected namespace.""" __logger = logging.getLogger(__name__) @@ -273,12 +281,13 @@ class CheckK8sJobsStep(CheckK8sResourcesUsingPodsStep): if not any(waiver_elt in job.name for waiver_elt in settings.WAIVER_LIST): self.all_resources.append(job) else: - self.__logger.warn( + self.__logger.warning( "Waiver pattern found in job, exclude %s", job.name) jobs_pods += job_pods class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s pods in the selected namespace.""" __logger = logging.getLogger(__name__) @@ -400,7 +409,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): self.jinja_env.get_template('pod.html.j2').stream(pod=pod).dump( '{}/pod-{}.html'.format(self.res_dir, pod.name)) if any(waiver_elt in pod.name for waiver_elt in settings.WAIVER_LIST): - self.__logger.warn("Waiver pattern found in pod, exclude %s", pod.name) + self.__logger.warning("Waiver pattern found in pod, exclude %s", pod.name) else: self.all_resources.append(pod) @@ -412,7 +421,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): containers=containers).dump('{}/container_versions.html'.format( self.res_dir)) # create a json file for version tracking - with open(self.res_dir + "/onap_versions.json", "w") as write_file: + with open(self.res_dir + "/onap_versions.json", "w", encoding="utf-8") as write_file: json.dump(pod_versions, write_file) def _get_container_logs(self, pod, container, full=True, previous=False): @@ -430,9 +439,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): ) except UnicodeDecodeError: logs = "{0} has an unicode decode error...".format(pod.name) - self.__logger.error( - "{0} has an unicode decode error in the logs...", pod.name, - ) + self.__logger.error(logs) return logs def _parse_container(self, pod, k8s_container, init=False): # noqa @@ -463,7 +470,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): with open( "{}/pod-{}-{}.log".format(self.res_dir, pod.name, container.name), - 'w') as log_result: + 'w', encoding="utf-8") as log_result: log_result.write(logs) if (not container.ready) and container.restart_count > 0: old_logs = self._get_container_logs(pod=pod, container=container, @@ -472,16 +479,16 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): "{}/pod-{}-{}.old.log".format(self.res_dir, pod.name, container.name), - 'w') as log_result: + 'w', encoding="utf-8") as log_result: log_result.write(old_logs) - if (container.name in settings.FULL_LOGS_CONTAINERS): + if container.name in settings.FULL_LOGS_CONTAINERS: logs = self._get_container_logs(pod=pod, container=container) with open( "{}/pod-{}-{}.log".format(self.res_dir, pod.name, container.name), - 'w') as log_result: + 'w', encoding="utf-8") as log_result: log_result.write(logs) - if (container.name in settings.SPECIFIC_LOGS_CONTAINERS): + if container.name in settings.SPECIFIC_LOGS_CONTAINERS: for log_file in settings.SPECIFIC_LOGS_CONTAINERS[container.name]: exec_command = ['/bin/sh', '-c', "cat {}".format(log_file)] log_files[log_file] = stream( @@ -499,7 +506,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): "{}/pod-{}-{}-{}.log".format( self.res_dir, pod.name, container.name, log_file_slug), - 'w') as log_result: + 'w', encoding="utf-8") as log_result: log_result.write(log_files[log_file]) except client.rest.ApiException as exc: self.__logger.warning("%scontainer %s of pod %s has an exception: %s", @@ -512,7 +519,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): log_files=log_files).dump('{}/pod-{}-{}-logs.html'.format( self.res_dir, pod.name, container.name)) if any(waiver_elt in container.name for waiver_elt in settings.WAIVER_LIST): - self.__logger.warn( + self.__logger.warning( "Waiver pattern found in container, exclude %s", container.name) else: containers_list.append(container) @@ -522,6 +529,7 @@ class CheckK8sPodsStep(CheckK8sResourcesUsingPodsStep): class CheckK8sServicesStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s services in the selected namespace.""" def __init__(self, namespace: str, pods): """Init CheckK8sServicesStep.""" @@ -547,6 +555,7 @@ class CheckK8sServicesStep(CheckK8sResourcesUsingPodsStep): class CheckK8sDeploymentsStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s deployments in the selected namespace.""" def __init__(self, namespace: str, pods): """Init CheckK8sDeploymentsStep.""" @@ -585,6 +594,7 @@ class CheckK8sDeploymentsStep(CheckK8sResourcesUsingPodsStep): class CheckK8sReplicaSetsStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s replicasets in the selected namespace.""" def __init__(self, namespace: str, pods): """Init CheckK8sReplicaSetsStep.""" @@ -625,6 +635,7 @@ class CheckK8sReplicaSetsStep(CheckK8sResourcesUsingPodsStep): class CheckK8sStatefulSetsStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s statefulsets in the selected namespace.""" def __init__(self, namespace: str, pods): """Init CheckK8sStatefulSetsStep.""" @@ -665,6 +676,7 @@ class CheckK8sStatefulSetsStep(CheckK8sResourcesUsingPodsStep): class CheckK8sDaemonSetsStep(CheckK8sResourcesUsingPodsStep): + """Check of k8s daemonsets in the selected namespace.""" def __init__(self, namespace: str, pods): """Init CheckK8sDaemonSetsStep.""" @@ -694,7 +706,7 @@ class CheckK8sDaemonSetsStep(CheckK8sResourcesUsingPodsStep): daemonset=daemonset).dump('{}/daemonset-{}.html'.format( self.res_dir, daemonset.name)) - if (k8s.status.number_ready < k8s.status.desired_number_scheduled): + if k8s.status.number_ready < k8s.status.desired_number_scheduled: self._add_failing_resource(daemonset) self.all_resources.append(daemonset) @@ -716,6 +728,24 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): for namespace in ([self.namespace] + settings.EXTRA_NAMESPACE_LIST): self._init_namespace_steps(namespace) + self.pods = [] + self.services = [] + self.jobs = [] + self.deployments = [] + self.replicasets = [] + self.statefulsets = [] + self.daemonsets = [] + self.pvcs = [] + self.configmaps = [] + self.secrets = [] + self.ingresses = [] + self.failing_statefulsets = [] + self.failing_jobs = [] + self.failing_deployments = [] + self.failing_replicasets = [] + self.failing_daemonsets = [] + self.failing_pvcs = [] + def _init_namespace_steps(self, namespace: str): self.job_list_step = CheckK8sJobsStep(namespace) self.pod_list_step = CheckK8sPodsStep(namespace, self.job_list_step) @@ -820,10 +850,12 @@ class CheckNamespaceStatusStep(CheckK8sResourcesStep): ns_details = ns_details[step.namespace] store_results(ns_details, step) - with (Path(self.res_dir).joinpath(settings.STATUS_DETAILS_JSON)).open('w') as file: + with (Path(self.res_dir).joinpath(settings.STATUS_DETAILS_JSON) + ).open('w', encoding="utf-8") as file: json.dump(details, file, indent=4) if self.failing: raise StatusCheckException def map_by_name(self, resources): + """Get resources' names.""" return list(map(lambda resource: resource.name, resources)) diff --git a/src/onaptests/steps/cloud/complex_create.py b/src/onaptests/steps/cloud/complex_create.py index 96d8e7f..7afad16 100644 --- a/src/onaptests/steps/cloud/complex_create.py +++ b/src/onaptests/steps/cloud/complex_create.py @@ -38,4 +38,4 @@ class ComplexCreateStep(BaseStep): data_center_code=settings.COMPLEX_DATA_CENTER_CODE, name=settings.COMPLEX_PHYSICAL_LOCATION_ID) except APIError: - self._logger.warn("Try to update the complex failed.") + self._logger.warning("Try to update the complex failed.") diff --git a/src/onaptests/steps/cloud/customer_create.py b/src/onaptests/steps/cloud/customer_create.py index 96d192a..7bffb1a 100644 --- a/src/onaptests/steps/cloud/customer_create.py +++ b/src/onaptests/steps/cloud/customer_create.py @@ -33,4 +33,4 @@ class CustomerCreateStep(BaseStep): try: Customer.create(settings.GLOBAL_CUSTOMER_ID, settings.GLOBAL_CUSTOMER_ID, "INFRA") except APIError: - self._logger.warn("Try to update the Customer failed.") + self._logger.warning("Try to update the Customer failed.") diff --git a/src/onaptests/steps/cloud/expose_service_node_port.py b/src/onaptests/steps/cloud/expose_service_node_port.py index 1f43a65..4f6eecc 100644 --- a/src/onaptests/steps/cloud/expose_service_node_port.py +++ b/src/onaptests/steps/cloud/expose_service_node_port.py @@ -22,10 +22,6 @@ class ExposeServiceNodePortStep(BaseStep): self.service_name = service_name self.port = port self.node_port = node_port - if settings.IN_CLUSTER: - config.load_incluster_config() - else: - config.load_kube_config(config_file=settings.K8S_CONFIG) self.k8s_client: client.CoreV1Api = client.CoreV1Api() @property @@ -53,9 +49,9 @@ class ExposeServiceNodePortStep(BaseStep): settings.K8S_ONAP_NAMESPACE ) return service_data.spec.type == "NodePort" - except ApiException: + except ApiException as exc: self._logger.exception("Kubernetes API exception") - raise OnapTestException + raise OnapTestException from exc @BaseStep.store_state def execute(self) -> None: @@ -68,6 +64,10 @@ class ExposeServiceNodePortStep(BaseStep): """ super().execute() + if settings.IN_CLUSTER: + config.load_incluster_config() + else: + config.load_kube_config(config_file=settings.K8S_CONFIG) if not self.is_service_node_port_type(): try: self.k8s_client.patch_namespaced_service( @@ -77,15 +77,16 @@ class ExposeServiceNodePortStep(BaseStep): "nodePort": self.node_port}], "type": "NodePort"}} ) - except ApiException: + except ApiException as exc: self._logger.exception("Kubernetes API exception") - raise OnapTestException - except urllib3.exceptions.HTTPError: + raise OnapTestException from exc + except urllib3.exceptions.HTTPError as exc: self._logger.exception("Can't connect with k8s") - raise OnapTestException + raise OnapTestException from exc else: self._logger.debug("Service already patched, skip") + @BaseStep.store_state(cleanup=True) def cleanup(self) -> None: """Step cleanup. @@ -109,12 +110,12 @@ class ExposeServiceNodePortStep(BaseStep): } ] ) - except ApiException: + except ApiException as exc: self._logger.exception("Kubernetes API exception") - raise OnapTestException - except urllib3.exceptions.HTTPError: + raise OnapTestException from exc + except urllib3.exceptions.HTTPError as exc: self._logger.exception("Can't connect with k8s") - raise OnapTestException + raise OnapTestException from exc else: self._logger.debug("Service is not 'NodePort' type, skip") return super().cleanup() diff --git a/src/onaptests/steps/cloud/k8s_connectivity_info_create.py b/src/onaptests/steps/cloud/k8s_connectivity_info_create.py index c22df47..410ac47 100644 --- a/src/onaptests/steps/cloud/k8s_connectivity_info_create.py +++ b/src/onaptests/steps/cloud/k8s_connectivity_info_create.py @@ -42,7 +42,7 @@ class K8SConnectivityInfoStep(BaseStep): except APIError: if settings.IN_CLUSTER: token_file = "/var/run/secrets/kubernetes.io/serviceaccount/token" - with open(token_file, "r") as file: + with open(token_file, "r", encoding="utf-8") as file: user_token_value = file.read().strip() jinja_env = Environment(autoescape=select_autoescape(['json.j2']), loader=PackageLoader('onaptests.templates', 'kubeconfig')) @@ -56,9 +56,10 @@ class K8SConnectivityInfoStep(BaseStep): kubeconfig_data.encode('utf-8')) else: self._logger.info("Create the k8s connectivity information") - ConnectivityInfo.create(settings.CLOUD_REGION_ID, - settings.CLOUD_REGION_CLOUD_OWNER, - open(settings.K8S_CONFIG, 'rb').read()) + with open(settings.K8S_CONFIG, 'rb') as k8s_config: + ConnectivityInfo.create(settings.CLOUD_REGION_ID, + settings.CLOUD_REGION_CLOUD_OWNER, + k8s_config.read()) @BaseStep.store_state(cleanup=True) def cleanup(self) -> None: diff --git a/src/onaptests/steps/cloud/register_cloud.py b/src/onaptests/steps/cloud/register_cloud.py index fbfb6f3..5035f86 100644 --- a/src/onaptests/steps/cloud/register_cloud.py +++ b/src/onaptests/steps/cloud/register_cloud.py @@ -5,9 +5,9 @@ from uuid import uuid4 from onapsdk.aai.cloud_infrastructure import CloudRegion from onapsdk.configuration import settings from onapsdk.exceptions import ResourceNotFound +from onaptests.steps.cloud.cloud_region_create import CloudRegionCreateStep from ..base import BaseStep -from onaptests.steps.cloud.cloud_region_create import CloudRegionCreateStep class RegisterCloudRegionStep(BaseStep): diff --git a/src/onaptests/steps/cloud/resources.py b/src/onaptests/steps/cloud/resources.py index f00b60e..775d574 100644 --- a/src/onaptests/steps/cloud/resources.py +++ b/src/onaptests/steps/cloud/resources.py @@ -23,7 +23,6 @@ class K8sResource(): def specific_k8s_init(self): """Do the specific part for k8s resource when k8s object is present.""" - pass def __repr__(self): return self.name @@ -32,10 +31,9 @@ class K8sResource(): return self.name def __eq__(self, other): - if (isinstance(other, K8sResource)): + if isinstance(other, K8sResource): return self.name == other.name - else: - return False + return False class K8sPodParentResource(K8sResource): diff --git a/src/onaptests/steps/instantiate/k8s_profile_create.py b/src/onaptests/steps/instantiate/k8s_profile_create.py index d3798e2..4be5684 100644 --- a/src/onaptests/steps/instantiate/k8s_profile_create.py +++ b/src/onaptests/steps/instantiate/k8s_profile_create.py @@ -48,7 +48,7 @@ class K8SProfileStep(BaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -152,7 +152,8 @@ class K8SProfileStep(BaseStep): k8s_profile_namespace, settings.K8S_PROFILE_K8S_VERSION) # Upload artifact for created profile - profile.upload_artifact(open(settings.K8S_PROFILE_ARTIFACT_PATH, 'rb').read()) + with open(settings.K8S_PROFILE_ARTIFACT_PATH, 'rb') as k8s_profile: + profile.upload_artifact(k8s_profile.read()) @BaseStep.store_state(cleanup=True) def cleanup(self) -> None: @@ -179,7 +180,7 @@ class K8SProfileStep(BaseStep): try: profile = rbdef.get_profile_by_name(k8s_profile_name) profile.delete() - except APIError: + except APIError as exc: self._logger.error("K8s profile deletion %s failed", k8s_profile_name) - raise onap_test_exceptions.ProfileCleanupException + raise onap_test_exceptions.ProfileCleanupException from exc super().cleanup() diff --git a/src/onaptests/steps/instantiate/sdnc_service.py b/src/onaptests/steps/instantiate/sdnc_service.py index 1c2437f..851902a 100644 --- a/src/onaptests/steps/instantiate/sdnc_service.py +++ b/src/onaptests/steps/instantiate/sdnc_service.py @@ -1,3 +1,5 @@ +import logging + from onapsdk.configuration import settings from onapsdk.exceptions import APIError from onapsdk.sdnc import VfModulePreload @@ -59,7 +61,7 @@ class ServiceCreateStep(BaseSdncStep): if exc.response_status_code == 409: self._logger.warning("SDNC service already exists.") else: - raise OnapTestException("SDNC service creation failed.") + raise OnapTestException("SDNC service creation failed.") from exc @BaseStep.store_state(cleanup=True) def cleanup(self) -> None: @@ -107,8 +109,8 @@ class UpdateSdncService(BaseSdncStep): service.service_data = settings.SERVICE_CHANGED_DATA service.update() self._logger.info("SDNC service update is completed.") - except APIError: - raise OnapTestException("SDNC service update is failed.") + except APIError as exc: + raise OnapTestException("SDNC service update is failed.") from exc class UploadVfModulePreloadStep(BaseSdncStep): @@ -153,6 +155,8 @@ class GetSdncPreloadStep(BaseSdncStep): Get preload information from SDNC over GR-API. """ + __logger = logging.getLogger(__name__) + def __init__(self): """Initialize step. @@ -180,7 +184,7 @@ class GetSdncPreloadStep(BaseSdncStep): self._logger.info("Get existing SDNC service instance and update it over GR-API") preloads = PreloadInformation.get_all() for preload_information in preloads: - print(preload_information) + self.__logger.debug(preload_information) class TestSdncStep(BaseScenarioStep): diff --git a/src/onaptests/steps/instantiate/service_ala_carte.py b/src/onaptests/steps/instantiate/service_ala_carte.py index 528215d..4a0a4c1 100644 --- a/src/onaptests/steps/instantiate/service_ala_carte.py +++ b/src/onaptests/steps/instantiate/service_ala_carte.py @@ -3,7 +3,6 @@ from uuid import uuid4 from yaml import load, SafeLoader from onapsdk.aai.cloud_infrastructure import CloudRegion, Tenant -from onapsdk.aai.business import Customer from onapsdk.aai.business.owning_entity import OwningEntity as AaiOwningEntity from onapsdk.configuration import settings from onapsdk.exceptions import ResourceNotFound @@ -11,82 +10,9 @@ from onapsdk.sdc.service import Service from onapsdk.so.instantiation import ServiceInstantiation import onaptests.utils.exceptions as onap_test_exceptions -from ..base import BaseStep, YamlTemplateBaseStep +from ..base import YamlTemplateBaseStep from ..cloud.connect_service_subscription_to_cloud_region import ConnectServiceSubToCloudRegionStep -from ..onboard.service import ServiceOnboardStep, YamlTemplateServiceOnboardStep - - -class ServiceAlaCarteInstantiateStep(BaseStep): - """Instantiate service a'la carte.""" - - def __init__(self, cleanup=False): - """Initialize step. - - Substeps: - - ServiceOnboardStep, - - ConnectServiceSubToCloudRegionStep. - """ - super().__init__(cleanup=cleanup) - if not settings.ONLY_INSTANTIATE: - self.add_step(ServiceOnboardStep(cleanup)) - self.add_step(ConnectServiceSubToCloudRegionStep(cleanup)) - - @property - def description(self) -> str: - """Step description.""" - return "Instantiate service using SO a'la carte method." - - @property - def component(self) -> str: - """Component name.""" - return "SO" - - @BaseStep.store_state - def execute(self): - """Instantiate service. - - Use settings values: - - SERVICE_NAME, - - GLOBAL_CUSTOMER_ID, - - CLOUD_REGION_CLOUD_OWNER, - - CLOUD_REGION_ID, - - TENANT_ID, - - OWNING_ENTITY, - - PROJECT, - - SERVICE_INSTANCE_NAME. - """ - super().execute() - service = Service(settings.SERVICE_NAME) - customer: Customer = Customer.get_by_global_customer_id(settings.GLOBAL_CUSTOMER_ID) - cloud_region: CloudRegion = CloudRegion.get_by_id( - cloud_owner=settings.CLOUD_REGION_CLOUD_OWNER, - cloud_region_id=settings.CLOUD_REGION_ID, - ) - tenant: Tenant = cloud_region.get_tenant(settings.TENANT_ID) - try: - owning_entity = AaiOwningEntity.get_by_owning_entity_name( - settings.OWNING_ENTITY) - except ResourceNotFound: - self._logger.info("Owning entity not found, create it") - owning_entity = AaiOwningEntity.create(settings.OWNING_ENTITY) - - service_instantiation = ServiceInstantiation.instantiate_ala_carte( - service, - cloud_region, - tenant, - customer, - owning_entity, - settings.PROJECT, - service_instance_name=settings.SERVICE_INSTANCE_NAME - ) - try: - service_instantiation.wait_for_finish(settings.ORCHESTRATION_REQUEST_TIMEOUT) - except TimeoutError: - self._logger.error("Service instantiation %s timed out", self.service_instance_name) - raise onap_test_exceptions.ServiceInstantiateException - if service_instantiation.failed: - self._logger.error("Service instantiation %s failed", self.service_instance_name) - raise onap_test_exceptions.ServiceInstantiateException +from ..onboard.service import YamlTemplateServiceOnboardStep class YamlTemplateServiceAlaCarteInstantiateStep(YamlTemplateBaseStep): @@ -128,7 +54,7 @@ class YamlTemplateServiceAlaCarteInstantiateStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -198,9 +124,9 @@ class YamlTemplateServiceAlaCarteInstantiateStep(YamlTemplateBaseStep): service.name) break self._logger.info( - "Service Distribution for %s ongoing, Wait for 60 s", - service.name) - time.sleep(60) + "Service Distribution for %s ongoing, Wait for %d s", + service.name, settings.SERVICE_DISTRIBUTION_SLEEP_TIME) + time.sleep(settings.SERVICE_DISTRIBUTION_SLEEP_TIME) nb_try += 1 if distribution_completed is False: @@ -220,15 +146,14 @@ class YamlTemplateServiceAlaCarteInstantiateStep(YamlTemplateBaseStep): ) try: service_instantiation.wait_for_finish(settings.ORCHESTRATION_REQUEST_TIMEOUT) - except TimeoutError: + except TimeoutError as exc: self._logger.error("Service instantiation %s timed out", self.service_instance_name) - raise onap_test_exceptions.ServiceCleanupException + raise onap_test_exceptions.ServiceCleanupException from exc if service_instantiation.failed: self._logger.error("Service instantiation %s failed", self.service_instance_name) raise onap_test_exceptions.ServiceInstantiateException - else: - self._load_customer_and_subscription(reload=True) - self._load_service_instance() + self._load_customer_and_subscription(reload=True) + self._load_service_instance() @YamlTemplateBaseStep.store_state(cleanup=True) def cleanup(self) -> None: @@ -244,9 +169,9 @@ class YamlTemplateServiceAlaCarteInstantiateStep(YamlTemplateBaseStep): service_deletion = self._service_instance.delete(a_la_carte=True) try: service_deletion.wait_for_finish(settings.ORCHESTRATION_REQUEST_TIMEOUT) - except TimeoutError: + except TimeoutError as exc: self._logger.error("Service deletion %s timed out", self._service_instance_name) - raise onap_test_exceptions.ServiceCleanupException + raise onap_test_exceptions.ServiceCleanupException from exc if service_deletion.finished: self._logger.info("Service %s deleted", self._service_instance_name) else: diff --git a/src/onaptests/steps/instantiate/service_macro.py b/src/onaptests/steps/instantiate/service_macro.py index 4211cbf..a513d1e 100644 --- a/src/onaptests/steps/instantiate/service_macro.py +++ b/src/onaptests/steps/instantiate/service_macro.py @@ -77,7 +77,7 @@ class YamlTemplateServiceMacroInstantiateStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -94,8 +94,9 @@ class YamlTemplateServiceMacroInstantiateStep(YamlTemplateBaseStep): """ if self.is_root: if not self._model_yaml_template: - with open(settings.MODEL_YAML_TEMPLATE, "r") as model_yaml_template: - self._model_yaml_template: dict = load(model_yaml_template) + with open(settings.MODEL_YAML_TEMPLATE, "r", + encoding="utf-8") as model_yaml_template: + self._model_yaml_template: dict = load(model_yaml_template, SafeLoader) return self._model_yaml_template return self.parent.model_yaml_template @@ -215,9 +216,9 @@ class YamlTemplateServiceMacroInstantiateStep(YamlTemplateBaseStep): ) try: service_instantiation.wait_for_finish(timeout=settings.ORCHESTRATION_REQUEST_TIMEOUT) - except TimeoutError: + except TimeoutError as exc: self._logger.error("Service instantiation %s timed out", self.service_instance_name) - raise onap_test_exceptions.ServiceInstantiateException + raise onap_test_exceptions.ServiceInstantiateException from exc if service_instantiation.failed: self._logger.error("Service instantiation %s failed", self.service_instance_name) raise onap_test_exceptions.ServiceInstantiateException @@ -239,9 +240,9 @@ class YamlTemplateServiceMacroInstantiateStep(YamlTemplateBaseStep): service_deletion = self._service_instance.delete(a_la_carte=False) try: service_deletion.wait_for_finish(timeout=settings.ORCHESTRATION_REQUEST_TIMEOUT) - except TimeoutError: + except TimeoutError as exc: self._logger.error("Service deletion %s timed out", self._service_instance_name) - raise onap_test_exceptions.ServiceCleanupException + raise onap_test_exceptions.ServiceCleanupException from exc if service_deletion.finished: self._logger.info("Service %s deleted", self._service_instance_name) else: diff --git a/src/onaptests/steps/instantiate/vf_module_ala_carte.py b/src/onaptests/steps/instantiate/vf_module_ala_carte.py index 1682536..e148e4c 100644 --- a/src/onaptests/steps/instantiate/vf_module_ala_carte.py +++ b/src/onaptests/steps/instantiate/vf_module_ala_carte.py @@ -54,7 +54,7 @@ class YamlTemplateVfModuleAlaCarteInstantiateStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -135,9 +135,9 @@ class YamlTemplateVfModuleAlaCarteInstantiateStep(YamlTemplateBaseStep): if vf_module_instantiation.failed: self._logger.error("VfModule instantiation %s failed", vf_module.name) raise onap_test_exceptions.VfModuleInstantiateException - except TimeoutError: + except TimeoutError as exc: self._logger.error("VfModule instantiation %s timed out", vf_module.name) - raise onap_test_exceptions.VfModuleInstantiateException + raise onap_test_exceptions.VfModuleInstantiateException from exc @YamlTemplateBaseStep.store_state(cleanup=True) def cleanup(self) -> None: @@ -163,7 +163,7 @@ class YamlTemplateVfModuleAlaCarteInstantiateStep(YamlTemplateBaseStep): self._logger.error("VfModule deletion %s failed", vf_module.name) raise onap_test_exceptions.VfModuleCleanupException self._logger.info("VfModule %s deleted", vf_module.name) - except TimeoutError: + except TimeoutError as exc: self._logger.error("VfModule deletion %s timed out", vf_module.name) - raise onap_test_exceptions.VfModuleCleanupException + raise onap_test_exceptions.VfModuleCleanupException from exc super().cleanup() diff --git a/src/onaptests/steps/instantiate/vl_ala_carte.py b/src/onaptests/steps/instantiate/vl_ala_carte.py index 7c2f4d9..72dcb2f 100644 --- a/src/onaptests/steps/instantiate/vl_ala_carte.py +++ b/src/onaptests/steps/instantiate/vl_ala_carte.py @@ -48,7 +48,7 @@ class YamlTemplateVlAlaCarteInstantiateStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -89,7 +89,7 @@ class YamlTemplateVlAlaCarteInstantiateStep(YamlTemplateBaseStep): for net in self.yaml_template[self.service_name]["networks"]: if net["vl_name"] == network_name: if net['subnets'] is None: - print("No Subnet defined") + self._logger.warning("No Subnet defined") else: for subnet in net['subnets']: yield Subnet( @@ -128,9 +128,9 @@ class YamlTemplateVlAlaCarteInstantiateStep(YamlTemplateBaseStep): if net_instantiation.failed: self._logger.error("VL instantiation %s failed", net_instantiation.name) raise onap_test_exceptions.NetworkInstantiateException - except TimeoutError: + except TimeoutError as exc: self._logger.error("VL instantiation %s timed out", net_instantiation.name) - raise onap_test_exceptions.NetworkInstantiateException + raise onap_test_exceptions.NetworkInstantiateException from exc @YamlTemplateBaseStep.store_state(cleanup=True) def cleanup(self) -> None: @@ -148,7 +148,7 @@ class YamlTemplateVlAlaCarteInstantiateStep(YamlTemplateBaseStep): if net_deletion.failed: self._logger.error("VL deletion %s failed", net_instance.name) raise onap_test_exceptions.NetworkCleanupException - except TimeoutError: + except TimeoutError as exc: self._logger.error("VL deletion %s timed out", net_instance.name) - raise onap_test_exceptions.NetworkCleanupException + raise onap_test_exceptions.NetworkCleanupException from exc super().cleanup() diff --git a/src/onaptests/steps/instantiate/vnf_ala_carte.py b/src/onaptests/steps/instantiate/vnf_ala_carte.py index 2aa5e5e..e45f71e 100644 --- a/src/onaptests/steps/instantiate/vnf_ala_carte.py +++ b/src/onaptests/steps/instantiate/vnf_ala_carte.py @@ -46,7 +46,7 @@ class YamlTemplateVnfAlaCarteInstantiateStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -106,9 +106,9 @@ class YamlTemplateVnfAlaCarteInstantiateStep(YamlTemplateBaseStep): if vnf_instantiation.failed: self._logger.error("VNF instantiation %s failed", vnf.name) raise onap_test_exceptions.VnfInstantiateException - except TimeoutError: + except TimeoutError as exc: self._logger.error("VNF instantiation %s timed out", vnf.name) - raise onap_test_exceptions.VnfInstantiateException + raise onap_test_exceptions.VnfInstantiateException from exc @YamlTemplateBaseStep.store_state(cleanup=True) def cleanup(self) -> None: @@ -127,7 +127,7 @@ class YamlTemplateVnfAlaCarteInstantiateStep(YamlTemplateBaseStep): if vnf_deletion.failed: self._logger.error("VNF deletion %s failed", vnf_instance.name) raise onap_test_exceptions.VnfCleanupException - except TimeoutError: + except TimeoutError as exc: self._logger.error("VNF deletion %s timed out", vnf_instance.name) - raise onap_test_exceptions.VnfCleanupException + raise onap_test_exceptions.VnfCleanupException from exc super().cleanup() diff --git a/src/onaptests/steps/loop/clamp.py b/src/onaptests/steps/loop/clamp.py index dc00eb1..68a2dde 100644 --- a/src/onaptests/steps/loop/clamp.py +++ b/src/onaptests/steps/loop/clamp.py @@ -6,20 +6,19 @@ # # http://www.apache.org/licenses/LICENSE-2.0 """Clamp Scenario class.""" -from yaml import load, SafeLoader import random import string import time -from onapsdk.clamp.clamp_element import Clamp -from onapsdk.sdc.service import Service +from yaml import SafeLoader, load import onaptests.utils.exceptions as onap_test_exceptions +from onapsdk.clamp.clamp_element import Clamp from onapsdk.configuration import settings -from onaptests.steps.onboard.clamp import OnboardClampStep +from onapsdk.sdc.service import Service +from onaptests.steps.base import YamlTemplateBaseStep from onaptests.steps.loop.instantiate_loop import InstantiateLoop - -from ..base import YamlTemplateBaseStep +from onaptests.steps.onboard.clamp import OnboardClampStep class ClampStep(YamlTemplateBaseStep): @@ -56,7 +55,7 @@ class ClampStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -73,13 +72,14 @@ class ClampStep(YamlTemplateBaseStep): req_policies=30) # 30 required policy self._logger.info("Operational policy found.") if not exist: - raise ValueError("Couldn't load the policy %s", policy) + raise ValueError("Couldn't load the policy %s" % policy) # retrieve the service..based on service name service: Service = Service(self.service_name) if is_template: loop_template = Clamp.check_loop_template(service=service) self._logger.info("Loop template checked.") return loop_template + return None def instantiate_clamp(self, loop_template: str, loop_name: str, operational_policies: list): """Instantite a closed loopin CLAMP.""" @@ -116,9 +116,9 @@ class ClampStep(YamlTemplateBaseStep): service.name) break self._logger.info( - "Service Distribution for %s ongoing, Wait for 60 s", - service.name) - time.sleep(60) + "Service Distribution for %s ongoing, Wait for %d s", + service.name, settings.SERVICE_DISTRIBUTION_SLEEP_TIME) + time.sleep(settings.SERVICE_DISTRIBUTION_SLEEP_TIME) nb_try += 1 if distribution_completed is False: diff --git a/src/onaptests/steps/loop/instantiate_loop.py b/src/onaptests/steps/loop/instantiate_loop.py index fab70ad..3d34731 100644 --- a/src/onaptests/steps/loop/instantiate_loop.py +++ b/src/onaptests/steps/loop/instantiate_loop.py @@ -9,6 +9,7 @@ from onapsdk.configuration import settings import onaptests.utils.exceptions as onap_test_exceptions +# pylint: disable=protected-access class InstantiateLoop(): """class instantiating a closed loop in clamp.""" @@ -71,8 +72,8 @@ class InstantiateLoop(): loop = LoopInstance(template=self.template, name=self.loop_name, details={}) - details = loop.create() - if details: + loop.create() + if loop.details: self._logger.info("Loop instance %s successfully created !!", self.loop_name) else: self._logger.error("An error occured while creating the loop instance") diff --git a/src/onaptests/steps/onboard/cds.py b/src/onaptests/steps/onboard/cds.py index e489145..b9cc458 100644 --- a/src/onaptests/steps/onboard/cds.py +++ b/src/onaptests/steps/onboard/cds.py @@ -129,7 +129,7 @@ class CbaPublishStep(CDSBaseStep): def __init__(self) -> None: """Initialize CBA publish step.""" super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP) - """Let's skip enrichment if enriched CBA is already present""" + # Let's skip enrichment if enriched CBA is already present if Path.is_file(settings.CDS_CBA_UNENRICHED): self.add_step(CbaEnrichStep()) elif settings.EXPOSE_SERVICES_NODE_PORTS: diff --git a/src/onaptests/steps/onboard/clamp.py b/src/onaptests/steps/onboard/clamp.py index 22783c6..c2fe191 100644 --- a/src/onaptests/steps/onboard/clamp.py +++ b/src/onaptests/steps/onboard/clamp.py @@ -49,7 +49,7 @@ class OnboardClampStep(YamlTemplateBaseStep): """ if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -79,14 +79,14 @@ class OnboardClampStep(YamlTemplateBaseStep): # we add the artifact to the first VNF self._logger.info("Try to add blueprint to %s", vf.name) - payload_file = open(settings.CONFIGURATION_PATH + 'tca-microservice.yaml', 'rb') - data = payload_file.read() - self._logger.info("DCAE INVENTORY BLUEPRINT file retrieved") - service.add_artifact_to_vf(vnf_name=vf.name, - artifact_type="DCAE_INVENTORY_BLUEPRINT", - artifact_name="tca-microservice.yaml", - artifact=data) - payload_file.close() + with open(settings.CONFIGURATION_PATH + 'tca-microservice.yaml', + 'rb') as payload_file: + data = payload_file.read() + self._logger.info("DCAE INVENTORY BLUEPRINT file retrieved") + service.add_artifact_to_vf(vnf_name=vf.name, + artifact_type="DCAE_INVENTORY_BLUEPRINT", + artifact_name="tca-microservice.yaml", + artifact=data) service.checkin() service.onboard() self._logger.info("DCAE INVENTORY BLUEPRINT ADDED") diff --git a/src/onaptests/steps/onboard/cps.py b/src/onaptests/steps/onboard/cps.py index f5820c7..04471c2 100644 --- a/src/onaptests/steps/onboard/cps.py +++ b/src/onaptests/steps/onboard/cps.py @@ -254,6 +254,8 @@ class CheckPostgressDataBaseConnectionStep(CpsBaseStep): def __init__(self) -> None: """Initialize step.""" super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP) + self.login = None + self.password = None @property def description(self) -> str: @@ -261,6 +263,8 @@ class CheckPostgressDataBaseConnectionStep(CpsBaseStep): return "Establish connection with Postgress and execute the query" def get_database_credentials(self): + """Resolve CPS datbase credentials from k8s secret.""" + if settings.IN_CLUSTER: config.load_incluster_config() else: @@ -286,6 +290,8 @@ class CheckPostgressDataBaseConnectionStep(CpsBaseStep): raise EnvironmentPreparationException("Error accessing secret") from e def connect_to_postgress(self): + """Connect to CPS database and execute select query.""" + self.get_database_credentials() if self.login and self.password: db_params = { diff --git a/src/onaptests/steps/onboard/service.py b/src/onaptests/steps/onboard/service.py index bcf153f..d2391b3 100644 --- a/src/onaptests/steps/onboard/service.py +++ b/src/onaptests/steps/onboard/service.py @@ -147,9 +147,9 @@ class YamlTemplateServiceOnboardStep(YamlTemplateBaseStep): """ if settings.MODEL_YAML_TEMPLATE: return self.model_yaml_template - elif self.is_root: + if self.is_root: if not self._yaml_template: - with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template: + with open(settings.SERVICE_YAML_TEMPLATE, "r", encoding="utf-8") as yaml_template: self._yaml_template: dict = load(yaml_template, SafeLoader) return self._yaml_template return self.parent.yaml_template @@ -166,7 +166,8 @@ class YamlTemplateServiceOnboardStep(YamlTemplateBaseStep): """ if self.is_root: if not self._model_yaml_template: - with open(settings.MODEL_YAML_TEMPLATE, "r") as model_yaml_template: + with open(settings.MODEL_YAML_TEMPLATE, "r", + encoding="utf-8") as model_yaml_template: self._model_yaml_template: dict = load(model_yaml_template, SafeLoader) return self._model_yaml_template return self.parent.model_yaml_template diff --git a/src/onaptests/steps/onboard/vf.py b/src/onaptests/steps/onboard/vf.py index 3fc7443..0c68835 100644 --- a/src/onaptests/steps/onboard/vf.py +++ b/src/onaptests/steps/onboard/vf.py @@ -104,8 +104,7 @@ class YamlTemplateVfOnboardStep(YamlTemplateBaseStep): """ if settings.MODEL_YAML_TEMPLATE: return self.model_yaml_template - else: - return self.parent.yaml_template[self.parent.service_name] + return self.parent.yaml_template[self.parent.service_name] @property def model_yaml_template(self) -> dict: @@ -128,10 +127,10 @@ class YamlTemplateVfOnboardStep(YamlTemplateBaseStep): vsp: Vsp = Vsp(name=f"{vnf['vnf_name']}_VSP") vf: Vf = Vf(name=vnf['vnf_name'], vsp=vsp) if not vf.created(): - if all([x in vnf for x in ["vnf_artifact_type", - "vnf_artifact_name", - "vnf_artifact_label", - "vnf_artifact_file_path"]]): + if all(x in vnf for x in ["vnf_artifact_type", + "vnf_artifact_name", + "vnf_artifact_label", + "vnf_artifact_file_path"]): vf.create() artifact_file_path: Path = Path(vnf["vnf_artifact_file_path"]) if not artifact_file_path.exists(): diff --git a/src/onaptests/steps/onboard/vsp.py b/src/onaptests/steps/onboard/vsp.py index 06e0fa2..36e78df 100644 --- a/src/onaptests/steps/onboard/vsp.py +++ b/src/onaptests/steps/onboard/vsp.py @@ -48,10 +48,11 @@ class VspOnboardStep(BaseStep): """ super().execute() vendor: Vendor = Vendor(name=settings.VENDOR_NAME) - vsp: Vsp = Vsp(name=settings.VSP_NAME, - vendor=vendor, - package=open(settings.VSP_FILE_PATH, "rb")) - vsp.onboard() + with open(settings.VSP_FILE_PATH, "rb") as vsp_file: + vsp: Vsp = Vsp(name=settings.VSP_NAME, + vendor=vendor, + package=vsp_file) + vsp.onboard() @BaseStep.store_state(cleanup=True) def cleanup(self): @@ -103,8 +104,7 @@ class YamlTemplateVspOnboardStep(YamlTemplateBaseStep): """ if settings.MODEL_YAML_TEMPLATE: return self.model_yaml_template - else: - return self.parent.yaml_template + return self.parent.yaml_template @property def model_yaml_template(self) -> dict: diff --git a/src/onaptests/steps/reports_collection.py b/src/onaptests/steps/reports_collection.py index 99a6ef2..dfdc09a 100644 --- a/src/onaptests/steps/reports_collection.py +++ b/src/onaptests/steps/reports_collection.py @@ -28,9 +28,10 @@ class Report: class ReportsCollection: """Collection to store steps execution statuses.""" - def __init__(self) -> None: + def __init__(self, components: list) -> None: """Initialize collection.""" self._collection: list = [] + self._components = components def put(self, item: Report) -> None: """Put execution status dictionary. @@ -65,16 +66,18 @@ class ReportsCollection: step_report.step_execution_status == ReportStepStatus.FAIL)) def generate_report(self) -> None: + """Generate report files after execution of the test.""" + usecase = settings.SERVICE_NAME try: details = settings.SERVICE_DETAILS except (KeyError, AttributeError, SettingsError): details = "" - try: - components = settings.SERVICE_COMPONENTS - except (KeyError, AttributeError, SettingsError): - components = "" + components = "" + for component in self._components: + components = f"{component}, {components}" + components = components.rstrip(", ") jinja_env = Environment( autoescape=select_autoescape(['html']), @@ -104,5 +107,5 @@ class ReportsCollection: ] } with (Path(settings.REPORTING_FILE_DIRECTORY).joinpath( - settings.JSON_REPORTING_FILE_NAME)).open('w') as file: + settings.JSON_REPORTING_FILE_NAME)).open('w', encoding="utf-8") as file: json.dump(report_dict, file, indent=4) diff --git a/src/onaptests/steps/simulator/cds_mockserver.py b/src/onaptests/steps/simulator/cds_mockserver.py index 75d0767..8773248 100644 --- a/src/onaptests/steps/simulator/cds_mockserver.py +++ b/src/onaptests/steps/simulator/cds_mockserver.py @@ -54,9 +54,10 @@ class CdsMockserverCnfConfigureStep(BaseStep): "httpResponse": { "body": expectation["response"] } - } + }, + timeout=settings.DEFAULT_REQUEST_TIMEOUT ) response.raise_for_status() except (requests.ConnectionError, requests.HTTPError) as http_error: self._logger.debug(f"Can't register cds-mockserver expectation: {str(http_error)}") - raise OnapTestException("CDS mockserver not configured") + raise OnapTestException("CDS mockserver not configured") from http_error 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 5adba2c..0ecc2ca 100644 --- a/src/onaptests/steps/simulator/pnf_simulator_cnf/pnf_register.py +++ b/src/onaptests/steps/simulator/pnf_simulator_cnf/pnf_register.py @@ -5,13 +5,14 @@ import time from typing import Tuple import requests -from kubernetes import client, config, watch -from onapsdk.configuration import settings import urllib3 +from kubernetes import client, config, watch +from onapsdk.configuration import settings from onaptests.steps.base import BaseStep from onaptests.steps.instantiate.msb_k8s import CreateInstanceStep -from onaptests.utils.exceptions import EnvironmentPreparationException, OnapTestException +from onaptests.utils.exceptions import (EnvironmentPreparationException, + OnapTestException) class PnfSimulatorCnfRegisterStep(BaseStep): @@ -64,9 +65,9 @@ class PnfSimulatorCnfRegisterStep(BaseStep): if event["object"].status.phase == "Running": return True return status - except urllib3.exceptions.HTTPError: + except urllib3.exceptions.HTTPError as exc: self._logger.error("Can't connect with k8s") - raise OnapTestException + raise OnapTestException from exc def get_ves_protocol_ip_and_port(self) -> Tuple[str, str, str]: """Static method to get VES protocol, ip address and port. @@ -92,9 +93,9 @@ class PnfSimulatorCnfRegisterStep(BaseStep): proto = "https" return proto, service.spec.cluster_ip, service.spec.ports[0].port raise EnvironmentPreparationException("Couldn't get VES protocol, ip and port") - except Exception: + except Exception as exc: self._logger.exception("Can't connect with k8s") - raise OnapTestException + raise OnapTestException from exc @BaseStep.store_state def execute(self) -> None: @@ -131,7 +132,8 @@ class PnfSimulatorCnfRegisterStep(BaseStep): } } } - } + }, + timeout=settings.DEFAULT_REQUEST_TIMEOUT ) response.raise_for_status() registered_successfully = True diff --git a/src/onaptests/steps/wrapper/helm_charts.py b/src/onaptests/steps/wrapper/helm_charts.py index f745150..874e1ff 100644 --- a/src/onaptests/steps/wrapper/helm_charts.py +++ b/src/onaptests/steps/wrapper/helm_charts.py @@ -29,7 +29,7 @@ class HelmChartStep(BaseStep): chart_info_path = get_local_dir() / chart_info_file try: - with open(chart_info_path, 'r') as stream: + with open(chart_info_path, 'r', encoding="utf-8") as stream: chart_info = yaml.safe_load(stream) except IOError as err: msg = f"{chart_info_file} not found." @@ -80,6 +80,7 @@ class HelmChartStep(BaseStep): msg = "Error during helm release installation." raise EnvironmentPreparationException(msg) from err + @BaseStep.store_state(cleanup=True) def cleanup(self) -> None: """Uninstall helm release.""" try: diff --git a/src/onaptests/steps/wrapper/start.py b/src/onaptests/steps/wrapper/start.py index 8b950da..e08f108 100644 --- a/src/onaptests/steps/wrapper/start.py +++ b/src/onaptests/steps/wrapper/start.py @@ -1,6 +1,7 @@ """Start simulators via simulators' API.""" from typing import Union, Optional, Dict import requests +from onapsdk.configuration import settings from onaptests.steps.base import BaseStep from onaptests.utils.exceptions import TestConfigurationException @@ -62,4 +63,5 @@ class SimulatorStartStep(BaseStep): def execute(self) -> None: """Send a start command to the simulator application.""" super().execute() - requests.request(self.method.upper(), self.url, **self.data) + requests.request(self.method.upper(), self.url, **self.data, + timeout=settings.DEFAULT_REQUEST_TIMEOUT) |