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