blob: 24b53e4a57d843c7c4ad7e316cb0fb756a9828b8 [file] [log] [blame]
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00001import pytest
2import json
Oleksii Zhurba943a0932017-11-01 22:27:53 +00003import os
Hanna Arhipova1eef8312019-05-06 20:14:18 +03004import logging
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00005
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00006
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00007def test_k8s_get_cs_status(local_salt_client):
8 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03009 tgt='etcd:server',
10 param='kubectl get cs',
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000011 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 Zhurba943a0932017-11-01 22:27:53 +000019 if 'MESSAGE' in line or 'proto' in line:
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000020 continue
21 else:
22 if 'Healthy' not in line:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000023 errors.append(line)
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000024 break
25 assert not errors, 'k8s is not healthy: {}'.format(json.dumps(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000026 errors,
27 indent=4))
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000028
29
Hanna Arhipovaa3c6a852019-03-28 09:30:20 +020030@pytest.mark.xfail
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000031def test_k8s_get_nodes_status(local_salt_client):
32 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030033 tgt='etcd:server',
34 param='kubectl get nodes',
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000035 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 Zhurba943a0932017-11-01 22:27:53 +000043 if 'STATUS' in line or 'proto' in line:
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000044 continue
45 else:
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050046 if 'Ready' != line.split()[1]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000047 errors.append(line)
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000048 break
49 assert not errors, 'k8s is not healthy: {}'.format(json.dumps(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000050 errors,
51 indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000052
53
54def test_k8s_get_calico_status(local_salt_client):
55 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030056 tgt='kubernetes:pool',
57 param='calicoctl node status',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000058 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
76def test_k8s_cluster_status(local_salt_client):
77 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030078 tgt='kubernetes:master',
79 param='kubectl cluster-info',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000080 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
98def test_k8s_kubelet_status(local_salt_client):
99 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300100 tgt='kubernetes:pool',
101 fun='service.status',
102 param='kubelet',
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000103 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 Zhurba3eb58012017-11-02 22:01:06 +0000115def test_k8s_check_system_pods_status(local_salt_client):
116 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300117 tgt='etcd:server',
118 param='kubectl --namespace="kube-system" get pods',
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000119 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
138def test_check_k8s_image_availability(local_salt_client):
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000139 # not a test actually
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000140 hostname = 'https://docker-dev-virtual.docker.mirantis.net/artifactory/webapp/'
141 response = os.system('curl -s --insecure {} > /dev/null'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000142 if response == 0:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300143 logging.info('{} is AVAILABLE'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000144 else:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300145 logging.error('{} IS NOT AVAILABLE'.format(hostname))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300146
147
Oleksii Zhurba5a863862019-05-28 18:58:07 -0500148@pytest.mark.xfail
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300149def 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 Zhurba5a863862019-05-28 18:58:07 -0500176 assert external_ip, "Kubernetes dashboard public ip is not found in pillars"
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300177 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)