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