Add testcase for PMMapper (Support 28.532 file naming convention)
[integration/csit.git] / tests / oom-platform-cert-service / 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
14 class CertClientManager:
15
16     def __init__(self, mount_path, truststore_path):
17         self.mount_path = mount_path
18         self.truststore_path = truststore_path
19
20     def run_client_container(self, client_image, container_name, path_to_env, request_url, network):
21         self.create_mount_dir()
22         client = docker.from_env()
23         environment = EnvsReader().read_env_list_from_file(path_to_env)
24         environment.append("REQUEST_URL=" + request_url)
25         container = client.containers.run(
26             image=client_image,
27             name=container_name,
28             environment=environment,
29             network=network,
30             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'),
32                     Mount(target='/etc/onap/oom-platform-cert-service/certservice/certs/', source=self.truststore_path, type='bind')],
33             detach=True
34         )
35         exitcode = container.wait()
36         return exitcode
37
38     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)
41         text_file = open(ARCHIVES_PATH + "client_container_" + log_file_name + ".log", "w")
42         text_file.write(container.logs())
43         text_file.close()
44         container.remove()
45         self.remove_mount_dir()
46
47     def create_mount_dir(self):
48         if not os.path.exists(self.mount_path):
49             os.makedirs(self.mount_path)
50
51     def remove_mount_dir(self):
52         shutil.rmtree(self.mount_path)
53
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