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