Use raw configuration endpoint
[integration/csit.git] / tests / dcaegen2-collectors-hv-ves / testcases / libraries / XnfSimulatorLibrary.py
1 import HttpRequests
2 import os
3 import docker
4 from robot.api import logger
5 from time import sleep
6
7 XNF_SIMULATOR_NAME = "xNF Simulator"
8 HV_VES_COLLECTOR_NAMESPACE="onap"
9 HV_VES_GROUP_ID="org.onap.dcaegen2.collectors.hv-ves"
10 SIMULATOR_IMAGE_NAME = HV_VES_COLLECTOR_NAMESPACE + "/" + HV_VES_GROUP_ID + ".hv-collector-xnf-simulator"
11 SIMULATOR_IMAGE_FULL_NAME = os.getenv("DOCKER_REGISTRY_PREFIX") + SIMULATOR_IMAGE_NAME + ":latest"
12 WORKSPACE_ENV = os.getenv("WORKSPACE")
13 certificates_dir_path = WORKSPACE_ENV + "/plans/dcaegen2-collectors-hv-ves/testsuites/ssl/"
14 collector_certs_lookup_dir = "/etc/ves-hv/"
15 ONE_SECOND_IN_NANOS = 10 ** 9
16
17
18 class XnfSimulatorLibrary:
19
20     def start_xnf_simulators(self, list_of_ports,
21                              should_use_valid_certs=True,
22                              should_disable_ssl=False,
23                              should_connect_to_unencrypted_hv_ves=False):
24         logger.info("Creating " + str(len(list_of_ports)) + " xNF Simulator containers")
25         dockerClient = docker.from_env()
26
27         self.pullImageIfAbsent(dockerClient)
28         logger.info("Using image: " + SIMULATOR_IMAGE_FULL_NAME)
29
30         simulators_addresses = self.create_containers(dockerClient,
31                                                       list_of_ports,
32                                                       should_use_valid_certs,
33                                                       should_disable_ssl,
34                                                       should_connect_to_unencrypted_hv_ves)
35
36         self.assert_containers_startup_was_successful(dockerClient)
37         dockerClient.close()
38         return simulators_addresses
39
40     def pullImageIfAbsent(self, dockerClient):
41         try:
42             dockerClient.images.get(SIMULATOR_IMAGE_FULL_NAME)
43         except:
44             logger.console("Image " + SIMULATOR_IMAGE_FULL_NAME + " will be pulled from repository. "
45                                                                   "This can take a while.")
46             dockerClient.images.pull(SIMULATOR_IMAGE_FULL_NAME)
47
48     def create_containers(self,
49                           dockerClient,
50                           list_of_ports,
51                           should_use_valid_certs,
52                           should_disable_ssl,
53                           should_connect_to_unencrypted_hv_ves):
54         simulators_addresses = []
55         for port in list_of_ports:
56             xnf = XnfSimulator(port, should_use_valid_certs, should_disable_ssl, should_connect_to_unencrypted_hv_ves)
57             container = self.run_simulator(dockerClient, xnf)
58             logger.info("Started container: " + container.name + "  " + container.id)
59             simulators_addresses.append(container.name + ":" + xnf.port)
60         return simulators_addresses
61
62     def run_simulator(self, dockerClient, xnf):
63         xNF_startup_command = xnf.get_startup_command()
64         xNF_healthcheck_command = xnf.get_healthcheck_command()
65         port = xnf.port
66         logger.info("Startup command: " + str(xNF_startup_command))
67         logger.info("Healthcheck command: " + str(xNF_healthcheck_command))
68         return dockerClient.containers.run(SIMULATOR_IMAGE_FULL_NAME,
69                                            command=xNF_startup_command,
70                                            healthcheck=xNF_healthcheck_command,
71                                            detach=True,
72                                            network="ves-hv-default",
73                                            ports={port + "/tcp": port},
74                                            volumes=self.container_volumes(),
75                                            name=xnf.container_name_prefix + port)
76
77     def container_volumes(self):
78         return {certificates_dir_path: {"bind": collector_certs_lookup_dir, "mode": 'rw'}}
79
80     def assert_containers_startup_was_successful(self, dockerClient):
81         checks_amount = 6
82         check_interval_in_seconds = 5
83         for _ in range(checks_amount):
84             sleep(check_interval_in_seconds)
85             all_containers_healthy = True
86             for container in self.get_simulators_list(dockerClient):
87                 all_containers_healthy = all_containers_healthy and self.is_container_healthy(container)
88             if (all_containers_healthy):
89                 return
90         raise ContainerException("One of xNF simulators containers did not pass the healthcheck.")
91
92     def is_container_healthy(self, container):
93         container_health = container.attrs['State']['Health']['Status']
94         return container_health == 'healthy' and container.status == 'running'
95
96     def stop_and_remove_all_xnf_simulators(self, suite_name):
97         dockerClient = docker.from_env()
98         for container in self.get_simulators_list(dockerClient):
99             logger.info("Stopping and removing container: " + container.id)
100             log_filename = WORKSPACE_ENV + "/archives/containers_logs/" + \
101                            suite_name.split(".")[-1] + "_" + container.name + ".log"
102             file = open(log_filename, "w+")
103             file.write(container.logs())
104             file.close()
105             container.stop()
106             container.remove()
107         dockerClient.close()
108
109     def get_simulators_list(self, dockerClient):
110         return dockerClient.containers.list(filters={"ancestor": SIMULATOR_IMAGE_FULL_NAME}, all=True)
111
112     def send_messages(self, simulator_url, message_filepath):
113         logger.info("Reading message to simulator from: " + message_filepath)
114
115         file = open(message_filepath, "rb")
116         data = file.read()
117         file.close()
118
119         logger.info("POST at: " + simulator_url)
120         resp = HttpRequests.session_without_env().post(simulator_url, data=data, timeout=5)
121         HttpRequests.checkStatusCode(resp.status_code, XNF_SIMULATOR_NAME)
122
123
124 class XnfSimulator:
125     container_name_prefix = "ves-hv-collector-xnf-simulator"
126
127     def __init__(self,
128                  port,
129                  should_use_valid_certs,
130                  should_disable_ssl,
131                  should_connect_to_unencrypted_hv_ves):
132         self.port = port
133         cert_name_prefix = "" if should_use_valid_certs else "untrusted"
134         certificates_path_with_file_prefix = collector_certs_lookup_dir + cert_name_prefix
135         self.key_store_path = certificates_path_with_file_prefix + "client.p12"
136         self.trust_store_path = certificates_path_with_file_prefix + "trust.p12"
137         self.sec_store_passwd = "onaponap"
138         self.disable_ssl = should_disable_ssl
139         self.hv_collector_host = "unencrypted-ves-hv-collector" \
140             if should_connect_to_unencrypted_hv_ves else "ves-hv-collector"
141
142     def get_startup_command(self):
143         startup_command = ["--listen-port", self.port,
144                            "--ves-host", self.hv_collector_host,
145                            "--ves-port", "6061",
146                            "--key-store", self.key_store_path,
147                            "--trust-store", self.trust_store_path,
148                            "--key-store-password", self.sec_store_passwd,
149                            "--trust-store-password", self.sec_store_passwd
150                            ]
151         if self.disable_ssl:
152             startup_command.append("--ssl-disable")
153         return startup_command
154
155     def get_healthcheck_command(self):
156         return {
157             "interval": 5 * ONE_SECOND_IN_NANOS,
158             "timeout": 3 * ONE_SECOND_IN_NANOS,
159             "retries": 1,
160             "test": ["CMD", "curl", "--request", "GET",
161                      "--fail", "--silent", "--show-error",
162                      "localhost:" + self.port + "/healthcheck"]
163         }
164
165
166 class ContainerException(Exception):
167     def __init__(self, message):
168         super(ContainerException, self).__init__(message)