blob: 97c3490769745281bb745f6539dc01a2f63b0454 [file] [log] [blame]
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00001import pytest
2import json
Oleksii Zhurba943a0932017-11-01 22:27:53 +00003import os
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00004
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00005
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00006def test_k8s_get_cs_status(local_salt_client):
7 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03008 tgt='etcd:server',
9 param='kubectl get cs',
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000010 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 Zhurba943a0932017-11-01 22:27:53 +000018 if 'MESSAGE' in line or 'proto' in line:
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000019 continue
20 else:
21 if 'Healthy' not in line:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000022 errors.append(line)
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000023 break
24 assert not errors, 'k8s is not healthy: {}'.format(json.dumps(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000025 errors,
26 indent=4))
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000027
28
Hanna Arhipovaa3c6a852019-03-28 09:30:20 +020029@pytest.mark.xfail
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000030def test_k8s_get_nodes_status(local_salt_client):
31 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030032 tgt='etcd:server',
33 param='kubectl get nodes',
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000034 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 Zhurba943a0932017-11-01 22:27:53 +000042 if 'STATUS' in line or 'proto' in line:
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000043 continue
44 else:
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050045 if 'Ready' != line.split()[1]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000046 errors.append(line)
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000047 break
48 assert not errors, 'k8s is not healthy: {}'.format(json.dumps(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000049 errors,
50 indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000051
52
53def test_k8s_get_calico_status(local_salt_client):
54 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030055 tgt='kubernetes:pool',
56 param='calicoctl node status',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000057 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
75def test_k8s_cluster_status(local_salt_client):
76 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030077 tgt='kubernetes:master',
78 param='kubectl cluster-info',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000079 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
97def test_k8s_kubelet_status(local_salt_client):
98 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030099 tgt='kubernetes:pool',
100 fun='service.status',
101 param='kubelet',
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000102 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 Zhurba3eb58012017-11-02 22:01:06 +0000114def test_k8s_check_system_pods_status(local_salt_client):
115 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300116 tgt='etcd:server',
117 param='kubectl --namespace="kube-system" get pods',
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000118 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
137def test_check_k8s_image_availability(local_salt_client):
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000138 # not a test actually
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000139 hostname = 'https://docker-dev-virtual.docker.mirantis.net/artifactory/webapp/'
140 response = os.system('curl -s --insecure {} > /dev/null'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000141 if response == 0:
142 print '{} is AVAILABLE'.format(hostname)
143 else:
144 print '{} IS NOT AVAILABLE'.format(hostname)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300145
146
147def 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)