blob: d554a917eed434e1c20e7f5d00b2420388f1eb4f [file] [log] [blame]
Oleksii Zhurba020fab42017-11-01 20:13:28 +00001import json
2import requests
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +00003import datetime
Oleksii Zhurba468e6c72018-01-16 17:43:15 +00004from cvp_checks import utils
Oleksii Zhurba020fab42017-11-01 20:13:28 +00005
6
7def test_elasticsearch_cluster(local_salt_client):
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +00008 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -06009 'kibana:server',
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +000010 'pillar.get',
11 ['_param:haproxy_elasticsearch_bind_host'],
12 expr_form='pillar')
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000013 for node in salt_output.keys():
14 IP = salt_output[node]
15 assert requests.get('http://{}:9200/'.format(IP)).status_code == 200, \
16 'Cannot check elasticsearch url on {}.'.format(IP)
17 resp = requests.get('http://{}:9200/_cat/health'.format(IP)).content
18 assert resp.split()[3] == 'green', \
19 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000020 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000021 assert resp.split()[4] == '3', \
22 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000023 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000024 assert resp.split()[5] == '3', \
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()[10] == '0', \
28 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000029 json.dumps(resp, indent=4))
Oleksii Zhurbab31323f2017-11-20 15:35:19 -060030 assert resp.split()[13] == '100.0%', \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000031 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000032 json.dumps(resp, indent=4))
Oleksii Zhurba020fab42017-11-01 20:13:28 +000033
34
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000035def test_elasticsearch_node_count(local_salt_client):
36 now = datetime.datetime.now()
37 today = now.strftime("%Y.%m.%d")
38 active_nodes = utils.get_active_nodes()
39 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060040 'kibana:server',
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000041 'pillar.get',
42 ['_param:haproxy_elasticsearch_bind_host'],
43 expr_form='pillar')
44 IP = salt_output.values()[0]
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060045 resp = json.loads(requests.post('http://{0}:9200/log-{1}/_search?pretty'.
46 format(IP, today),
Oleksii Zhurba7f463412018-03-21 16:32:44 -050047 data='{"size": 0, "aggs": '
48 '{"uniq_hostname": '
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060049 '{"terms": {"size": 500, '
Oleksii Zhurba7f463412018-03-21 16:32:44 -050050 '"field": "Hostname.keyword"}}}}').text)
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060051 cluster_domain = local_salt_client.cmd('salt:control',
52 'pillar.get',
53 ['_param:cluster_domain'],
54 expr_form='pillar').values()[0]
55 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050056 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060057 node_name = item_['key']
58 monitored_nodes.append(node_name + '.' + cluster_domain)
59 missing_nodes = []
60 for node in active_nodes.keys():
61 if node not in monitored_nodes:
62 missing_nodes.append(node)
63 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000064 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060065 'expected {1}. Missing nodes: \n{2}'. \
66 format(len(monitored_nodes), len(active_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000067
68
Oleksii Zhurba020fab42017-11-01 20:13:28 +000069def test_stacklight_services_replicas(local_salt_client):
70 salt_output = local_salt_client.cmd(
71 'docker:client:stack:monitoring',
72 'cmd.run',
73 ['docker service ls'],
74 expr_form='pillar')
75 wrong_items = []
76 for line in salt_output[salt_output.keys()[0]].split('\n'):
77 if line[line.find('/') - 1] != line[line.find('/') + 1] \
78 and 'replicated' in line:
79 wrong_items.append(line)
80 assert len(wrong_items) == 0, \
81 '''Some monitoring services doesn't have expected number of replicas:
82 {}'''.format(json.dumps(wrong_items, indent=4))
83
84
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000085def test_prometheus_alert_count(local_salt_client):
86 IP = utils.get_monitoring_ip('cluster_public_host')
87 # keystone:server can return 3 nodes instead of 1
88 # this will be fixed later
89 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000090 nodes_info = local_salt_client.cmd(
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000091 'keystone:server',
92 'cmd.run',
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000093 ['curl -s http://{}:15010/alerts | grep icon-chevron-down | '
94 'grep -v "0 active"'.format(IP)],
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000095 expr_form='pillar')
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000096 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
97 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
98 assert result == '', 'AlertManager page has some alerts! {}'.format(
99 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000100
101
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000102def test_stacklight_containers_status(local_salt_client):
103 salt_output = local_salt_client.cmd(
104 'docker:swarm:role:master',
105 'cmd.run',
106 ['docker service ps $(docker stack services -q monitoring)'],
107 expr_form='pillar')
108 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000109 # for old reclass models, docker:swarm:role:master can return
110 # 2 nodes instead of one. Here is temporary fix.
111 # TODO
112 if len(salt_output.keys()) > 1:
113 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
114 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000115 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000116 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000117 if line.split()[1] == '\\_':
118 shift = 1
119 if line.split()[1 + shift] not in result.keys():
120 result[line.split()[1]] = 'NOT OK'
121 if line.split()[4 + shift] == 'Running' \
122 or line.split()[4 + shift] == 'Ready':
123 result[line.split()[1 + shift]] = 'OK'
124 assert 'NOT OK' not in result.values(), \
125 '''Some containers are in incorrect state:
126 {}'''.format(json.dumps(result, indent=4))