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