Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 1 | import pytest |
| 2 | import json |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 3 | import os |
Hanna Arhipova | 1eef831 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 4 | import logging |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 5 | |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 6 | |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 7 | def test_k8s_get_cs_status(local_salt_client): |
| 8 | result = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 9 | tgt='etcd:server', |
| 10 | param='kubectl get cs', |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 11 | expr_form='pillar' |
| 12 | ) |
| 13 | errors = [] |
| 14 | if not result: |
| 15 | pytest.skip("k8s is not found on this environment") |
| 16 | for node in result: |
| 17 | for line in result[node].split('\n'): |
| 18 | line = line.strip() |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 19 | if 'MESSAGE' in line or 'proto' in line: |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 20 | continue |
| 21 | else: |
| 22 | if 'Healthy' not in line: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 23 | errors.append(line) |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 24 | break |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 25 | assert not errors, 'k8s is not healthy:\n{}'.format( |
| 26 | json.dumps(errors, indent=4)) |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 27 | |
| 28 | |
Hanna Arhipova | a3c6a85 | 2019-03-28 09:30:20 +0200 | [diff] [blame] | 29 | @pytest.mark.xfail |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 30 | def test_k8s_get_nodes_status(local_salt_client): |
| 31 | result = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 32 | tgt='etcd:server', |
| 33 | param='kubectl get nodes', |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 34 | expr_form='pillar' |
| 35 | ) |
| 36 | errors = [] |
| 37 | if not result: |
| 38 | pytest.skip("k8s is not found on this environment") |
| 39 | for node in result: |
| 40 | for line in result[node].split('\n'): |
| 41 | line = line.strip() |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 42 | if 'STATUS' in line or 'proto' in line: |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 43 | continue |
| 44 | else: |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 45 | if 'Ready' != line.split()[1]: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 46 | errors.append(line) |
Oleksii Zhurba | 11de14e | 2017-10-23 19:13:00 +0000 | [diff] [blame] | 47 | break |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 48 | assert not errors, 'k8s is not healthy:\n{}'.format( |
| 49 | json.dumps(errors, indent=4)) |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def test_k8s_get_calico_status(local_salt_client): |
| 53 | result = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 54 | tgt='kubernetes:pool', |
| 55 | param='calicoctl node status', |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 56 | expr_form='pillar' |
| 57 | ) |
| 58 | errors = [] |
| 59 | if not result: |
| 60 | pytest.skip("k8s is not found on this environment") |
| 61 | for node in result: |
| 62 | for line in result[node].split('\n'): |
| 63 | line = line.strip('|') |
| 64 | if 'STATE' in line or '| ' not in line: |
| 65 | continue |
| 66 | else: |
| 67 | if 'up' not in line or 'Established' not in line: |
| 68 | errors.append(line) |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 69 | assert not errors, 'Calico node status is not good:\n{}'.format( |
| 70 | json.dumps(errors, indent=4)) |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | def test_k8s_cluster_status(local_salt_client): |
| 74 | result = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 75 | tgt='kubernetes:master', |
| 76 | param='kubectl cluster-info', |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 77 | expr_form='pillar' |
| 78 | ) |
| 79 | errors = [] |
| 80 | if not result: |
| 81 | pytest.skip("k8s is not found on this environment") |
| 82 | for node in result: |
| 83 | for line in result[node].split('\n'): |
| 84 | if 'proto' in line or 'further' in line or line == '': |
| 85 | continue |
| 86 | else: |
| 87 | if 'is running' not in line: |
| 88 | errors.append(line) |
| 89 | break |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 90 | assert not errors, 'k8s cluster info is not good:\n{}'.format( |
| 91 | json.dumps(errors, indent=4)) |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def test_k8s_kubelet_status(local_salt_client): |
| 95 | result = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 96 | tgt='kubernetes:pool', |
| 97 | fun='service.status', |
| 98 | param='kubelet', |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 99 | expr_form='pillar' |
| 100 | ) |
| 101 | errors = [] |
| 102 | if not result: |
| 103 | pytest.skip("k8s is not found on this environment") |
| 104 | for node in result: |
| 105 | if not result[node]: |
| 106 | errors.append(node) |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 107 | assert not errors, 'Kublete is not running on the nodes:\n{}'.format( |
| 108 | errors) |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 109 | |
| 110 | |
Oleksii Zhurba | 3eb5801 | 2017-11-02 22:01:06 +0000 | [diff] [blame] | 111 | def test_k8s_check_system_pods_status(local_salt_client): |
| 112 | result = local_salt_client.cmd( |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 113 | tgt='etcd:server', |
| 114 | param='kubectl --namespace="kube-system" get pods', |
Oleksii Zhurba | 3eb5801 | 2017-11-02 22:01:06 +0000 | [diff] [blame] | 115 | expr_form='pillar' |
| 116 | ) |
| 117 | errors = [] |
| 118 | if not result: |
| 119 | pytest.skip("k8s is not found on this environment") |
| 120 | for node in result: |
| 121 | for line in result[node].split('\n'): |
| 122 | line = line.strip('|') |
| 123 | if 'STATUS' in line or 'proto' in line: |
| 124 | continue |
| 125 | else: |
| 126 | if 'Running' not in line: |
| 127 | errors.append(line) |
| 128 | break |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 129 | assert not errors, 'Some system pods are not running:\n{}'.format( |
| 130 | json.dumps(errors, indent=4)) |
Oleksii Zhurba | 3eb5801 | 2017-11-02 22:01:06 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def test_check_k8s_image_availability(local_salt_client): |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 134 | # not a test actually |
Oleksii Zhurba | 3eb5801 | 2017-11-02 22:01:06 +0000 | [diff] [blame] | 135 | hostname = 'https://docker-dev-virtual.docker.mirantis.net/artifactory/webapp/' |
| 136 | response = os.system('curl -s --insecure {} > /dev/null'.format(hostname)) |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 137 | if response == 0: |
Hanna Arhipova | 1eef831 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 138 | logging.info('{} is AVAILABLE'.format(hostname)) |
Oleksii Zhurba | 943a093 | 2017-11-01 22:27:53 +0000 | [diff] [blame] | 139 | else: |
Hanna Arhipova | 1eef831 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 140 | logging.error('{} IS NOT AVAILABLE'.format(hostname)) |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 141 | |
| 142 | |
Oleksii Zhurba | 5a86386 | 2019-05-28 18:58:07 -0500 | [diff] [blame] | 143 | @pytest.mark.xfail |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 144 | def test_k8s_dashboard_available(local_salt_client): |
| 145 | """ |
| 146 | # Check is kubernetes enabled on the cluster with command `salt -C 'etcd:server' cmd.run 'kubectl get svc -n kube-system'` |
| 147 | # If yes then check Dashboard addon with next command: `salt -C 'etcd:server' pillar.get kubernetes:common:addons:dashboard:enabled` |
| 148 | # If dashboard enabled get its IP from pillar `salt -C 'etcd:server' pillar.get kubernetes:common:addons:dashboard:public_ip` |
| 149 | # Check that public_ip exists |
| 150 | # Check that public_ip:8443 is accessible with curl |
| 151 | """ |
| 152 | result = local_salt_client.cmd( |
| 153 | tgt='etcd:server', |
| 154 | param='kubectl get svc -n kube-system', |
| 155 | expr_form='pillar' |
| 156 | ) |
| 157 | if not result: |
| 158 | pytest.skip("k8s is not found on this environment") |
| 159 | |
| 160 | # service name 'kubernetes-dashboard' is hardcoded in kubernetes formula |
| 161 | dashboard_enabled = local_salt_client.pillar_get( |
| 162 | tgt='etcd:server', |
| 163 | param='kubernetes:common:addons:dashboard:enabled',) |
| 164 | if not dashboard_enabled: |
| 165 | pytest.skip("Kubernetes dashboard is not enabled in the cluster.") |
| 166 | |
| 167 | external_ip = local_salt_client.pillar_get( |
| 168 | tgt='etcd:server', |
| 169 | param='kubernetes:common:addons:dashboard:public_ip') |
| 170 | |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 171 | assert external_ip, ( |
| 172 | "Kubernetes dashboard public ip is not found in pillars") |
| 173 | assert external_ip.__len__() > 0, ( |
| 174 | "Kubernetes dashboard is enabled but not defined in pillars") |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 175 | # dashboard port 8443 is hardcoded in kubernetes formula |
| 176 | url = "https://{}:8443".format(external_ip) |
| 177 | check = local_salt_client.cmd( |
| 178 | tgt='etcd:server', |
| 179 | param='curl {} 2>&1 | grep kubernetesDashboard'.format(url), |
| 180 | expr_form='pillar' |
| 181 | ) |
Dmitriy Kruglov | bc0a88b | 2019-08-20 11:45:35 +0200 | [diff] [blame^] | 182 | assert len(check.values()[0]) != 0, ( |
| 183 | 'Kubernetes dashboard is not reachable on {} from ' |
| 184 | 'ctl nodes'.format(url) |
| 185 | ) |