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 |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 24 | from tempest.common.utils import net_utils |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 25 | from tempest.common import waiters |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 26 | from tempest.lib.common.utils import data_utils |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 27 | from tempest.lib.common.utils import test_utils |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 28 | from tempest.lib import exceptions as lib_exc |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 29 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 30 | from neutron_tempest_plugin.api import base as base_api |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 31 | from neutron_tempest_plugin.common import ip as ip_utils |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 32 | from neutron_tempest_plugin.common import shell |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 33 | from neutron_tempest_plugin.common import ssh |
| 34 | from neutron_tempest_plugin import config |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 35 | from neutron_tempest_plugin import exceptions |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 36 | from neutron_tempest_plugin.scenario import constants |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 37 | |
| 38 | CONF = config.CONF |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 39 | |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 40 | LOG = log.getLogger(__name__) |
| 41 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 42 | |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 43 | def get_ncat_version(ssh_client=None): |
| 44 | cmd = "ncat --version 2>&1" |
| 45 | try: |
| 46 | version_result = shell.execute(cmd, ssh_client=ssh_client).stdout |
| 47 | except exceptions.ShellCommandFailed: |
| 48 | m = None |
| 49 | else: |
| 50 | m = re.match(r"Ncat: Version ([\d.]+) *.", version_result) |
| 51 | # NOTE(slaweq): by default lets assume we have ncat 7.60 which is in Ubuntu |
| 52 | # 18.04 which is used on u/s gates |
| 53 | return distutils.version.StrictVersion(m.group(1) if m else '7.60') |
| 54 | |
| 55 | |
| 56 | def get_ncat_server_cmd(port, protocol, msg): |
| 57 | udp = '' |
| 58 | if protocol.lower() == neutron_lib_constants.PROTO_NAME_UDP: |
| 59 | udp = '-u' |
| 60 | cmd = "nc %(udp)s -p %(port)s -lk " % { |
| 61 | 'udp': udp, 'port': port} |
| 62 | if CONF.neutron_plugin_options.default_image_is_advanced: |
| 63 | cmd += "-c 'echo %s' &" % msg |
| 64 | else: |
| 65 | cmd += "-e echo %s &" % msg |
| 66 | return cmd |
| 67 | |
| 68 | |
| 69 | def get_ncat_client_cmd(ip_address, port, protocol): |
| 70 | udp = '' |
| 71 | if protocol.lower() == neutron_lib_constants.PROTO_NAME_UDP: |
| 72 | udp = '-u' |
| 73 | cmd = 'echo "knock knock" | nc ' |
| 74 | ncat_version = get_ncat_version() |
| 75 | if ncat_version > distutils.version.StrictVersion('7.60'): |
| 76 | cmd += '-z ' |
| 77 | cmd += '-w 1 %(udp)s %(host)s %(port)s' % { |
| 78 | 'udp': udp, 'host': ip_address, 'port': port} |
| 79 | return cmd |
| 80 | |
| 81 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 82 | class BaseTempestTestCase(base_api.BaseNetworkTest): |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 83 | |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 84 | def create_server(self, flavor_ref, image_ref, key_name, networks, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 85 | **kwargs): |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 86 | """Create a server using tempest lib |
Brian Haley | aee61ac | 2018-10-09 20:00:27 -0400 | [diff] [blame] | 87 | |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 88 | All the parameters are the ones used in Compute API |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 89 | * - Kwargs that require admin privileges |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 90 | |
| 91 | Args: |
| 92 | flavor_ref(str): The flavor of the server to be provisioned. |
| 93 | image_ref(str): The image of the server to be provisioned. |
| 94 | key_name(str): SSH key to to be used to connect to the |
| 95 | provisioned server. |
| 96 | networks(list): List of dictionaries where each represent |
| 97 | an interface to be attached to the server. For network |
| 98 | it should be {'uuid': network_uuid} and for port it should |
| 99 | be {'port': port_uuid} |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 100 | kwargs: |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 101 | name(str): Name of the server to be provisioned. |
| 102 | security_groups(list): List of dictionaries where |
| 103 | the keys is 'name' and the value is the name of |
| 104 | the security group. If it's not passed the default |
| 105 | security group will be used. |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 106 | availability_zone(str)*: The availability zone that |
| 107 | the instance will be in. |
| 108 | You can request a specific az without actually creating one, |
| 109 | Just pass 'X:Y' where X is the default availability |
| 110 | zone, and Y is the compute host name. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 111 | """ |
| 112 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 113 | kwargs.setdefault('name', data_utils.rand_name('server-test')) |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 114 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 115 | # We cannot use setdefault() here because caller could have passed |
| 116 | # security_groups=None and we don't want to pass None to |
| 117 | # client.create_server() |
| 118 | if not kwargs.get('security_groups'): |
| 119 | kwargs['security_groups'] = [{'name': 'default'}] |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 120 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 121 | client = self.os_primary.servers_client |
| 122 | if kwargs.get('availability_zone'): |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 123 | client = self.os_admin.servers_client |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 124 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 125 | server = client.create_server( |
| 126 | flavorRef=flavor_ref, |
| 127 | imageRef=image_ref, |
| 128 | key_name=key_name, |
| 129 | networks=networks, |
| 130 | **kwargs) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 131 | |
| 132 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 133 | waiters.wait_for_server_termination, |
| 134 | client, |
| 135 | server['server']['id']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 136 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 137 | client.delete_server, |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 138 | server['server']['id']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 139 | return server |
| 140 | |
| 141 | @classmethod |
Jens Harbott | 5435763 | 2017-11-21 11:47:06 +0000 | [diff] [blame] | 142 | def create_secgroup_rules(cls, rule_list, secgroup_id=None, |
| 143 | client=None): |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 144 | client = client or cls.os_primary.network_client |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 145 | if not secgroup_id: |
| 146 | sgs = client.list_security_groups()['security_groups'] |
| 147 | for sg in sgs: |
| 148 | if sg['name'] == constants.DEFAULT_SECURITY_GROUP: |
| 149 | secgroup_id = sg['id'] |
| 150 | break |
| 151 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 152 | for rule in rule_list: |
| 153 | direction = rule.pop('direction') |
| 154 | client.create_security_group_rule( |
| 155 | direction=direction, |
| 156 | security_group_id=secgroup_id, |
| 157 | **rule) |
| 158 | |
| 159 | @classmethod |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 160 | def create_loginable_secgroup_rule(cls, secgroup_id=None, |
| 161 | client=None): |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 162 | """This rule is intended to permit inbound ssh |
| 163 | |
| 164 | Allowing ssh traffic traffic from all sources, so no group_id is |
| 165 | provided. |
| 166 | Setting a group_id would only permit traffic from ports |
| 167 | belonging to the same security group. |
| 168 | """ |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 169 | return cls.create_security_group_rule( |
| 170 | security_group_id=secgroup_id, |
| 171 | client=client, |
| 172 | protocol=neutron_lib_constants.PROTO_NAME_TCP, |
| 173 | direction=neutron_lib_constants.INGRESS_DIRECTION, |
| 174 | port_range_min=22, |
| 175 | port_range_max=22) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 176 | |
| 177 | @classmethod |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 178 | def create_pingable_secgroup_rule(cls, secgroup_id=None, |
| 179 | client=None): |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 180 | """This rule is intended to permit inbound ping |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 181 | |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 182 | """ |
| 183 | return cls.create_security_group_rule( |
| 184 | security_group_id=secgroup_id, client=client, |
| 185 | protocol=neutron_lib_constants.PROTO_NAME_ICMP, |
| 186 | direction=neutron_lib_constants.INGRESS_DIRECTION) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 187 | |
| 188 | @classmethod |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 189 | def create_router_by_client(cls, is_admin=False, **kwargs): |
| 190 | kwargs.update({'router_name': data_utils.rand_name('router'), |
| 191 | 'admin_state_up': True, |
| 192 | 'external_network_id': CONF.network.public_network_id}) |
| 193 | if not is_admin: |
| 194 | router = cls.create_router(**kwargs) |
| 195 | else: |
| 196 | router = cls.create_admin_router(**kwargs) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 197 | LOG.debug("Created router %s", router['name']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 198 | cls.routers.append(router) |
| 199 | return router |
| 200 | |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 201 | @removals.remove(version='Stein', |
| 202 | message="Please use create_floatingip method instead of " |
| 203 | "create_and_associate_floatingip.") |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 204 | def create_and_associate_floatingip(self, port_id, client=None): |
| 205 | client = client or self.os_primary.network_client |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 206 | return self.create_floatingip(port_id=port_id, client=client) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 207 | |
Hongbin Lu | 965b03d | 2018-04-25 22:32:30 +0000 | [diff] [blame] | 208 | def create_interface(cls, server_id, port_id, client=None): |
| 209 | client = client or cls.os_primary.interfaces_client |
| 210 | body = client.create_interface(server_id, port_id=port_id) |
| 211 | return body['interfaceAttachment'] |
| 212 | |
| 213 | def delete_interface(cls, server_id, port_id, client=None): |
| 214 | client = client or cls.os_primary.interfaces_client |
| 215 | client.delete_interface(server_id, port_id=port_id) |
| 216 | |
Brian Haley | d11f4ec | 2019-08-13 12:09:57 -0400 | [diff] [blame] | 217 | def setup_network_and_server(self, router=None, server_name=None, |
| 218 | network=None, **kwargs): |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 219 | """Create network resources and a server. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 220 | |
| 221 | Creating a network, subnet, router, keypair, security group |
| 222 | and a server. |
| 223 | """ |
Assaf Muller | d54ae6c | 2018-05-31 11:38:00 -0400 | [diff] [blame] | 224 | self.network = network or self.create_network() |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 225 | LOG.debug("Created network %s", self.network['name']) |
| 226 | self.subnet = self.create_subnet(self.network) |
| 227 | LOG.debug("Created subnet %s", self.subnet['id']) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 228 | |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 229 | secgroup = self.os_primary.network_client.create_security_group( |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 230 | name=data_utils.rand_name('secgroup')) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 231 | LOG.debug("Created security group %s", |
| 232 | secgroup['security_group']['name']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 233 | self.security_groups.append(secgroup['security_group']) |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 234 | if not router: |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 235 | router = self.create_router_by_client(**kwargs) |
| 236 | self.create_router_interface(router['id'], self.subnet['id']) |
| 237 | self.keypair = self.create_keypair() |
| 238 | self.create_loginable_secgroup_rule( |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 239 | secgroup_id=secgroup['security_group']['id']) |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 240 | |
| 241 | server_kwargs = { |
| 242 | 'flavor_ref': CONF.compute.flavor_ref, |
| 243 | 'image_ref': CONF.compute.image_ref, |
| 244 | 'key_name': self.keypair['name'], |
| 245 | 'networks': [{'uuid': self.network['id']}], |
| 246 | 'security_groups': [{'name': secgroup['security_group']['name']}], |
| 247 | } |
| 248 | if server_name is not None: |
| 249 | server_kwargs['name'] = server_name |
| 250 | |
| 251 | self.server = self.create_server(**server_kwargs) |
Federico Ressi | e7417b7 | 2018-05-30 05:50:58 +0200 | [diff] [blame] | 252 | self.wait_for_server_active(self.server['server']) |
Jakub Libosvar | 1345d9d | 2017-06-09 13:59:05 +0000 | [diff] [blame] | 253 | self.port = self.client.list_ports(network_id=self.network['id'], |
| 254 | device_id=self.server[ |
| 255 | 'server']['id'])['ports'][0] |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 256 | self.fip = self.create_floatingip(port=self.port) |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 257 | |
| 258 | def check_connectivity(self, host, ssh_user, ssh_key, servers=None): |
| 259 | ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key) |
| 260 | try: |
| 261 | ssh_client.test_connection_auth() |
| 262 | except lib_exc.SSHTimeout as ssh_e: |
| 263 | LOG.debug(ssh_e) |
| 264 | self._log_console_output(servers) |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 265 | self._log_local_network_status() |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 266 | raise |
| 267 | |
| 268 | def _log_console_output(self, servers=None): |
| 269 | if not CONF.compute_feature_enabled.console_output: |
| 270 | LOG.debug('Console output not supported, cannot log') |
| 271 | return |
| 272 | if not servers: |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 273 | servers = self.os_primary.servers_client.list_servers() |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 274 | servers = servers['servers'] |
| 275 | for server in servers: |
Slawek Kaplonski | cff7923 | 2020-03-03 14:12:18 +0100 | [diff] [blame] | 276 | # NOTE(slaweq): sometimes servers are passed in dictionary with |
| 277 | # "server" key as first level key and in other cases it may be that |
| 278 | # it is just the "inner" dict without "server" key. Lets try to |
| 279 | # handle both cases |
| 280 | server = server.get("server") or server |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 281 | try: |
| 282 | console_output = ( |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 283 | self.os_primary.servers_client.get_console_output( |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 284 | server['id'])['output']) |
| 285 | LOG.debug('Console output for %s\nbody=\n%s', |
| 286 | server['id'], console_output) |
| 287 | except lib_exc.NotFound: |
| 288 | LOG.debug("Server %s disappeared(deleted) while looking " |
| 289 | "for the console log", server['id']) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 290 | |
Rodolfo Alonso Hernandez | 4849f00 | 2020-01-16 16:01:10 +0000 | [diff] [blame] | 291 | def _log_local_network_status(self): |
| 292 | local_routes = ip_utils.IPCommand().list_routes() |
| 293 | LOG.debug('Local routes:\n%s', '\n'.join(str(r) for r in local_routes)) |
| 294 | arp_table = ip_utils.arp_table() |
| 295 | LOG.debug('Local ARP table:\n%s', '\n'.join(str(r) for r in arp_table)) |
| 296 | |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 297 | def _check_remote_connectivity(self, source, dest, count, |
| 298 | should_succeed=True, |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 299 | nic=None, mtu=None, fragmentation=True, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 300 | timeout=None, pattern=None): |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 301 | """check ping server via source ssh connection |
| 302 | |
| 303 | :param source: RemoteClient: an ssh connection from which to ping |
| 304 | :param dest: and IP to ping against |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 305 | :param count: Number of ping packet(s) to send |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 306 | :param should_succeed: boolean should ping succeed or not |
| 307 | :param nic: specific network interface to ping from |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 308 | :param mtu: mtu size for the packet to be sent |
| 309 | :param fragmentation: Flag for packet fragmentation |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 310 | :param timeout: Timeout for all ping packet(s) to succeed |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 311 | :param pattern: hex digits included in ICMP messages |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 312 | :returns: boolean -- should_succeed == ping |
| 313 | :returns: ping is false if ping failed |
| 314 | """ |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 315 | def ping_host(source, host, count, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 316 | size=CONF.validation.ping_size, nic=None, mtu=None, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 317 | fragmentation=True, pattern=None): |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 318 | IP_VERSION_4 = neutron_lib_constants.IP_VERSION_4 |
| 319 | IP_VERSION_6 = neutron_lib_constants.IP_VERSION_6 |
| 320 | |
| 321 | # Use 'ping6' for IPv6 addresses, 'ping' for IPv4 and hostnames |
| 322 | ip_version = ( |
| 323 | IP_VERSION_6 if netaddr.valid_ipv6(host) else IP_VERSION_4) |
| 324 | cmd = ( |
| 325 | 'ping6' if ip_version == IP_VERSION_6 else 'ping') |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 326 | if nic: |
| 327 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 328 | if mtu: |
| 329 | if not fragmentation: |
| 330 | cmd += ' -M do' |
| 331 | size = str(net_utils.get_ping_payload_size( |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 332 | mtu=mtu, ip_version=ip_version)) |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 333 | if pattern: |
| 334 | cmd += ' -p {pattern}'.format(pattern=pattern) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 335 | cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host) |
| 336 | return source.exec_command(cmd) |
| 337 | |
| 338 | def ping_remote(): |
| 339 | try: |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 340 | result = ping_host(source, dest, count, nic=nic, mtu=mtu, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 341 | fragmentation=fragmentation, |
| 342 | pattern=pattern) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 343 | |
| 344 | except lib_exc.SSHExecCommandFailed: |
| 345 | LOG.warning('Failed to ping IP: %s via a ssh connection ' |
| 346 | 'from: %s.', dest, source.host) |
| 347 | return not should_succeed |
| 348 | LOG.debug('ping result: %s', result) |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 349 | |
| 350 | if validators.validate_ip_address(dest) is None: |
| 351 | # Assert that the return traffic was from the correct |
| 352 | # source address. |
| 353 | from_source = 'from %s' % dest |
| 354 | self.assertIn(from_source, result) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 355 | return should_succeed |
| 356 | |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 357 | return test_utils.call_until_true( |
| 358 | ping_remote, timeout or CONF.validation.ping_timeout, 1) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 359 | |
| 360 | def check_remote_connectivity(self, source, dest, should_succeed=True, |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 361 | nic=None, mtu=None, fragmentation=True, |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 362 | servers=None, timeout=None, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 363 | ping_count=CONF.validation.ping_count, |
| 364 | pattern=None): |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 365 | try: |
| 366 | self.assertTrue(self._check_remote_connectivity( |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 367 | source, dest, ping_count, should_succeed, nic, mtu, |
| 368 | fragmentation, |
Eduardo Olivares | f2b6054 | 2020-01-30 09:37:22 +0100 | [diff] [blame] | 369 | timeout=timeout, pattern=pattern)) |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 370 | except lib_exc.SSHTimeout as ssh_e: |
| 371 | LOG.debug(ssh_e) |
| 372 | self._log_console_output(servers) |
| 373 | raise |
| 374 | except AssertionError: |
| 375 | self._log_console_output(servers) |
| 376 | raise |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 377 | |
| 378 | def ping_ip_address(self, ip_address, should_succeed=True, |
| 379 | ping_timeout=None, mtu=None): |
| 380 | # the code is taken from tempest/scenario/manager.py in tempest git |
| 381 | timeout = ping_timeout or CONF.validation.ping_timeout |
| 382 | cmd = ['ping', '-c1', '-w1'] |
| 383 | |
| 384 | if mtu: |
| 385 | cmd += [ |
| 386 | # don't fragment |
| 387 | '-M', 'do', |
| 388 | # ping receives just the size of ICMP payload |
| 389 | '-s', str(net_utils.get_ping_payload_size(mtu, 4)) |
| 390 | ] |
| 391 | cmd.append(ip_address) |
| 392 | |
| 393 | def ping(): |
| 394 | proc = subprocess.Popen(cmd, |
| 395 | stdout=subprocess.PIPE, |
| 396 | stderr=subprocess.PIPE) |
| 397 | proc.communicate() |
| 398 | |
| 399 | return (proc.returncode == 0) == should_succeed |
| 400 | |
| 401 | caller = test_utils.find_test_caller() |
| 402 | LOG.debug('%(caller)s begins to ping %(ip)s in %(timeout)s sec and the' |
| 403 | ' expected result is %(should_succeed)s', { |
| 404 | 'caller': caller, 'ip': ip_address, 'timeout': timeout, |
| 405 | 'should_succeed': |
| 406 | 'reachable' if should_succeed else 'unreachable' |
| 407 | }) |
| 408 | result = test_utils.call_until_true(ping, timeout, 1) |
Manjeet Singh Bhatia | 8bbf899 | 2019-03-04 11:59:57 -0800 | [diff] [blame] | 409 | |
| 410 | # To make sure ping_ip_address called by test works |
| 411 | # as expected. |
| 412 | self.assertTrue(result) |
| 413 | |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 414 | LOG.debug('%(caller)s finishes ping %(ip)s in %(timeout)s sec and the ' |
| 415 | 'ping result is %(result)s', { |
| 416 | 'caller': caller, 'ip': ip_address, 'timeout': timeout, |
| 417 | 'result': 'expected' if result else 'unexpected' |
| 418 | }) |
| 419 | return result |
Federico Ressi | e7417b7 | 2018-05-30 05:50:58 +0200 | [diff] [blame] | 420 | |
| 421 | def wait_for_server_status(self, server, status, client=None, **kwargs): |
| 422 | """Waits for a server to reach a given status. |
| 423 | |
| 424 | :param server: mapping having schema {'id': <server_id>} |
| 425 | :param status: string status to wait for (es: 'ACTIVE') |
| 426 | :param clien: servers client (self.os_primary.servers_client as |
| 427 | default value) |
| 428 | """ |
| 429 | |
| 430 | client = client or self.os_primary.servers_client |
| 431 | waiters.wait_for_server_status(client, server['id'], status, **kwargs) |
| 432 | |
| 433 | def wait_for_server_active(self, server, client=None): |
| 434 | """Waits for a server to reach active status. |
| 435 | |
| 436 | :param server: mapping having schema {'id': <server_id>} |
| 437 | :param clien: servers client (self.os_primary.servers_client as |
| 438 | default value) |
| 439 | """ |
| 440 | self.wait_for_server_status( |
| 441 | server, constants.SERVER_STATUS_ACTIVE, client) |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 442 | |
| 443 | def check_servers_hostnames(self, servers, log_errors=True): |
| 444 | """Compare hostnames of given servers with their names.""" |
| 445 | try: |
| 446 | for server in servers: |
| 447 | kwargs = {} |
| 448 | try: |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 449 | kwargs['port'] = ( |
| 450 | server['port_forwarding_tcp']['external_port']) |
Jakub Libosvar | 031fd5a | 2019-07-15 16:10:07 +0000 | [diff] [blame] | 451 | except KeyError: |
| 452 | pass |
| 453 | ssh_client = ssh.Client( |
| 454 | self.fip['floating_ip_address'], |
| 455 | CONF.validation.image_ssh_user, |
| 456 | pkey=self.keypair['private_key'], |
| 457 | **kwargs) |
| 458 | self.assertIn(server['name'], |
| 459 | ssh_client.exec_command('hostname')) |
| 460 | except lib_exc.SSHTimeout as ssh_e: |
| 461 | LOG.debug(ssh_e) |
| 462 | if log_errors: |
| 463 | self._log_console_output(servers) |
| 464 | raise |
| 465 | except AssertionError as assert_e: |
| 466 | LOG.debug(assert_e) |
| 467 | if log_errors: |
| 468 | self._log_console_output(servers) |
| 469 | raise |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 470 | |
| 471 | def nc_listen(self, server, ssh_client, port, protocol, echo_msg): |
| 472 | """Create nc server listening on the given TCP/UDP port. |
| 473 | |
| 474 | Listener is created always on remote host. |
| 475 | """ |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 476 | try: |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 477 | return ssh_client.execute_script( |
| 478 | get_ncat_server_cmd(port, protocol, echo_msg), |
| 479 | become_root=True) |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 480 | except lib_exc.SSHTimeout as ssh_e: |
| 481 | LOG.debug(ssh_e) |
| 482 | self._log_console_output([server]) |
| 483 | raise |
| 484 | |
| 485 | def nc_client(self, ip_address, port, protocol): |
| 486 | """Check connectivity to TCP/UDP port at host via nc. |
| 487 | |
| 488 | Client is always executed locally on host where tests are executed. |
| 489 | """ |
Slawek Kaplonski | af83e83 | 2020-02-05 12:11:54 +0100 | [diff] [blame] | 490 | cmd = get_ncat_client_cmd(ip_address, port, protocol) |
Slawek Kaplonski | c4e963e | 2019-09-11 22:55:34 +0200 | [diff] [blame] | 491 | result = shell.execute_local_command(cmd) |
| 492 | self.assertEqual(0, result.exit_status) |
| 493 | return result.stdout |