blob: c91a5f64337b8f20fd09b5f805fbcc81bafca2a5 [file] [log] [blame]
Oleksii Zhurba020fab42017-11-01 20:13:28 +00001import json
2import requests
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +00003import datetime
Oleksii Zhurba9848e212018-09-05 10:53:51 -05004import pytest
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02005import utils
Oleksii Zhurba020fab42017-11-01 20:13:28 +00006
7
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +03008@pytest.mark.usefixtures('check_kibana')
Oleksii Zhurba020fab42017-11-01 20:13:28 +00009def test_elasticsearch_cluster(local_salt_client):
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +000010 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060011 'kibana:server',
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +000012 'pillar.get',
13 ['_param:haproxy_elasticsearch_bind_host'],
14 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050015
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050016 proxies = {"http": None, "https": None}
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000017 for node in salt_output.keys():
18 IP = salt_output[node]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050019 assert requests.get('http://{}:9200/'.format(IP),
20 proxies=proxies).status_code == 200, \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000021 'Cannot check elasticsearch url on {}.'.format(IP)
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050022 resp = requests.get('http://{}:9200/_cat/health'.format(IP),
23 proxies=proxies).content
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000024 assert resp.split()[3] == 'green', \
25 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000026 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000027 assert resp.split()[4] == '3', \
28 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000029 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000030 assert resp.split()[5] == '3', \
31 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000032 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000033 assert resp.split()[10] == '0', \
34 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000035 json.dumps(resp, indent=4))
Oleksii Zhurbab31323f2017-11-20 15:35:19 -060036 assert resp.split()[13] == '100.0%', \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000037 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000038 json.dumps(resp, indent=4))
Oleksii Zhurba020fab42017-11-01 20:13:28 +000039
40
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030041@pytest.mark.usefixtures('check_kibana')
Ievgeniia Zadorozhna511f0ce2018-11-08 17:43:10 +030042def test_kibana_status(local_salt_client):
43 proxies = {"http": None, "https": None}
44 IP = utils.get_monitoring_ip('stacklight_log_address')
45 resp = requests.get('http://{}:5601/api/status'.format(IP),
46 proxies=proxies).content
47 body = json.loads(resp)
48 assert body['status']['overall']['state'] == "green", \
49 "Kibana status is not expected: {}".format(
50 body['status']['overall'])
51 for i in body['status']['statuses']:
52 assert i['state'] == "green", \
53 "Kibana statuses are unexpected: {}".format(i)
54
55
56@pytest.mark.usefixtures('check_kibana')
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000057def test_elasticsearch_node_count(local_salt_client):
58 now = datetime.datetime.now()
59 today = now.strftime("%Y.%m.%d")
60 active_nodes = utils.get_active_nodes()
61 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060062 'kibana:server',
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000063 'pillar.get',
64 ['_param:haproxy_elasticsearch_bind_host'],
65 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050066
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000067 IP = salt_output.values()[0]
Tatyana Leontovich30bd90c2019-01-11 16:26:32 +020068 headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050069 proxies = {"http": None, "https": None}
Tatyana Leontovich30bd90c2019-01-11 16:26:32 +020070 data = ('{"size": 0, "aggs": '
71 '{"uniq_hostname": '
Ievgeniia Zadorozhna03af2922019-01-28 15:55:02 +030072 '{"terms": {"size": 1000, '
Tatyana Leontovich30bd90c2019-01-11 16:26:32 +020073 '"field": "Hostname.keyword"}}}}')
74 response = requests.post(
75 'http://{0}:9200/log-{1}/_search?pretty'.format(IP, today),
76 proxies=proxies,
77 headers=headers,
78 data=data)
79 assert 200 == response.status_code, 'Unexpected code {}'.format(
80 response.text)
81 resp = json.loads(response.text)
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060082 cluster_domain = local_salt_client.cmd('salt:control',
83 'pillar.get',
84 ['_param:cluster_domain'],
85 expr_form='pillar').values()[0]
86 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050087 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060088 node_name = item_['key']
89 monitored_nodes.append(node_name + '.' + cluster_domain)
90 missing_nodes = []
91 for node in active_nodes.keys():
92 if node not in monitored_nodes:
93 missing_nodes.append(node)
94 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000095 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060096 'expected {1}. Missing nodes: \n{2}'. \
97 format(len(monitored_nodes), len(active_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000098
99
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000100def test_stacklight_services_replicas(local_salt_client):
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300101 # TODO
102 # change to docker:swarm:role:master ?
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000103 salt_output = local_salt_client.cmd(
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300104 'I@docker:client:stack:monitoring and I@prometheus:server',
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000105 'cmd.run',
106 ['docker service ls'],
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300107 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500108
109 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300110 pytest.skip("docker:client:stack:monitoring or \
111 prometheus:server pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500112
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000113 wrong_items = []
114 for line in salt_output[salt_output.keys()[0]].split('\n'):
115 if line[line.find('/') - 1] != line[line.find('/') + 1] \
116 and 'replicated' in line:
117 wrong_items.append(line)
118 assert len(wrong_items) == 0, \
119 '''Some monitoring services doesn't have expected number of replicas:
120 {}'''.format(json.dumps(wrong_items, indent=4))
121
122
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300123@pytest.mark.usefixtures('check_prometheus')
Oleksii Zhurba25215d92019-01-31 16:35:57 -0600124def test_prometheus_alert_count(local_salt_client, ctl_nodes_pillar):
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000125 IP = utils.get_monitoring_ip('cluster_public_host')
126 # keystone:server can return 3 nodes instead of 1
127 # this will be fixed later
128 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000129 nodes_info = local_salt_client.cmd(
Oleksii Zhurba25215d92019-01-31 16:35:57 -0600130 ctl_nodes_pillar,
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000131 'cmd.run',
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000132 ['curl -s http://{}:15010/alerts | grep icon-chevron-down | '
133 'grep -v "0 active"'.format(IP)],
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000134 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500135
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000136 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
137 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
138 assert result == '', 'AlertManager page has some alerts! {}'.format(
139 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000140
141
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000142def test_stacklight_containers_status(local_salt_client):
143 salt_output = local_salt_client.cmd(
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300144 'I@docker:swarm:role:master and I@prometheus:server',
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000145 'cmd.run',
146 ['docker service ps $(docker stack services -q monitoring)'],
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300147 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500148
149 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300150 pytest.skip("docker:swarm:role:master or prometheus:server \
151 pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500152
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000153 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000154 # for old reclass models, docker:swarm:role:master can return
155 # 2 nodes instead of one. Here is temporary fix.
156 # TODO
157 if len(salt_output.keys()) > 1:
158 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
159 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000160 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000161 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000162 if line.split()[1] == '\\_':
163 shift = 1
164 if line.split()[1 + shift] not in result.keys():
165 result[line.split()[1]] = 'NOT OK'
166 if line.split()[4 + shift] == 'Running' \
167 or line.split()[4 + shift] == 'Ready':
168 result[line.split()[1 + shift]] = 'OK'
169 assert 'NOT OK' not in result.values(), \
170 '''Some containers are in incorrect state:
171 {}'''.format(json.dumps(result, indent=4))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500172
173
174def test_running_telegraf_services(local_salt_client):
175 salt_output = local_salt_client.cmd('telegraf:agent',
176 'service.status',
177 'telegraf',
178 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500179
180 if not salt_output:
181 pytest.skip("Telegraf or telegraf:agent \
182 pillar are not found on this environment.")
183
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500184 result = [{node: status} for node, status
185 in salt_output.items()
186 if status is False]
187 assert result == [], 'Telegraf service is not running ' \
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500188 'on following nodes: {}'.format(result)
Ievgeniia Zadorozhna6775eb72018-11-09 19:50:04 +0300189
190
191def test_running_fluentd_services(local_salt_client):
192 salt_output = local_salt_client.cmd('fluentd:agent',
193 'service.status',
194 'td-agent',
195 expr_form='pillar')
196 result = [{node: status} for node, status
197 in salt_output.items()
198 if status is False]
199 assert result == [], 'Fluentd check failed: td-agent service is not ' \
200 'running on following nodes:'.format(result)