blob: 46d69bb84ccbdb562c7cc221b91d6b56befad6a2 [file] [log] [blame]
Tomasz Wrobel75573b02020-03-05 16:43:50 +01001import docker
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +01002import os
3import shutil
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +01004import re
Joanna Jeremicz9c14f192020-03-19 14:51:51 +01005from EnvsReader import EnvsReader
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +01006from docker.types import Mount
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +01007
8ARCHIVES_PATH = os.getenv("WORKSPACE") + "/archives/"
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +01009
10ERROR_API_REGEX = 'Error on API response.*[0-9]{3}'
11RESPONSE_CODE_REGEX = '[0-9]{3}'
Tomasz Wrobel75573b02020-03-05 16:43:50 +010012
Aleksandra Maciaga13b7d052020-03-26 17:28:47 +010013
Tomasz Wrobel75573b02020-03-05 16:43:50 +010014class CertClientManager:
15
Aleksandra Maciaga13b7d052020-03-26 17:28:47 +010016 def __init__(self, mount_path, truststore_path):
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010017 self.mount_path = mount_path
Aleksandra Maciaga13b7d052020-03-26 17:28:47 +010018 self.truststore_path = truststore_path
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010019
Tomasz Wrobel75573b02020-03-05 16:43:50 +010020 def run_client_container(self, client_image, container_name, path_to_env, request_url, network):
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010021 self.create_mount_dir()
Tomasz Wrobel75573b02020-03-05 16:43:50 +010022 client = docker.from_env()
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010023 environment = EnvsReader().read_env_list_from_file(path_to_env)
Tomasz Wrobel75573b02020-03-05 16:43:50 +010024 environment.append("REQUEST_URL=" + request_url)
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010025 container = client.containers.run(
26 image=client_image,
27 name=container_name,
28 environment=environment,
29 network=network,
Aleksandra Maciaga13b7d052020-03-26 17:28:47 +010030 user='root', # Run container as root to avoid permission issues with volume mount access
31 mounts=[Mount(target='/var/certs', source=self.mount_path, type='bind'),
Pawelb7a057e2020-08-07 14:53:48 +020032 Mount(target='/etc/onap/oom-platform-cert-service/certservice/certs/', source=self.truststore_path, type='bind')],
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010033 detach=True
34 )
Tomasz Wrobel75573b02020-03-05 16:43:50 +010035 exitcode = container.wait()
36 return exitcode
37
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010038 def remove_client_container_and_save_logs(self, container_name, log_file_name):
39 client = docker.from_env()
40 container = client.containers.get(container_name)
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010041 text_file = open(ARCHIVES_PATH + "client_container_" + log_file_name + ".log", "w")
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010042 text_file.write(container.logs())
43 text_file.close()
44 container.remove()
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010045 self.remove_mount_dir()
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010046
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010047 def create_mount_dir(self):
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010048 if not os.path.exists(self.mount_path):
49 os.makedirs(self.mount_path)
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010050
51 def remove_mount_dir(self):
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010052 shutil.rmtree(self.mount_path)
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010053
54 def can_find_api_response_in_logs(self, container_name):
55 logs = self.get_container_logs(container_name)
56 api_logs = re.findall(ERROR_API_REGEX, logs)
57 if api_logs:
58 return True
59 else:
60 return False
61
62 def get_api_response_from_logs(self, container_name):
63 logs = self.get_container_logs(container_name)
64 error_api_message = re.findall(ERROR_API_REGEX, logs)
65 code = re.findall(RESPONSE_CODE_REGEX, error_api_message[0])
66 return code[0]
67
68 def get_container_logs(self, container_name):
69 client = docker.from_env()
70 container = client.containers.get(container_name)
71 logs = container.logs()
72 return logs