blob: a959c9eea6b7ea77f3965beb22aba6bce81210da [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
Tomasz Wrobel75573b02020-03-05 16:43:50 +010013class CertClientManager:
14
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010015 def __init__(self, mount_path):
16 self.mount_path = mount_path
17
Tomasz Wrobel75573b02020-03-05 16:43:50 +010018 def run_client_container(self, client_image, container_name, path_to_env, request_url, network):
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010019 self.create_mount_dir()
Tomasz Wrobel75573b02020-03-05 16:43:50 +010020 client = docker.from_env()
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010021 environment = EnvsReader().read_env_list_from_file(path_to_env)
Tomasz Wrobel75573b02020-03-05 16:43:50 +010022 environment.append("REQUEST_URL=" + request_url)
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010023 container = client.containers.run(
24 image=client_image,
25 name=container_name,
26 environment=environment,
27 network=network,
28 user='root', #Run container as root to avoid permission issues with volume mount access
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010029 mounts=[Mount(target='/var/certs', source=self.mount_path, type='bind')],
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010030 detach=True
31 )
Tomasz Wrobel75573b02020-03-05 16:43:50 +010032 exitcode = container.wait()
33 return exitcode
34
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010035 def remove_client_container_and_save_logs(self, container_name, log_file_name):
36 client = docker.from_env()
37 container = client.containers.get(container_name)
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010038 text_file = open(ARCHIVES_PATH + "client_container_" + log_file_name + ".log", "w")
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010039 text_file.write(container.logs())
40 text_file.close()
41 container.remove()
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010042 self.remove_mount_dir()
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010043
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010044 def create_mount_dir(self):
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010045 if not os.path.exists(self.mount_path):
46 os.makedirs(self.mount_path)
Remigiusz Janeczeked0f3be2020-03-19 14:20:22 +010047
48 def remove_mount_dir(self):
Joanna Jeremicz9c14f192020-03-19 14:51:51 +010049 shutil.rmtree(self.mount_path)
Tomasz Wrobel6ed06d52020-03-11 15:31:10 +010050
51 def can_find_api_response_in_logs(self, container_name):
52 logs = self.get_container_logs(container_name)
53 api_logs = re.findall(ERROR_API_REGEX, logs)
54 if api_logs:
55 return True
56 else:
57 return False
58
59 def get_api_response_from_logs(self, container_name):
60 logs = self.get_container_logs(container_name)
61 error_api_message = re.findall(ERROR_API_REGEX, logs)
62 code = re.findall(RESPONSE_CODE_REGEX, error_api_message[0])
63 return code[0]
64
65 def get_container_logs(self, container_name):
66 client = docker.from_env()
67 container = client.containers.get(container_name)
68 logs = container.logs()
69 return logs