Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 1 | import json |
| 2 | import requests |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame] | 3 | import datetime |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 4 | import pytest |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 5 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 6 | @pytest.mark.sl_dup |
| 7 | #ElasticsearchClusterHealthStatusMajor or stacklight-pytest |
| 8 | @pytest.mark.full |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 9 | @pytest.mark.usefixtures('check_kibana') |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 10 | def test_elasticsearch_cluster(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 11 | salt_output = local_salt_client.pillar_get( |
| 12 | tgt='kibana:server', |
| 13 | param='_param:haproxy_elasticsearch_bind_host') |
Oleksii Zhurba | 85f55fe | 2019-06-10 17:30:53 -0500 | [diff] [blame] | 14 | ssl = local_salt_client.pillar_get( |
| 15 | tgt='elasticsearch:server', |
| 16 | param='haproxy:proxy:listen:elasticsearch:binds:ssl:enabled') |
| 17 | proto = "https" if ssl else "http" |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 18 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 19 | proxies = {"http": None, "https": None} |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 20 | IP = salt_output |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 21 | response = requests.get( |
| 22 | '{0}://{1}:9200/'.format(proto, IP), |
| 23 | proxies=proxies, |
| 24 | verify=False) |
| 25 | assert response.status_code == 200, ( |
| 26 | "Issues with accessing elasticsearch on {}.".format(IP)) |
| 27 | response = requests.get( |
| 28 | '{0}://{1}:9200/_cat/health'.format(proto, IP), |
| 29 | proxies=proxies, |
| 30 | verify=False).content |
| 31 | msg = "elasticsearch is not healthy:\n{}".format( |
| 32 | json.dumps(response, indent=4)) |
| 33 | assert response.split()[3] == 'green',msg |
| 34 | assert response.split()[4] == '3', msg |
| 35 | assert response.split()[5] == '3', msg |
| 36 | assert response.split()[10] == '0', msg |
| 37 | assert response.split()[13] == '100.0%', msg |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 38 | |
| 39 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 40 | @pytest.mark.sl_dup |
| 41 | #stacklight-pytest |
| 42 | @pytest.mark.full |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 43 | @pytest.mark.usefixtures('check_kibana') |
Ievgeniia Zadorozhna | 511f0ce | 2018-11-08 17:43:10 +0300 | [diff] [blame] | 44 | def test_kibana_status(local_salt_client): |
| 45 | proxies = {"http": None, "https": None} |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 46 | IP = local_salt_client.pillar_get(param='_param:stacklight_log_address') |
Oleksii Zhurba | 85f55fe | 2019-06-10 17:30:53 -0500 | [diff] [blame] | 47 | ssl = local_salt_client.pillar_get( |
| 48 | tgt='kibana:server', |
| 49 | param='haproxy:proxy:listen:kibana:binds:ssl:enabled') |
| 50 | proto = "https" if ssl else "http" |
| 51 | |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 52 | response = requests.get( |
| 53 | '{0}://{1}:5601/api/status'.format(proto, IP), |
| 54 | proxies=proxies, |
| 55 | verify=False).content |
| 56 | body = json.loads(response) |
| 57 | assert body['status']['overall']['state'] == "green", ( |
| 58 | "Kibana overall status is not 'green':\n{}".format( |
| 59 | body['status']['overall']) |
| 60 | ) |
Ievgeniia Zadorozhna | 511f0ce | 2018-11-08 17:43:10 +0300 | [diff] [blame] | 61 | for i in body['status']['statuses']: |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 62 | assert i['state'] == "green", ( |
| 63 | "Kibana statuses are unexpected:\n{}".format(i)) |
Ievgeniia Zadorozhna | 511f0ce | 2018-11-08 17:43:10 +0300 | [diff] [blame] | 64 | |
| 65 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 66 | @pytest.mark.smoke |
| 67 | #TODO: recheck |
Ievgeniia Zadorozhna | 511f0ce | 2018-11-08 17:43:10 +0300 | [diff] [blame] | 68 | @pytest.mark.usefixtures('check_kibana') |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame] | 69 | def test_elasticsearch_node_count(local_salt_client): |
| 70 | now = datetime.datetime.now() |
| 71 | today = now.strftime("%Y.%m.%d") |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 72 | salt_output = local_salt_client.pillar_get( |
| 73 | tgt='kibana:server', |
| 74 | param='_param:haproxy_elasticsearch_bind_host') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 75 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 76 | IP = salt_output |
Oleksii Zhurba | 85f55fe | 2019-06-10 17:30:53 -0500 | [diff] [blame] | 77 | ssl = local_salt_client.pillar_get( |
| 78 | tgt='elasticsearch:server', |
| 79 | param='haproxy:proxy:listen:elasticsearch:binds:ssl:enabled') |
| 80 | proto = "https" if ssl else "http" |
| 81 | |
Tatyana Leontovich | 30bd90c | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 82 | headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 83 | proxies = {"http": None, "https": None} |
Tatyana Leontovich | 30bd90c | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 84 | data = ('{"size": 0, "aggs": ' |
| 85 | '{"uniq_hostname": ' |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 86 | '{"terms": {"size": 500, ' |
Tatyana Leontovich | 30bd90c | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 87 | '"field": "Hostname.keyword"}}}}') |
| 88 | response = requests.post( |
Oleksii Zhurba | 85f55fe | 2019-06-10 17:30:53 -0500 | [diff] [blame] | 89 | '{0}://{1}:9200/log-{2}/_search?pretty'.format(proto, IP, today), |
Tatyana Leontovich | 30bd90c | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 90 | proxies=proxies, |
| 91 | headers=headers, |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 92 | verify=False, |
Tatyana Leontovich | 30bd90c | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 93 | data=data) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 94 | assert response.status_code == 200, ( |
| 95 | 'Issues with accessing elasticsearch on {}:\n{}'.format( |
| 96 | IP, response.text) |
| 97 | ) |
Tatyana Leontovich | 30bd90c | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 98 | resp = json.loads(response.text) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 99 | cluster_domain = local_salt_client.pillar_get(param='_param:cluster_domain') |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 100 | monitored_nodes = [] |
Oleksii Zhurba | 7f46341 | 2018-03-21 16:32:44 -0500 | [diff] [blame] | 101 | for item_ in resp['aggregations']['uniq_hostname']['buckets']: |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 102 | node_name = item_['key'] |
| 103 | monitored_nodes.append(node_name + '.' + cluster_domain) |
| 104 | missing_nodes = [] |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 105 | all_nodes = local_salt_client.test_ping(tgt='*').keys() |
| 106 | for node in all_nodes: |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 107 | if node not in monitored_nodes: |
| 108 | missing_nodes.append(node) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 109 | assert len(missing_nodes) == 0, ( |
| 110 | "Not all nodes are in Elasticsearch. Expected {}, but found {} keys.\n" |
| 111 | "Missing nodes:\n{}".format( |
| 112 | len(monitored_nodes), len(all_nodes), missing_nodes) |
| 113 | ) |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame] | 114 | |
| 115 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 116 | @pytest.mark.sl_dup |
| 117 | #DockerServiceMonitoring* |
| 118 | @pytest.mark.full |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 119 | def test_stacklight_services_replicas(local_salt_client): |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 120 | # TODO |
| 121 | # change to docker:swarm:role:master ? |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 122 | salt_output = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 123 | tgt='I@docker:client:stack:monitoring and I@prometheus:server', |
| 124 | param='docker service ls', |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 125 | expr_form='compound') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 126 | |
| 127 | if not salt_output: |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 128 | pytest.skip("docker:client:stack:monitoring or \ |
| 129 | prometheus:server pillars are not found on this environment.") |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 130 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 131 | wrong_items = [] |
| 132 | for line in salt_output[salt_output.keys()[0]].split('\n'): |
| 133 | if line[line.find('/') - 1] != line[line.find('/') + 1] \ |
| 134 | and 'replicated' in line: |
| 135 | wrong_items.append(line) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 136 | assert len(wrong_items) == 0, ( |
| 137 | "Some monitoring services don't have the expected number of " |
| 138 | "replicas:\n{}".format(json.dumps(wrong_items, indent=4)) |
| 139 | ) |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 140 | |
| 141 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 142 | @pytest.mark.smoke |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 143 | @pytest.mark.usefixtures('check_prometheus') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 144 | def test_prometheus_alert_count(local_salt_client, ctl_nodes_pillar): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 145 | IP = local_salt_client.pillar_get(param='_param:cluster_public_host') |
Sergey Galkin | ea5a9ae | 2019-11-18 15:15:07 +0400 | [diff] [blame] | 146 | prometheus_password_old = local_salt_client.pillar_get( |
| 147 | param='_param:keepalived_prometheus_vip_password_generated') |
| 148 | prometheus_password = local_salt_client.pillar_get( |
| 149 | param='_param:prometheus_server_proxy_password_generated') |
Oleksii Zhurba | 85f55fe | 2019-06-10 17:30:53 -0500 | [diff] [blame] | 150 | proto = local_salt_client.pillar_get( |
| 151 | param='_param:cluster_public_protocol') |
Sergey Galkin | ea5a9ae | 2019-11-18 15:15:07 +0400 | [diff] [blame] | 152 | proxies = {"http": None, "https": None} |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 153 | # keystone:server can return 3 nodes instead of 1 |
| 154 | # this will be fixed later |
| 155 | # TODO |
Sergey Galkin | ea5a9ae | 2019-11-18 15:15:07 +0400 | [diff] [blame] | 156 | if prometheus_password == '': |
| 157 | prometheus_password = prometheus_password_old |
| 158 | response = requests.get( |
| 159 | '{0}://{1}:15010/api/v1/alerts'.format(proto, IP), |
| 160 | proxies=proxies, |
| 161 | auth=('prometheus', prometheus_password)) |
| 162 | assert response.status_code == 200, ( |
| 163 | 'Issues with accessing prometheus alerts on {}:\n{}'.format( |
| 164 | IP, response.text) |
| 165 | ) |
| 166 | alerts = json.loads(response.content) |
| 167 | short_alerts = '' |
| 168 | for i in alerts['data']['alerts']: |
| 169 | short_alerts = '{}* {}\n'.format(short_alerts, i['annotations']['description']) |
| 170 | assert alerts['data']['alerts'] == [], 'AlertManager page has some alerts!\n{}'.format( |
| 171 | short_alerts) |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 172 | |
| 173 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 174 | @pytest.mark.sl_dup |
| 175 | #DockerServiceMonitoring* ?? |
| 176 | @pytest.mark.full |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 177 | def test_stacklight_containers_status(local_salt_client): |
| 178 | salt_output = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 179 | tgt='I@docker:swarm:role:master and I@prometheus:server', |
| 180 | param='docker service ps $(docker stack services -q monitoring)', |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 181 | expr_form='compound') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 182 | |
| 183 | if not salt_output: |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 184 | pytest.skip("docker:swarm:role:master or prometheus:server pillars " |
| 185 | "are not found on this environment.") |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 186 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 187 | result = {} |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 188 | # for old reclass models, docker:swarm:role:master can return |
| 189 | # 2 nodes instead of one. Here is temporary fix. |
| 190 | # TODO |
| 191 | if len(salt_output.keys()) > 1: |
| 192 | if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]: |
| 193 | del salt_output[salt_output.keys()[0]] |
Oleksii Zhurba | f2af637 | 2017-11-01 22:53:03 +0000 | [diff] [blame] | 194 | for line in salt_output[salt_output.keys()[0]].split('\n')[1:]: |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 195 | shift = 0 |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 196 | if line.split()[1] == '\\_': |
| 197 | shift = 1 |
| 198 | if line.split()[1 + shift] not in result.keys(): |
| 199 | result[line.split()[1]] = 'NOT OK' |
| 200 | if line.split()[4 + shift] == 'Running' \ |
| 201 | or line.split()[4 + shift] == 'Ready': |
| 202 | result[line.split()[1 + shift]] = 'OK' |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 203 | assert 'NOT OK' not in result.values(), ( |
| 204 | "Some containers have incorrect state:\n{}".format( |
| 205 | json.dumps(result, indent=4)) |
| 206 | ) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 207 | |
| 208 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 209 | @pytest.mark.sl_dup |
| 210 | #PrometheusTargetDown |
| 211 | @pytest.mark.full |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 212 | def test_running_telegraf_services(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 213 | salt_output = local_salt_client.cmd(tgt='telegraf:agent', |
| 214 | fun='service.status', |
| 215 | param='telegraf', |
| 216 | expr_form='pillar',) |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 217 | |
| 218 | if not salt_output: |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 219 | pytest.skip("Telegraf or telegraf:agent pillars are not found on " |
| 220 | "this environment.") |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 221 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 222 | result = [{node: status} for node, status |
| 223 | in salt_output.items() |
| 224 | if status is False] |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 225 | assert result == [], ( |
| 226 | "Telegraf service is not running on the following nodes:\n{}".format( |
| 227 | result) |
| 228 | ) |
Ievgeniia Zadorozhna | 6775eb7 | 2018-11-09 19:50:04 +0300 | [diff] [blame] | 229 | |
| 230 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 231 | @pytest.mark.sl_dup |
| 232 | #PrometheusTargetDown |
| 233 | @pytest.mark.full |
Ievgeniia Zadorozhna | 6775eb7 | 2018-11-09 19:50:04 +0300 | [diff] [blame] | 234 | def test_running_fluentd_services(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 235 | salt_output = local_salt_client.cmd(tgt='fluentd:agent', |
| 236 | fun='service.status', |
| 237 | param='td-agent', |
Ievgeniia Zadorozhna | 6775eb7 | 2018-11-09 19:50:04 +0300 | [diff] [blame] | 238 | expr_form='pillar') |
| 239 | result = [{node: status} for node, status |
| 240 | in salt_output.items() |
| 241 | if status is False] |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 242 | assert result == [], ( |
| 243 | "Fluentd check failed - td-agent service is not running on the " |
| 244 | "following nodes:\n{}".format(result) |
| 245 | ) |