blob: 364f2192a15c1d986c9344c00cb13bfcbde41ff4 [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
sgudzbf4de572017-11-23 14:37:01 +020040 def install(self, commands, label='Install SL services'):
41 self.execute_commands(commands, label=label)
vrovachev700a7b02017-05-23 18:36:48 +040042 self.__config.stack_light.sl_installed = True
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030043 self.__config.stack_light.sl_vip_host = self.get_sl_vip()
44
45 def get_sl_vip(self):
sgudzcced67d2017-10-11 15:56:09 +030046 tgt = 'I@prometheus:server:enabled:True'
47 pillar = 'keepalived:cluster:instance:prometheus_server_vip:address'
Tatyana Leontovichaf20b672018-04-04 20:41:21 +030048 pill = 'keepalived:cluster:instance:stacklight_monitor_vip:address'
sgudzcced67d2017-10-11 15:56:09 +030049 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 Leontovichaf20b672018-04-04 20:41:21 +030061 if len(sl_vip_ip) != 1:
Tatyana Leontovich25e1f912018-04-04 16:29:10 +030062 sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt,
Tatyana Leontovichaf20b672018-04-04 20:41:21 +030063 pillar=pill)
Tatyana Leontovich25e1f912018-04-04 16:29:10 +030064 sl_vip_ip = set([ip
65 for item in sl_vip_address_pillars
66 for node, ip in item.items() if ip])
Tatyana Leontovichaf20b672018-04-04 20:41:21 +030067 LOG.info("Current response is {}".format(sl_vip_address_pillars))
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030068 assert len(sl_vip_ip) == 1, (
sgudz4fab65f2017-10-25 16:51:56 +030069 "SL VIP not found or found more than one SL VIP in pillars:{0}, "
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030070 "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 Leontovich126b0032017-08-30 20:51:20 +030082
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 Leontovich063d0ff2017-09-05 18:11:55 +030098
Tatyana Leontovich58ae7552017-09-22 11:24:06 +030099 def run_sl_functional_tests(self, node_to_run, tests_path,
100 test_to_run, skip_tests):
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300101 target_node_name = [node_name for node_name
102 in self.__underlay.node_names()
103 if node_to_run in node_name]
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200104 cmd = (". venv-stacklight-pytest/bin/activate;"
105 "cd {0}; "
Dennis Dmitrievd7883112018-01-18 00:50:56 +0200106 "export VOLUME_STATUS='available';"
107 "pytest -k {1} {2}".format(
108 tests_path,
109 "'not " + skip_tests + "'" if skip_tests else '',
110 test_to_run))
111
Dina Belovae6fdffb2017-09-19 13:58:34 -0700112 with self.__underlay.remote(node_name=target_node_name[0]) \
113 as node_remote:
Tatyana Leontovich58ae7552017-09-22 11:24:06 +0300114 LOG.debug("Run {0} on the node {1}".format(
115 cmd, target_node_name[0]))
Dennis Dmitriev092c6f32018-02-20 15:12:19 +0200116 result = node_remote.execute(cmd, verbose=True)
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300117 LOG.debug("Test execution result is {}".format(result))
118 return result
119
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300120 def run_sl_tests_json(self, node_to_run, tests_path,
121 test_to_run, skip_tests):
122 target_node_name = [node_name for node_name
123 in self.__underlay.node_names()
124 if node_to_run in node_name]
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200125 cmd = (". venv-stacklight-pytest/bin/activate;"
126 "cd {0}; "
Dennis Dmitrievd7883112018-01-18 00:50:56 +0200127 "export VOLUME_STATUS='available';"
128 "pip install pytest-json;"
129 "pytest --json=report.json -k {1} {2}".format(
130 tests_path,
131 "'not " + skip_tests + "'" if skip_tests else '',
132 test_to_run))
133
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300134 with self.__underlay.remote(node_name=target_node_name[0]) \
135 as node_remote:
136 LOG.debug("Run {0} on the node {1}".format(
137 cmd, target_node_name[0]))
Dennis Dmitriev092c6f32018-02-20 15:12:19 +0200138 node_remote.execute(cmd, verbose=True)
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200139 res = node_remote.check_call('cd {0}; cat report.json'.format(
140 tests_path), verbose=True)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300141 LOG.debug("Test execution result is {}".format(res['stdout']))
142 result = json.loads(res['stdout'][0])
143 return result['report']['tests']
144
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300145 def download_sl_test_report(self, stored_node, file_path):
146 target_node_name = [node_name for node_name
147 in self.__underlay.node_names()
148 if stored_node in node_name]
149 with self.__underlay.remote(node_name=target_node_name[0]) as r:
150 r.download(
151 destination=file_path,
152 target=os.getcwd())
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300153
154 def check_docker_services(self, nodes, expected_services):
155 """Check presense of the specified docker services on all the nodes
156 :param nodes: list of strings, names of nodes to check
157 :param expected_services: list of strings, names of services to find
158 """
159 for node in nodes:
160 services_status = self.get_service_info_from_node(node)
Dennis Dmitrievea291ee2018-03-16 12:27:43 +0200161 assert set(services_status) >= set(expected_services), \
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300162 'Some services are missed on node {0}. ' \
163 'Current service list: {1}\nExpected service list: {2}' \
164 .format(node, services_status, expected_services)
165 for service in expected_services:
166 assert service in services_status,\
Dina Belovae6fdffb2017-09-19 13:58:34 -0700167 'Missing service {0} in {1}'.format(service,
168 services_status)
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300169 assert '0' not in services_status.get(service),\
170 'Service {0} failed to start'.format(service)
171
172 @decorators.retry(AssertionError, count=10, delay=5)
173 def check_prometheus_targets(self, nodes):
174 """Check the status for Prometheus targets
175 :param nodes: list of strings, names of nodes with keepalived VIP
176 """
177 prometheus_client = self.api
Dennis Dmitrievd9403e22017-12-01 12:28:26 +0200178 current_targets = prometheus_client.get_targets()
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300179
180 LOG.debug('Current targets after install {0}'
181 .format(current_targets))
182 # Assert that targets are up
183 for entry in current_targets:
184 assert 'up' in entry['health'], \
185 'Next target is down {}'.format(entry)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300186
187 def kill_sl_service_on_node(self, node_sub_name, service_name):
188 target_node_name = [node_name for node_name
189 in self.__underlay.node_names()
190 if node_sub_name in node_name]
191 cmd = 'kill -9 $(pidof {0})'.format(service_name)
192 with self.__underlay.remote(node_name=target_node_name[0]) \
193 as node_remote:
194 LOG.debug("Run {0} on the node {1}".format(
195 cmd, target_node_name[0]))
196 res = node_remote.execute(cmd)
197 LOG.debug("Test execution result is {}".format(res))
198 assert res['exit_code'] == 0, (
199 'Unexpected exit code for command {0}, '
200 'current result {1}'.format(cmd, res))
201
202 def stop_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 = 'systemctl stop {}'.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 post_data_into_influx(self, node_sub_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 vip = self.get_sl_vip()
222 cmd = ("curl -POST 'http://{0}:8086/write?db=lma' -u "
223 "lma:lmapass --data-binary 'mymeas value=777'".format(vip))
224 with self.__underlay.remote(node_name=target_node_name[0]) \
225 as node_remote:
226 LOG.debug("Run {0} on the node {1}".format(
227 cmd, target_node_name[0]))
228 res = node_remote.execute(cmd)
229 assert res['exit_code'] == 0, (
230 'Unexpected exit code for command {0}, '
231 'current result {1}'.format(cmd, res))
232
233 def check_data_in_influxdb(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 = ("influx -host {0} -port 8086 -database lma "
239 "-username lma -password lmapass -execute "
240 "'select * from mymeas' -precision rfc3339;".format(vip))
241 with self.__underlay.remote(node_name=target_node_name[0]) \
242 as node_remote:
243 LOG.debug("Run {0} on the node {1}".format(
244 cmd, target_node_name[0]))
245 res = node_remote.execute(cmd)
246 assert res['exit_code'] == 0, (
247 'Unexpected exit code for command {0}, '
248 'current result {1}'.format(cmd, res))
Dennis Dmitriev8ce85152017-11-29 00:05:12 +0200249 if res['stdout']:
250 return res['stdout'][0].rstrip()
251 else:
252 return ''
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300253
254 def start_service(self, node_sub_name, service_name):
255 target_node_name = [node_name for node_name
256 in self.__underlay.node_names()
257 if node_sub_name in node_name]
258 cmd = 'systemctl start {0}'.format(service_name)
259 with self.__underlay.remote(node_name=target_node_name[0]) \
260 as node_remote:
261 LOG.debug("Run {0} on the node {1}".format(
262 cmd, target_node_name[0]))
263 res = node_remote.execute(cmd)
264 assert res['exit_code'] == 0, (
265 'Unexpected exit code for command {0}, '
266 'current result {1}'.format(cmd, res))