blob: aac6edfb57999366008e15aa734d5eebe3569b90 [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
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020025 assert not errors, 'k8s is not healthy:\n{}'.format(
26 json.dumps(errors, 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
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020048 assert not errors, 'k8s is not healthy:\n{}'.format(
49 json.dumps(errors, indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000050
51
52def test_k8s_get_calico_status(local_salt_client):
53 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030054 tgt='kubernetes:pool',
55 param='calicoctl node status',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000056 expr_form='pillar'
57 )
58 errors = []
59 if not result:
60 pytest.skip("k8s is not found on this environment")
61 for node in result:
62 for line in result[node].split('\n'):
63 line = line.strip('|')
64 if 'STATE' in line or '| ' not in line:
65 continue
66 else:
67 if 'up' not in line or 'Established' not in line:
68 errors.append(line)
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020069 assert not errors, 'Calico node status is not good:\n{}'.format(
70 json.dumps(errors, indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000071
72
73def test_k8s_cluster_status(local_salt_client):
74 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030075 tgt='kubernetes:master',
76 param='kubectl cluster-info',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000077 expr_form='pillar'
78 )
79 errors = []
80 if not result:
81 pytest.skip("k8s is not found on this environment")
82 for node in result:
83 for line in result[node].split('\n'):
84 if 'proto' in line or 'further' in line or line == '':
85 continue
86 else:
87 if 'is running' not in line:
88 errors.append(line)
89 break
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020090 assert not errors, 'k8s cluster info is not good:\n{}'.format(
91 json.dumps(errors, indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000092
93
94def test_k8s_kubelet_status(local_salt_client):
95 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030096 tgt='kubernetes:pool',
97 fun='service.status',
98 param='kubelet',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000099 expr_form='pillar'
100 )
101 errors = []
102 if not result:
103 pytest.skip("k8s is not found on this environment")
104 for node in result:
105 if not result[node]:
106 errors.append(node)
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +0200107 assert not errors, 'Kublete is not running on the nodes:\n{}'.format(
108 errors)
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000109
110
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000111def test_k8s_check_system_pods_status(local_salt_client):
112 result = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300113 tgt='etcd:server',
114 param='kubectl --namespace="kube-system" get pods',
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000115 expr_form='pillar'
116 )
117 errors = []
118 if not result:
119 pytest.skip("k8s is not found on this environment")
120 for node in result:
121 for line in result[node].split('\n'):
122 line = line.strip('|')
123 if 'STATUS' in line or 'proto' in line:
124 continue
125 else:
126 if 'Running' not in line:
127 errors.append(line)
128 break
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +0200129 assert not errors, 'Some system pods are not running:\n{}'.format(
130 json.dumps(errors, indent=4))
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000131
132
133def test_check_k8s_image_availability(local_salt_client):
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000134 # not a test actually
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000135 hostname = 'https://docker-dev-virtual.docker.mirantis.net/artifactory/webapp/'
136 response = os.system('curl -s --insecure {} > /dev/null'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000137 if response == 0:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300138 logging.info('{} is AVAILABLE'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000139 else:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300140 logging.error('{} IS NOT AVAILABLE'.format(hostname))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300141
142
Oleksii Zhurba5a863862019-05-28 18:58:07 -0500143@pytest.mark.xfail
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300144def test_k8s_dashboard_available(local_salt_client):
145 """
146 # Check is kubernetes enabled on the cluster with command `salt -C 'etcd:server' cmd.run 'kubectl get svc -n kube-system'`
147 # If yes then check Dashboard addon with next command: `salt -C 'etcd:server' pillar.get kubernetes:common:addons:dashboard:enabled`
148 # If dashboard enabled get its IP from pillar `salt -C 'etcd:server' pillar.get kubernetes:common:addons:dashboard:public_ip`
149 # Check that public_ip exists
150 # Check that public_ip:8443 is accessible with curl
151 """
152 result = local_salt_client.cmd(
153 tgt='etcd:server',
154 param='kubectl get svc -n kube-system',
155 expr_form='pillar'
156 )
157 if not result:
158 pytest.skip("k8s is not found on this environment")
159
160 # service name 'kubernetes-dashboard' is hardcoded in kubernetes formula
161 dashboard_enabled = local_salt_client.pillar_get(
162 tgt='etcd:server',
163 param='kubernetes:common:addons:dashboard:enabled',)
164 if not dashboard_enabled:
165 pytest.skip("Kubernetes dashboard is not enabled in the cluster.")
166
167 external_ip = local_salt_client.pillar_get(
168 tgt='etcd:server',
169 param='kubernetes:common:addons:dashboard:public_ip')
170
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +0200171 assert external_ip, (
172 "Kubernetes dashboard public ip is not found in pillars")
173 assert external_ip.__len__() > 0, (
174 "Kubernetes dashboard is enabled but not defined in pillars")
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300175 # 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 )
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +0200182 assert len(check.values()[0]) != 0, (
183 'Kubernetes dashboard is not reachable on {} from '
184 'ctl nodes'.format(url)
185 )