1 # ============LICENSE_START=======================================================
2 # csit-dcaegen2-collectors-hv-ves
3 # ================================================================================
4 # Copyright (C) 2018-2019 NOKIA
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
22 from robot.api import logger
23 from time import sleep
25 XNF_SIMULATOR_NAME = "xNF Simulator"
26 HV_VES_COLLECTOR_NAMESPACE="onap"
27 HV_VES_GROUP_ID="org.onap.dcaegen2.collectors.hv-ves"
28 SIMULATOR_IMAGE_NAME = HV_VES_COLLECTOR_NAMESPACE + "/" + HV_VES_GROUP_ID + ".hv-collector-xnf-simulator"
29 HV_VES_VERSION="1.1-SNAPSHOT"
30 SIMULATOR_IMAGE_FULL_NAME = os.getenv("DOCKER_REGISTRY_PREFIX") + SIMULATOR_IMAGE_NAME + ":" + HV_VES_VERSION
31 WORKSPACE_ENV = os.getenv("WORKSPACE")
32 certificates_dir_path = WORKSPACE_ENV + "/plans/dcaegen2-collectors-hv-ves/testsuites/collector/ssl/"
33 collector_certs_lookup_dir = "/etc/ves-hv/"
34 ONE_SECOND_IN_NANOS = 10 ** 9
37 class XnfSimulatorLibrary:
39 def start_xnf_simulators(self, list_of_ports,
40 should_use_valid_certs=True,
41 should_disable_ssl=False,
42 should_connect_to_unencrypted_hv_ves=False):
43 logger.info("Creating " + str(len(list_of_ports)) + " xNF Simulator containers")
44 dockerClient = docker.from_env()
46 self.pullImageIfAbsent(dockerClient)
47 logger.info("Using image: " + SIMULATOR_IMAGE_FULL_NAME)
49 simulators_addresses = self.create_containers(dockerClient,
51 should_use_valid_certs,
53 should_connect_to_unencrypted_hv_ves)
55 self.assert_containers_startup_was_successful(dockerClient)
57 return simulators_addresses
59 def pullImageIfAbsent(self, dockerClient):
61 dockerClient.images.get(SIMULATOR_IMAGE_FULL_NAME)
63 logger.console("Image " + SIMULATOR_IMAGE_FULL_NAME + " will be pulled from repository. "
64 "This can take a while.")
65 dockerClient.images.pull(SIMULATOR_IMAGE_FULL_NAME)
67 def create_containers(self,
70 should_use_valid_certs,
72 should_connect_to_unencrypted_hv_ves):
73 simulators_addresses = []
74 for port in list_of_ports:
75 xnf = XnfSimulator(port, should_use_valid_certs, should_disable_ssl, should_connect_to_unencrypted_hv_ves)
76 container = self.run_simulator(dockerClient, xnf)
77 logger.info("Started container: " + container.name + " " + container.id)
78 simulators_addresses.append(container.name + ":" + xnf.port)
79 return simulators_addresses
81 def run_simulator(self, dockerClient, xnf):
82 xNF_startup_command = xnf.get_startup_command()
83 xNF_healthcheck_command = xnf.get_healthcheck_command()
85 logger.info("Startup command: " + str(xNF_startup_command))
86 logger.info("Healthcheck command: " + str(xNF_healthcheck_command))
87 return dockerClient.containers.run(SIMULATOR_IMAGE_FULL_NAME,
88 command=xNF_startup_command,
89 healthcheck=xNF_healthcheck_command,
91 network="ves-hv-default",
92 ports={port + "/tcp": port},
93 volumes=self.container_volumes(),
94 name=xnf.container_name_prefix + port)
96 def container_volumes(self):
97 return {certificates_dir_path: {"bind": collector_certs_lookup_dir, "mode": 'rw'}}
99 def assert_containers_startup_was_successful(self, dockerClient):
101 check_interval_in_seconds = 5
102 for _ in range(checks_amount):
103 sleep(check_interval_in_seconds)
104 all_containers_healthy = True
105 for container in self.get_simulators_list(dockerClient):
106 all_containers_healthy = all_containers_healthy and self.is_container_healthy(container)
107 if (all_containers_healthy):
109 raise ContainerException("One of xNF simulators containers did not pass the healthcheck.")
111 def is_container_healthy(self, container):
112 container_health = container.attrs['State']['Health']['Status']
113 return container_health == 'healthy' and container.status == 'running'
115 def stop_and_remove_all_xnf_simulators(self, suite_name):
116 dockerClient = docker.from_env()
117 for container in self.get_simulators_list(dockerClient):
118 logger.info("Stopping and removing container: " + container.id)
119 log_filename = WORKSPACE_ENV + "/archives/containers_logs/" + \
120 suite_name.split(".")[-1] + "_" + container.name + ".log"
121 file = open(log_filename, "w+")
122 file.write(container.logs())
128 def get_simulators_list(self, dockerClient):
129 return dockerClient.containers.list(filters={"ancestor": SIMULATOR_IMAGE_FULL_NAME}, all=True)
131 def send_messages(self, simulator_url, message_filepath):
132 logger.info("Reading message to simulator from: " + message_filepath)
134 file = open(message_filepath, "rb")
138 logger.info("POST at: " + simulator_url)
139 resp = HttpRequests.session_without_env().post(simulator_url, data=data, timeout=5)
140 HttpRequests.checkStatusCode(resp.status_code, XNF_SIMULATOR_NAME)
144 container_name_prefix = "ves-hv-collector-xnf-simulator"
148 should_use_valid_certs,
150 should_connect_to_unencrypted_hv_ves):
152 self.healthcheck_server_port = "6063"
153 cert_name_prefix = "" if should_use_valid_certs else "untrusted"
154 certificates_path_with_file_prefix = collector_certs_lookup_dir + cert_name_prefix
155 self.key_store_path = certificates_path_with_file_prefix + "client.p12"
156 self.trust_store_path = certificates_path_with_file_prefix + "trust.p12"
157 self.sec_store_passwd = "onaponap"
158 self.disable_ssl = should_disable_ssl
159 self.hv_collector_host = "unencrypted-ves-hv-collector" \
160 if should_connect_to_unencrypted_hv_ves else "ves-hv-collector"
162 def get_startup_command(self):
163 startup_command = ["--listen-port", self.port,
164 "--health-check-api-port", self.healthcheck_server_port,
165 "--ves-host", self.hv_collector_host,
166 "--ves-port", "6061",
167 "--key-store", self.key_store_path,
168 "--trust-store", self.trust_store_path,
169 "--key-store-password", self.sec_store_passwd,
170 "--trust-store-password", self.sec_store_passwd
173 startup_command.append("--ssl-disable")
174 return startup_command
176 def get_healthcheck_command(self):
178 "interval": 5 * ONE_SECOND_IN_NANOS,
179 "timeout": 3 * ONE_SECOND_IN_NANOS,
181 "test": ["CMD", "curl", "--request", "GET",
182 "--fail", "--silent", "--show-error",
183 "localhost:" + self.healthcheck_server_port + "/health/ready"]
187 class ContainerException(Exception):
188 def __init__(self, message):
189 super(ContainerException, self).__init__(message)