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