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 | |
| 6 | |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 7 | @pytest.mark.usefixtures('check_kibana') |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 8 | def test_elasticsearch_cluster(local_salt_client): |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 9 | salt_output = local_salt_client.pillar_get( |
| 10 | tgt='kibana:server', |
| 11 | param='_param:haproxy_elasticsearch_bind_host') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 12 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 13 | proxies = {"http": None, "https": None} |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 14 | IP = salt_output |
| 15 | assert requests.get('http://{}:9200/'.format(IP), |
| 16 | proxies=proxies).status_code == 200, \ |
| 17 | 'Cannot check elasticsearch url on {}.'.format(IP) |
| 18 | resp = requests.get('http://{}:9200/_cat/health'.format(IP), |
| 19 | proxies=proxies).content |
| 20 | assert resp.split()[3] == 'green', \ |
| 21 | 'elasticsearch status is not good {}'.format( |
| 22 | json.dumps(resp, indent=4)) |
| 23 | assert resp.split()[4] == '3', \ |
| 24 | 'elasticsearch status is not good {}'.format( |
| 25 | json.dumps(resp, indent=4)) |
| 26 | assert resp.split()[5] == '3', \ |
| 27 | 'elasticsearch status is not good {}'.format( |
| 28 | json.dumps(resp, indent=4)) |
| 29 | assert resp.split()[10] == '0', \ |
| 30 | 'elasticsearch status is not good {}'.format( |
| 31 | json.dumps(resp, indent=4)) |
| 32 | assert resp.split()[13] == '100.0%', \ |
| 33 | 'elasticsearch status is not good {}'.format( |
| 34 | json.dumps(resp, indent=4)) |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 35 | |
| 36 | |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 37 | @pytest.mark.usefixtures('check_kibana') |
Ievgeniia Zadorozhna | 511f0ce | 2018-11-08 17:43:10 +0300 | [diff] [blame] | 38 | def test_kibana_status(local_salt_client): |
| 39 | proxies = {"http": None, "https": None} |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 40 | IP = local_salt_client.pillar_get(param='_param:stacklight_log_address') |
Ievgeniia Zadorozhna | 511f0ce | 2018-11-08 17:43:10 +0300 | [diff] [blame] | 41 | resp = requests.get('http://{}:5601/api/status'.format(IP), |
| 42 | proxies=proxies).content |
| 43 | body = json.loads(resp) |
| 44 | assert body['status']['overall']['state'] == "green", \ |
| 45 | "Kibana status is not expected: {}".format( |
| 46 | body['status']['overall']) |
| 47 | for i in body['status']['statuses']: |
| 48 | assert i['state'] == "green", \ |
| 49 | "Kibana statuses are unexpected: {}".format(i) |
| 50 | |
| 51 | |
| 52 | @pytest.mark.usefixtures('check_kibana') |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame] | 53 | def test_elasticsearch_node_count(local_salt_client): |
| 54 | now = datetime.datetime.now() |
| 55 | today = now.strftime("%Y.%m.%d") |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 56 | salt_output = local_salt_client.pillar_get( |
| 57 | tgt='kibana:server', |
| 58 | param='_param:haproxy_elasticsearch_bind_host') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 59 | |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 60 | IP = salt_output |
Tatyana Leontovich | ed57cb0 | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 61 | headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 62 | proxies = {"http": None, "https": None} |
Tatyana Leontovich | ed57cb0 | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 63 | data = ('{"size": 0, "aggs": ' |
| 64 | '{"uniq_hostname": ' |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 65 | '{"terms": {"size": 1000, ' |
Tatyana Leontovich | ed57cb0 | 2019-01-11 16:26:32 +0200 | [diff] [blame] | 66 | '"field": "Hostname.keyword"}}}}') |
| 67 | response = requests.post( |
| 68 | 'http://{0}:9200/log-{1}/_search?pretty'.format(IP, today), |
| 69 | proxies=proxies, |
| 70 | headers=headers, |
| 71 | data=data) |
| 72 | assert 200 == response.status_code, 'Unexpected code {}'.format( |
| 73 | response.text) |
| 74 | resp = json.loads(response.text) |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 75 | cluster_domain = local_salt_client.pillar_get(param='_param:cluster_domain') |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 76 | monitored_nodes = [] |
Oleksii Zhurba | 7f46341 | 2018-03-21 16:32:44 -0500 | [diff] [blame] | 77 | for item_ in resp['aggregations']['uniq_hostname']['buckets']: |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 78 | node_name = item_['key'] |
| 79 | monitored_nodes.append(node_name + '.' + cluster_domain) |
| 80 | missing_nodes = [] |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 81 | all_nodes = local_salt_client.test_ping(tgt='*').keys() |
| 82 | for node in all_nodes: |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 83 | if node not in monitored_nodes: |
| 84 | missing_nodes.append(node) |
| 85 | assert len(missing_nodes) == 0, \ |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame] | 86 | 'Not all nodes are in Elasticsearch. Found {0} keys, ' \ |
Oleksii Zhurba | d2847dc | 2018-02-16 15:13:09 -0600 | [diff] [blame] | 87 | 'expected {1}. Missing nodes: \n{2}'. \ |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 88 | format(len(monitored_nodes), len(all_nodes), missing_nodes) |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame] | 89 | |
| 90 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 91 | def test_stacklight_services_replicas(local_salt_client): |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 92 | # TODO |
| 93 | # change to docker:swarm:role:master ? |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 94 | salt_output = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 95 | tgt='I@docker:client:stack:monitoring and I@prometheus:server', |
| 96 | param='docker service ls', |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 97 | expr_form='compound') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 98 | |
| 99 | if not salt_output: |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 100 | pytest.skip("docker:client:stack:monitoring or \ |
| 101 | prometheus:server pillars are not found on this environment.") |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 102 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 103 | wrong_items = [] |
| 104 | for line in salt_output[salt_output.keys()[0]].split('\n'): |
| 105 | if line[line.find('/') - 1] != line[line.find('/') + 1] \ |
| 106 | and 'replicated' in line: |
| 107 | wrong_items.append(line) |
| 108 | assert len(wrong_items) == 0, \ |
| 109 | '''Some monitoring services doesn't have expected number of replicas: |
| 110 | {}'''.format(json.dumps(wrong_items, indent=4)) |
| 111 | |
| 112 | |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 113 | @pytest.mark.usefixtures('check_prometheus') |
Oleksii Zhurba | 2c2dc94 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 114 | def test_prometheus_alert_count(local_salt_client, ctl_nodes_pillar): |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 115 | IP = local_salt_client.pillar_get(param='_param:cluster_public_host') |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 116 | # keystone:server can return 3 nodes instead of 1 |
| 117 | # this will be fixed later |
| 118 | # TODO |
Oleksii Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 119 | nodes_info = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 120 | tgt=ctl_nodes_pillar, |
| 121 | param='curl -s http://{}:15010/alerts | grep icon-chevron-down | ' |
| 122 | 'grep -v "0 active"'.format(IP), |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 123 | expr_form='pillar') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 124 | |
Oleksii Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 125 | result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace( |
| 126 | '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '') |
| 127 | assert result == '', 'AlertManager page has some alerts! {}'.format( |
| 128 | json.dumps(result), indent=4) |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 129 | |
| 130 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 131 | def test_stacklight_containers_status(local_salt_client): |
| 132 | salt_output = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 133 | tgt='I@docker:swarm:role:master and I@prometheus:server', |
| 134 | param='docker service ps $(docker stack services -q monitoring)', |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 135 | expr_form='compound') |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 136 | |
| 137 | if not salt_output: |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 138 | pytest.skip("docker:swarm:role:master or prometheus:server \ |
| 139 | pillars are not found on this environment.") |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 140 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 141 | result = {} |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 142 | # for old reclass models, docker:swarm:role:master can return |
| 143 | # 2 nodes instead of one. Here is temporary fix. |
| 144 | # TODO |
| 145 | if len(salt_output.keys()) > 1: |
| 146 | if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]: |
| 147 | del salt_output[salt_output.keys()[0]] |
Oleksii Zhurba | f2af637 | 2017-11-01 22:53:03 +0000 | [diff] [blame] | 148 | for line in salt_output[salt_output.keys()[0]].split('\n')[1:]: |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 149 | shift = 0 |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 150 | if line.split()[1] == '\\_': |
| 151 | shift = 1 |
| 152 | if line.split()[1 + shift] not in result.keys(): |
| 153 | result[line.split()[1]] = 'NOT OK' |
| 154 | if line.split()[4 + shift] == 'Running' \ |
| 155 | or line.split()[4 + shift] == 'Ready': |
| 156 | result[line.split()[1 + shift]] = 'OK' |
| 157 | assert 'NOT OK' not in result.values(), \ |
| 158 | '''Some containers are in incorrect state: |
| 159 | {}'''.format(json.dumps(result, indent=4)) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 160 | |
| 161 | |
| 162 | def test_running_telegraf_services(local_salt_client): |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 163 | salt_output = local_salt_client.cmd(tgt='telegraf:agent', |
| 164 | fun='service.status', |
| 165 | param='telegraf', |
| 166 | expr_form='pillar',) |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 167 | |
| 168 | if not salt_output: |
| 169 | pytest.skip("Telegraf or telegraf:agent \ |
| 170 | pillar are not found on this environment.") |
| 171 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 172 | result = [{node: status} for node, status |
| 173 | in salt_output.items() |
| 174 | if status is False] |
| 175 | assert result == [], 'Telegraf service is not running ' \ |
Oleksii Zhurba | 9848e21 | 2018-09-05 10:53:51 -0500 | [diff] [blame] | 176 | 'on following nodes: {}'.format(result) |
Ievgeniia Zadorozhna | 6775eb7 | 2018-11-09 19:50:04 +0300 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def test_running_fluentd_services(local_salt_client): |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame^] | 180 | salt_output = local_salt_client.cmd(tgt='fluentd:agent', |
| 181 | fun='service.status', |
| 182 | param='td-agent', |
Ievgeniia Zadorozhna | 6775eb7 | 2018-11-09 19:50:04 +0300 | [diff] [blame] | 183 | expr_form='pillar') |
| 184 | result = [{node: status} for node, status |
| 185 | in salt_output.items() |
| 186 | if status is False] |
| 187 | assert result == [], 'Fluentd check failed: td-agent service is not ' \ |
| 188 | 'running on following nodes:'.format(result) |