diff options
-rw-r--r-- | src/onaptests/steps/cloud/check_status.py | 182 |
1 files changed, 84 insertions, 98 deletions
diff --git a/src/onaptests/steps/cloud/check_status.py b/src/onaptests/steps/cloud/check_status.py index 80205b5..0bfce35 100644 --- a/src/onaptests/steps/cloud/check_status.py +++ b/src/onaptests/steps/cloud/check_status.py @@ -163,8 +163,7 @@ class CheckNamespaceStatusStep(BaseStep): """ super().execute() - if settings.STORE_ARTIFACTS: - os.makedirs(self.res_dir, exist_ok=True) + os.makedirs(self.res_dir, exist_ok=True) self.__logger.debug("start test") try: self.k8s_pods = self.core.list_namespaced_pod(NAMESPACE).items @@ -243,13 +242,12 @@ class CheckNamespaceStatusStep(BaseStep): self.parse_secrets() self.parse_ingresses() self.parse_versions() - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('index.html.j2').stream( - ns=self, - delta=delta).dump('{}/index.html'.format(self.res_dir)) - self.jinja_env.get_template('raw_output.txt.j2').stream( - ns=self, namespace=NAMESPACE).dump('{}/onap-k8s.log'.format( - self.res_dir)) + self.jinja_env.get_template('index.html.j2').stream( + ns=self, + delta=delta).dump('{}/index.html'.format(self.res_dir)) + self.jinja_env.get_template('raw_output.txt.j2').stream( + ns=self, namespace=NAMESPACE).dump('{}/onap-k8s.log'.format( + self.res_dir)) if len(self.jobs) > 0: self.details['jobs'] = { @@ -323,13 +321,12 @@ class CheckNamespaceStatusStep(BaseStep): pod.events = self.core.list_namespaced_event( NAMESPACE, field_selector="involvedObject.name={}".format(pod.name)).items - if settings.STORE_ARTIFACTS: - 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 WAIVER_LIST): - self.__logger.warn("Waiver pattern found in pod, exclude %s", pod.name) - else: - self.pods.append(pod) + 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 WAIVER_LIST): + self.__logger.warn("Waiver pattern found in pod, exclude %s", pod.name) + else: + self.pods.append(pod) def parse_container(self, pod, k8s_container, init=False): """Get the logs of a container.""" @@ -352,74 +349,70 @@ class CheckNamespaceStatusStep(BaseStep): else: if container.restart_count > pod.restart_count: pod.restart_count = container.restart_count - - try: - log_files = {} - logs = "" + if settings.STORE_ARTIFACTS: try: - logs = self.core.read_namespaced_pod_log( - pod.name, - NAMESPACE, - container=container.name, - limit_bytes=MAX_LOG_BYTES, - ) - 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, - ) - if settings.STORE_ARTIFACTS: + log_files = {} + logs = "" + try: + logs = self.core.read_namespaced_pod_log( + pod.name, + NAMESPACE, + container=container.name, + limit_bytes=MAX_LOG_BYTES, + ) + 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, + ) with open( "{}/pod-{}-{}.log".format(self.res_dir, pod.name, container.name), 'w') as log_result: log_result.write(logs) - if (not container.ready) and container.restart_count > 0: - old_logs = self.core.read_namespaced_pod_log( - pod.name, - NAMESPACE, - container=container.name, - previous=True) - if settings.STORE_ARTIFACTS: + if (not container.ready) and container.restart_count > 0: + old_logs = self.core.read_namespaced_pod_log( + pod.name, + NAMESPACE, + container=container.name, + previous=True) with open( "{}/pod-{}-{}.old.log".format(self.res_dir, pod.name, container.name), 'w') as log_result: log_result.write(old_logs) - if (container.name in FULL_LOGS_CONTAINERS): - logs = self.core.read_namespaced_pod_log( - pod.name, NAMESPACE, container=container.name) - if settings.STORE_ARTIFACTS: + if (container.name in FULL_LOGS_CONTAINERS): + logs = self.core.read_namespaced_pod_log( + pod.name, NAMESPACE, container=container.name) with open( "{}/pod-{}-{}.log".format(self.res_dir, pod.name, container.name), 'w') as log_result: log_result.write(logs) - if (container.name in SPECIFIC_LOGS_CONTAINERS): - for log_file in SPECIFIC_LOGS_CONTAINERS[container.name]: - exec_command = ['/bin/sh', '-c', "cat {}".format(log_file)] - log_files[log_file] = stream( - self.core.connect_get_namespaced_pod_exec, - pod.name, - NAMESPACE, - container=container.name, - command=exec_command, - stderr=True, - stdin=False, - stdout=True, - tty=False) - log_file_slug = log_file.split('.')[0].split('/')[-1] - with open( - "{}/pod-{}-{}-{}.log".format( - self.res_dir, pod.name, - container.name, log_file_slug), - 'w') 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", - prefix, container.name, pod.name, exc.reason) - if settings.STORE_ARTIFACTS: + if (container.name in SPECIFIC_LOGS_CONTAINERS): + for log_file in SPECIFIC_LOGS_CONTAINERS[container.name]: + exec_command = ['/bin/sh', '-c', "cat {}".format(log_file)] + log_files[log_file] = stream( + self.core.connect_get_namespaced_pod_exec, + pod.name, + NAMESPACE, + container=container.name, + command=exec_command, + stderr=True, + stdin=False, + stdout=True, + tty=False) + log_file_slug = log_file.split('.')[0].split('/')[-1] + with open( + "{}/pod-{}-{}-{}.log".format( + self.res_dir, pod.name, + container.name, log_file_slug), + 'w') 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", + prefix, container.name, pod.name, exc.reason) self.jinja_env.get_template('container_log.html.j2').stream( container=container, pod_name=pod.name, @@ -445,11 +438,10 @@ class CheckNamespaceStatusStep(BaseStep): (service.pods, service.failed_pods) = self._find_child_pods(k8s.spec.selector) - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('service.html.j2').stream( - service=service).dump('{}/service-{}.html'.format( - self.res_dir, service.name)) - self.services.append(service) + self.jinja_env.get_template('service.html.j2').stream( + service=service).dump('{}/service-{}.html'.format( + self.res_dir, service.name)) + self.services.append(service) def parse_jobs(self): """Parse the jobs. @@ -472,9 +464,8 @@ class CheckNamespaceStatusStep(BaseStep): NAMESPACE, field_selector=field_selector).items - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('job.html.j2').stream(job=job).dump( - '{}/job-{}.html'.format(self.res_dir, job.name)) + self.jinja_env.get_template('job.html.j2').stream(job=job).dump( + '{}/job-{}.html'.format(self.res_dir, job.name)) # timemout job if not k8s.status.completion_time: @@ -507,10 +498,9 @@ class CheckNamespaceStatusStep(BaseStep): NAMESPACE, field_selector=field_selector).items - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('deployment.html.j2').stream( - deployment=deployment).dump('{}/deployment-{}.html'.format( - self.res_dir, deployment.name)) + self.jinja_env.get_template('deployment.html.j2').stream( + deployment=deployment).dump('{}/deployment-{}.html'.format( + self.res_dir, deployment.name)) if k8s.status.unavailable_replicas: self.__logger.warning("a Deployment is in error: {}".format(deployment.name)) @@ -537,10 +527,9 @@ class CheckNamespaceStatusStep(BaseStep): NAMESPACE, field_selector=field_selector).items - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('replicaset.html.j2').stream( - replicaset=replicaset).dump('{}/replicaset-{}.html'.format( - self.res_dir, replicaset.name)) + self.jinja_env.get_template('replicaset.html.j2').stream( + replicaset=replicaset).dump('{}/replicaset-{}.html'.format( + self.res_dir, replicaset.name)) if (not k8s.status.ready_replicas or (k8s.status.ready_replicas < k8s.status.replicas)): @@ -568,10 +557,9 @@ class CheckNamespaceStatusStep(BaseStep): NAMESPACE, field_selector=field_selector).items - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('statefulset.html.j2').stream( - statefulset=statefulset).dump('{}/statefulset-{}.html'.format( - self.res_dir, statefulset.name)) + self.jinja_env.get_template('statefulset.html.j2').stream( + statefulset=statefulset).dump('{}/statefulset-{}.html'.format( + self.res_dir, statefulset.name)) if ((not k8s.status.ready_replicas) or (k8s.status.ready_replicas < k8s.status.replicas)): @@ -598,10 +586,9 @@ class CheckNamespaceStatusStep(BaseStep): NAMESPACE, field_selector=field_selector).items - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('daemonset.html.j2').stream( - daemonset=daemonset).dump('{}/daemonset-{}.html'.format( - self.res_dir, daemonset.name)) + self.jinja_env.get_template('daemonset.html.j2').stream( + daemonset=daemonset).dump('{}/daemonset-{}.html'.format( + self.res_dir, daemonset.name)) if (k8s.status.number_ready < k8s.status.desired_number_scheduled): self.__logger.warning("a DaemonSet is in error: {}".format(daemonset.name)) @@ -732,13 +719,12 @@ class CheckNamespaceStatusStep(BaseStep): 'number_components': 1 } - if settings.STORE_ARTIFACTS: - self.jinja_env.get_template('version.html.j2').stream( - pod_versions=pod_versions).dump('{}/versions.html'.format( - self.res_dir)) - self.jinja_env.get_template('container_versions.html.j2').stream( - containers=containers).dump('{}/container_versions.html'.format( - self.res_dir)) + self.jinja_env.get_template('version.html.j2').stream( + pod_versions=pod_versions).dump('{}/versions.html'.format( + self.res_dir)) + self.jinja_env.get_template('container_versions.html.j2').stream( + 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: json.dump(pod_versions, write_file) |