blob: 80917f1d91b5b9a6bf448305a849df50786288cf [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 Zhurba468e6c72018-01-16 17:43:15 +00005from cvp_checks import utils
Oleksii Zhurba020fab42017-11-01 20:13:28 +00006
7
8def test_elasticsearch_cluster(local_salt_client):
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +00009 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060010 'kibana:server',
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +000011 'pillar.get',
12 ['_param:haproxy_elasticsearch_bind_host'],
13 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050014
15 if not salt_output:
16 pytest.skip("Kibana service or kibana:server pillar \
17 are not found on this environment.")
18
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050019 proxies = {"http": None, "https": None}
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000020 for node in salt_output.keys():
21 IP = salt_output[node]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050022 assert requests.get('http://{}:9200/'.format(IP),
23 proxies=proxies).status_code == 200, \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000024 'Cannot check elasticsearch url on {}.'.format(IP)
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050025 resp = requests.get('http://{}:9200/_cat/health'.format(IP),
26 proxies=proxies).content
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000027 assert resp.split()[3] == 'green', \
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()[4] == '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()[5] == '3', \
34 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000035 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000036 assert resp.split()[10] == '0', \
37 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000038 json.dumps(resp, indent=4))
Oleksii Zhurbab31323f2017-11-20 15:35:19 -060039 assert resp.split()[13] == '100.0%', \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000040 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000041 json.dumps(resp, indent=4))
Oleksii Zhurba020fab42017-11-01 20:13:28 +000042
43
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000044def test_elasticsearch_node_count(local_salt_client):
45 now = datetime.datetime.now()
46 today = now.strftime("%Y.%m.%d")
47 active_nodes = utils.get_active_nodes()
48 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060049 'kibana:server',
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000050 'pillar.get',
51 ['_param:haproxy_elasticsearch_bind_host'],
52 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050053
54 if not salt_output:
55 pytest.skip("Kibana service or kibana:server pillar \
56 are not found on this environment.")
57
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000058 IP = salt_output.values()[0]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050059 proxies = {"http": None, "https": None}
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060060 resp = json.loads(requests.post('http://{0}:9200/log-{1}/_search?pretty'.
61 format(IP, today),
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050062 proxies=proxies,
Oleksii Zhurba7f463412018-03-21 16:32:44 -050063 data='{"size": 0, "aggs": '
64 '{"uniq_hostname": '
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060065 '{"terms": {"size": 500, '
Oleksii Zhurba7f463412018-03-21 16:32:44 -050066 '"field": "Hostname.keyword"}}}}').text)
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060067 cluster_domain = local_salt_client.cmd('salt:control',
68 'pillar.get',
69 ['_param:cluster_domain'],
70 expr_form='pillar').values()[0]
71 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050072 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060073 node_name = item_['key']
74 monitored_nodes.append(node_name + '.' + cluster_domain)
75 missing_nodes = []
76 for node in active_nodes.keys():
77 if node not in monitored_nodes:
78 missing_nodes.append(node)
79 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000080 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060081 'expected {1}. Missing nodes: \n{2}'. \
82 format(len(monitored_nodes), len(active_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000083
84
Oleksii Zhurba020fab42017-11-01 20:13:28 +000085def test_stacklight_services_replicas(local_salt_client):
86 salt_output = local_salt_client.cmd(
87 'docker:client:stack:monitoring',
88 'cmd.run',
89 ['docker service ls'],
90 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050091
92 if not salt_output:
93 pytest.skip("Docker replicas or \
94 docker:client:stack:monitoring pillar \
95 are not found on this environment.")
96
Oleksii Zhurba020fab42017-11-01 20:13:28 +000097 wrong_items = []
98 for line in salt_output[salt_output.keys()[0]].split('\n'):
99 if line[line.find('/') - 1] != line[line.find('/') + 1] \
100 and 'replicated' in line:
101 wrong_items.append(line)
102 assert len(wrong_items) == 0, \
103 '''Some monitoring services doesn't have expected number of replicas:
104 {}'''.format(json.dumps(wrong_items, indent=4))
105
106
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000107def test_prometheus_alert_count(local_salt_client):
108 IP = utils.get_monitoring_ip('cluster_public_host')
109 # keystone:server can return 3 nodes instead of 1
110 # this will be fixed later
111 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000112 nodes_info = local_salt_client.cmd(
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000113 'keystone:server',
114 'cmd.run',
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000115 ['curl -s http://{}:15010/alerts | grep icon-chevron-down | '
116 'grep -v "0 active"'.format(IP)],
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000117 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500118
119 if not nodes_info:
120 pytest.skip("Prometheus server url or keystone:server \
121 pillar are not found on this environment.")
122
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000123 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
124 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
125 assert result == '', 'AlertManager page has some alerts! {}'.format(
126 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000127
128
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000129def test_stacklight_containers_status(local_salt_client):
130 salt_output = local_salt_client.cmd(
131 'docker:swarm:role:master',
132 'cmd.run',
133 ['docker service ps $(docker stack services -q monitoring)'],
134 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500135
136 if not salt_output:
137 pytest.skip("Docker or docker:swarm:role:master \
138 pillar are not found on this environment.")
139
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000140 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000141 # for old reclass models, docker:swarm:role:master can return
142 # 2 nodes instead of one. Here is temporary fix.
143 # TODO
144 if len(salt_output.keys()) > 1:
145 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
146 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000147 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000148 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000149 if line.split()[1] == '\\_':
150 shift = 1
151 if line.split()[1 + shift] not in result.keys():
152 result[line.split()[1]] = 'NOT OK'
153 if line.split()[4 + shift] == 'Running' \
154 or line.split()[4 + shift] == 'Ready':
155 result[line.split()[1 + shift]] = 'OK'
156 assert 'NOT OK' not in result.values(), \
157 '''Some containers are in incorrect state:
158 {}'''.format(json.dumps(result, indent=4))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500159
160
161def test_running_telegraf_services(local_salt_client):
162 salt_output = local_salt_client.cmd('telegraf:agent',
163 'service.status',
164 'telegraf',
165 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500166
167 if not salt_output:
168 pytest.skip("Telegraf or telegraf:agent \
169 pillar are not found on this environment.")
170
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500171 result = [{node: status} for node, status
172 in salt_output.items()
173 if status is False]
174 assert result == [], 'Telegraf service is not running ' \
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500175 'on following nodes: {}'.format(result)