blob: 91ad4efcaab2df4b86b6418e6d593c0db62670ca [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(
9 'elasticsearch:client',
10 '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(
40 'elasticsearch:client',
41 'pillar.get',
42 ['_param:haproxy_elasticsearch_bind_host'],
43 expr_form='pillar')
44 IP = salt_output.values()[0]
45 resp = requests.post('http://{0}:9200/log-{1}/_search?pretty'.
46 format(IP, today), data='{"size": 500, "aggs": '
47 '{"group_by_hostname": {"terms": {"size": 500, '
48 '"field": "Hostname"}}}}').text
49 # TODO
50 # we should check every node in output, not simply # of entries
51 assert resp.count('"key"') > len(active_nodes), \
52 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
53 'expected {1}.'.format(resp.count('"key"'), len(active_nodes))
54
55
Oleksii Zhurba020fab42017-11-01 20:13:28 +000056def test_stacklight_services_replicas(local_salt_client):
57 salt_output = local_salt_client.cmd(
58 'docker:client:stack:monitoring',
59 'cmd.run',
60 ['docker service ls'],
61 expr_form='pillar')
62 wrong_items = []
63 for line in salt_output[salt_output.keys()[0]].split('\n'):
64 if line[line.find('/') - 1] != line[line.find('/') + 1] \
65 and 'replicated' in line:
66 wrong_items.append(line)
67 assert len(wrong_items) == 0, \
68 '''Some monitoring services doesn't have expected number of replicas:
69 {}'''.format(json.dumps(wrong_items, indent=4))
70
71
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000072def test_prometheus_alert_count(local_salt_client):
73 IP = utils.get_monitoring_ip('cluster_public_host')
74 # keystone:server can return 3 nodes instead of 1
75 # this will be fixed later
76 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000077 nodes_info = local_salt_client.cmd(
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000078 'keystone:server',
79 'cmd.run',
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000080 ['curl -s http://{}:15010/alerts | grep icon-chevron-down | '
81 'grep -v "0 active"'.format(IP)],
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000082 expr_form='pillar')
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000083 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
84 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
85 assert result == '', 'AlertManager page has some alerts! {}'.format(
86 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000087
88
Oleksii Zhurba020fab42017-11-01 20:13:28 +000089def test_stacklight_containers_status(local_salt_client):
90 salt_output = local_salt_client.cmd(
91 'docker:swarm:role:master',
92 'cmd.run',
93 ['docker service ps $(docker stack services -q monitoring)'],
94 expr_form='pillar')
95 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000096 # for old reclass models, docker:swarm:role:master can return
97 # 2 nodes instead of one. Here is temporary fix.
98 # TODO
99 if len(salt_output.keys()) > 1:
100 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
101 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000102 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000103 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000104 if line.split()[1] == '\\_':
105 shift = 1
106 if line.split()[1 + shift] not in result.keys():
107 result[line.split()[1]] = 'NOT OK'
108 if line.split()[4 + shift] == 'Running' \
109 or line.split()[4 + shift] == 'Ready':
110 result[line.split()[1 + shift]] = 'OK'
111 assert 'NOT OK' not in result.values(), \
112 '''Some containers are in incorrect state:
113 {}'''.format(json.dumps(result, indent=4))