blob: cc44ba414c49b34fdfdebfde8545c39e72dd566e [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
29 def test_k8s_netchecker_calico(self, show_step, config, k8s_deployed):
30 """Test for deploying k8s environment with Calico plugin and check
31 network connectivity between different pods by k8s-netchecker
32
33 Scenario:
34 1. Install k8s with Calico network plugin.
35 2. Run netchecker-server service.
36 3. Run netchecker-agent daemon set.
37 4. Get network verification status. Check status is 'OK'.
38
39 Duration: 3000 seconds
40 """
41
42 # STEP #1
43 show_step(1)
44 k8sclient = k8s_deployed.api
45 assert k8sclient.nodes.list() is not None, "Can not get nodes list"
46
47 # STEP #2
48 show_step(2)
49 netchecker.start_server(k8s=k8s_deployed, config=config)
50 netchecker.wait_check_network(k8sclient, works=True,
51 timeout=300)
52
53 # STEP #3
54 show_step(3)
55 netchecker.start_agent(k8s=k8s_deployed, config=config)
56
57 # STEP #4
58 show_step(4)
59 netchecker.wait_check_network(k8sclient, works=True,
60 timeout=300)
61
62 @pytest.mark.fail_snapshot
Artem Panchenko0872ec02017-06-29 17:14:12 +030063 @pytest.mark.calico_ci
Tatyana Leontovich071ce6a2017-10-24 18:08:10 +030064 @pytest.mark.cz8116
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.
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030072 2. Check netchecker-server service.
73 3. Check netchecker-agent daemon set.
Artem Panchenko501e67e2017-06-14 14:59:18 +030074 4. Get network verification status. Check status is 'OK'.
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030075 5. Get metrics from netchecker
76 6. Remove local route to netchecker-agent pod on the first node
77 7. Check that the route is automatically recovered
78 8. Get network verification status. Check status is 'OK'.
Artem Panchenko501e67e2017-06-14 14:59:18 +030079
80 Duration: 3000 seconds
81 """
82
Artem Panchenko501e67e2017-06-14 14:59:18 +030083 show_step(1)
84 k8sclient = k8s_deployed.api
85 assert k8sclient.nodes.list() is not None, "Can not get nodes list"
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030086 netchecker_port = netchecker.get_service_port(k8sclient)
Artem Panchenko501e67e2017-06-14 14:59:18 +030087 show_step(2)
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030088 netchecker.get_netchecker_pod_status(k8s=k8s_deployed,
89 namespace='netchecker')
Artem Panchenko501e67e2017-06-14 14:59:18 +030090
Artem Panchenko501e67e2017-06-14 14:59:18 +030091 show_step(3)
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030092 netchecker.get_netchecker_pod_status(k8s=k8s_deployed,
93 pod_name='netchecker-agent',
94 namespace='netchecker')
Artem Panchenko501e67e2017-06-14 14:59:18 +030095
Artem Panchenko501e67e2017-06-14 14:59:18 +030096 show_step(4)
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030097 netchecker.wait_check_network(k8sclient, namespace='netchecker',
98 netchecker_pod_port=netchecker_port)
Artem Panchenko501e67e2017-06-14 14:59:18 +030099 show_step(5)
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300100 res = netchecker.get_metric(k8sclient,
101 netchecker_pod_port=netchecker_port,
102 namespace='netchecker')
103
104 assert res.status_code == 200, 'Unexpected response code {}'\
105 .format(res)
106 metrics = ['ncagent_error_count_total', 'ncagent_http_probe_code',
107 'ncagent_http_probe_connect_time_ms',
108 'ncagent_http_probe_connection_result',
109 'ncagent_http_probe_content_transfer_time_ms',
110 'ncagent_http_probe_dns_lookup_time_ms',
111 'ncagent_http_probe_server_processing_time_ms',
112 'ncagent_http_probe_tcp_connection_time_ms',
113 'ncagent_http_probe_total_time_ms',
114 'ncagent_report_count_tota']
115 for metric in metrics:
116 assert metric in res.text.strip(), \
117 'Mandotory metric {0} is missing in {1}'.format(
118 metric, res.text)
119
120 # STEP #6
121 show_step(6)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300122 first_node = k8sclient.nodes.list()[0]
123 first_node_ips = [addr.address for addr in first_node.status.addresses
124 if 'IP' in addr.type]
125 assert len(first_node_ips) > 0, "Couldn't find first k8s node IP!"
126 first_node_names = [name for name in underlay.node_names()
127 if name.startswith(first_node.name)]
128 assert len(first_node_names) == 1, "Couldn't find first k8s node " \
129 "hostname in SSH config!"
130 first_node_name = first_node_names.pop()
131
132 target_pod_ip = None
133
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300134 for pod in k8sclient.pods.list(namespace='netchecker'):
Artem Panchenko501e67e2017-06-14 14:59:18 +0300135 if pod.status.host_ip not in first_node_ips:
136 continue
137 # TODO: get pods by daemonset with name 'netchecker-agent'
138 if 'netchecker-agent-' in pod.name and 'hostnet' not in pod.name:
139 target_pod_ip = pod.status.pod_ip
140
141 assert target_pod_ip is not None, "Could not find netchecker pod IP!"
142
143 route_del_cmd = 'ip route delete {0}'.format(target_pod_ip)
144 underlay.sudo_check_call(cmd=route_del_cmd, node_name=first_node_name)
145 LOG.debug('Removed local route to pod IP {0} on node {1}'.format(
146 target_pod_ip, first_node.name
147 ))
148
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300149 # STEP #7
150 show_step(7)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300151 route_chk_cmd = 'ip route list | grep -q "{0}"'.format(target_pod_ip)
152 helpers.wait_pass(
153 lambda: underlay.sudo_check_call(cmd=route_chk_cmd,
154 node_name=first_node_name),
Valentyn Yakovlev13a0fc22017-08-01 11:21:57 +0300155 timeout=120,
156 interval=2
Artem Panchenko501e67e2017-06-14 14:59:18 +0300157 )
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300158 pod_ping_cmd = 'sleep 120 && ping -q -c 1 -w 3 {0}'.format(
159 target_pod_ip)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300160 underlay.sudo_check_call(cmd=pod_ping_cmd, node_name=first_node_name)
161 LOG.debug('Local route to pod IP {0} on node {1} is '
Dina Belovae6fdffb2017-09-19 13:58:34 -0700162 'recovered'.format(target_pod_ip, first_node.name))
Artem Panchenko501e67e2017-06-14 14:59:18 +0300163
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300164 # STEP #8
165 show_step(8)
166 netchecker.wait_check_network(k8sclient, namespace='netchecker',
167 netchecker_pod_port=netchecker_port,
168 works=True)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300169
170 @pytest.mark.fail_snapshot
valentyn.yakovlev361a6792017-07-20 07:44:43 -0400171 # FIXME(apanchenko): uncomment as soon as the following bug is fixed
172 # FIXME(apanchenko): https://mirantis.jira.com/browse/PROD-12532
Dina Belovae6fdffb2017-09-19 13:58:34 -0700173 # @pytest.mark.calico_ci
Artem Panchenko501e67e2017-06-14 14:59:18 +0300174 def test_calico_network_policies(self, show_step, config, underlay,
175 k8s_deployed):
176 """Test for deploying k8s environment with Calico and check
177 that network policies work as expected
178
179 Scenario:
180 1. Install k8s.
181 2. Create new namespace 'netchecker'
182 3. Run netchecker-server service
183 4. Check that netchecker-server returns '200 OK'
184 5. Run netchecker-agent daemon set in default namespace
185 6. Get network verification status. Check status is 'OK'
186 7. Enable network isolation for 'netchecker' namespace
187 8. Allow connections to netchecker-server from tests using
188 Calico policy
189 9. Get network verification status. Check status is 'FAIL' because
190 no netcheker-agent pods can reach netchecker-service pod
191 10. Add kubernetes network policies which allow connections
192 from netchecker-agent pods (including ones with host network)
193 11. Get network verification status. Check status is 'OK'
194
195 Duration: 3000 seconds
196 """
197
198 show_step(1)
199 k8sclient = k8s_deployed.api
200 assert k8sclient.nodes.list() is not None, "Can not get nodes list"
201 kube_master_nodes = k8s_deployed.get_k8s_masters()
202 assert kube_master_nodes, "No k8s masters found in pillars!"
203
204 show_step(2)
205 k8s_deployed.check_namespace_create(name='netchecker')
206
207 show_step(3)
208 netchecker.start_server(k8s=k8s_deployed, config=config,
209 namespace='netchecker')
210
211 show_step(4)
212 netchecker.wait_check_network(k8sclient, namespace='netchecker',
213 works=True)
214
215 show_step(5)
216 netchecker.start_agent(k8s=k8s_deployed, config=config,
217 namespace='default',
218 service_namespace='netchecker')
219
220 show_step(6)
221 netchecker.wait_check_network(k8sclient, namespace='netchecker',
222 works=True, timeout=300)
223
224 show_step(7)
225 netchecker.kubernetes_block_traffic_namespace(underlay,
226 kube_master_nodes[0],
227 'netchecker')
228
229 show_step(8)
230 netchecker.calico_allow_netchecker_connections(underlay,
231 kube_master_nodes[0],
232 config.k8s.kube_host,
233 'netchecker')
234
235 show_step(9)
236 netchecker.wait_check_network(k8sclient, namespace='netchecker',
237 works=False, timeout=500)
238
239 show_step(10)
240 netchecker.kubernetes_allow_traffic_from_agents(underlay,
241 kube_master_nodes[0],
242 'netchecker')
243
244 show_step(11)
245 netchecker.wait_check_network(k8sclient, namespace='netchecker',
246 works=True, timeout=300)