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