Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 1 | # 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 | |
| 15 | import pytest |
| 16 | |
| 17 | from devops.helpers import helpers |
| 18 | |
| 19 | from tcp_tests import logger |
| 20 | from tcp_tests.helpers import netchecker |
| 21 | |
| 22 | LOG = logger.logger |
| 23 | |
| 24 | |
| 25 | class TestMCPCalico(object): |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 26 | """Test class for Calico network provider in k8s. |
| 27 | Common calico tests requirements: |
| 28 | KUBERNETES_NETCHECKER_ENABLED=true |
| 29 | """ |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 30 | |
| 31 | @pytest.mark.fail_snapshot |
Vladimir Jigulin | eb8b813 | 2019-03-19 15:34:02 +0400 | [diff] [blame] | 32 | @pytest.mark.k8s_system |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 33 | def test_k8s_netchecker_calico(self, show_step, config, k8s_deployed): |
| 34 | """Test for deploying k8s environment with Calico plugin and check |
| 35 | network connectivity between different pods by k8s-netchecker |
| 36 | |
| 37 | Scenario: |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 38 | 1. Check k8s installation. |
| 39 | 2. Get network verification status. Excepted status is 'OK'. |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 40 | |
| 41 | Duration: 3000 seconds |
| 42 | """ |
| 43 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 44 | show_step(1) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 45 | nch = netchecker.Netchecker(k8s_deployed.api) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 46 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 47 | show_step(2) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 48 | nch.wait_check_network(works=True) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 49 | |
| 50 | @pytest.mark.fail_snapshot |
Artem Panchenko | 0872ec0 | 2017-06-29 17:14:12 +0300 | [diff] [blame] | 51 | @pytest.mark.calico_ci |
Tatyana Leontovich | 071ce6a | 2017-10-24 18:08:10 +0300 | [diff] [blame] | 52 | @pytest.mark.cz8116 |
Dennis Dmitriev | 0f624a8 | 2018-06-11 12:57:13 +0300 | [diff] [blame] | 53 | @pytest.mark.k8s_calico |
Vladimir Jigulin | eb8b813 | 2019-03-19 15:34:02 +0400 | [diff] [blame] | 54 | @pytest.mark.k8s_system |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 55 | def test_calico_route_recovery(self, show_step, config, underlay, |
| 56 | k8s_deployed): |
| 57 | """Test for deploying k8s environment with Calico plugin and check |
| 58 | that local routes are recovered by felix after removal |
| 59 | |
| 60 | Scenario: |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 61 | 1. Check k8s installation. |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 62 | 2. Check netchecker-server service. |
| 63 | 3. Check netchecker-agent daemon set. |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 64 | 4. Get network verification status. Excepted status is 'OK'. |
| 65 | 5. Get metrics from netchecker. |
| 66 | 6. Remove local route to netchecker-agent pod on the first node. |
| 67 | 7. Check that the route is automatically recovered. |
| 68 | 8. Get network verification status. Excepted status is 'OK'. |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 69 | |
| 70 | Duration: 3000 seconds |
| 71 | """ |
| 72 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 73 | show_step(1) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 74 | nch = netchecker.Netchecker(k8s_deployed.api) |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 75 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 76 | show_step(2) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 77 | nch.wait_netchecker_pods_running('netchecker-server') |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 78 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 79 | show_step(3) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 80 | nch.wait_netchecker_pods_running('netchecker-agent') |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 81 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 82 | show_step(4) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 83 | nch.wait_check_network(works=True) |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 84 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 85 | show_step(5) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 86 | res = nch.get_metric() |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 87 | |
| 88 | assert res.status_code == 200, 'Unexpected response code {}'\ |
| 89 | .format(res) |
| 90 | metrics = ['ncagent_error_count_total', 'ncagent_http_probe_code', |
| 91 | 'ncagent_http_probe_connect_time_ms', |
| 92 | 'ncagent_http_probe_connection_result', |
| 93 | 'ncagent_http_probe_content_transfer_time_ms', |
| 94 | 'ncagent_http_probe_dns_lookup_time_ms', |
| 95 | 'ncagent_http_probe_server_processing_time_ms', |
| 96 | 'ncagent_http_probe_tcp_connection_time_ms', |
| 97 | 'ncagent_http_probe_total_time_ms', |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 98 | 'ncagent_report_count_total'] |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 99 | for metric in metrics: |
| 100 | assert metric in res.text.strip(), \ |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 101 | 'Mandatory metric {0} is missing in {1}'.format( |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 102 | metric, res.text) |
| 103 | |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 104 | show_step(6) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 105 | first_node = k8s_deployed.api.nodes.list()[0] |
| 106 | first_node_ips = [addr.address for addr in |
| 107 | first_node.read().status.addresses |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 108 | if 'IP' in addr.type] |
| 109 | assert len(first_node_ips) > 0, "Couldn't find first k8s node IP!" |
| 110 | first_node_names = [name for name in underlay.node_names() |
| 111 | if name.startswith(first_node.name)] |
Victor Ryzhenkin | 0c37382 | 2018-10-30 17:55:50 +0400 | [diff] [blame] | 112 | first_node_name = first_node_names[0] |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 113 | |
| 114 | target_pod_ip = None |
| 115 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 116 | for pod in k8s_deployed.api.pods.list(namespace='netchecker'): |
| 117 | LOG.debug('NC pod IP: {0}'.format(pod.read().status.pod_ip)) |
| 118 | if pod.read().status.host_ip not in first_node_ips: |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 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: |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 122 | target_pod_ip = pod.read().status.pod_ip |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 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 | |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 132 | show_step(7) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 133 | route_chk_cmd = 'ip route list | grep -q "{0}"'.format(target_pod_ip) |
| 134 | helpers.wait_pass( |
| 135 | lambda: underlay.sudo_check_call(cmd=route_chk_cmd, |
| 136 | node_name=first_node_name), |
Valentyn Yakovlev | 13a0fc2 | 2017-08-01 11:21:57 +0300 | [diff] [blame] | 137 | timeout=120, |
| 138 | interval=2 |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 139 | ) |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 140 | pod_ping_cmd = 'sleep 120 && ping -q -c 1 -w 3 {0}'.format( |
| 141 | target_pod_ip) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 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 Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 144 | 'recovered'.format(target_pod_ip, first_node.name)) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 145 | |
Dmitry Tyzhnenko | e20367c | 2017-10-27 19:16:45 +0300 | [diff] [blame] | 146 | show_step(8) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 147 | nch.wait_check_network(works=True) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 148 | |
| 149 | @pytest.mark.fail_snapshot |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 150 | @pytest.mark.calico_ci |
Vladimir Jigulin | eb8b813 | 2019-03-19 15:34:02 +0400 | [diff] [blame] | 151 | @pytest.mark.k8s_system |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 152 | def test_calico_network_policies(self, show_step, config, underlay, |
| 153 | k8s_deployed): |
| 154 | """Test for deploying k8s environment with Calico and check |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 155 | that network policies work as expected. |
| 156 | Policy test additional requirement: |
| 157 | KUBERNETES_CALICO_POLICY_ENABLED=true |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 158 | |
| 159 | Scenario: |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 160 | 1. Check k8s installation. |
| 161 | 2. Get network verification status. Excepted status is 'OK'. |
| 162 | 3. Enable network isolation for 'netchecker' namespace. |
| 163 | 4. Allow connections to netchecker-server from tests. |
| 164 | 5. Get network verification status. Excepted status is 'FAIL' |
| 165 | because no netcheker-agent pods should be able to reach |
| 166 | netchecker-service pod. |
| 167 | 6. Add kubernetes network policies which allow connections |
| 168 | from netchecker-agent pods (including ones with host network). |
| 169 | 7. Get network verification status. Excepted status is 'OK'. |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 170 | |
| 171 | Duration: 3000 seconds |
| 172 | """ |
| 173 | |
| 174 | show_step(1) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 175 | kube_master_nodes = k8s_deployed.get_masters() |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 176 | assert kube_master_nodes, "No k8s masters found in pillars!" |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 177 | |
| 178 | nch = netchecker.Netchecker(k8s_deployed.api) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 179 | |
| 180 | show_step(2) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 181 | nch.wait_check_network(works=True) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 182 | |
| 183 | show_step(3) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 184 | nch.kubernetes_block_traffic_namespace() |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 185 | |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 186 | show_step(4) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 187 | nch.calico_allow_netchecker_connections() |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 188 | |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 189 | show_step(5) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 190 | nch.wait_check_network(works=False) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 191 | |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 192 | show_step(6) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 193 | nch.kubernetes_allow_traffic_from_agents() |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 194 | |
Aleksei Kasatkin | 99dd786 | 2018-05-23 17:58:07 +0200 | [diff] [blame] | 195 | show_step(7) |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 196 | nch.wait_check_network(works=True) |