Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 1 | # Copyright 2016 Mirantis, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 14 | import json |
Tatyana Leontovich | 063d0ff | 2017-09-05 18:11:55 +0300 | [diff] [blame] | 15 | import os |
| 16 | |
Dennis Dmitriev | 9d9ba9f | 2017-09-13 17:34:03 +0300 | [diff] [blame] | 17 | from devops.helpers import decorators |
| 18 | |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 19 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 20 | from tcp_tests.managers.clients.prometheus import prometheus_client |
Tatyana Leontovich | 126b003 | 2017-08-30 20:51:20 +0300 | [diff] [blame] | 21 | from tcp_tests import logger |
| 22 | |
| 23 | LOG = logger.logger |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class SLManager(ExecuteCommandsMixin): |
| 27 | """docstring for OpenstackManager""" |
| 28 | |
| 29 | __config = None |
| 30 | __underlay = None |
| 31 | |
| 32 | def __init__(self, config, underlay, salt): |
| 33 | self.__config = config |
| 34 | self.__underlay = underlay |
| 35 | self._salt = salt |
Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 36 | self._p_client = None |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 37 | super(SLManager, self).__init__( |
| 38 | config=config, underlay=underlay) |
| 39 | |
sgudz | bf4de57 | 2017-11-23 14:37:01 +0200 | [diff] [blame] | 40 | def install(self, commands, label='Install SL services'): |
| 41 | self.execute_commands(commands, label=label) |
Dennis Dmitriev | fde667f | 2018-07-23 16:26:50 +0300 | [diff] [blame] | 42 | self.__config.stack_light.stacklight_installed = True |
Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 43 | self.__config.stack_light.sl_vip_host = self.get_sl_vip() |
| 44 | |
| 45 | def get_sl_vip(self): |
sgudz | cced67d | 2017-10-11 15:56:09 +0300 | [diff] [blame] | 46 | tgt = 'I@prometheus:server:enabled:True' |
| 47 | pillar = 'keepalived:cluster:instance:prometheus_server_vip:address' |
Tatyana Leontovich | af20b67 | 2018-04-04 20:41:21 +0300 | [diff] [blame] | 48 | pill = 'keepalived:cluster:instance:stacklight_monitor_vip:address' |
sgudz | cced67d | 2017-10-11 15:56:09 +0300 | [diff] [blame] | 49 | sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt, |
| 50 | pillar=pillar) |
Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 51 | sl_vip_ip = set([ip |
Dina Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 52 | for item in sl_vip_address_pillars |
| 53 | for node, ip in item.items() if ip]) |
sgudz | cced67d | 2017-10-11 15:56:09 +0300 | [diff] [blame] | 54 | if not sl_vip_ip: |
dbrz | 19bd2ab | 2018-07-26 14:48:32 +0300 | [diff] [blame] | 55 | tgt = 'I@prometheus:server:enabled:True and mon*' |
sgudz | cced67d | 2017-10-11 15:56:09 +0300 | [diff] [blame] | 56 | pillar = 'keepalived:cluster:instance:VIP:address' |
| 57 | sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt, |
| 58 | pillar=pillar) |
| 59 | sl_vip_ip = set([ip |
| 60 | for item in sl_vip_address_pillars |
| 61 | for node, ip in item.items() if ip]) |
Tatyana Leontovich | af20b67 | 2018-04-04 20:41:21 +0300 | [diff] [blame] | 62 | if len(sl_vip_ip) != 1: |
Tatyana Leontovich | 25e1f91 | 2018-04-04 16:29:10 +0300 | [diff] [blame] | 63 | sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt, |
Tatyana Leontovich | af20b67 | 2018-04-04 20:41:21 +0300 | [diff] [blame] | 64 | pillar=pill) |
Tatyana Leontovich | 25e1f91 | 2018-04-04 16:29:10 +0300 | [diff] [blame] | 65 | sl_vip_ip = set([ip |
| 66 | for item in sl_vip_address_pillars |
| 67 | for node, ip in item.items() if ip]) |
Tatyana Leontovich | af20b67 | 2018-04-04 20:41:21 +0300 | [diff] [blame] | 68 | LOG.info("Current response is {}".format(sl_vip_address_pillars)) |
Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 69 | assert len(sl_vip_ip) == 1, ( |
sgudz | 4fab65f | 2017-10-25 16:51:56 +0300 | [diff] [blame] | 70 | "SL VIP not found or found more than one SL VIP in pillars:{0}, " |
Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 71 | "expected one!").format(sl_vip_ip) |
| 72 | sl_vip_ip_host = sl_vip_ip.pop() |
| 73 | return sl_vip_ip_host |
| 74 | |
| 75 | @property |
| 76 | def api(self): |
| 77 | if self._p_client is None: |
| 78 | self._p_client = prometheus_client.PrometheusClient( |
| 79 | host=self.__config.stack_light.sl_vip_host, |
| 80 | port=self.__config.stack_light.sl_prometheus_port, |
| 81 | proto=self.__config.stack_light.sl_prometheus_proto) |
| 82 | return self._p_client |
Tatyana Leontovich | 126b003 | 2017-08-30 20:51:20 +0300 | [diff] [blame] | 83 | |
| 84 | def get_monitoring_nodes(self): |
| 85 | return [node_name for node_name |
| 86 | in self.__underlay.node_names() if 'mon' in node_name] |
| 87 | |
| 88 | def get_service_info_from_node(self, node_name): |
| 89 | service_stat_dict = {} |
| 90 | with self.__underlay.remote(node_name=node_name) as node_remote: |
| 91 | result = node_remote.execute( |
| 92 | "docker service ls --format '{{.Name}}:{{.Replicas}}'") |
| 93 | LOG.debug("Service ls result {0} from node {1}".format( |
| 94 | result['stdout'], node_name)) |
| 95 | for line in result['stdout']: |
| 96 | tmp = line.split(':') |
| 97 | service_stat_dict.update({tmp[0]: tmp[1]}) |
| 98 | return service_stat_dict |
Tatyana Leontovich | 063d0ff | 2017-09-05 18:11:55 +0300 | [diff] [blame] | 99 | |
Tatyana Leontovich | 58ae755 | 2017-09-22 11:24:06 +0300 | [diff] [blame] | 100 | def run_sl_functional_tests(self, node_to_run, tests_path, |
Dmitry Kalashnik | 1832059 | 2018-04-16 17:17:01 +0400 | [diff] [blame] | 101 | test_to_run, skip_tests, |
| 102 | reruns=5, reruns_delay=60): |
Tatyana Leontovich | 063d0ff | 2017-09-05 18:11:55 +0300 | [diff] [blame] | 103 | target_node_name = [node_name for node_name |
| 104 | in self.__underlay.node_names() |
| 105 | if node_to_run in node_name] |
Dennis Dmitriev | bc0b094 | 2018-02-07 22:37:37 +0200 | [diff] [blame] | 106 | cmd = (". venv-stacklight-pytest/bin/activate;" |
Dmitry Kalashnik | 1832059 | 2018-04-16 17:17:01 +0400 | [diff] [blame] | 107 | "cd {tests_path}; " |
Dennis Dmitriev | d788311 | 2018-01-18 00:50:56 +0200 | [diff] [blame] | 108 | "export VOLUME_STATUS='available';" |
Dmitry Kalashnik | 1832059 | 2018-04-16 17:17:01 +0400 | [diff] [blame] | 109 | "pytest {reruns} {reruns_delay} " |
| 110 | "-k {skip_tests} {test_to_run}".format(**{ |
| 111 | "tests_path": tests_path, |
| 112 | "skip_tests": ("'not " + skip_tests + "'" |
| 113 | if skip_tests else ''), |
| 114 | "test_to_run": test_to_run, |
| 115 | "reruns": ("--reruns {}".format(reruns) |
| 116 | if reruns > 1 else ""), |
| 117 | "reruns_delay": ("--reruns-delay {}".format(reruns_delay) |
| 118 | if reruns_delay > 0 else ""), |
| 119 | })) |
Dennis Dmitriev | d788311 | 2018-01-18 00:50:56 +0200 | [diff] [blame] | 120 | |
Dina Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 121 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 122 | as node_remote: |
Tatyana Leontovich | 58ae755 | 2017-09-22 11:24:06 +0300 | [diff] [blame] | 123 | LOG.debug("Run {0} on the node {1}".format( |
| 124 | cmd, target_node_name[0])) |
Dennis Dmitriev | 092c6f3 | 2018-02-20 15:12:19 +0200 | [diff] [blame] | 125 | result = node_remote.execute(cmd, verbose=True) |
Tatyana Leontovich | 063d0ff | 2017-09-05 18:11:55 +0300 | [diff] [blame] | 126 | LOG.debug("Test execution result is {}".format(result)) |
| 127 | return result |
| 128 | |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 129 | def run_sl_tests_json(self, node_to_run, tests_path, |
Dmitry Kalashnik | 1832059 | 2018-04-16 17:17:01 +0400 | [diff] [blame] | 130 | test_to_run, skip_tests, reruns=5, reruns_delay=60): |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 131 | target_node_name = [node_name for node_name |
| 132 | in self.__underlay.node_names() |
| 133 | if node_to_run in node_name] |
Dennis Dmitriev | bc0b094 | 2018-02-07 22:37:37 +0200 | [diff] [blame] | 134 | cmd = (". venv-stacklight-pytest/bin/activate;" |
Dmitry Kalashnik | 1832059 | 2018-04-16 17:17:01 +0400 | [diff] [blame] | 135 | "cd {tests_path}; " |
Dennis Dmitriev | d788311 | 2018-01-18 00:50:56 +0200 | [diff] [blame] | 136 | "export VOLUME_STATUS='available';" |
| 137 | "pip install pytest-json;" |
Dmitry Tyzhnenko | 11db68e | 2018-05-16 18:11:35 +0300 | [diff] [blame] | 138 | "pytest --json=report.json {reruns} {reruns_delay} " |
Dmitry Kalashnik | 1832059 | 2018-04-16 17:17:01 +0400 | [diff] [blame] | 139 | "-k {skip_tests} {test_to_run}".format(**{ |
| 140 | "tests_path": tests_path, |
| 141 | "skip_tests": ("'not " + skip_tests + "'" |
| 142 | if skip_tests else ''), |
| 143 | "test_to_run": test_to_run, |
| 144 | "reruns": ("--reruns {}".format(reruns) |
| 145 | if reruns > 1 else ""), |
| 146 | "reruns_delay": ("--reruns-delay {}".format(reruns_delay) |
| 147 | if reruns_delay > 0 else ""), |
| 148 | })) |
Dennis Dmitriev | d788311 | 2018-01-18 00:50:56 +0200 | [diff] [blame] | 149 | |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 150 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 151 | as node_remote: |
| 152 | LOG.debug("Run {0} on the node {1}".format( |
| 153 | cmd, target_node_name[0])) |
Dennis Dmitriev | 092c6f3 | 2018-02-20 15:12:19 +0200 | [diff] [blame] | 154 | node_remote.execute(cmd, verbose=True) |
Dennis Dmitriev | bc0b094 | 2018-02-07 22:37:37 +0200 | [diff] [blame] | 155 | res = node_remote.check_call('cd {0}; cat report.json'.format( |
| 156 | tests_path), verbose=True) |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 157 | LOG.debug("Test execution result is {}".format(res['stdout'])) |
| 158 | result = json.loads(res['stdout'][0]) |
| 159 | return result['report']['tests'] |
| 160 | |
Tatyana Leontovich | 063d0ff | 2017-09-05 18:11:55 +0300 | [diff] [blame] | 161 | def download_sl_test_report(self, stored_node, file_path): |
| 162 | target_node_name = [node_name for node_name |
| 163 | in self.__underlay.node_names() |
| 164 | if stored_node in node_name] |
| 165 | with self.__underlay.remote(node_name=target_node_name[0]) as r: |
| 166 | r.download( |
| 167 | destination=file_path, |
| 168 | target=os.getcwd()) |
Dennis Dmitriev | 9d9ba9f | 2017-09-13 17:34:03 +0300 | [diff] [blame] | 169 | |
| 170 | def check_docker_services(self, nodes, expected_services): |
| 171 | """Check presense of the specified docker services on all the nodes |
| 172 | :param nodes: list of strings, names of nodes to check |
| 173 | :param expected_services: list of strings, names of services to find |
| 174 | """ |
| 175 | for node in nodes: |
| 176 | services_status = self.get_service_info_from_node(node) |
Dennis Dmitriev | ea291ee | 2018-03-16 12:27:43 +0200 | [diff] [blame] | 177 | assert set(services_status) >= set(expected_services), \ |
Dennis Dmitriev | 9d9ba9f | 2017-09-13 17:34:03 +0300 | [diff] [blame] | 178 | 'Some services are missed on node {0}. ' \ |
| 179 | 'Current service list: {1}\nExpected service list: {2}' \ |
| 180 | .format(node, services_status, expected_services) |
| 181 | for service in expected_services: |
| 182 | assert service in services_status,\ |
Dina Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 183 | 'Missing service {0} in {1}'.format(service, |
| 184 | services_status) |
Dennis Dmitriev | 9d9ba9f | 2017-09-13 17:34:03 +0300 | [diff] [blame] | 185 | assert '0' not in services_status.get(service),\ |
| 186 | 'Service {0} failed to start'.format(service) |
| 187 | |
| 188 | @decorators.retry(AssertionError, count=10, delay=5) |
| 189 | def check_prometheus_targets(self, nodes): |
| 190 | """Check the status for Prometheus targets |
| 191 | :param nodes: list of strings, names of nodes with keepalived VIP |
| 192 | """ |
| 193 | prometheus_client = self.api |
Dennis Dmitriev | d9403e2 | 2017-12-01 12:28:26 +0200 | [diff] [blame] | 194 | current_targets = prometheus_client.get_targets() |
Dennis Dmitriev | 9d9ba9f | 2017-09-13 17:34:03 +0300 | [diff] [blame] | 195 | |
| 196 | LOG.debug('Current targets after install {0}' |
| 197 | .format(current_targets)) |
| 198 | # Assert that targets are up |
| 199 | for entry in current_targets: |
| 200 | assert 'up' in entry['health'], \ |
| 201 | 'Next target is down {}'.format(entry) |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 202 | |
| 203 | def kill_sl_service_on_node(self, node_sub_name, service_name): |
| 204 | target_node_name = [node_name for node_name |
| 205 | in self.__underlay.node_names() |
| 206 | if node_sub_name in node_name] |
| 207 | cmd = 'kill -9 $(pidof {0})'.format(service_name) |
| 208 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 209 | as node_remote: |
| 210 | LOG.debug("Run {0} on the node {1}".format( |
| 211 | cmd, target_node_name[0])) |
| 212 | res = node_remote.execute(cmd) |
| 213 | LOG.debug("Test execution result is {}".format(res)) |
| 214 | assert res['exit_code'] == 0, ( |
| 215 | 'Unexpected exit code for command {0}, ' |
| 216 | 'current result {1}'.format(cmd, res)) |
| 217 | |
| 218 | def stop_sl_service_on_node(self, node_sub_name, service_name): |
| 219 | target_node_name = [node_name for node_name |
| 220 | in self.__underlay.node_names() |
| 221 | if node_sub_name in node_name] |
| 222 | cmd = 'systemctl stop {}'.format(service_name) |
| 223 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 224 | as node_remote: |
| 225 | LOG.debug("Run {0} on the node {1}".format( |
| 226 | cmd, target_node_name[0])) |
| 227 | res = node_remote.execute(cmd) |
| 228 | LOG.debug("Test execution result is {}".format(res)) |
| 229 | assert res['exit_code'] == 0, ( |
| 230 | 'Unexpected exit code for command {0}, ' |
| 231 | 'current result {1}'.format(cmd, res)) |
| 232 | |
| 233 | def post_data_into_influx(self, node_sub_name): |
| 234 | target_node_name = [node_name for node_name |
| 235 | in self.__underlay.node_names() |
| 236 | if node_sub_name in node_name] |
| 237 | vip = self.get_sl_vip() |
| 238 | cmd = ("curl -POST 'http://{0}:8086/write?db=lma' -u " |
| 239 | "lma:lmapass --data-binary 'mymeas value=777'".format(vip)) |
| 240 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 241 | as node_remote: |
| 242 | LOG.debug("Run {0} on the node {1}".format( |
| 243 | cmd, target_node_name[0])) |
| 244 | res = node_remote.execute(cmd) |
| 245 | assert res['exit_code'] == 0, ( |
| 246 | 'Unexpected exit code for command {0}, ' |
| 247 | 'current result {1}'.format(cmd, res)) |
| 248 | |
| 249 | def check_data_in_influxdb(self, node_sub_name): |
| 250 | target_node_name = [node_name for node_name |
| 251 | in self.__underlay.node_names() |
| 252 | if node_sub_name in node_name] |
| 253 | vip = self.get_sl_vip() |
| 254 | cmd = ("influx -host {0} -port 8086 -database lma " |
| 255 | "-username lma -password lmapass -execute " |
| 256 | "'select * from mymeas' -precision rfc3339;".format(vip)) |
| 257 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 258 | as node_remote: |
| 259 | LOG.debug("Run {0} on the node {1}".format( |
| 260 | cmd, target_node_name[0])) |
| 261 | res = node_remote.execute(cmd) |
| 262 | assert res['exit_code'] == 0, ( |
| 263 | 'Unexpected exit code for command {0}, ' |
| 264 | 'current result {1}'.format(cmd, res)) |
Dennis Dmitriev | 8ce8515 | 2017-11-29 00:05:12 +0200 | [diff] [blame] | 265 | if res['stdout']: |
| 266 | return res['stdout'][0].rstrip() |
| 267 | else: |
| 268 | return '' |
Tatyana Leontovich | a6c64a7 | 2017-10-25 22:21:18 +0300 | [diff] [blame] | 269 | |
| 270 | def start_service(self, node_sub_name, service_name): |
| 271 | target_node_name = [node_name for node_name |
| 272 | in self.__underlay.node_names() |
| 273 | if node_sub_name in node_name] |
| 274 | cmd = 'systemctl start {0}'.format(service_name) |
| 275 | with self.__underlay.remote(node_name=target_node_name[0]) \ |
| 276 | as node_remote: |
| 277 | LOG.debug("Run {0} on the node {1}".format( |
| 278 | cmd, target_node_name[0])) |
| 279 | res = node_remote.execute(cmd) |
| 280 | assert res['exit_code'] == 0, ( |
| 281 | 'Unexpected exit code for command {0}, ' |
| 282 | 'current result {1}'.format(cmd, res)) |