blob: 81127d45014d8abd055846fb172f8d7f9921aa1f [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
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')
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000042def test_elasticsearch_node_count(local_salt_client):
43 now = datetime.datetime.now()
44 today = now.strftime("%Y.%m.%d")
45 active_nodes = utils.get_active_nodes()
46 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060047 'kibana:server',
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000048 'pillar.get',
49 ['_param:haproxy_elasticsearch_bind_host'],
50 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050051
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000052 IP = salt_output.values()[0]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050053 proxies = {"http": None, "https": None}
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060054 resp = json.loads(requests.post('http://{0}:9200/log-{1}/_search?pretty'.
55 format(IP, today),
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050056 proxies=proxies,
Oleksii Zhurba7f463412018-03-21 16:32:44 -050057 data='{"size": 0, "aggs": '
58 '{"uniq_hostname": '
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060059 '{"terms": {"size": 500, '
Oleksii Zhurba7f463412018-03-21 16:32:44 -050060 '"field": "Hostname.keyword"}}}}').text)
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060061 cluster_domain = local_salt_client.cmd('salt:control',
62 'pillar.get',
63 ['_param:cluster_domain'],
64 expr_form='pillar').values()[0]
65 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050066 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060067 node_name = item_['key']
68 monitored_nodes.append(node_name + '.' + cluster_domain)
69 missing_nodes = []
70 for node in active_nodes.keys():
71 if node not in monitored_nodes:
72 missing_nodes.append(node)
73 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000074 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060075 'expected {1}. Missing nodes: \n{2}'. \
76 format(len(monitored_nodes), len(active_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000077
78
Oleksii Zhurba020fab42017-11-01 20:13:28 +000079def test_stacklight_services_replicas(local_salt_client):
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030080 # TODO
81 # change to docker:swarm:role:master ?
Oleksii Zhurba020fab42017-11-01 20:13:28 +000082 salt_output = local_salt_client.cmd(
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030083 'I@docker:client:stack:monitoring and I@prometheus:server',
Oleksii Zhurba020fab42017-11-01 20:13:28 +000084 'cmd.run',
85 ['docker service ls'],
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030086 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050087
88 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030089 pytest.skip("docker:client:stack:monitoring or \
90 prometheus:server pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -050091
Oleksii Zhurba020fab42017-11-01 20:13:28 +000092 wrong_items = []
93 for line in salt_output[salt_output.keys()[0]].split('\n'):
94 if line[line.find('/') - 1] != line[line.find('/') + 1] \
95 and 'replicated' in line:
96 wrong_items.append(line)
97 assert len(wrong_items) == 0, \
98 '''Some monitoring services doesn't have expected number of replicas:
99 {}'''.format(json.dumps(wrong_items, indent=4))
100
101
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300102@pytest.mark.usefixtures('check_prometheus')
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000103def test_prometheus_alert_count(local_salt_client):
104 IP = utils.get_monitoring_ip('cluster_public_host')
105 # keystone:server can return 3 nodes instead of 1
106 # this will be fixed later
107 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000108 nodes_info = local_salt_client.cmd(
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000109 'keystone:server',
110 'cmd.run',
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000111 ['curl -s http://{}:15010/alerts | grep icon-chevron-down | '
112 'grep -v "0 active"'.format(IP)],
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000113 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500114
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000115 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
116 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
117 assert result == '', 'AlertManager page has some alerts! {}'.format(
118 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000119
120
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000121def test_stacklight_containers_status(local_salt_client):
122 salt_output = local_salt_client.cmd(
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300123 'I@docker:swarm:role:master and I@prometheus:server',
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000124 'cmd.run',
125 ['docker service ps $(docker stack services -q monitoring)'],
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300126 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500127
128 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300129 pytest.skip("docker:swarm:role:master or prometheus:server \
130 pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500131
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000132 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000133 # for old reclass models, docker:swarm:role:master can return
134 # 2 nodes instead of one. Here is temporary fix.
135 # TODO
136 if len(salt_output.keys()) > 1:
137 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
138 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000139 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000140 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000141 if line.split()[1] == '\\_':
142 shift = 1
143 if line.split()[1 + shift] not in result.keys():
144 result[line.split()[1]] = 'NOT OK'
145 if line.split()[4 + shift] == 'Running' \
146 or line.split()[4 + shift] == 'Ready':
147 result[line.split()[1 + shift]] = 'OK'
148 assert 'NOT OK' not in result.values(), \
149 '''Some containers are in incorrect state:
150 {}'''.format(json.dumps(result, indent=4))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500151
152
153def test_running_telegraf_services(local_salt_client):
154 salt_output = local_salt_client.cmd('telegraf:agent',
155 'service.status',
156 'telegraf',
157 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500158
159 if not salt_output:
160 pytest.skip("Telegraf or telegraf:agent \
161 pillar are not found on this environment.")
162
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500163 result = [{node: status} for node, status
164 in salt_output.items()
165 if status is False]
166 assert result == [], 'Telegraf service is not running ' \
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500167 'on following nodes: {}'.format(result)