blob: 5bc5f36ba6a938cf69ef1fa0356ed80a2eaa1046 [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'
48 sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt,
49 pillar=pillar)
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030050 sl_vip_ip = set([ip
Dina Belovae6fdffb2017-09-19 13:58:34 -070051 for item in sl_vip_address_pillars
52 for node, ip in item.items() if ip])
sgudzcced67d2017-10-11 15:56:09 +030053 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 Leontovich25e1f912018-04-04 16:29:10 +030060 if not sl_vip_ip:
61
62 pillar = ('keepalived:cluster:instance'
63 ':stacklight_monitor_vip:address')
64 sl_vip_address_pillars = self._salt.get_pillar(tgt=tgt,
65 pillar=pillar)
66 sl_vip_ip = set([ip
67 for item in sl_vip_address_pillars
68 for node, ip in item.items() if ip])
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030069 assert len(sl_vip_ip) == 1, (
sgudz4fab65f2017-10-25 16:51:56 +030070 "SL VIP not found or found more than one SL VIP in pillars:{0}, "
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030071 "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 Leontovich126b0032017-08-30 20:51:20 +030083
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 Leontovich063d0ff2017-09-05 18:11:55 +030099
Tatyana Leontovich58ae7552017-09-22 11:24:06 +0300100 def run_sl_functional_tests(self, node_to_run, tests_path,
101 test_to_run, skip_tests):
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300102 target_node_name = [node_name for node_name
103 in self.__underlay.node_names()
104 if node_to_run in node_name]
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200105 cmd = (". venv-stacklight-pytest/bin/activate;"
106 "cd {0}; "
Dennis Dmitrievd7883112018-01-18 00:50:56 +0200107 "export VOLUME_STATUS='available';"
108 "pytest -k {1} {2}".format(
109 tests_path,
110 "'not " + skip_tests + "'" if skip_tests else '',
111 test_to_run))
112
Dina Belovae6fdffb2017-09-19 13:58:34 -0700113 with self.__underlay.remote(node_name=target_node_name[0]) \
114 as node_remote:
Tatyana Leontovich58ae7552017-09-22 11:24:06 +0300115 LOG.debug("Run {0} on the node {1}".format(
116 cmd, target_node_name[0]))
Dennis Dmitriev092c6f32018-02-20 15:12:19 +0200117 result = node_remote.execute(cmd, verbose=True)
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300118 LOG.debug("Test execution result is {}".format(result))
119 return result
120
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300121 def run_sl_tests_json(self, node_to_run, tests_path,
122 test_to_run, skip_tests):
123 target_node_name = [node_name for node_name
124 in self.__underlay.node_names()
125 if node_to_run in node_name]
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200126 cmd = (". venv-stacklight-pytest/bin/activate;"
127 "cd {0}; "
Dennis Dmitrievd7883112018-01-18 00:50:56 +0200128 "export VOLUME_STATUS='available';"
129 "pip install pytest-json;"
130 "pytest --json=report.json -k {1} {2}".format(
131 tests_path,
132 "'not " + skip_tests + "'" if skip_tests else '',
133 test_to_run))
134
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300135 with self.__underlay.remote(node_name=target_node_name[0]) \
136 as node_remote:
137 LOG.debug("Run {0} on the node {1}".format(
138 cmd, target_node_name[0]))
Dennis Dmitriev092c6f32018-02-20 15:12:19 +0200139 node_remote.execute(cmd, verbose=True)
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200140 res = node_remote.check_call('cd {0}; cat report.json'.format(
141 tests_path), verbose=True)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300142 LOG.debug("Test execution result is {}".format(res['stdout']))
143 result = json.loads(res['stdout'][0])
144 return result['report']['tests']
145
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300146 def download_sl_test_report(self, stored_node, file_path):
147 target_node_name = [node_name for node_name
148 in self.__underlay.node_names()
149 if stored_node in node_name]
150 with self.__underlay.remote(node_name=target_node_name[0]) as r:
151 r.download(
152 destination=file_path,
153 target=os.getcwd())
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300154
155 def check_docker_services(self, nodes, expected_services):
156 """Check presense of the specified docker services on all the nodes
157 :param nodes: list of strings, names of nodes to check
158 :param expected_services: list of strings, names of services to find
159 """
160 for node in nodes:
161 services_status = self.get_service_info_from_node(node)
Dennis Dmitrievea291ee2018-03-16 12:27:43 +0200162 assert set(services_status) >= set(expected_services), \
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300163 'Some services are missed on node {0}. ' \
164 'Current service list: {1}\nExpected service list: {2}' \
165 .format(node, services_status, expected_services)
166 for service in expected_services:
167 assert service in services_status,\
Dina Belovae6fdffb2017-09-19 13:58:34 -0700168 'Missing service {0} in {1}'.format(service,
169 services_status)
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300170 assert '0' not in services_status.get(service),\
171 'Service {0} failed to start'.format(service)
172
173 @decorators.retry(AssertionError, count=10, delay=5)
174 def check_prometheus_targets(self, nodes):
175 """Check the status for Prometheus targets
176 :param nodes: list of strings, names of nodes with keepalived VIP
177 """
178 prometheus_client = self.api
Dennis Dmitrievd9403e22017-12-01 12:28:26 +0200179 current_targets = prometheus_client.get_targets()
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300180
181 LOG.debug('Current targets after install {0}'
182 .format(current_targets))
183 # Assert that targets are up
184 for entry in current_targets:
185 assert 'up' in entry['health'], \
186 'Next target is down {}'.format(entry)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300187
188 def kill_sl_service_on_node(self, node_sub_name, service_name):
189 target_node_name = [node_name for node_name
190 in self.__underlay.node_names()
191 if node_sub_name in node_name]
192 cmd = 'kill -9 $(pidof {0})'.format(service_name)
193 with self.__underlay.remote(node_name=target_node_name[0]) \
194 as node_remote:
195 LOG.debug("Run {0} on the node {1}".format(
196 cmd, target_node_name[0]))
197 res = node_remote.execute(cmd)
198 LOG.debug("Test execution result is {}".format(res))
199 assert res['exit_code'] == 0, (
200 'Unexpected exit code for command {0}, '
201 'current result {1}'.format(cmd, res))
202
203 def stop_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 = 'systemctl stop {}'.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 post_data_into_influx(self, node_sub_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 vip = self.get_sl_vip()
223 cmd = ("curl -POST 'http://{0}:8086/write?db=lma' -u "
224 "lma:lmapass --data-binary 'mymeas value=777'".format(vip))
225 with self.__underlay.remote(node_name=target_node_name[0]) \
226 as node_remote:
227 LOG.debug("Run {0} on the node {1}".format(
228 cmd, target_node_name[0]))
229 res = node_remote.execute(cmd)
230 assert res['exit_code'] == 0, (
231 'Unexpected exit code for command {0}, '
232 'current result {1}'.format(cmd, res))
233
234 def check_data_in_influxdb(self, node_sub_name):
235 target_node_name = [node_name for node_name
236 in self.__underlay.node_names()
237 if node_sub_name in node_name]
238 vip = self.get_sl_vip()
239 cmd = ("influx -host {0} -port 8086 -database lma "
240 "-username lma -password lmapass -execute "
241 "'select * from mymeas' -precision rfc3339;".format(vip))
242 with self.__underlay.remote(node_name=target_node_name[0]) \
243 as node_remote:
244 LOG.debug("Run {0} on the node {1}".format(
245 cmd, target_node_name[0]))
246 res = node_remote.execute(cmd)
247 assert res['exit_code'] == 0, (
248 'Unexpected exit code for command {0}, '
249 'current result {1}'.format(cmd, res))
Dennis Dmitriev8ce85152017-11-29 00:05:12 +0200250 if res['stdout']:
251 return res['stdout'][0].rstrip()
252 else:
253 return ''
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300254
255 def start_service(self, node_sub_name, service_name):
256 target_node_name = [node_name for node_name
257 in self.__underlay.node_names()
258 if node_sub_name in node_name]
259 cmd = 'systemctl start {0}'.format(service_name)
260 with self.__underlay.remote(node_name=target_node_name[0]) \
261 as node_remote:
262 LOG.debug("Run {0} on the node {1}".format(
263 cmd, target_node_name[0]))
264 res = node_remote.execute(cmd)
265 assert res['exit_code'] == 0, (
266 'Unexpected exit code for command {0}, '
267 'current result {1}'.format(cmd, res))