blob: 142cd3f1e9ee34c1cc9c6cf11e1a6e34f808e9ec [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 Leontovich09b7b012017-07-10 12:53:45 +030060 assert len(sl_vip_ip) == 1, (
sgudz4fab65f2017-10-25 16:51:56 +030061 "SL VIP not found or found more than one SL VIP in pillars:{0}, "
Tatyana Leontovich09b7b012017-07-10 12:53:45 +030062 "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 Leontovich126b0032017-08-30 20:51:20 +030074
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 Leontovich063d0ff2017-09-05 18:11:55 +030090
Tatyana Leontovich58ae7552017-09-22 11:24:06 +030091 def run_sl_functional_tests(self, node_to_run, tests_path,
92 test_to_run, skip_tests):
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +030093 target_node_name = [node_name for node_name
94 in self.__underlay.node_names()
95 if node_to_run in node_name]
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +020096 cmd = (". venv-stacklight-pytest/bin/activate;"
97 "cd {0}; "
Dennis Dmitrievd7883112018-01-18 00:50:56 +020098 "export VOLUME_STATUS='available';"
99 "pytest -k {1} {2}".format(
100 tests_path,
101 "'not " + skip_tests + "'" if skip_tests else '',
102 test_to_run))
103
Dina Belovae6fdffb2017-09-19 13:58:34 -0700104 with self.__underlay.remote(node_name=target_node_name[0]) \
105 as node_remote:
Tatyana Leontovich58ae7552017-09-22 11:24:06 +0300106 LOG.debug("Run {0} on the node {1}".format(
107 cmd, target_node_name[0]))
Dennis Dmitriev092c6f32018-02-20 15:12:19 +0200108 result = node_remote.execute(cmd, verbose=True)
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300109 LOG.debug("Test execution result is {}".format(result))
110 return result
111
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300112 def run_sl_tests_json(self, node_to_run, tests_path,
113 test_to_run, skip_tests):
114 target_node_name = [node_name for node_name
115 in self.__underlay.node_names()
116 if node_to_run in node_name]
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200117 cmd = (". venv-stacklight-pytest/bin/activate;"
118 "cd {0}; "
Dennis Dmitrievd7883112018-01-18 00:50:56 +0200119 "export VOLUME_STATUS='available';"
120 "pip install pytest-json;"
121 "pytest --json=report.json -k {1} {2}".format(
122 tests_path,
123 "'not " + skip_tests + "'" if skip_tests else '',
124 test_to_run))
125
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300126 with self.__underlay.remote(node_name=target_node_name[0]) \
127 as node_remote:
128 LOG.debug("Run {0} on the node {1}".format(
129 cmd, target_node_name[0]))
Dennis Dmitriev092c6f32018-02-20 15:12:19 +0200130 node_remote.execute(cmd, verbose=True)
Dennis Dmitrievbc0b0942018-02-07 22:37:37 +0200131 res = node_remote.check_call('cd {0}; cat report.json'.format(
132 tests_path), verbose=True)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300133 LOG.debug("Test execution result is {}".format(res['stdout']))
134 result = json.loads(res['stdout'][0])
135 return result['report']['tests']
136
Tatyana Leontovich063d0ff2017-09-05 18:11:55 +0300137 def download_sl_test_report(self, stored_node, file_path):
138 target_node_name = [node_name for node_name
139 in self.__underlay.node_names()
140 if stored_node in node_name]
141 with self.__underlay.remote(node_name=target_node_name[0]) as r:
142 r.download(
143 destination=file_path,
144 target=os.getcwd())
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300145
146 def check_docker_services(self, nodes, expected_services):
147 """Check presense of the specified docker services on all the nodes
148 :param nodes: list of strings, names of nodes to check
149 :param expected_services: list of strings, names of services to find
150 """
151 for node in nodes:
152 services_status = self.get_service_info_from_node(node)
153 assert len(services_status) == len(expected_services), \
154 'Some services are missed on node {0}. ' \
155 'Current service list: {1}\nExpected service list: {2}' \
156 .format(node, services_status, expected_services)
157 for service in expected_services:
158 assert service in services_status,\
Dina Belovae6fdffb2017-09-19 13:58:34 -0700159 'Missing service {0} in {1}'.format(service,
160 services_status)
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300161 assert '0' not in services_status.get(service),\
162 'Service {0} failed to start'.format(service)
163
164 @decorators.retry(AssertionError, count=10, delay=5)
165 def check_prometheus_targets(self, nodes):
166 """Check the status for Prometheus targets
167 :param nodes: list of strings, names of nodes with keepalived VIP
168 """
169 prometheus_client = self.api
Dennis Dmitrievd9403e22017-12-01 12:28:26 +0200170 current_targets = prometheus_client.get_targets()
Dennis Dmitriev9d9ba9f2017-09-13 17:34:03 +0300171
172 LOG.debug('Current targets after install {0}'
173 .format(current_targets))
174 # Assert that targets are up
175 for entry in current_targets:
176 assert 'up' in entry['health'], \
177 'Next target is down {}'.format(entry)
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300178
179 def kill_sl_service_on_node(self, node_sub_name, service_name):
180 target_node_name = [node_name for node_name
181 in self.__underlay.node_names()
182 if node_sub_name in node_name]
183 cmd = 'kill -9 $(pidof {0})'.format(service_name)
184 with self.__underlay.remote(node_name=target_node_name[0]) \
185 as node_remote:
186 LOG.debug("Run {0} on the node {1}".format(
187 cmd, target_node_name[0]))
188 res = node_remote.execute(cmd)
189 LOG.debug("Test execution result is {}".format(res))
190 assert res['exit_code'] == 0, (
191 'Unexpected exit code for command {0}, '
192 'current result {1}'.format(cmd, res))
193
194 def stop_sl_service_on_node(self, node_sub_name, service_name):
195 target_node_name = [node_name for node_name
196 in self.__underlay.node_names()
197 if node_sub_name in node_name]
198 cmd = 'systemctl stop {}'.format(service_name)
199 with self.__underlay.remote(node_name=target_node_name[0]) \
200 as node_remote:
201 LOG.debug("Run {0} on the node {1}".format(
202 cmd, target_node_name[0]))
203 res = node_remote.execute(cmd)
204 LOG.debug("Test execution result is {}".format(res))
205 assert res['exit_code'] == 0, (
206 'Unexpected exit code for command {0}, '
207 'current result {1}'.format(cmd, res))
208
209 def post_data_into_influx(self, node_sub_name):
210 target_node_name = [node_name for node_name
211 in self.__underlay.node_names()
212 if node_sub_name in node_name]
213 vip = self.get_sl_vip()
214 cmd = ("curl -POST 'http://{0}:8086/write?db=lma' -u "
215 "lma:lmapass --data-binary 'mymeas value=777'".format(vip))
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 assert res['exit_code'] == 0, (
222 'Unexpected exit code for command {0}, '
223 'current result {1}'.format(cmd, res))
224
225 def check_data_in_influxdb(self, node_sub_name):
226 target_node_name = [node_name for node_name
227 in self.__underlay.node_names()
228 if node_sub_name in node_name]
229 vip = self.get_sl_vip()
230 cmd = ("influx -host {0} -port 8086 -database lma "
231 "-username lma -password lmapass -execute "
232 "'select * from mymeas' -precision rfc3339;".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))
Dennis Dmitriev8ce85152017-11-29 00:05:12 +0200241 if res['stdout']:
242 return res['stdout'][0].rstrip()
243 else:
244 return ''
Tatyana Leontovicha6c64a72017-10-25 22:21:18 +0300245
246 def start_service(self, node_sub_name, service_name):
247 target_node_name = [node_name for node_name
248 in self.__underlay.node_names()
249 if node_sub_name in node_name]
250 cmd = 'systemctl start {0}'.format(service_name)
251 with self.__underlay.remote(node_name=target_node_name[0]) \
252 as node_remote:
253 LOG.debug("Run {0} on the node {1}".format(
254 cmd, target_node_name[0]))
255 res = node_remote.execute(cmd)
256 assert res['exit_code'] == 0, (
257 'Unexpected exit code for command {0}, '
258 'current result {1}'.format(cmd, res))