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 | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 4 | from cvp_checks import utils |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | def test_elasticsearch_cluster(local_salt_client): |
Oleksii Zhurba | e37cdab | 2017-11-02 20:00:03 +0000 | [diff] [blame] | 8 | salt_output = local_salt_client.cmd( |
| 9 | 'elasticsearch:client', |
| 10 | 'pillar.get', |
| 11 | ['_param:haproxy_elasticsearch_bind_host'], |
| 12 | expr_form='pillar') |
Oleksii Zhurba | 88bc047 | 2017-11-09 21:04:09 +0000 | [diff] [blame] | 13 | 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 Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 20 | json.dumps(resp, indent=4)) |
Oleksii Zhurba | 88bc047 | 2017-11-09 21:04:09 +0000 | [diff] [blame] | 21 | assert resp.split()[4] == '3', \ |
| 22 | 'elasticsearch status is not good {}'.format( |
Oleksii Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 23 | json.dumps(resp, indent=4)) |
Oleksii Zhurba | 88bc047 | 2017-11-09 21:04:09 +0000 | [diff] [blame] | 24 | assert resp.split()[5] == '3', \ |
| 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()[10] == '0', \ |
| 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 | b31323f | 2017-11-20 15:35:19 -0600 | [diff] [blame] | 30 | assert resp.split()[13] == '100.0%', \ |
Oleksii Zhurba | 88bc047 | 2017-11-09 21:04:09 +0000 | [diff] [blame] | 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 | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 33 | |
| 34 | |
Oleksii Zhurba | 84ce7fe | 2018-01-16 21:34:01 +0000 | [diff] [blame^] | 35 | def 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 Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 56 | def 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 Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 72 | def 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 Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 77 | nodes_info = local_salt_client.cmd( |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 78 | 'keystone:server', |
| 79 | 'cmd.run', |
Oleksii Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 80 | ['curl -s http://{}:15010/alerts | grep icon-chevron-down | ' |
| 81 | 'grep -v "0 active"'.format(IP)], |
Oleksii Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 82 | expr_form='pillar') |
Oleksii Zhurba | 0c039ee | 2018-01-16 19:44:53 +0000 | [diff] [blame] | 83 | 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 Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 87 | |
| 88 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 89 | def 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 Zhurba | 468e6c7 | 2018-01-16 17:43:15 +0000 | [diff] [blame] | 96 | # 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 Zhurba | f2af637 | 2017-11-01 22:53:03 +0000 | [diff] [blame] | 102 | for line in salt_output[salt_output.keys()[0]].split('\n')[1:]: |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 103 | shift = 0 |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 104 | 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)) |