blob: 6510a67dcd802ee5610e44b4073d3205a3022b16 [file] [log] [blame]
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +04001# Copyright 2017 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import pytest
Vladimir Jigulin34dfa942018-07-23 21:05:48 +040016import netaddr
17import os
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040018
19from tcp_tests import logger
20from tcp_tests import settings
21
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040022from tcp_tests.managers.k8s import read_yaml_file
23
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040024LOG = logger.logger
25
26
27class TestMCPK8sActions(object):
28 """Test class for different k8s actions"""
29
Tatyana Leontovichc411ec32017-10-09 14:48:00 +030030 @pytest.mark.grab_versions
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040031 @pytest.mark.fail_snapshot
Tatyana Leontovich071ce6a2017-10-24 18:08:10 +030032 @pytest.mark.cz8116
Dennis Dmitriev0f624a82018-06-11 12:57:13 +030033 @pytest.mark.k8s_calico
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040034 def test_k8s_externaldns_coredns(self, show_step, config, k8s_deployed):
35 """Test externaldns integration with coredns
36
37 Scenario:
38 1. Install k8s with externaldns addon enabled(including etcd, coredns)
39 2. Start simple service
40 3. Expose deployment
41 4. Annotate service with domain name
42 5. Try to get service using nslookup
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040043 6. Delete service and deployment
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040044 """
45
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040046 show_step(1)
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040047 if not (config.k8s_deploy.kubernetes_externaldns_enabled and
48 config.k8s_deploy.kubernetes_coredns_enabled):
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040049 pytest.skip("Test requires externaldns and coredns addons enabled")
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040050
51 show_step(2)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040052 deployment = k8s_deployed.run_sample_deployment('test-dep')
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040053
54 show_step(3)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040055 svc = deployment.expose()
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040056
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040057 show_step(4)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040058 hostname = "test.{0}.local.".format(settings.LAB_CONFIG_NAME)
59 svc.patch({
60 "metadata": {
61 "annotations": {
62 "external-dns.alpha.kubernetes.io/hostname": hostname
63 }
64 }
65 })
Victor Ryzhenkin14354ac2017-09-27 17:42:30 +040066
67 show_step(5)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040068 k8s_deployed.nslookup(hostname, svc.get_ip())
69
70 show_step(6)
71 deployment.delete()
Victor Ryzhenkin87a31422018-03-16 22:25:27 +040072
73 @pytest.mark.grab_versions
74 @pytest.mark.cncf_publisher(name=['e2e.log', 'junit_01.xml', 'version.txt',
75 'cncf_results.tar.gz'])
76 @pytest.mark.fail_snapshot
77 def test_k8s_cncf_certification(self, show_step, config, k8s_deployed,
Vladimir Jigulin0c8dd5a2018-08-28 05:08:35 +040078 k8s_cncf_log_helper):
Victor Ryzhenkin87a31422018-03-16 22:25:27 +040079 """Run cncf e2e suite and provide files needed for pull request
80 to the CNCF repo
81
82 Scenario:
83 1. Run cncf from https://github.com/cncf/k8s-conformance
84 """
85
86 show_step(1)
87 k8s_deployed.start_k8s_cncf_verification()
Vladimir Jigulin62bcf462018-05-28 18:17:01 +040088
89 @pytest.mark.grap_versions
90 @pytest.mark.fail_snapshot
91 def test_k8s_chain_update(self, show_step, underlay, config, k8s_deployed,
92 k8s_chain_update_log_helper):
93 """Test for chain-upgrading k8s hypercube pool and checking it
94
95 Scenario:
96 1. Prepare salt on hosts
97 2. Setup controller nodes
98 3. Setup compute nodes
99 4. Setup Kubernetes cluster
100 5. Run and expose sample test service
101 6. Run conformance to check consistency
102 7. For every version in update chain:
103 Update cluster to new version, check test sample service
104 availability, run conformance
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400105 8. Delete service and deployment
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400106 """
107
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400108 show_step(5)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400109 sample = k8s_deployed.run_sample_deployment('test-dep-chain-upgrade')
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400110 sample.expose()
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400111 sample.wait_ready()
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400112
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400113 assert sample.is_service_available()
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400114
115 show_step(6)
116 k8s_deployed.run_conformance(log_out="k8s_conformance.log")
117
118 show_step(7)
119 chain_versions = config.k8s.k8s_update_chain.split(" ")
120 for version in chain_versions:
121 LOG.info("Chain update to '{}' version".format(version))
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400122 k8s_deployed.update_k8s_version(version)
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400123
124 LOG.info("Checking test service availability")
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400125 assert sample.is_service_available()
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400126
127 LOG.info("Running conformance on {} version".format(version))
128 log_name = "k8s_conformance_{}.log".format(version)
129 k8s_deployed.run_conformance(log_out=log_name, raise_on_err=False)
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400130
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400131 assert sample.is_service_available()
132
133 show_step(8)
134 sample.delete()
135
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400136 @pytest.mark.grap_versions
137 @pytest.mark.fail_snapshot
138 def test_k8s_metallb(self, show_step, config, k8s_deployed):
139 """Enable metallb in cluster and do basic tests
140
141 Scenario:
142 1. Setup Kubernetes cluster with enabled metallb
143 2. Check that metallb pods created in metallb-system namespace
144 3. Run 5 sample deployments
145 4. Expose deployments with type=LoadBalancer
146 5. Check services availability from outside of cluster
147 6. Run conformance
148 7. Check services availability from outside of cluster
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400149 8. Delete deployments
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400150 """
151 show_step(1)
152 if not config.k8s_deploy.kubernetes_metallb_enabled:
153 pytest.skip("Test requires metallb addon enabled")
154
155 show_step(2)
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400156 ns = "metallb-system"
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400157 assert \
158 len(k8s_deployed.api.pods.list(ns, name_prefix="controller")) > 0
159 assert \
160 len(k8s_deployed.api.pods.list(ns, name_prefix="speaker")) > 0
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400161
162 show_step(3)
163 samples = []
164 for i in range(5):
165 name = 'test-dep-metallb-{}'.format(i)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400166 samples.append(k8s_deployed.run_sample_deployment(name))
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400167
168 show_step(4)
169 for sample in samples:
170 sample.expose('LoadBalancer')
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400171 sample.wait_ready()
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400172
173 show_step(5)
174 for sample in samples:
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400175 assert sample.is_service_available(external=False)
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400176 assert sample.is_service_available(external=True)
177
178 show_step(6)
179 k8s_deployed.run_conformance()
180
181 show_step(7)
182 for sample in samples:
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400183 assert sample.is_service_available(external=False)
Vladimir Jigulina6b018b2018-07-18 15:19:01 +0400184 assert sample.is_service_available(external=True)
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400185
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400186 show_step(8)
187 for sample in samples:
188 sample.delete()
189
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400190 @pytest.mark.grap_versions
191 @pytest.mark.fail_snapshot
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400192 def test_k8s_genie_flannel(self, show_step, config,
193 salt_deployed, k8s_deployed):
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400194 """Test genie-cni+flannel cni setup
195
196 Scenario:
197 1. Setup Kubernetes cluster with genie cni and flannel
198 2. Check that flannel pods created in kube-system namespace
199 3. Create sample deployment with flannel cni annotation
200 4. Check that the deployment have 1 ip addresses from cni provider
201 5. Create sample deployment with calico cni annotation
202 6. Check that the deployment have 1 ip addresses from cni provider
203 7. Create sample deployment with multi-cni annotation
204 8. Check that the deployment have 2 ip addresses from different
205 cni providers
206 9. Create sample deployment without cni annotation
207 10. Check that the deployment have 1 ip address
208 11. Check pods availability
209 12. Run conformance
210 13. Check pods availability
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400211 14. Delete pods
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400212 """
213 show_step(1)
214
215 # Find out calico and flannel networks
216 tgt_k8s_control = "I@kubernetes:control:enabled:True"
217
218 flannel_pillar = salt_deployed.get_pillar(
219 tgt=tgt_k8s_control,
220 pillar="kubernetes:master:network:flannel:private_ip_range")[0]
221 flannel_network = netaddr.IPNetwork(flannel_pillar.values()[0])
222 LOG.info("Flannel network: {}".format(flannel_network))
223
224 calico_network_pillar = salt_deployed.get_pillar(
225 tgt=tgt_k8s_control, pillar="_param:calico_private_network")[0]
226 calico_netmask_pillar = salt_deployed.get_pillar(
227 tgt=tgt_k8s_control, pillar="_param:calico_private_netmask")[0]
228 calico_network = netaddr.IPNetwork(
229 "{0}/{1}".format(calico_network_pillar.values()[0],
230 calico_netmask_pillar.values()[0]))
231 LOG.info("Calico network: {}".format(calico_network))
232
233 show_step(2)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400234 assert k8s_deployed.api.pods.list(
235 namespace="kube-system", name_prefix="kube-flannel-") > 0
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400236
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400237 show_step(3)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400238 data_dir = os.path.join(os.path.dirname(__file__), 'testdata/k8s')
239 flannel_pod = k8s_deployed.api.pods.create(
240 body=read_yaml_file(data_dir, 'pod-sample-flannel.yaml'))
241 flannel_pod.wait_running()
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400242
243 show_step(4)
244 flannel_ips = k8s_deployed.get_pod_ips_from_container(flannel_pod.name)
245 assert len(flannel_ips) == 1
246 assert netaddr.IPAddress(flannel_ips[0]) in flannel_network
247
248 show_step(5)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400249 calico_pod = k8s_deployed.api.pods.create(
250 body=read_yaml_file(data_dir, 'pod-sample-calico.yaml'))
251 calico_pod.wait_running()
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400252
253 show_step(6)
254 calico_ips = k8s_deployed.get_pod_ips_from_container(calico_pod.name)
255 assert len(calico_ips) == 1
256 assert netaddr.IPAddress(calico_ips[0]) in calico_network
257
258 show_step(7)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400259 multicni_pod = k8s_deployed.api.pods.create(
260 body=read_yaml_file(data_dir, 'pod-sample-multicni.yaml'))
261 multicni_pod.wait_running()
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400262
263 show_step(8)
264 multicni_ips = \
265 k8s_deployed.get_pod_ips_from_container(multicni_pod.name)
266 assert len(multicni_ips) == 2
267 for net in [calico_network, flannel_network]:
268 assert netaddr.IPAddress(multicni_ips[0]) in net or \
269 netaddr.IPAddress(multicni_ips[1]) in net
270
271 show_step(9)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400272 nocni_pod = k8s_deployed.api.pods.create(
273 body=read_yaml_file(data_dir, 'pod-sample.yaml'))
274 nocni_pod.wait_running()
Vladimir Jigulin34dfa942018-07-23 21:05:48 +0400275
276 show_step(10)
277 nocni_ips = k8s_deployed.get_pod_ips_from_container(nocni_pod.name)
278 assert len(nocni_ips) == 1
279 assert (netaddr.IPAddress(nocni_ips[0]) in calico_network or
280 netaddr.IPAddress(nocni_ips[0]) in flannel_network)
281
282 show_step(11)
283
284 def check_pod_availability(ip):
285 assert "Hello Kubernetes!" in k8s_deployed.curl(
286 "http://{}:8080".format(ip))
287
288 def check_pods_availability():
289 check_pod_availability(flannel_ips[0])
290 check_pod_availability(calico_ips[0])
291 check_pod_availability(multicni_ips[0])
292 check_pod_availability(multicni_ips[1])
293 check_pod_availability(nocni_ips[0])
294
295 check_pods_availability()
296
297 show_step(12)
298 k8s_deployed.run_conformance()
299
300 show_step(13)
301 check_pods_availability()
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400302
303 show_step(14)
304 flannel_pod.delete()
305 calico_pod.delete()
306 multicni_pod.delete()
307 nocni_pod.delete()