blob: 6399eb36e9f6394d0dddf594181aa403033dac84 [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):
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020026 """Test class for Calico network provider in k8s.
27 Common calico tests requirements:
28 KUBERNETES_NETCHECKER_ENABLED=true
29 """
Artem Panchenko501e67e2017-06-14 14:59:18 +030030
31 @pytest.mark.fail_snapshot
32 def test_k8s_netchecker_calico(self, show_step, config, k8s_deployed):
33 """Test for deploying k8s environment with Calico plugin and check
34 network connectivity between different pods by k8s-netchecker
35
36 Scenario:
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020037 1. Check k8s installation.
38 2. Get network verification status. Excepted status is 'OK'.
Artem Panchenko501e67e2017-06-14 14:59:18 +030039
40 Duration: 3000 seconds
41 """
42
Artem Panchenko501e67e2017-06-14 14:59:18 +030043 show_step(1)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040044 nch = netchecker.Netchecker(k8s_deployed.api)
Artem Panchenko501e67e2017-06-14 14:59:18 +030045
Artem Panchenko501e67e2017-06-14 14:59:18 +030046 show_step(2)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040047 nch.wait_check_network(works=True)
Artem Panchenko501e67e2017-06-14 14:59:18 +030048
49 @pytest.mark.fail_snapshot
Artem Panchenko0872ec02017-06-29 17:14:12 +030050 @pytest.mark.calico_ci
Tatyana Leontovich071ce6a2017-10-24 18:08:10 +030051 @pytest.mark.cz8116
Dennis Dmitriev0f624a82018-06-11 12:57:13 +030052 @pytest.mark.k8s_calico
Artem Panchenko501e67e2017-06-14 14:59:18 +030053 def test_calico_route_recovery(self, show_step, config, underlay,
54 k8s_deployed):
55 """Test for deploying k8s environment with Calico plugin and check
56 that local routes are recovered by felix after removal
57
58 Scenario:
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020059 1. Check k8s installation.
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030060 2. Check netchecker-server service.
61 3. Check netchecker-agent daemon set.
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020062 4. Get network verification status. Excepted status is 'OK'.
63 5. Get metrics from netchecker.
64 6. Remove local route to netchecker-agent pod on the first node.
65 7. Check that the route is automatically recovered.
66 8. Get network verification status. Excepted status is 'OK'.
Artem Panchenko501e67e2017-06-14 14:59:18 +030067
68 Duration: 3000 seconds
69 """
70
Artem Panchenko501e67e2017-06-14 14:59:18 +030071 show_step(1)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040072 nch = netchecker.Netchecker(k8s_deployed.api)
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020073
Artem Panchenko501e67e2017-06-14 14:59:18 +030074 show_step(2)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040075 nch.wait_netchecker_pods_running('netchecker-server')
Artem Panchenko501e67e2017-06-14 14:59:18 +030076
Artem Panchenko501e67e2017-06-14 14:59:18 +030077 show_step(3)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040078 nch.wait_netchecker_pods_running('netchecker-agent')
Artem Panchenko501e67e2017-06-14 14:59:18 +030079
Artem Panchenko501e67e2017-06-14 14:59:18 +030080 show_step(4)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040081 nch.wait_check_network(works=True)
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020082
Artem Panchenko501e67e2017-06-14 14:59:18 +030083 show_step(5)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +040084 res = nch.get_metric()
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030085
86 assert res.status_code == 200, 'Unexpected response code {}'\
87 .format(res)
88 metrics = ['ncagent_error_count_total', 'ncagent_http_probe_code',
89 'ncagent_http_probe_connect_time_ms',
90 'ncagent_http_probe_connection_result',
91 'ncagent_http_probe_content_transfer_time_ms',
92 'ncagent_http_probe_dns_lookup_time_ms',
93 'ncagent_http_probe_server_processing_time_ms',
94 'ncagent_http_probe_tcp_connection_time_ms',
95 'ncagent_http_probe_total_time_ms',
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020096 'ncagent_report_count_total']
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +030097 for metric in metrics:
98 assert metric in res.text.strip(), \
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +020099 'Mandatory metric {0} is missing in {1}'.format(
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300100 metric, res.text)
101
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300102 show_step(6)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400103 first_node = k8s_deployed.api.nodes.list()[0]
104 first_node_ips = [addr.address for addr in
105 first_node.read().status.addresses
Artem Panchenko501e67e2017-06-14 14:59:18 +0300106 if 'IP' in addr.type]
107 assert len(first_node_ips) > 0, "Couldn't find first k8s node IP!"
108 first_node_names = [name for name in underlay.node_names()
109 if name.startswith(first_node.name)]
Victor Ryzhenkin0c373822018-10-30 17:55:50 +0400110 first_node_name = first_node_names[0]
Artem Panchenko501e67e2017-06-14 14:59:18 +0300111
112 target_pod_ip = None
113
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400114 for pod in k8s_deployed.api.pods.list(namespace='netchecker'):
115 LOG.debug('NC pod IP: {0}'.format(pod.read().status.pod_ip))
116 if pod.read().status.host_ip not in first_node_ips:
Artem Panchenko501e67e2017-06-14 14:59:18 +0300117 continue
118 # TODO: get pods by daemonset with name 'netchecker-agent'
119 if 'netchecker-agent-' in pod.name and 'hostnet' not in pod.name:
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400120 target_pod_ip = pod.read().status.pod_ip
Artem Panchenko501e67e2017-06-14 14:59:18 +0300121
122 assert target_pod_ip is not None, "Could not find netchecker pod IP!"
123
124 route_del_cmd = 'ip route delete {0}'.format(target_pod_ip)
125 underlay.sudo_check_call(cmd=route_del_cmd, node_name=first_node_name)
126 LOG.debug('Removed local route to pod IP {0} on node {1}'.format(
127 target_pod_ip, first_node.name
128 ))
129
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300130 show_step(7)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300131 route_chk_cmd = 'ip route list | grep -q "{0}"'.format(target_pod_ip)
132 helpers.wait_pass(
133 lambda: underlay.sudo_check_call(cmd=route_chk_cmd,
134 node_name=first_node_name),
Valentyn Yakovlev13a0fc22017-08-01 11:21:57 +0300135 timeout=120,
136 interval=2
Artem Panchenko501e67e2017-06-14 14:59:18 +0300137 )
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300138 pod_ping_cmd = 'sleep 120 && ping -q -c 1 -w 3 {0}'.format(
139 target_pod_ip)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300140 underlay.sudo_check_call(cmd=pod_ping_cmd, node_name=first_node_name)
141 LOG.debug('Local route to pod IP {0} on node {1} is '
Dina Belovae6fdffb2017-09-19 13:58:34 -0700142 'recovered'.format(target_pod_ip, first_node.name))
Artem Panchenko501e67e2017-06-14 14:59:18 +0300143
Dmitry Tyzhnenkoe20367c2017-10-27 19:16:45 +0300144 show_step(8)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400145 nch.wait_check_network(works=True)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300146
147 @pytest.mark.fail_snapshot
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200148 @pytest.mark.calico_ci
Artem Panchenko501e67e2017-06-14 14:59:18 +0300149 def test_calico_network_policies(self, show_step, config, underlay,
150 k8s_deployed):
151 """Test for deploying k8s environment with Calico and check
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200152 that network policies work as expected.
153 Policy test additional requirement:
154 KUBERNETES_CALICO_POLICY_ENABLED=true
Artem Panchenko501e67e2017-06-14 14:59:18 +0300155
156 Scenario:
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200157 1. Check k8s installation.
158 2. Get network verification status. Excepted status is 'OK'.
159 3. Enable network isolation for 'netchecker' namespace.
160 4. Allow connections to netchecker-server from tests.
161 5. Get network verification status. Excepted status is 'FAIL'
162 because no netcheker-agent pods should be able to reach
163 netchecker-service pod.
164 6. Add kubernetes network policies which allow connections
165 from netchecker-agent pods (including ones with host network).
166 7. Get network verification status. Excepted status is 'OK'.
Artem Panchenko501e67e2017-06-14 14:59:18 +0300167
168 Duration: 3000 seconds
169 """
170
171 show_step(1)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400172 kube_master_nodes = k8s_deployed.get_masters()
Artem Panchenko501e67e2017-06-14 14:59:18 +0300173 assert kube_master_nodes, "No k8s masters found in pillars!"
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400174
175 nch = netchecker.Netchecker(k8s_deployed.api)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300176
177 show_step(2)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400178 nch.wait_check_network(works=True)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300179
180 show_step(3)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400181 nch.kubernetes_block_traffic_namespace()
Artem Panchenko501e67e2017-06-14 14:59:18 +0300182
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200183 show_step(4)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400184 nch.calico_allow_netchecker_connections()
Artem Panchenko501e67e2017-06-14 14:59:18 +0300185
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200186 show_step(5)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400187 nch.wait_check_network(works=False)
Artem Panchenko501e67e2017-06-14 14:59:18 +0300188
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200189 show_step(6)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400190 nch.kubernetes_allow_traffic_from_agents()
Artem Panchenko501e67e2017-06-14 14:59:18 +0300191
Aleksei Kasatkin99dd7862018-05-23 17:58:07 +0200192 show_step(7)
Vladimir Jigulin4ad52a82018-08-12 05:51:30 +0400193 nch.wait_check_network(works=True)