blob: c253b45a4c0a4a4bd3af96a3a8b5ef6b6b4afb52 [file] [log] [blame]
Oleksii Zhurba11de14e2017-10-23 19:13:00 +00001import pytest
2import json
Oleksii Zhurba943a0932017-11-01 22:27:53 +00003import os
Hanna Arhipova56eab942019-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(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -05009 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 Kruglova34a3042019-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
29def test_k8s_get_nodes_status(local_salt_client):
30 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050031 tgt='etcd:server',
32 param='kubectl get nodes',
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000033 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 Zhurba943a0932017-11-01 22:27:53 +000041 if 'STATUS' in line or 'proto' in line:
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000042 continue
43 else:
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050044 if 'Ready' != line.split()[1]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000045 errors.append(line)
Oleksii Zhurba11de14e2017-10-23 19:13:00 +000046 break
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020047 assert not errors, 'k8s is not healthy:\n{}'.format(
48 json.dumps(errors, indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000049
50
51def test_k8s_get_calico_status(local_salt_client):
52 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050053 tgt='kubernetes:pool',
54 param='calicoctl node status',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000055 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 Kruglova34a3042019-08-20 11:45:35 +020068 assert not errors, 'Calico node status is not good:\n{}'.format(
69 json.dumps(errors, indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000070
71
72def test_k8s_cluster_status(local_salt_client):
73 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050074 tgt='kubernetes:master',
75 param='kubectl cluster-info',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000076 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 Kruglova34a3042019-08-20 11:45:35 +020089 assert not errors, 'k8s cluster info is not good:\n{}'.format(
90 json.dumps(errors, indent=4))
Oleksii Zhurba943a0932017-11-01 22:27:53 +000091
92
93def test_k8s_kubelet_status(local_salt_client):
94 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050095 tgt='kubernetes:pool',
96 fun='service.status',
97 param='kubelet',
Oleksii Zhurba943a0932017-11-01 22:27:53 +000098 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 Kruglova34a3042019-08-20 11:45:35 +0200106 assert not errors, 'Kublete is not running on the nodes:\n{}'.format(
107 errors)
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000108
109
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000110def test_k8s_check_system_pods_status(local_salt_client):
111 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500112 tgt='etcd:server',
113 param='kubectl --namespace="kube-system" get pods',
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000114 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 Kruglova34a3042019-08-20 11:45:35 +0200128 assert not errors, 'Some system pods are not running:\n{}'.format(
129 json.dumps(errors, indent=4))
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000130
131
132def test_check_k8s_image_availability(local_salt_client):
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000133 # not a test actually
Oleksii Zhurba3eb58012017-11-02 22:01:06 +0000134 hostname = 'https://docker-dev-virtual.docker.mirantis.net/artifactory/webapp/'
135 response = os.system('curl -s --insecure {} > /dev/null'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000136 if response == 0:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300137 logging.info('{} is AVAILABLE'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000138 else:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300139 logging.error('{} IS NOT AVAILABLE'.format(hostname))
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300140
141
Oleksii Zhurba3d21fc72019-05-28 18:58:07 -0500142@pytest.mark.xfail
Hanna Arhipova79f8d602019-05-08 16:23:16 +0300143def test_k8s_dashboard_available(local_salt_client, contrail):
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300144 """
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 Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300151 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500152 tgt='etcd:server',
153 param='kubectl get svc -n kube-system',
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300154 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 Zhurba4bfd2ee2019-04-10 21:56:58 -0500160 dashboard_enabled = local_salt_client.pillar_get(
161 tgt='etcd:server',
162 param='kubernetes:common:addons:dashboard:enabled',)
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300163 if not dashboard_enabled:
164 pytest.skip("Kubernetes dashboard is not enabled in the cluster.")
165
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500166 external_ip = local_salt_client.pillar_get(
167 tgt='etcd:server',
168 param='kubernetes:common:addons:dashboard:public_ip')
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300169
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200170 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 Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300174 # dashboard port 8443 is hardcoded in kubernetes formula
175 url = "https://{}:8443".format(external_ip)
176 check = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500177 tgt='etcd:server',
178 param='curl {} 2>&1 | grep kubernetesDashboard'.format(url),
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300179 expr_form='pillar'
180 )
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300181 assert len(list(check.values())[0]) != 0, (
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200182 'Kubernetes dashboard is not reachable on {} from '
183 'ctl nodes'.format(url)
184 )