blob: 6f28327d27bb339204d07f0d1460d41b15fc5612 [file] [log] [blame]
Artem Panchenko501e67e2017-06-14 14:59:18 +03001# 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
16
17from devops.helpers import helpers
18
19from tcp_tests import logger
20from tcp_tests.helpers import netchecker
21
22LOG = logger.logger
23
24
25class TestMCPCalico(object):
26 """Test class for Calico network provider in k8s"""
27
28 @pytest.mark.fail_snapshot
Artem Panchenko0872ec02017-06-29 17:14:12 +030029 @pytest.mark.calico_ci
Artem Panchenko501e67e2017-06-14 14:59:18 +030030 def test_k8s_netchecker_calico(self, show_step, config, k8s_deployed):
31 """Test for deploying k8s environment with Calico plugin and check
32 network connectivity between different pods by k8s-netchecker
33
34 Scenario:
35 1. Install k8s with Calico network plugin.
36 2. Run netchecker-server service.
37 3. Run netchecker-agent daemon set.
38 4. Get network verification status. Check status is 'OK'.
39
40 Duration: 3000 seconds
41 """
42
43 # STEP #1
44 show_step(1)
45 k8sclient = k8s_deployed.api
46 assert k8sclient.nodes.list() is not None, "Can not get nodes list"
47
48 # STEP #2
49 show_step(2)
50 netchecker.start_server(k8s=k8s_deployed, config=config)
51 netchecker.wait_check_network(k8sclient, works=True,
52 timeout=300)
53
54 # STEP #3
55 show_step(3)
56 netchecker.start_agent(k8s=k8s_deployed, config=config)
57
58 # STEP #4
59 show_step(4)
60 netchecker.wait_check_network(k8sclient, works=True,
61 timeout=300)
62
63 @pytest.mark.fail_snapshot
Artem Panchenko0872ec02017-06-29 17:14:12 +030064 @pytest.mark.calico_ci
Artem Panchenko501e67e2017-06-14 14:59:18 +030065 def test_calico_route_recovery(self, show_step, config, underlay,
66 k8s_deployed):
67 """Test for deploying k8s environment with Calico plugin and check
68 that local routes are recovered by felix after removal
69
70 Scenario:
71 1. Install k8s with Calico network plugin.
72 2. Run netchecker-server service.
73 3. Run netchecker-agent daemon set.
74 4. Get network verification status. Check status is 'OK'.
75 5. Remove local route to netchecker-agent pod on the first node
76 6. Check that the route is automatically recovered
77 7. Get network verification status. Check status is 'OK'.
78
79 Duration: 3000 seconds
80 """
81
82 # STEP #1
83 show_step(1)
84 k8sclient = k8s_deployed.api
85 assert k8sclient.nodes.list() is not None, "Can not get nodes list"
86
87 # STEP #2
88 show_step(2)
89 netchecker.start_server(k8s=k8s_deployed, config=config)
90 LOG.info("Waiting for netchecker server is running")
91 netchecker.wait_check_network(k8sclient, works=True,
92 timeout=300)
93
94 # STEP #3
95 show_step(3)
96 netchecker.start_agent(k8s=k8s_deployed, config=config)
97
98 # STEP #4
99 show_step(4)
100 netchecker.wait_check_network(k8sclient, works=True,
101 timeout=300)
102
103 # STEP #5
104 show_step(5)
105 first_node = k8sclient.nodes.list()[0]
106 first_node_ips = [addr.address for addr in first_node.status.addresses
107 if 'IP' in addr.type]
108 assert len(first_node_ips) > 0, "Couldn't find first k8s node IP!"
109 first_node_names = [name for name in underlay.node_names()
110 if name.startswith(first_node.name)]
111 assert len(first_node_names) == 1, "Couldn't find first k8s node " \
112 "hostname in SSH config!"
113 first_node_name = first_node_names.pop()
114
115 target_pod_ip = None
116
117 for pod in k8sclient.pods.list():
118 if pod.status.host_ip not in first_node_ips:
119 continue
120 # TODO: get pods by daemonset with name 'netchecker-agent'
121 if 'netchecker-agent-' in pod.name and 'hostnet' not in pod.name:
122 target_pod_ip = pod.status.pod_ip
123
124 assert target_pod_ip is not None, "Could not find netchecker pod IP!"
125
126 route_del_cmd = 'ip route delete {0}'.format(target_pod_ip)
127 underlay.sudo_check_call(cmd=route_del_cmd, node_name=first_node_name)
128 LOG.debug('Removed local route to pod IP {0} on node {1}'.format(
129 target_pod_ip, first_node.name
130 ))
131
132 # STEP #6
133 show_step(6)
134 route_chk_cmd = 'ip route list | grep -q "{0}"'.format(target_pod_ip)
135 helpers.wait_pass(
136 lambda: underlay.sudo_check_call(cmd=route_chk_cmd,
137 node_name=first_node_name),
Valentyn Yakovlev13a0fc22017-08-01 11:21:57 +0300138 timeout=120,
139 interval=2
Artem Panchenko501e67e2017-06-14 14:59:18 +0300140 )
141 pod_ping_cmd = 'sleep 3 && ping -q -c 1 -w 3 {0}'.format(target_pod_ip)
142 underlay.sudo_check_call(cmd=pod_ping_cmd, node_name=first_node_name)
143 LOG.debug('Local route to pod IP {0} on node {1} is '
Dina Belovae6fdffb2017-09-19 13:58:34 -0700144 'recovered'.format(target_pod_ip, first_node.name))
Artem Panchenko501e67e2017-06-14 14:59:18 +0300145
146 # STEP #7
147 show_step(7)
148 netchecker.wait_check_network(k8sclient, works=True)
149
150 @pytest.mark.fail_snapshot
valentyn.yakovlev361a6792017-07-20 07:44:43 -0400151 # FIXME(apanchenko): uncomment as soon as the following bug is fixed
152 # FIXME(apanchenko): https://mirantis.jira.com/browse/PROD-12532
Dina Belovae6fdffb2017-09-19 13:58:34 -0700153 # @pytest.mark.calico_ci
Artem Panchenko501e67e2017-06-14 14:59:18 +0300154 def test_calico_network_policies(self, show_step, config, underlay,
155 k8s_deployed):
156 """Test for deploying k8s environment with Calico and check
157 that network policies work as expected
158
159 Scenario:
160 1. Install k8s.
161 2. Create new namespace 'netchecker'
162 3. Run netchecker-server service
163 4. Check that netchecker-server returns '200 OK'
164 5. Run netchecker-agent daemon set in default namespace
165 6. Get network verification status. Check status is 'OK'
166 7. Enable network isolation for 'netchecker' namespace
167 8. Allow connections to netchecker-server from tests using
168 Calico policy
169 9. Get network verification status. Check status is 'FAIL' because
170 no netcheker-agent pods can reach netchecker-service pod
171 10. Add kubernetes network policies which allow connections
172 from netchecker-agent pods (including ones with host network)
173 11. Get network verification status. Check status is 'OK'
174
175 Duration: 3000 seconds
176 """
177
178 show_step(1)
179 k8sclient = k8s_deployed.api
180 assert k8sclient.nodes.list() is not None, "Can not get nodes list"
181 kube_master_nodes = k8s_deployed.get_k8s_masters()
182 assert kube_master_nodes, "No k8s masters found in pillars!"
183
184 show_step(2)
185 k8s_deployed.check_namespace_create(name='netchecker')
186
187 show_step(3)
188 netchecker.start_server(k8s=k8s_deployed, config=config,
189 namespace='netchecker')
190
191 show_step(4)
192 netchecker.wait_check_network(k8sclient, namespace='netchecker',
193 works=True)
194
195 show_step(5)
196 netchecker.start_agent(k8s=k8s_deployed, config=config,
197 namespace='default',
198 service_namespace='netchecker')
199
200 show_step(6)
201 netchecker.wait_check_network(k8sclient, namespace='netchecker',
202 works=True, timeout=300)
203
204 show_step(7)
205 netchecker.kubernetes_block_traffic_namespace(underlay,
206 kube_master_nodes[0],
207 'netchecker')
208
209 show_step(8)
210 netchecker.calico_allow_netchecker_connections(underlay,
211 kube_master_nodes[0],
212 config.k8s.kube_host,
213 'netchecker')
214
215 show_step(9)
216 netchecker.wait_check_network(k8sclient, namespace='netchecker',
217 works=False, timeout=500)
218
219 show_step(10)
220 netchecker.kubernetes_allow_traffic_from_agents(underlay,
221 kube_master_nodes[0],
222 'netchecker')
223
224 show_step(11)
225 netchecker.wait_check_network(k8sclient, namespace='netchecker',
226 works=True, timeout=300)