Verification of fields in trust/key store
[integration/csit.git] / tests / aaf / certservice / libraries / CertClientManager.py
1 import docker
2 import os
3 import shutil
4 import re
5 from EnvsReader import EnvsReader
6 from docker.types import Mount
7
8 ARCHIVES_PATH = os.getenv("WORKSPACE") + "/archives/"
9
10 ERROR_API_REGEX = 'Error on API response.*[0-9]{3}'
11 RESPONSE_CODE_REGEX = '[0-9]{3}'
12
13 class CertClientManager:
14
15     def __init__(self, mount_path):
16         self.mount_path = mount_path
17
18     def run_client_container(self, client_image, container_name, path_to_env, request_url, network):
19         self.create_mount_dir()
20         client = docker.from_env()
21         environment = EnvsReader().read_env_list_from_file(path_to_env)
22         environment.append("REQUEST_URL=" + request_url)
23         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
29             mounts=[Mount(target='/var/certs', source=self.mount_path, type='bind')],
30             detach=True
31         )
32         exitcode = container.wait()
33         return exitcode
34
35     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)
38         text_file = open(ARCHIVES_PATH + "client_container_" + log_file_name + ".log", "w")
39         text_file.write(container.logs())
40         text_file.close()
41         container.remove()
42         self.remove_mount_dir()
43
44     def create_mount_dir(self):
45         if not os.path.exists(self.mount_path):
46             os.makedirs(self.mount_path)
47
48     def remove_mount_dir(self):
49         shutil.rmtree(self.mount_path)
50
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