Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 1 | # Copyright 2016 Red Hat, Inc. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 15 | import distutils |
| 16 | import re |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 17 | import subprocess |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 18 | |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 19 | from debtcollector import removals |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 20 | import netaddr |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 21 | from neutron_lib.api import validators |
| 22 | from neutron_lib import constants as neutron_lib_constants |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 23 | from oslo_log import log |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 24 | from paramiko import ssh_exception as ssh_exc |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 25 | from tempest.common.utils import net_utils |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 26 | from tempest.common import waiters |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 27 | from tempest.lib.common.utils import data_utils |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 28 | from tempest.lib.common.utils import test_utils |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 29 | from tempest.lib import exceptions as lib_exc |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 30 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 31 | from neutron_tempest_plugin.api import base as base_api |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 32 | from neutron_tempest_plugin.common import ip as ip_utils |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 33 | from neutron_tempest_plugin.common import shell |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 34 | from neutron_tempest_plugin.common import ssh |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 35 | from neutron_tempest_plugin.common import utils |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 36 | from neutron_tempest_plugin import config |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 37 | from neutron_tempest_plugin import exceptions |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 38 | from neutron_tempest_plugin.scenario import constants |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 39 | |
| 40 | CONF = config.CONF |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 41 | |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 42 | LOG = log.getLogger(__name__) |
| 43 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 44 | |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 45 | def get_ncat_version(ssh_client=None): |
| 46 | cmd = "ncat --version 2>&1" |
| 47 | try: |
| 48 | version_result = shell.execute(cmd, ssh_client=ssh_client).stdout |
| 49 | except exceptions.ShellCommandFailed: |
| 50 | m = None |
| 51 | else: |
| 52 | m = re.match(r"Ncat: Version ([\d.]+) *.", version_result) |
| 53 | # NOTE(slaweq): by default lets assume we have ncat 7.60 which is in Ubuntu |
| 54 | # 18.04 which is used on u/s gates |
| 55 | return distutils.version.StrictVersion(m.group(1) if m else '7.60') |
| 56 | |
| 57 | |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 58 | def get_ncat_server_cmd(port, protocol, msg=None): |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 59 | udp = '' |
| 60 | if protocol.lower() == neutron_lib_constants.PROTO_NAME_UDP: |
| 61 | udp = '-u' |
| 62 | cmd = "nc %(udp)s -p %(port)s -lk " % { |
| 63 | 'udp': udp, 'port': port} |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 64 | if msg: |
| 65 | if CONF.neutron_plugin_options.default_image_is_advanced: |
Flavio Fernandes | b056ac2 | 2020-07-01 14:57:13 -0400 | [diff] [blame] | 66 | cmd += "-c 'echo %s' " % msg |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 67 | else: |
Flavio Fernandes | b056ac2 | 2020-07-01 14:57:13 -0400 | [diff] [blame] | 68 | cmd += "-e echo %s " % msg |
| 69 | cmd += "< /dev/zero &{0}sleep 0.1{0}".format('\n') |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 70 | return cmd |
| 71 | |
| 72 | |
| 73 | def get_ncat_client_cmd(ip_address, port, protocol): |
| 74 | udp = '' |
| 75 | if protocol.lower() == neutron_lib_constants.PROTO_NAME_UDP: |
| 76 | udp = '-u' |
| 77 | cmd = 'echo "knock knock" | nc ' |
| 78 | ncat_version = get_ncat_version() |
| 79 | if ncat_version > distutils.version.StrictVersion('7.60'): |
| 80 | cmd += '-z ' |
| 81 | cmd += '-w 1 %(udp)s %(host)s %(port)s' % { |
| 82 | 'udp': udp, 'host': ip_address, 'port': port} |
| 83 | return cmd |
| 84 | |
| 85 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 86 | class BaseTempestTestCase(base_api.BaseNetworkTest): |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 87 | |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 88 | def create_server(self, flavor_ref, image_ref, key_name, networks, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 89 | **kwargs): |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 90 | """Create a server using tempest lib |
Brian Haley | aee61ac | 2018-10-09 20:00:27 -0400 | [diff] [blame] | 91 | |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 92 | All the parameters are the ones used in Compute API |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 93 | * - Kwargs that require admin privileges |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 94 | |
| 95 | Args: |
| 96 | flavor_ref(str): The flavor of the server to be provisioned. |
| 97 | image_ref(str): The image of the server to be provisioned. |
| 98 | key_name(str): SSH key to to be used to connect to the |
| 99 | provisioned server. |
| 100 | networks(list): List of dictionaries where each represent |
| 101 | an interface to be attached to the server. For network |
| 102 | it should be {'uuid': network_uuid} and for port it should |
| 103 | be {'port': port_uuid} |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 104 | kwargs: |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 105 | name(str): Name of the server to be provisioned. |
| 106 | security_groups(list): List of dictionaries where |
| 107 | the keys is 'name' and the value is the name of |
| 108 | the security group. If it's not passed the default |
| 109 | security group will be used. |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 110 | availability_zone(str)*: The availability zone that |
| 111 | the instance will be in. |
| 112 | You can request a specific az without actually creating one, |
| 113 | Just pass 'X:Y' where X is the default availability |
| 114 | zone, and Y is the compute host name. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 115 | """ |
| 116 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 117 | kwargs.setdefault('name', data_utils.rand_name('server-test')) |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 118 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 119 | # We cannot use setdefault() here because caller could have passed |
| 120 | # security_groups=None and we don't want to pass None to |
| 121 | # client.create_server() |
| 122 | if not kwargs.get('security_groups'): |
| 123 | kwargs['security_groups'] = [{'name': 'default'}] |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 124 | |
Slawek Kaplonski | 2211eab | 2020-10-20 16:43:53 +0200 | [diff] [blame] | 125 | client = kwargs.pop('client', None) |
| 126 | if client is None: |
| 127 | client = self.os_primary.servers_client |
| 128 | if kwargs.get('availability_zone'): |
| 129 | client = self.os_admin.servers_client |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 130 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 131 | server = client.create_server( |
| 132 | flavorRef=flavor_ref, |
| 133 | imageRef=image_ref, |
| 134 | key_name=key_name, |
| 135 | networks=networks, |
| 136 | **kwargs) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 137 | |
| 138 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 139 | waiters.wait_for_server_termination, |
| 140 | client, |
| 141 | server['server']['id']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 142 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 143 | client.delete_server, |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 144 | server['server']['id']) |
Slawek Kaplonski | 2211eab | 2020-10-20 16:43:53 +0200 | [diff] [blame] | 145 | |
| 146 | self.wait_for_server_active(server['server'], client=client) |
| 147 | self.wait_for_guest_os_ready(server['server'], client=client) |
| 148 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 149 | return server |
| 150 | |
| 151 | @classmethod |
Jens Harbott | 5435763 | 2017-11-21 11:47:06 +0000 | [diff] [blame] | 152 | def create_secgroup_rules(cls, rule_list, secgroup_id=None, |
| 153 | client=None): |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 154 | client = client or cls.os_primary.network_client |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 155 | if not secgroup_id: |
| 156 | sgs = client.list_security_groups()['security_groups'] |
| 157 | for sg in sgs: |
| 158 | if sg['name'] == constants.DEFAULT_SECURITY_GROUP: |
| 159 | secgroup_id = sg['id'] |
| 160 | break |
Hang Yang | e6e0ccf | 2021-02-26 15:07:05 -0600 | [diff] [blame] | 161 | resp = [] |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 162 | for rule in rule_list: |
| 163 | direction = rule.pop('direction') |
Hang Yang | e6e0ccf | 2021-02-26 15:07:05 -0600 | [diff] [blame] | 164 | resp.append(client.create_security_group_rule( |
| 165 | direction=direction, |
| 166 | security_group_id=secgroup_id, |
| 167 | **rule)['security_group_rule']) |
| 168 | return resp |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 169 | |
| 170 | @classmethod |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 171 | def create_loginable_secgroup_rule(cls, secgroup_id=None, |
| 172 | client=None): |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 173 | """This rule is intended to permit inbound ssh |
| 174 | |
| 175 | Allowing ssh traffic traffic from all sources, so no group_id is |
| 176 | provided. |
| 177 | Setting a group_id would only permit traffic from ports |
| 178 | belonging to the same security group. |
| 179 | """ |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 180 | return cls.create_security_group_rule( |
| 181 | security_group_id=secgroup_id, |
| 182 | client=client, |
| 183 | protocol=neutron_lib_constants.PROTO_NAME_TCP, |
| 184 | direction=neutron_lib_constants.INGRESS_DIRECTION, |
| 185 | port_range_min=22, |
| 186 | port_range_max=22) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 187 | |
| 188 | @classmethod |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 189 | def create_pingable_secgroup_rule(cls, secgroup_id=None, |
| 190 | client=None): |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 191 | """This rule is intended to permit inbound ping |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 192 | |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 193 | """ |
| 194 | return cls.create_security_group_rule( |
| 195 | security_group_id=secgroup_id, client=client, |
| 196 | protocol=neutron_lib_constants.PROTO_NAME_ICMP, |
| 197 | direction=neutron_lib_constants.INGRESS_DIRECTION) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 198 | |
| 199 | @classmethod |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 200 | def create_router_by_client(cls, is_admin=False, **kwargs): |
| 201 | kwargs.update({'router_name': data_utils.rand_name('router'), |
| 202 | 'admin_state_up': True, |
| 203 | 'external_network_id': CONF.network.public_network_id}) |
| 204 | if not is_admin: |
| 205 | router = cls.create_router(**kwargs) |
| 206 | else: |
| 207 | router = cls.create_admin_router(**kwargs) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 208 | LOG.debug("Created router %s", router['name']) |
Slawek Kaplonski | edf3cba | 2021-04-21 10:34:02 +0200 | [diff] [blame] | 209 | cls._wait_for_router_ha_active(router['id']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 210 | return router |
| 211 | |
ccamposr | e6b1004 | 2021-02-12 12:26:08 +0100 | [diff] [blame] | 212 | @classmethod |
Slawek Kaplonski | edf3cba | 2021-04-21 10:34:02 +0200 | [diff] [blame] | 213 | def _wait_for_router_ha_active(cls, router_id): |
| 214 | router = cls.os_admin.network_client.show_router(router_id)['router'] |
| 215 | if not router.get('ha'): |
| 216 | return |
| 217 | |
| 218 | def _router_active_on_l3_agent(): |
| 219 | agents = cls.os_admin.network_client.list_l3_agents_hosting_router( |
| 220 | router_id)['agents'] |
| 221 | return "active" in [agent['ha_state'] for agent in agents] |
| 222 | |
| 223 | error_msg = ( |
| 224 | "Router %s is not active on any of the L3 agents" % router_id) |
Slawek Kaplonski | bb1532a | 2021-06-17 13:45:56 +0000 | [diff] [blame] | 225 | # NOTE(slaweq): timeout here should be lower for sure, but due to |
| 226 | # the bug https://launchpad.net/bugs/1923633 let's wait even 10 |
| 227 | # minutes until router will be active on some of the L3 agents |
| 228 | utils.wait_until_true(_router_active_on_l3_agent, |
| 229 | timeout=600, sleep=5, |
| 230 | exception=lib_exc.TimeoutException(error_msg)) |
Slawek Kaplonski | edf3cba | 2021-04-21 10:34:02 +0200 | [diff] [blame] | 231 | |
| 232 | @classmethod |
ccamposr | e6b1004 | 2021-02-12 12:26:08 +0100 | [diff] [blame] | 233 | def skip_if_no_extension_enabled_in_l3_agents(cls, extension): |
| 234 | l3_agents = cls.os_admin.network_client.list_agents( |
| 235 | binary='neutron-l3-agent')['agents'] |
| 236 | if not l3_agents: |
| 237 | # the tests should not be skipped when neutron-l3-agent does not |
| 238 | # exist (this validation doesn't apply to the setups like |
| 239 | # e.g. ML2/OVN) |
| 240 | return |
| 241 | for agent in l3_agents: |
| 242 | if extension in agent['configurations'].get('extensions', []): |
| 243 | return |
| 244 | raise cls.skipTest("No L3 agent with '%s' extension enabled found." % |
| 245 | extension) |
| 246 | |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 247 | @removals.remove(version='Stein', |
| 248 | message="Please use create_floatingip method instead of " |
| 249 | "create_and_associate_floatingip.") |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 250 | def create_and_associate_floatingip(self, port_id, client=None): |
| 251 | client = client or self.os_primary.network_client |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 252 | return self.create_floatingip(port_id=port_id, client=client) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 253 | |
Hongbin Lu | 965b03d | 2018-04-25 22:32:30 +0000 | [diff] [blame] | 254 | def create_interface(cls, server_id, port_id, client=None): |
| 255 | client = client or cls.os_primary.interfaces_client |
| 256 | body = client.create_interface(server_id, port_id=port_id) |
| 257 | return body['interfaceAttachment'] |
| 258 | |
| 259 | def delete_interface(cls, server_id, port_id, client=None): |
| 260 | client = client or cls.os_primary.interfaces_client |
| 261 | client.delete_interface(server_id, port_id=port_id) |
| 262 | |
Brian Haley | d11f4ec | 2019-08-13 12:09:57 -0400 | [diff] [blame] | 263 | def setup_network_and_server(self, router=None, server_name=None, |
| 264 | network=None, **kwargs): |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 265 | """Create network resources and a server. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 266 | |
| 267 | Creating a network, subnet, router, keypair, security group |
| 268 | and a server. |
| 269 | """ |
Assaf Muller | d54ae6c | 2018-05-31 11:38:00 -0400 | [diff] [blame] | 270 | self.network = network or self.create_network() |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 271 | LOG.debug("Created network %s", self.network['name']) |
| 272 | self.subnet = self.create_subnet(self.network) |
| 273 | LOG.debug("Created subnet %s", self.subnet['id']) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 274 | |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 275 | secgroup = self.os_primary.network_client.create_security_group( |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 276 | name=data_utils.rand_name('secgroup')) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 277 | LOG.debug("Created security group %s", |
| 278 | secgroup['security_group']['name']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 279 | self.security_groups.append(secgroup['security_group']) |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 280 | if not router: |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 281 | router = self.create_router_by_client(**kwargs) |
| 282 | self.create_router_interface(router['id'], self.subnet['id']) |
| 283 | self.keypair = self.create_keypair() |
| 284 | self.create_loginable_secgroup_rule( |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 285 | secgroup_id=secgroup['security_group']['id']) |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 286 | |
| 287 | server_kwargs = { |
| 288 | 'flavor_ref': CONF.compute.flavor_ref, |
| 289 | 'image_ref': CONF.compute.image_ref, |
| 290 | 'key_name': self.keypair['name'], |
| 291 | 'networks': [{'uuid': self.network['id']}], |
| 292 | 'security_groups': [{'name': secgroup['security_group']['name']}], |
| 293 | } |
| 294 | if server_name is not None: |
| 295 | server_kwargs['name'] = server_name |
| 296 | |
| 297 | self.server = self.create_server(**server_kwargs) |
Jakub Libosvar | 1345d9d | 2017-06-09 13:59:05 +0000 | [diff] [blame] | 298 | self.port = self.client.list_ports(network_id=self.network['id'], |
| 299 | device_id=self.server[ |
| 300 | 'server']['id'])['ports'][0] |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 301 | self.fip = self.create_floatingip(port=self.port) |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 302 | |
Bence Romsics | 2abbc92 | 2020-09-30 16:10:07 +0200 | [diff] [blame] | 303 | def check_connectivity(self, host, ssh_user=None, ssh_key=None, |
| 304 | servers=None, ssh_timeout=None, ssh_client=None): |
| 305 | # Either ssh_client or ssh_user+ssh_key is mandatory. |
| 306 | if ssh_client is None: |
| 307 | ssh_client = ssh.Client(host, ssh_user, |
| 308 | pkey=ssh_key, timeout=ssh_timeout) |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 309 | try: |
| 310 | ssh_client.test_connection_auth() |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 311 | except (lib_exc.SSHTimeout, ssh_exc.AuthenticationException) as ssh_e: |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 312 | LOG.debug(ssh_e) |
| 313 | self._log_console_output(servers) |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 314 | self._log_local_network_status() |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 315 | raise |
| 316 | |
| 317 | def _log_console_output(self, servers=None): |
| 318 | if not CONF.compute_feature_enabled.console_output: |
| 319 | LOG.debug('Console output not supported, cannot log') |
| 320 | return |
| 321 | if not servers: |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 322 | servers = self.os_primary.servers_client.list_servers() |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 323 | servers = servers['servers'] |
| 324 | for server in servers: |
Slawek Kaplonski | cff7923 | 2020-03-03 14:12:18 +0100 | [diff] [blame] | 325 | # NOTE(slaweq): sometimes servers are passed in dictionary with |
| 326 | # "server" key as first level key and in other cases it may be that |
| 327 | # it is just the "inner" dict without "server" key. Lets try to |
| 328 | # handle both cases |
| 329 | server = server.get("server") or server |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 330 | try: |
| 331 | console_output = ( |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 332 | self.os_primary.servers_client.get_console_output( |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 333 | server['id'])['output']) |
| 334 | LOG.debug('Console output for %s\nbody=\n%s', |
| 335 | server['id'], console_output) |
| 336 | except lib_exc.NotFound: |
| 337 | LOG.debug("Server %s disappeared(deleted) while looking " |
| 338 | "for the console log", server['id']) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 339 | |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 340 | def _log_local_network_status(self): |
Slawek Kaplonski | 8033af7 | 2020-05-05 12:01:37 +0200 | [diff] [blame] | 341 | self._log_ns_network_status() |
| 342 | for ns_name in ip_utils.IPCommand().list_namespaces(): |
| 343 | self._log_ns_network_status(ns_name=ns_name) |
| 344 | |
| 345 | def _log_ns_network_status(self, ns_name=None): |
Rodolfo Alonso Hernandez | 9817d4f | 2020-11-17 08:50:50 +0000 | [diff] [blame] | 346 | try: |
| 347 | local_ips = ip_utils.IPCommand(namespace=ns_name).list_addresses() |
| 348 | local_routes = ip_utils.IPCommand(namespace=ns_name).list_routes() |
Rodolfo Alonso Hernandez | c134ea9 | 2021-04-14 15:15:01 +0000 | [diff] [blame] | 349 | arp_table = ip_utils.arp_table(namespace=ns_name) |
| 350 | iptables = ip_utils.list_iptables(namespace=ns_name) |
| 351 | lsockets = ip_utils.list_listening_sockets(namespace=ns_name) |
Rodolfo Alonso Hernandez | 9817d4f | 2020-11-17 08:50:50 +0000 | [diff] [blame] | 352 | except exceptions.ShellCommandFailed: |
| 353 | LOG.debug('Namespace %s has been deleted synchronously during the ' |
| 354 | 'host network collection process', ns_name) |
| 355 | return |
| 356 | |
Slawek Kaplonski | 8033af7 | 2020-05-05 12:01:37 +0200 | [diff] [blame] | 357 | LOG.debug('Namespace %s; IP Addresses:\n%s', |
| 358 | ns_name, '\n'.join(str(r) for r in local_ips)) |
Slawek Kaplonski | 8033af7 | 2020-05-05 12:01:37 +0200 | [diff] [blame] | 359 | LOG.debug('Namespace %s; Local routes:\n%s', |
| 360 | ns_name, '\n'.join(str(r) for r in local_routes)) |
Slawek Kaplonski | 8033af7 | 2020-05-05 12:01:37 +0200 | [diff] [blame] | 361 | LOG.debug('Namespace %s; Local ARP table:\n%s', |
| 362 | ns_name, '\n'.join(str(r) for r in arp_table)) |
Rodolfo Alonso Hernandez | c134ea9 | 2021-04-14 15:15:01 +0000 | [diff] [blame] | 363 | LOG.debug('Namespace %s; Local iptables:\n%s', ns_name, iptables) |
| 364 | LOG.debug('Namespace %s; Listening sockets:\n%s', ns_name, lsockets) |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 365 | |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 366 | def _check_remote_connectivity(self, source, dest, count, |
| 367 | should_succeed=True, |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 368 | nic=None, mtu=None, fragmentation=True, |
Roman Safronov | 12663cf | 2020-07-27 13:11:07 +0300 | [diff] [blame] | 369 | timeout=None, pattern=None, |
Nurmatov Mamatisa | 1b1c9d3 | 2021-12-27 15:37:03 +0300 | [diff] [blame^] | 370 | forbid_packet_loss=False, |
| 371 | check_response_ip=True): |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 372 | """check ping server via source ssh connection |
| 373 | |
| 374 | :param source: RemoteClient: an ssh connection from which to ping |
| 375 | :param dest: and IP to ping against |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 376 | :param count: Number of ping packet(s) to send |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 377 | :param should_succeed: boolean should ping succeed or not |
| 378 | :param nic: specific network interface to ping from |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 379 | :param mtu: mtu size for the packet to be sent |
| 380 | :param fragmentation: Flag for packet fragmentation |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 381 | :param timeout: Timeout for all ping packet(s) to succeed |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 382 | :param pattern: hex digits included in ICMP messages |
Roman Safronov | 12663cf | 2020-07-27 13:11:07 +0300 | [diff] [blame] | 383 | :param forbid_packet_loss: forbid or allow some lost packets |
Nurmatov Mamatisa | 1b1c9d3 | 2021-12-27 15:37:03 +0300 | [diff] [blame^] | 384 | :param check_response_ip: check response ip |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 385 | :returns: boolean -- should_succeed == ping |
| 386 | :returns: ping is false if ping failed |
| 387 | """ |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 388 | def ping_host(source, host, count, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 389 | size=CONF.validation.ping_size, nic=None, mtu=None, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 390 | fragmentation=True, pattern=None): |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 391 | IP_VERSION_4 = neutron_lib_constants.IP_VERSION_4 |
| 392 | IP_VERSION_6 = neutron_lib_constants.IP_VERSION_6 |
| 393 | |
| 394 | # Use 'ping6' for IPv6 addresses, 'ping' for IPv4 and hostnames |
| 395 | ip_version = ( |
| 396 | IP_VERSION_6 if netaddr.valid_ipv6(host) else IP_VERSION_4) |
| 397 | cmd = ( |
| 398 | 'ping6' if ip_version == IP_VERSION_6 else 'ping') |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 399 | if nic: |
| 400 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 401 | if mtu: |
| 402 | if not fragmentation: |
| 403 | cmd += ' -M do' |
| 404 | size = str(net_utils.get_ping_payload_size( |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 405 | mtu=mtu, ip_version=ip_version)) |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 406 | if pattern: |
| 407 | cmd += ' -p {pattern}'.format(pattern=pattern) |
Maciej Józefczyk | 3c324e0 | 2020-03-16 10:52:08 +0000 | [diff] [blame] | 408 | cmd += ' -c{0} -W{0} -s{1} {2}'.format(count, size, host) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 409 | return source.exec_command(cmd) |
| 410 | |
| 411 | def ping_remote(): |
| 412 | try: |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 413 | result = ping_host(source, dest, count, nic=nic, mtu=mtu, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 414 | fragmentation=fragmentation, |
| 415 | pattern=pattern) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 416 | |
| 417 | except lib_exc.SSHExecCommandFailed: |
| 418 | LOG.warning('Failed to ping IP: %s via a ssh connection ' |
| 419 | 'from: %s.', dest, source.host) |
| 420 | return not should_succeed |
| 421 | LOG.debug('ping result: %s', result) |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 422 | |
Roman Safronov | 12663cf | 2020-07-27 13:11:07 +0300 | [diff] [blame] | 423 | if forbid_packet_loss and ' 0% packet loss' not in result: |
| 424 | LOG.debug('Packet loss detected') |
| 425 | return not should_succeed |
| 426 | |
Nurmatov Mamatisa | 1b1c9d3 | 2021-12-27 15:37:03 +0300 | [diff] [blame^] | 427 | if (check_response_ip and |
| 428 | validators.validate_ip_address(dest) is None): |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 429 | # Assert that the return traffic was from the correct |
| 430 | # source address. |
| 431 | from_source = 'from %s' % dest |
| 432 | self.assertIn(from_source, result) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 433 | return should_succeed |
| 434 | |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 435 | return test_utils.call_until_true( |
| 436 | ping_remote, timeout or CONF.validation.ping_timeout, 1) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 437 | |
| 438 | def check_remote_connectivity(self, source, dest, should_succeed=True, |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 439 | nic=None, mtu=None, fragmentation=True, |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 440 | servers=None, timeout=None, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 441 | ping_count=CONF.validation.ping_count, |
Nurmatov Mamatisa | 1b1c9d3 | 2021-12-27 15:37:03 +0300 | [diff] [blame^] | 442 | pattern=None, forbid_packet_loss=False, |
| 443 | check_response_ip=True): |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 444 | try: |
| 445 | self.assertTrue(self._check_remote_connectivity( |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 446 | source, dest, ping_count, should_succeed, nic, mtu, |
| 447 | fragmentation, |
Roman Safronov | 12663cf | 2020-07-27 13:11:07 +0300 | [diff] [blame] | 448 | timeout=timeout, pattern=pattern, |
Nurmatov Mamatisa | 1b1c9d3 | 2021-12-27 15:37:03 +0300 | [diff] [blame^] | 449 | forbid_packet_loss=forbid_packet_loss, |
| 450 | check_response_ip=check_response_ip)) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 451 | except (lib_exc.SSHTimeout, ssh_exc.AuthenticationException) as ssh_e: |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 452 | LOG.debug(ssh_e) |
| 453 | self._log_console_output(servers) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 454 | self._log_local_network_status() |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 455 | raise |
| 456 | except AssertionError: |
| 457 | self._log_console_output(servers) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 458 | self._log_local_network_status() |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 459 | raise |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 460 | |
| 461 | def ping_ip_address(self, ip_address, should_succeed=True, |
| 462 | ping_timeout=None, mtu=None): |
| 463 | # the code is taken from tempest/scenario/manager.py in tempest git |
| 464 | timeout = ping_timeout or CONF.validation.ping_timeout |
| 465 | cmd = ['ping', '-c1', '-w1'] |
| 466 | |
| 467 | if mtu: |
| 468 | cmd += [ |
| 469 | # don't fragment |
| 470 | '-M', 'do', |
| 471 | # ping receives just the size of ICMP payload |
| 472 | '-s', str(net_utils.get_ping_payload_size(mtu, 4)) |
| 473 | ] |
| 474 | cmd.append(ip_address) |
| 475 | |
| 476 | def ping(): |
| 477 | proc = subprocess.Popen(cmd, |
| 478 | stdout=subprocess.PIPE, |
| 479 | stderr=subprocess.PIPE) |
| 480 | proc.communicate() |
| 481 | |
| 482 | return (proc.returncode == 0) == should_succeed |
| 483 | |
| 484 | caller = test_utils.find_test_caller() |
| 485 | LOG.debug('%(caller)s begins to ping %(ip)s in %(timeout)s sec and the' |
| 486 | ' expected result is %(should_succeed)s', { |
| 487 | 'caller': caller, 'ip': ip_address, 'timeout': timeout, |
| 488 | 'should_succeed': |
| 489 | 'reachable' if should_succeed else 'unreachable' |
| 490 | }) |
| 491 | result = test_utils.call_until_true(ping, timeout, 1) |
Manjeet Singh Bhatia | 8bbf899 | 2019-03-04 11:59:57 -0800 | [diff] [blame] | 492 | |
| 493 | # To make sure ping_ip_address called by test works |
| 494 | # as expected. |
| 495 | self.assertTrue(result) |
| 496 | |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 497 | LOG.debug('%(caller)s finishes ping %(ip)s in %(timeout)s sec and the ' |
| 498 | 'ping result is %(result)s', { |
| 499 | 'caller': caller, 'ip': ip_address, 'timeout': timeout, |
| 500 | 'result': 'expected' if result else 'unexpected' |
| 501 | }) |
| 502 | return result |
Federico Ressi | e7417b7 | 2018-05-30 05:50:58 +0200 | [diff] [blame] | 503 | |
| 504 | def wait_for_server_status(self, server, status, client=None, **kwargs): |
| 505 | """Waits for a server to reach a given status. |
| 506 | |
| 507 | :param server: mapping having schema {'id': <server_id>} |
| 508 | :param status: string status to wait for (es: 'ACTIVE') |
| 509 | :param clien: servers client (self.os_primary.servers_client as |
| 510 | default value) |
| 511 | """ |
| 512 | |
| 513 | client = client or self.os_primary.servers_client |
| 514 | waiters.wait_for_server_status(client, server['id'], status, **kwargs) |
| 515 | |
| 516 | def wait_for_server_active(self, server, client=None): |
| 517 | """Waits for a server to reach active status. |
| 518 | |
| 519 | :param server: mapping having schema {'id': <server_id>} |
| 520 | :param clien: servers client (self.os_primary.servers_client as |
| 521 | default value) |
| 522 | """ |
| 523 | self.wait_for_server_status( |
| 524 | server, constants.SERVER_STATUS_ACTIVE, client) |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 525 | |
Slawek Kaplonski | 2211eab | 2020-10-20 16:43:53 +0200 | [diff] [blame] | 526 | def wait_for_guest_os_ready(self, server, client=None): |
| 527 | if not CONF.compute_feature_enabled.console_output: |
| 528 | LOG.debug('Console output not supported, cannot check if server ' |
Rodolfo Alonso Hernandez | dff870b | 2020-11-06 08:41:44 +0000 | [diff] [blame] | 529 | '%s is ready.', server['id']) |
Slawek Kaplonski | 2211eab | 2020-10-20 16:43:53 +0200 | [diff] [blame] | 530 | return |
| 531 | |
| 532 | client = client or self.os_primary.servers_client |
| 533 | |
| 534 | def system_booted(): |
| 535 | console_output = client.get_console_output(server['id'])['output'] |
| 536 | for line in console_output.split('\n'): |
| 537 | if 'login:' in line.lower(): |
| 538 | return True |
| 539 | return False |
| 540 | |
| 541 | try: |
| 542 | utils.wait_until_true(system_booted, sleep=5) |
| 543 | except utils.WaitTimeout: |
| 544 | LOG.debug("No correct output in console of server %s found. " |
| 545 | "Guest operating system status can't be checked.", |
| 546 | server['id']) |
| 547 | |
Flavio Fernandes | a1952c6 | 2020-10-02 06:39:08 -0400 | [diff] [blame] | 548 | def check_servers_hostnames(self, servers, timeout=None, log_errors=True, |
| 549 | external_port=None): |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 550 | """Compare hostnames of given servers with their names.""" |
| 551 | try: |
| 552 | for server in servers: |
| 553 | kwargs = {} |
nfridman | d896954 | 2020-06-02 14:59:09 +0300 | [diff] [blame] | 554 | if timeout: |
| 555 | kwargs['timeout'] = timeout |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 556 | try: |
Flavio Fernandes | a1952c6 | 2020-10-02 06:39:08 -0400 | [diff] [blame] | 557 | kwargs['port'] = external_port or ( |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 558 | server['port_forwarding_tcp']['external_port']) |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 559 | except KeyError: |
| 560 | pass |
| 561 | ssh_client = ssh.Client( |
| 562 | self.fip['floating_ip_address'], |
| 563 | CONF.validation.image_ssh_user, |
| 564 | pkey=self.keypair['private_key'], |
| 565 | **kwargs) |
| 566 | self.assertIn(server['name'], |
Rodolfo Alonso Hernandez | af394dd | 2020-11-12 14:26:13 +0000 | [diff] [blame] | 567 | ssh_client.get_hostname()) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 568 | except (lib_exc.SSHTimeout, ssh_exc.AuthenticationException) as ssh_e: |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 569 | LOG.debug(ssh_e) |
| 570 | if log_errors: |
| 571 | self._log_console_output(servers) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 572 | self._log_local_network_status() |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 573 | raise |
| 574 | except AssertionError as assert_e: |
| 575 | LOG.debug(assert_e) |
| 576 | if log_errors: |
| 577 | self._log_console_output(servers) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 578 | self._log_local_network_status() |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 579 | raise |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 580 | |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 581 | def ensure_nc_listen(self, ssh_client, port, protocol, echo_msg=None, |
| 582 | servers=None): |
| 583 | """Ensure that nc server listening on the given TCP/UDP port is up. |
| 584 | |
| 585 | Listener is created always on remote host. |
| 586 | """ |
| 587 | def spawn_and_check_process(): |
| 588 | self.nc_listen(ssh_client, port, protocol, echo_msg, servers) |
| 589 | return utils.process_is_running(ssh_client, "nc") |
| 590 | |
| 591 | utils.wait_until_true(spawn_and_check_process) |
| 592 | |
| 593 | def nc_listen(self, ssh_client, port, protocol, echo_msg=None, |
| 594 | servers=None): |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 595 | """Create nc server listening on the given TCP/UDP port. |
| 596 | |
| 597 | Listener is created always on remote host. |
| 598 | """ |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 599 | try: |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 600 | return ssh_client.execute_script( |
| 601 | get_ncat_server_cmd(port, protocol, echo_msg), |
Flavio Fernandes | b056ac2 | 2020-07-01 14:57:13 -0400 | [diff] [blame] | 602 | become_root=True, combine_stderr=True) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 603 | except (lib_exc.SSHTimeout, ssh_exc.AuthenticationException) as ssh_e: |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 604 | LOG.debug(ssh_e) |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 605 | self._log_console_output(servers) |
Slawek Kaplonski | 8cccfe0 | 2020-09-29 22:34:09 +0200 | [diff] [blame] | 606 | self._log_local_network_status() |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 607 | raise |
| 608 | |
| 609 | def nc_client(self, ip_address, port, protocol): |
| 610 | """Check connectivity to TCP/UDP port at host via nc. |
| 611 | |
| 612 | Client is always executed locally on host where tests are executed. |
| 613 | """ |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 614 | cmd = get_ncat_client_cmd(ip_address, port, protocol) |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 615 | result = shell.execute_local_command(cmd) |
| 616 | self.assertEqual(0, result.exit_status) |
| 617 | return result.stdout |