blob: f65f5246a5de09231dab4c48d2c875f5c0bc3201 [file] [log] [blame]
Tatyana Leontovichc8b8ca22017-05-19 13:37:05 +03001# 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 Leontovicha6c64a72017-10-25 22:21:18 +030014import json
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +030015import os
16
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +030017from devops.helpers import decorators
18
Tatyana Leontovichc8b8ca22017-05-19 13:37:05 +030019from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030020from tcp_tests.managers.clients.prometheus import prometheus_client
Tatyana Leontovich126b0032017-08-30 20:51:20 +030021from tcp_tests import logger
22
23LOG = logger.logger
Tatyana Leontovichc8b8ca22017-05-19 13:37:05 +030024
25
26class 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 Leontovich09b7b012017-07-10 12:53:45 +030036 self._p_client = None
Tatyana Leontovichc8b8ca22017-05-19 13:37:05 +030037 super(SLManager, self).__init__(
38 config=config, underlay=underlay)
39
40 def install(self, commands):
41 self.execute_commands(commands,
42 label='Install SL services')
vrovachev700a7b02017-05-23 18:36:48 +040043 self.__config.stack_light.sl_installed = True
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030044 self.__config.stack_light.sl_vip_host = self.get_sl_vip()
45
46 def get_sl_vip(self):
sgudzcced67d2017-10-11 15:56:09 +030047 tgt = 'I@prometheus:server:enabled:True'
48 pillar = 'keepalived:cluster:instance:prometheus_server_vip:address'
49 sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt,
50 pillar=pillar)
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030051 sl_vip_ip = set([ip
Dina Belovae6fdffb2017-09-19 13:58:34 -070052 for item in sl_vip_address_pillars
53 for node, ip in item.items() if ip])
sgudzcced67d2017-10-11 15:56:09 +030054 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 Leontovich09b7b012017-07-10 12:53:45 +030061 assert len(sl_vip_ip) == 1, (
sgudz4fab65f2017-10-25 16:51:56 +030062 "SL VIP not found or found more than one SL VIP in pillars:{0}, "
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030063 "expected one!").format(sl_vip_ip)
64 sl_vip_ip_host = sl_vip_ip.pop()
65 return sl_vip_ip_host
66
67 @property
68 def api(self):
69 if self._p_client is None:
70 self._p_client = prometheus_client.PrometheusClient(
71 host=self.__config.stack_light.sl_vip_host,
72 port=self.__config.stack_light.sl_prometheus_port,
73 proto=self.__config.stack_light.sl_prometheus_proto)
74 return self._p_client
Tatyana Leontovich126b0032017-08-30 20:51:20 +030075
76 def get_monitoring_nodes(self):
77 return [node_name for node_name
78 in self.__underlay.node_names() if 'mon' in node_name]
79
80 def get_service_info_from_node(self, node_name):
81 service_stat_dict = {}
82 with self.__underlay.remote(node_name=node_name) as node_remote:
83 result = node_remote.execute(
84 "docker service ls --format '{{.Name}}:{{.Replicas}}'")
85 LOG.debug("Service ls result {0} from node {1}".format(
86 result['stdout'], node_name))
87 for line in result['stdout']:
88 tmp = line.split(':')
89 service_stat_dict.update({tmp[0]: tmp[1]})
90 return service_stat_dict
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +030091
Tatyana Leontovich58ae7552017-09-22 11:24:06 +030092 def run_sl_functional_tests(self, node_to_run, tests_path,
93 test_to_run, skip_tests):
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +030094 target_node_name = [node_name for node_name
95 in self.__underlay.node_names()
96 if node_to_run in node_name]
Tatyana Leontovich58ae7552017-09-22 11:24:06 +030097 if skip_tests:
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +030098 cmd = ("cd {0}; "
99 "export VOLUME_STATUS='available'; "
100 "pytest -k 'not {1}' {2}".format(
Dennis Dmitriev9b02c8b2017-11-13 15:31:35 +0200101 tests_path, skip_tests, test_to_run))
Tatyana Leontovich58ae7552017-09-22 11:24:06 +0300102 else:
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300103 cmd = ("cd {0}; "
104 "export VOLUME_STATUS='available'; "
105 "pytest -k {1}".format(tests_path, test_to_run))
Dina Belovae6fdffb2017-09-19 13:58:34 -0700106 with self.__underlay.remote(node_name=target_node_name[0]) \
107 as node_remote:
Tatyana Leontovich58ae7552017-09-22 11:24:06 +0300108 LOG.debug("Run {0} on the node {1}".format(
109 cmd, target_node_name[0]))
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300110 result = node_remote.execute(cmd)
111 LOG.debug("Test execution result is {}".format(result))
112 return result
113
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300114 def run_sl_tests_json(self, node_to_run, tests_path,
115 test_to_run, skip_tests):
116 target_node_name = [node_name for node_name
117 in self.__underlay.node_names()
118 if node_to_run in node_name]
119 if skip_tests:
120 cmd = ("cd {0}; "
121 "export VOLUME_STATUS='available'; "
122 "pytest --json=report.json -k 'not {1}' {2}".format(
Dennis Dmitriev9b02c8b2017-11-13 15:31:35 +0200123 tests_path, skip_tests, test_to_run))
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300124 else:
125 cmd = ("cd {0}; "
126 "export VOLUME_STATUS='available'; "
127 "pytest --json=report.json -k {1}".format(
Dennis Dmitriev9b02c8b2017-11-13 15:31:35 +0200128 tests_path, test_to_run))
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300129 with self.__underlay.remote(node_name=target_node_name[0]) \
130 as node_remote:
131 LOG.debug("Run {0} on the node {1}".format(
132 cmd, target_node_name[0]))
133 node_remote.execute('pip install pytest-json')
134 node_remote.execute(cmd)
135 res = node_remote.execute('cd {0}; cat report.json'.format(
136 tests_path))
137 LOG.debug("Test execution result is {}".format(res['stdout']))
138 result = json.loads(res['stdout'][0])
139 return result['report']['tests']
140
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300141 def download_sl_test_report(self, stored_node, file_path):
142 target_node_name = [node_name for node_name
143 in self.__underlay.node_names()
144 if stored_node in node_name]
145 with self.__underlay.remote(node_name=target_node_name[0]) as r:
146 r.download(
147 destination=file_path,
148 target=os.getcwd())
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300149
150 def check_docker_services(self, nodes, expected_services):
151 """Check presense of the specified docker services on all the nodes
152 :param nodes: list of strings, names of nodes to check
153 :param expected_services: list of strings, names of services to find
154 """
155 for node in nodes:
156 services_status = self.get_service_info_from_node(node)
157 assert len(services_status) == len(expected_services), \
158 'Some services are missed on node {0}. ' \
159 'Current service list: {1}\nExpected service list: {2}' \
160 .format(node, services_status, expected_services)
161 for service in expected_services:
162 assert service in services_status,\
Dina Belovae6fdffb2017-09-19 13:58:34 -0700163 'Missing service {0} in {1}'.format(service,
164 services_status)
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300165 assert '0' not in services_status.get(service),\
166 'Service {0} failed to start'.format(service)
167
168 @decorators.retry(AssertionError, count=10, delay=5)
169 def check_prometheus_targets(self, nodes):
170 """Check the status for Prometheus targets
171 :param nodes: list of strings, names of nodes with keepalived VIP
172 """
173 prometheus_client = self.api
174 try:
175 current_targets = prometheus_client.get_targets()
Dennis Dmitriev9b02c8b2017-11-13 15:31:35 +0200176 except Exception:
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300177 LOG.info('Restarting keepalived service on mon nodes...')
178 for node in nodes:
179 self._salt.local(tgt=node, fun='cmd.run',
Dina Belovae6fdffb2017-09-19 13:58:34 -0700180 args='systemctl restart keepalived')
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300181 LOG.warning(
182 'Ip states after force restart {0}'.format(
183 self._salt.local(tgt='mon*',
Dina Belovae6fdffb2017-09-19 13:58:34 -0700184 fun='cmd.run', args='ip a')))
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300185 self._salt.local(tgt="mon*", fun='cmd.run',
186 args='systemctl restart keepalived')
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300187 current_targets = prometheus_client.get_targets()
188
189 LOG.debug('Current targets after install {0}'
190 .format(current_targets))
191 # Assert that targets are up
192 for entry in current_targets:
193 assert 'up' in entry['health'], \
194 'Next target is down {}'.format(entry)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300195
196 def kill_sl_service_on_node(self, node_sub_name, service_name):
197 target_node_name = [node_name for node_name
198 in self.__underlay.node_names()
199 if node_sub_name in node_name]
200 cmd = 'kill -9 $(pidof {0})'.format(service_name)
201 with self.__underlay.remote(node_name=target_node_name[0]) \
202 as node_remote:
203 LOG.debug("Run {0} on the node {1}".format(
204 cmd, target_node_name[0]))
205 res = node_remote.execute(cmd)
206 LOG.debug("Test execution result is {}".format(res))
207 assert res['exit_code'] == 0, (
208 'Unexpected exit code for command {0}, '
209 'current result {1}'.format(cmd, res))
210
211 def stop_sl_service_on_node(self, node_sub_name, service_name):
212 target_node_name = [node_name for node_name
213 in self.__underlay.node_names()
214 if node_sub_name in node_name]
215 cmd = 'systemctl stop {}'.format(service_name)
216 with self.__underlay.remote(node_name=target_node_name[0]) \
217 as node_remote:
218 LOG.debug("Run {0} on the node {1}".format(
219 cmd, target_node_name[0]))
220 res = node_remote.execute(cmd)
221 LOG.debug("Test execution result is {}".format(res))
222 assert res['exit_code'] == 0, (
223 'Unexpected exit code for command {0}, '
224 'current result {1}'.format(cmd, res))
225
226 def post_data_into_influx(self, node_sub_name):
227 target_node_name = [node_name for node_name
228 in self.__underlay.node_names()
229 if node_sub_name in node_name]
230 vip = self.get_sl_vip()
231 cmd = ("curl -POST 'http://{0}:8086/write?db=lma' -u "
232 "lma:lmapass --data-binary 'mymeas value=777'".format(vip))
233 with self.__underlay.remote(node_name=target_node_name[0]) \
234 as node_remote:
235 LOG.debug("Run {0} on the node {1}".format(
236 cmd, target_node_name[0]))
237 res = node_remote.execute(cmd)
238 assert res['exit_code'] == 0, (
239 'Unexpected exit code for command {0}, '
240 'current result {1}'.format(cmd, res))
241
242 def check_data_in_influxdb(self, node_sub_name):
243 target_node_name = [node_name for node_name
244 in self.__underlay.node_names()
245 if node_sub_name in node_name]
246 vip = self.get_sl_vip()
247 cmd = ("influx -host {0} -port 8086 -database lma "
248 "-username lma -password lmapass -execute "
249 "'select * from mymeas' -precision rfc3339;".format(vip))
250 with self.__underlay.remote(node_name=target_node_name[0]) \
251 as node_remote:
252 LOG.debug("Run {0} on the node {1}".format(
253 cmd, target_node_name[0]))
254 res = node_remote.execute(cmd)
255 assert res['exit_code'] == 0, (
256 'Unexpected exit code for command {0}, '
257 'current result {1}'.format(cmd, res))
Dennis Dmitriev8ce85152017-11-29 00:05:12 +0200258 if res['stdout']:
259 return res['stdout'][0].rstrip()
260 else:
261 return ''
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300262
263 def start_service(self, node_sub_name, service_name):
264 target_node_name = [node_name for node_name
265 in self.__underlay.node_names()
266 if node_sub_name in node_name]
267 cmd = 'systemctl start {0}'.format(service_name)
268 with self.__underlay.remote(node_name=target_node_name[0]) \
269 as node_remote:
270 LOG.debug("Run {0} on the node {1}".format(
271 cmd, target_node_name[0]))
272 res = node_remote.execute(cmd)
273 assert res['exit_code'] == 0, (
274 'Unexpected exit code for command {0}, '
275 'current result {1}'.format(cmd, res))