blob: 312d16285a3711fa0080ebef2b8d0c3793d12c05 [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
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
30def test_k8s_get_nodes_status(local_salt_client):
31 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050032 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(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050055 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(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050077 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(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050099 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(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500116 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:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300142 logging.info('{} is AVAILABLE'.format(hostname))
Oleksii Zhurba943a0932017-11-01 22:27:53 +0000143 else:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300144 logging.error('{} IS NOT AVAILABLE'.format(hostname))
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300145
146
Oleksii Zhurba3d21fc72019-05-28 18:58:07 -0500147@pytest.mark.xfail
Hanna Arhipova79f8d602019-05-08 16:23:16 +0300148def test_k8s_dashboard_available(local_salt_client, contrail):
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300149 """
150 # Check is kubernetes enabled on the cluster with command `salt -C 'etcd:server' cmd.run 'kubectl get svc -n kube-system'`
151 # If yes then check Dashboard addon with next command: `salt -C 'etcd:server' pillar.get kubernetes:common:addons:dashboard:enabled`
152 # If dashboard enabled get its IP from pillar `salt -C 'etcd:server' pillar.get kubernetes:common:addons:dashboard:public_ip`
153 # Check that public_ip exists
154 # Check that public_ip:8443 is accessible with curl
155 """
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300156 result = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500157 tgt='etcd:server',
158 param='kubectl get svc -n kube-system',
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300159 expr_form='pillar'
160 )
161 if not result:
162 pytest.skip("k8s is not found on this environment")
163
164 # service name 'kubernetes-dashboard' is hardcoded in kubernetes formula
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500165 dashboard_enabled = local_salt_client.pillar_get(
166 tgt='etcd:server',
167 param='kubernetes:common:addons:dashboard:enabled',)
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300168 if not dashboard_enabled:
169 pytest.skip("Kubernetes dashboard is not enabled in the cluster.")
170
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500171 external_ip = local_salt_client.pillar_get(
172 tgt='etcd:server',
173 param='kubernetes:common:addons:dashboard:public_ip')
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300174
Oleksii Zhurba3d21fc72019-05-28 18:58:07 -0500175 assert external_ip, "Kubernetes dashboard public ip is not found in pillars"
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300176 assert external_ip.__len__() > 0, "Kubernetes dashboard is enabled but not defined in pillars"
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300177 # dashboard port 8443 is hardcoded in kubernetes formula
178 url = "https://{}:8443".format(external_ip)
179 check = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500180 tgt='etcd:server',
181 param='curl {} 2>&1 | grep kubernetesDashboard'.format(url),
Ievgeniia Zadorozhna7c5f3fd2019-02-05 18:01:33 +0300182 expr_form='pillar'
183 )
184 assert len(check.values()[0]) != 0, \
185 'Kubernetes dashboard is not reachable on {} ' \
186 'from ctl nodes'.format(url)