blob: 703deeaca62404f55258e185f301c0adee7c9951 [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
Oleksii Zhurba020fab42017-11-01 20:13:28 +00005
6
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +03007@pytest.mark.usefixtures('check_kibana')
Oleksii Zhurba020fab42017-11-01 20:13:28 +00008def test_elasticsearch_cluster(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03009 salt_output = local_salt_client.pillar_get(
10 tgt='kibana:server',
11 param='_param:haproxy_elasticsearch_bind_host')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050012
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050013 proxies = {"http": None, "https": None}
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030014 IP = salt_output
15 assert requests.get('http://{}:9200/'.format(IP),
16 proxies=proxies).status_code == 200, \
17 'Cannot check elasticsearch url on {}.'.format(IP)
18 resp = requests.get('http://{}:9200/_cat/health'.format(IP),
19 proxies=proxies).content
20 assert resp.split()[3] == 'green', \
21 'elasticsearch status is not good {}'.format(
22 json.dumps(resp, indent=4))
23 assert resp.split()[4] == '3', \
24 'elasticsearch status is not good {}'.format(
25 json.dumps(resp, indent=4))
26 assert resp.split()[5] == '3', \
27 'elasticsearch status is not good {}'.format(
28 json.dumps(resp, indent=4))
29 assert resp.split()[10] == '0', \
30 'elasticsearch status is not good {}'.format(
31 json.dumps(resp, indent=4))
32 assert resp.split()[13] == '100.0%', \
33 'elasticsearch status is not good {}'.format(
34 json.dumps(resp, indent=4))
Oleksii Zhurba020fab42017-11-01 20:13:28 +000035
36
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030037@pytest.mark.usefixtures('check_kibana')
Ievgeniia Zadorozhna511f0ce2018-11-08 17:43:10 +030038def test_kibana_status(local_salt_client):
39 proxies = {"http": None, "https": None}
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030040 IP = local_salt_client.pillar_get(param='_param:stacklight_log_address')
Ievgeniia Zadorozhna511f0ce2018-11-08 17:43:10 +030041 resp = requests.get('http://{}:5601/api/status'.format(IP),
42 proxies=proxies).content
43 body = json.loads(resp)
44 assert body['status']['overall']['state'] == "green", \
45 "Kibana status is not expected: {}".format(
46 body['status']['overall'])
47 for i in body['status']['statuses']:
48 assert i['state'] == "green", \
49 "Kibana statuses are unexpected: {}".format(i)
50
51
52@pytest.mark.usefixtures('check_kibana')
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000053def test_elasticsearch_node_count(local_salt_client):
54 now = datetime.datetime.now()
55 today = now.strftime("%Y.%m.%d")
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030056 salt_output = local_salt_client.pillar_get(
57 tgt='kibana:server',
58 param='_param:haproxy_elasticsearch_bind_host')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050059
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030060 IP = salt_output
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020061 headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050062 proxies = {"http": None, "https": None}
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020063 data = ('{"size": 0, "aggs": '
64 '{"uniq_hostname": '
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030065 '{"terms": {"size": 1000, '
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020066 '"field": "Hostname.keyword"}}}}')
67 response = requests.post(
68 'http://{0}:9200/log-{1}/_search?pretty'.format(IP, today),
69 proxies=proxies,
70 headers=headers,
71 data=data)
72 assert 200 == response.status_code, 'Unexpected code {}'.format(
73 response.text)
74 resp = json.loads(response.text)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030075 cluster_domain = local_salt_client.pillar_get(param='_param:cluster_domain')
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060076 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050077 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060078 node_name = item_['key']
79 monitored_nodes.append(node_name + '.' + cluster_domain)
80 missing_nodes = []
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030081 all_nodes = local_salt_client.test_ping(tgt='*').keys()
82 for node in all_nodes:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060083 if node not in monitored_nodes:
84 missing_nodes.append(node)
85 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000086 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060087 'expected {1}. Missing nodes: \n{2}'. \
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030088 format(len(monitored_nodes), len(all_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000089
90
Oleksii Zhurba020fab42017-11-01 20:13:28 +000091def test_stacklight_services_replicas(local_salt_client):
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030092 # TODO
93 # change to docker:swarm:role:master ?
Oleksii Zhurba020fab42017-11-01 20:13:28 +000094 salt_output = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030095 tgt='I@docker:client:stack:monitoring and I@prometheus:server',
96 param='docker service ls',
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030097 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050098
99 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300100 pytest.skip("docker:client:stack:monitoring or \
101 prometheus:server pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500102
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000103 wrong_items = []
104 for line in salt_output[salt_output.keys()[0]].split('\n'):
105 if line[line.find('/') - 1] != line[line.find('/') + 1] \
106 and 'replicated' in line:
107 wrong_items.append(line)
108 assert len(wrong_items) == 0, \
109 '''Some monitoring services doesn't have expected number of replicas:
110 {}'''.format(json.dumps(wrong_items, indent=4))
111
112
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300113@pytest.mark.usefixtures('check_prometheus')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -0600114def test_prometheus_alert_count(local_salt_client, ctl_nodes_pillar):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300115 IP = local_salt_client.pillar_get(param='_param:cluster_public_host')
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000116 # keystone:server can return 3 nodes instead of 1
117 # this will be fixed later
118 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000119 nodes_info = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300120 tgt=ctl_nodes_pillar,
121 param='curl -s http://{}:15010/alerts | grep icon-chevron-down | '
122 'grep -v "0 active"'.format(IP),
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000123 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500124
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000125 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
126 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
127 assert result == '', 'AlertManager page has some alerts! {}'.format(
128 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000129
130
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000131def test_stacklight_containers_status(local_salt_client):
132 salt_output = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300133 tgt='I@docker:swarm:role:master and I@prometheus:server',
134 param='docker service ps $(docker stack services -q monitoring)',
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300135 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500136
137 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300138 pytest.skip("docker:swarm:role:master or prometheus:server \
139 pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500140
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000141 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000142 # for old reclass models, docker:swarm:role:master can return
143 # 2 nodes instead of one. Here is temporary fix.
144 # TODO
145 if len(salt_output.keys()) > 1:
146 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
147 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000148 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000149 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000150 if line.split()[1] == '\\_':
151 shift = 1
152 if line.split()[1 + shift] not in result.keys():
153 result[line.split()[1]] = 'NOT OK'
154 if line.split()[4 + shift] == 'Running' \
155 or line.split()[4 + shift] == 'Ready':
156 result[line.split()[1 + shift]] = 'OK'
157 assert 'NOT OK' not in result.values(), \
158 '''Some containers are in incorrect state:
159 {}'''.format(json.dumps(result, indent=4))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500160
161
162def test_running_telegraf_services(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300163 salt_output = local_salt_client.cmd(tgt='telegraf:agent',
164 fun='service.status',
165 param='telegraf',
166 expr_form='pillar',)
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500167
168 if not salt_output:
169 pytest.skip("Telegraf or telegraf:agent \
170 pillar are not found on this environment.")
171
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500172 result = [{node: status} for node, status
173 in salt_output.items()
174 if status is False]
175 assert result == [], 'Telegraf service is not running ' \
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500176 'on following nodes: {}'.format(result)
Ievgeniia Zadorozhna6775eb72018-11-09 19:50:04 +0300177
178
179def test_running_fluentd_services(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300180 salt_output = local_salt_client.cmd(tgt='fluentd:agent',
181 fun='service.status',
182 param='td-agent',
Ievgeniia Zadorozhna6775eb72018-11-09 19:50:04 +0300183 expr_form='pillar')
184 result = [{node: status} for node, status
185 in salt_output.items()
186 if status is False]
187 assert result == [], 'Fluentd check failed: td-agent service is not ' \
188 'running on following nodes:'.format(result)