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