blob: 84624a942863d74f9c616adbf100f58a4da5dcd5 [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
Oleksii Zhurba23c18332019-05-09 18:53:40 -05006@pytest.mark.sl_dup
7#ElasticsearchClusterHealthStatusMajor or stacklight-pytest
8@pytest.mark.full
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +03009@pytest.mark.usefixtures('check_kibana')
Oleksii Zhurba020fab42017-11-01 20:13:28 +000010def test_elasticsearch_cluster(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030011 salt_output = local_salt_client.pillar_get(
12 tgt='kibana:server',
13 param='_param:haproxy_elasticsearch_bind_host')
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -050014 ssl = local_salt_client.pillar_get(
15 tgt='elasticsearch:server',
16 param='haproxy:proxy:listen:elasticsearch:binds:ssl:enabled')
17 proto = "https" if ssl == "True" else "http"
Oleksii Zhurba9848e212018-09-05 10:53:51 -050018
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050019 proxies = {"http": None, "https": None}
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030020 IP = salt_output
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -050021 assert requests.get('{0}://{1}:9200/'.format(proto, IP),
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030022 proxies=proxies).status_code == 200, \
23 'Cannot check elasticsearch url on {}.'.format(IP)
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -050024 resp = requests.get('{0}://{1}:9200/_cat/health'.format(proto, IP),
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030025 proxies=proxies).content
26 assert resp.split()[3] == 'green', \
27 'elasticsearch status is not good {}'.format(
28 json.dumps(resp, indent=4))
29 assert resp.split()[4] == '3', \
30 'elasticsearch status is not good {}'.format(
31 json.dumps(resp, indent=4))
32 assert resp.split()[5] == '3', \
33 'elasticsearch status is not good {}'.format(
34 json.dumps(resp, indent=4))
35 assert resp.split()[10] == '0', \
36 'elasticsearch status is not good {}'.format(
37 json.dumps(resp, indent=4))
38 assert resp.split()[13] == '100.0%', \
39 'elasticsearch status is not good {}'.format(
40 json.dumps(resp, indent=4))
Oleksii Zhurba020fab42017-11-01 20:13:28 +000041
42
Oleksii Zhurba23c18332019-05-09 18:53:40 -050043@pytest.mark.sl_dup
44#stacklight-pytest
45@pytest.mark.full
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030046@pytest.mark.usefixtures('check_kibana')
Ievgeniia Zadorozhna511f0ce2018-11-08 17:43:10 +030047def test_kibana_status(local_salt_client):
48 proxies = {"http": None, "https": None}
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030049 IP = local_salt_client.pillar_get(param='_param:stacklight_log_address')
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -050050 ssl = local_salt_client.pillar_get(
51 tgt='kibana:server',
52 param='haproxy:proxy:listen:kibana:binds:ssl:enabled')
53 proto = "https" if ssl == "True" else "http"
54
55 resp = requests.get('{0}://{1}:5601/api/status'.format(proto, IP),
Ievgeniia Zadorozhna511f0ce2018-11-08 17:43:10 +030056 proxies=proxies).content
57 body = json.loads(resp)
58 assert body['status']['overall']['state'] == "green", \
59 "Kibana status is not expected: {}".format(
60 body['status']['overall'])
61 for i in body['status']['statuses']:
62 assert i['state'] == "green", \
63 "Kibana statuses are unexpected: {}".format(i)
64
65
Oleksii Zhurba23c18332019-05-09 18:53:40 -050066@pytest.mark.smoke
67#TODO: recheck
Ievgeniia Zadorozhna511f0ce2018-11-08 17:43:10 +030068@pytest.mark.usefixtures('check_kibana')
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000069def test_elasticsearch_node_count(local_salt_client):
70 now = datetime.datetime.now()
71 today = now.strftime("%Y.%m.%d")
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030072 salt_output = local_salt_client.pillar_get(
73 tgt='kibana:server',
74 param='_param:haproxy_elasticsearch_bind_host')
Oleksii Zhurba9848e212018-09-05 10:53:51 -050075
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030076 IP = salt_output
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -050077 ssl = local_salt_client.pillar_get(
78 tgt='elasticsearch:server',
79 param='haproxy:proxy:listen:elasticsearch:binds:ssl:enabled')
80 proto = "https" if ssl == "True" else "http"
81
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020082 headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050083 proxies = {"http": None, "https": None}
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020084 data = ('{"size": 0, "aggs": '
85 '{"uniq_hostname": '
Oleksii Zhurba23c18332019-05-09 18:53:40 -050086 '{"terms": {"size": 500, '
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020087 '"field": "Hostname.keyword"}}}}')
88 response = requests.post(
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -050089 '{0}://{1}:9200/log-{2}/_search?pretty'.format(proto, IP, today),
Tatyana Leontoviched57cb02019-01-11 16:26:32 +020090 proxies=proxies,
91 headers=headers,
92 data=data)
93 assert 200 == response.status_code, 'Unexpected code {}'.format(
94 response.text)
95 resp = json.loads(response.text)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030096 cluster_domain = local_salt_client.pillar_get(param='_param:cluster_domain')
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060097 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050098 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060099 node_name = item_['key']
100 monitored_nodes.append(node_name + '.' + cluster_domain)
101 missing_nodes = []
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300102 all_nodes = local_salt_client.test_ping(tgt='*').keys()
103 for node in all_nodes:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -0600104 if node not in monitored_nodes:
105 missing_nodes.append(node)
106 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +0000107 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -0600108 'expected {1}. Missing nodes: \n{2}'. \
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300109 format(len(monitored_nodes), len(all_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +0000110
111
Oleksii Zhurba23c18332019-05-09 18:53:40 -0500112@pytest.mark.sl_dup
113#DockerServiceMonitoring*
114@pytest.mark.full
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000115def test_stacklight_services_replicas(local_salt_client):
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300116 # TODO
117 # change to docker:swarm:role:master ?
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000118 salt_output = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300119 tgt='I@docker:client:stack:monitoring and I@prometheus:server',
120 param='docker service ls',
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300121 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500122
123 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300124 pytest.skip("docker:client:stack:monitoring or \
125 prometheus:server pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500126
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000127 wrong_items = []
128 for line in salt_output[salt_output.keys()[0]].split('\n'):
129 if line[line.find('/') - 1] != line[line.find('/') + 1] \
130 and 'replicated' in line:
131 wrong_items.append(line)
132 assert len(wrong_items) == 0, \
133 '''Some monitoring services doesn't have expected number of replicas:
134 {}'''.format(json.dumps(wrong_items, indent=4))
135
136
Oleksii Zhurba23c18332019-05-09 18:53:40 -0500137@pytest.mark.smoke
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300138@pytest.mark.usefixtures('check_prometheus')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -0600139def test_prometheus_alert_count(local_salt_client, ctl_nodes_pillar):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300140 IP = local_salt_client.pillar_get(param='_param:cluster_public_host')
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -0500141 proto = local_salt_client.pillar_get(
142 param='_param:cluster_public_protocol')
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000143 # keystone:server can return 3 nodes instead of 1
144 # this will be fixed later
145 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000146 nodes_info = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300147 tgt=ctl_nodes_pillar,
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -0500148 param='curl -s {0}://{1}:15010/alerts | grep icon-chevron-down | '
149 'grep -v "0 active"'.format(proto, IP),
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000150 expr_form='pillar')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500151
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000152 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
153 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
154 assert result == '', 'AlertManager page has some alerts! {}'.format(
155 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000156
157
Oleksii Zhurba23c18332019-05-09 18:53:40 -0500158@pytest.mark.sl_dup
159#DockerServiceMonitoring* ??
160@pytest.mark.full
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000161def test_stacklight_containers_status(local_salt_client):
162 salt_output = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300163 tgt='I@docker:swarm:role:master and I@prometheus:server',
164 param='docker service ps $(docker stack services -q monitoring)',
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300165 expr_form='compound')
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500166
167 if not salt_output:
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300168 pytest.skip("docker:swarm:role:master or prometheus:server \
169 pillars are not found on this environment.")
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500170
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000171 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000172 # for old reclass models, docker:swarm:role:master can return
173 # 2 nodes instead of one. Here is temporary fix.
174 # TODO
175 if len(salt_output.keys()) > 1:
176 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
177 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000178 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000179 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000180 if line.split()[1] == '\\_':
181 shift = 1
182 if line.split()[1 + shift] not in result.keys():
183 result[line.split()[1]] = 'NOT OK'
184 if line.split()[4 + shift] == 'Running' \
185 or line.split()[4 + shift] == 'Ready':
186 result[line.split()[1 + shift]] = 'OK'
187 assert 'NOT OK' not in result.values(), \
188 '''Some containers are in incorrect state:
189 {}'''.format(json.dumps(result, indent=4))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500190
191
Oleksii Zhurba23c18332019-05-09 18:53:40 -0500192@pytest.mark.sl_dup
193#PrometheusTargetDown
194@pytest.mark.full
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500195def test_running_telegraf_services(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300196 salt_output = local_salt_client.cmd(tgt='telegraf:agent',
197 fun='service.status',
198 param='telegraf',
199 expr_form='pillar',)
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500200
201 if not salt_output:
202 pytest.skip("Telegraf or telegraf:agent \
203 pillar are not found on this environment.")
204
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500205 result = [{node: status} for node, status
206 in salt_output.items()
207 if status is False]
208 assert result == [], 'Telegraf service is not running ' \
Oleksii Zhurba9848e212018-09-05 10:53:51 -0500209 'on following nodes: {}'.format(result)
Ievgeniia Zadorozhna6775eb72018-11-09 19:50:04 +0300210
211
Oleksii Zhurba23c18332019-05-09 18:53:40 -0500212@pytest.mark.sl_dup
213#PrometheusTargetDown
214@pytest.mark.full
Ievgeniia Zadorozhna6775eb72018-11-09 19:50:04 +0300215def test_running_fluentd_services(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300216 salt_output = local_salt_client.cmd(tgt='fluentd:agent',
217 fun='service.status',
218 param='td-agent',
Ievgeniia Zadorozhna6775eb72018-11-09 19:50:04 +0300219 expr_form='pillar')
220 result = [{node: status} for node, status
221 in salt_output.items()
222 if status is False]
223 assert result == [], 'Fluentd check failed: td-agent service is not ' \
Oleksii Zhurba7d4f07e2019-06-10 17:30:53 -0500224 'running on following nodes:'.format(result)