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. |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 15 | import subprocess |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 16 | |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 17 | from debtcollector import removals |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 18 | import netaddr |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 19 | from neutron_lib.api import validators |
| 20 | from neutron_lib import constants as neutron_lib_constants |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 21 | from oslo_log import log |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 22 | from tempest.common.utils import net_utils |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 23 | from tempest.common import waiters |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 24 | from tempest.lib.common.utils import data_utils |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 25 | from tempest.lib.common.utils import test_utils |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 26 | from tempest.lib import exceptions as lib_exc |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 27 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 28 | from neutron_tempest_plugin.api import base as base_api |
| 29 | from neutron_tempest_plugin.common import ssh |
| 30 | from neutron_tempest_plugin import config |
| 31 | from neutron_tempest_plugin.scenario import constants |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 32 | |
| 33 | CONF = config.CONF |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 34 | |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 35 | LOG = log.getLogger(__name__) |
| 36 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 37 | |
| 38 | class BaseTempestTestCase(base_api.BaseNetworkTest): |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 39 | |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 40 | def create_server(self, flavor_ref, image_ref, key_name, networks, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 41 | **kwargs): |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 42 | """Create a server using tempest lib |
Brian Haley | aee61ac | 2018-10-09 20:00:27 -0400 | [diff] [blame] | 43 | |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 44 | All the parameters are the ones used in Compute API |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 45 | * - Kwargs that require admin privileges |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 46 | |
| 47 | Args: |
| 48 | flavor_ref(str): The flavor of the server to be provisioned. |
| 49 | image_ref(str): The image of the server to be provisioned. |
| 50 | key_name(str): SSH key to to be used to connect to the |
| 51 | provisioned server. |
| 52 | networks(list): List of dictionaries where each represent |
| 53 | an interface to be attached to the server. For network |
| 54 | it should be {'uuid': network_uuid} and for port it should |
| 55 | be {'port': port_uuid} |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 56 | kwargs: |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 57 | name(str): Name of the server to be provisioned. |
| 58 | security_groups(list): List of dictionaries where |
| 59 | the keys is 'name' and the value is the name of |
| 60 | the security group. If it's not passed the default |
| 61 | security group will be used. |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 62 | availability_zone(str)*: The availability zone that |
| 63 | the instance will be in. |
| 64 | You can request a specific az without actually creating one, |
| 65 | Just pass 'X:Y' where X is the default availability |
| 66 | zone, and Y is the compute host name. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 67 | """ |
| 68 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 69 | kwargs.setdefault('name', data_utils.rand_name('server-test')) |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 70 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 71 | # We cannot use setdefault() here because caller could have passed |
| 72 | # security_groups=None and we don't want to pass None to |
| 73 | # client.create_server() |
| 74 | if not kwargs.get('security_groups'): |
| 75 | kwargs['security_groups'] = [{'name': 'default'}] |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 76 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 77 | client = self.os_primary.servers_client |
| 78 | if kwargs.get('availability_zone'): |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 79 | client = self.os_admin.servers_client |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 80 | |
Jakub Libosvar | ffd9b91 | 2017-11-16 09:54:14 +0000 | [diff] [blame] | 81 | server = client.create_server( |
| 82 | flavorRef=flavor_ref, |
| 83 | imageRef=image_ref, |
| 84 | key_name=key_name, |
| 85 | networks=networks, |
| 86 | **kwargs) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 87 | |
| 88 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 89 | waiters.wait_for_server_termination, |
| 90 | client, |
| 91 | server['server']['id']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 92 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 93 | client.delete_server, |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 94 | server['server']['id']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 95 | return server |
| 96 | |
| 97 | @classmethod |
Jens Harbott | 5435763 | 2017-11-21 11:47:06 +0000 | [diff] [blame] | 98 | def create_secgroup_rules(cls, rule_list, secgroup_id=None, |
| 99 | client=None): |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 100 | client = client or cls.os_primary.network_client |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 101 | if not secgroup_id: |
| 102 | sgs = client.list_security_groups()['security_groups'] |
| 103 | for sg in sgs: |
| 104 | if sg['name'] == constants.DEFAULT_SECURITY_GROUP: |
| 105 | secgroup_id = sg['id'] |
| 106 | break |
| 107 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 108 | for rule in rule_list: |
| 109 | direction = rule.pop('direction') |
| 110 | client.create_security_group_rule( |
| 111 | direction=direction, |
| 112 | security_group_id=secgroup_id, |
| 113 | **rule) |
| 114 | |
| 115 | @classmethod |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 116 | def create_loginable_secgroup_rule(cls, secgroup_id=None, |
| 117 | client=None): |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 118 | """This rule is intended to permit inbound ssh |
| 119 | |
| 120 | Allowing ssh traffic traffic from all sources, so no group_id is |
| 121 | provided. |
| 122 | Setting a group_id would only permit traffic from ports |
| 123 | belonging to the same security group. |
| 124 | """ |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 125 | return cls.create_security_group_rule( |
| 126 | security_group_id=secgroup_id, |
| 127 | client=client, |
| 128 | protocol=neutron_lib_constants.PROTO_NAME_TCP, |
| 129 | direction=neutron_lib_constants.INGRESS_DIRECTION, |
| 130 | port_range_min=22, |
| 131 | port_range_max=22) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 132 | |
| 133 | @classmethod |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 134 | def create_pingable_secgroup_rule(cls, secgroup_id=None, |
| 135 | client=None): |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 136 | """This rule is intended to permit inbound ping |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 137 | |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 138 | """ |
| 139 | return cls.create_security_group_rule( |
| 140 | security_group_id=secgroup_id, client=client, |
| 141 | protocol=neutron_lib_constants.PROTO_NAME_ICMP, |
| 142 | direction=neutron_lib_constants.INGRESS_DIRECTION) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 143 | |
| 144 | @classmethod |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 145 | def create_router_by_client(cls, is_admin=False, **kwargs): |
| 146 | kwargs.update({'router_name': data_utils.rand_name('router'), |
| 147 | 'admin_state_up': True, |
| 148 | 'external_network_id': CONF.network.public_network_id}) |
| 149 | if not is_admin: |
| 150 | router = cls.create_router(**kwargs) |
| 151 | else: |
| 152 | router = cls.create_admin_router(**kwargs) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 153 | LOG.debug("Created router %s", router['name']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 154 | cls.routers.append(router) |
| 155 | return router |
| 156 | |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 157 | @removals.remove(version='Stein', |
| 158 | message="Please use create_floatingip method instead of " |
| 159 | "create_and_associate_floatingip.") |
Roee Agiman | 6a0a18a | 2017-11-16 11:51:56 +0200 | [diff] [blame] | 160 | def create_and_associate_floatingip(self, port_id, client=None): |
| 161 | client = client or self.os_primary.network_client |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 162 | return self.create_floatingip(port_id=port_id, client=client) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 163 | |
Hongbin Lu | 965b03d | 2018-04-25 22:32:30 +0000 | [diff] [blame] | 164 | def create_interface(cls, server_id, port_id, client=None): |
| 165 | client = client or cls.os_primary.interfaces_client |
| 166 | body = client.create_interface(server_id, port_id=port_id) |
| 167 | return body['interfaceAttachment'] |
| 168 | |
| 169 | def delete_interface(cls, server_id, port_id, client=None): |
| 170 | client = client or cls.os_primary.interfaces_client |
| 171 | client.delete_interface(server_id, port_id=port_id) |
| 172 | |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 173 | def setup_network_and_server( |
Assaf Muller | d54ae6c | 2018-05-31 11:38:00 -0400 | [diff] [blame] | 174 | self, router=None, server_name=None, network=None, **kwargs): |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 175 | """Create network resources and a server. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 176 | |
| 177 | Creating a network, subnet, router, keypair, security group |
| 178 | and a server. |
| 179 | """ |
Assaf Muller | d54ae6c | 2018-05-31 11:38:00 -0400 | [diff] [blame] | 180 | self.network = network or self.create_network() |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 181 | LOG.debug("Created network %s", self.network['name']) |
| 182 | self.subnet = self.create_subnet(self.network) |
| 183 | LOG.debug("Created subnet %s", self.subnet['id']) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 184 | |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 185 | secgroup = self.os_primary.network_client.create_security_group( |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 186 | name=data_utils.rand_name('secgroup')) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 187 | LOG.debug("Created security group %s", |
| 188 | secgroup['security_group']['name']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 189 | self.security_groups.append(secgroup['security_group']) |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 190 | if not router: |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 191 | router = self.create_router_by_client(**kwargs) |
| 192 | self.create_router_interface(router['id'], self.subnet['id']) |
| 193 | self.keypair = self.create_keypair() |
| 194 | self.create_loginable_secgroup_rule( |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 195 | secgroup_id=secgroup['security_group']['id']) |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 196 | |
| 197 | server_kwargs = { |
| 198 | 'flavor_ref': CONF.compute.flavor_ref, |
| 199 | 'image_ref': CONF.compute.image_ref, |
| 200 | 'key_name': self.keypair['name'], |
| 201 | 'networks': [{'uuid': self.network['id']}], |
| 202 | 'security_groups': [{'name': secgroup['security_group']['name']}], |
| 203 | } |
| 204 | if server_name is not None: |
| 205 | server_kwargs['name'] = server_name |
| 206 | |
| 207 | self.server = self.create_server(**server_kwargs) |
Federico Ressi | e7417b7 | 2018-05-30 05:50:58 +0200 | [diff] [blame] | 208 | self.wait_for_server_active(self.server['server']) |
Jakub Libosvar | 1345d9d | 2017-06-09 13:59:05 +0000 | [diff] [blame] | 209 | self.port = self.client.list_ports(network_id=self.network['id'], |
| 210 | device_id=self.server[ |
| 211 | 'server']['id'])['ports'][0] |
Federico Ressi | bf877c8 | 2018-08-22 08:36:37 +0200 | [diff] [blame] | 212 | self.fip = self.create_floatingip(port=self.port) |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 213 | |
| 214 | def check_connectivity(self, host, ssh_user, ssh_key, servers=None): |
| 215 | ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key) |
| 216 | try: |
| 217 | ssh_client.test_connection_auth() |
| 218 | except lib_exc.SSHTimeout as ssh_e: |
| 219 | LOG.debug(ssh_e) |
| 220 | self._log_console_output(servers) |
| 221 | raise |
| 222 | |
| 223 | def _log_console_output(self, servers=None): |
| 224 | if not CONF.compute_feature_enabled.console_output: |
| 225 | LOG.debug('Console output not supported, cannot log') |
| 226 | return |
| 227 | if not servers: |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 228 | servers = self.os_primary.servers_client.list_servers() |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 229 | servers = servers['servers'] |
| 230 | for server in servers: |
| 231 | try: |
| 232 | console_output = ( |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 233 | self.os_primary.servers_client.get_console_output( |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 234 | server['id'])['output']) |
| 235 | LOG.debug('Console output for %s\nbody=\n%s', |
| 236 | server['id'], console_output) |
| 237 | except lib_exc.NotFound: |
| 238 | LOG.debug("Server %s disappeared(deleted) while looking " |
| 239 | "for the console log", server['id']) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 240 | |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 241 | def _check_remote_connectivity(self, source, dest, count, |
| 242 | should_succeed=True, |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 243 | nic=None, mtu=None, fragmentation=True, |
| 244 | timeout=None): |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 245 | """check ping server via source ssh connection |
| 246 | |
| 247 | :param source: RemoteClient: an ssh connection from which to ping |
| 248 | :param dest: and IP to ping against |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 249 | :param count: Number of ping packet(s) to send |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 250 | :param should_succeed: boolean should ping succeed or not |
| 251 | :param nic: specific network interface to ping from |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 252 | :param mtu: mtu size for the packet to be sent |
| 253 | :param fragmentation: Flag for packet fragmentation |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 254 | :param timeout: Timeout for all ping packet(s) to succeed |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 255 | :returns: boolean -- should_succeed == ping |
| 256 | :returns: ping is false if ping failed |
| 257 | """ |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 258 | def ping_host(source, host, count, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 259 | size=CONF.validation.ping_size, nic=None, mtu=None, |
| 260 | fragmentation=True): |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 261 | IP_VERSION_4 = neutron_lib_constants.IP_VERSION_4 |
| 262 | IP_VERSION_6 = neutron_lib_constants.IP_VERSION_6 |
| 263 | |
| 264 | # Use 'ping6' for IPv6 addresses, 'ping' for IPv4 and hostnames |
| 265 | ip_version = ( |
| 266 | IP_VERSION_6 if netaddr.valid_ipv6(host) else IP_VERSION_4) |
| 267 | cmd = ( |
| 268 | 'ping6' if ip_version == IP_VERSION_6 else 'ping') |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 269 | if nic: |
| 270 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 271 | if mtu: |
| 272 | if not fragmentation: |
| 273 | cmd += ' -M do' |
| 274 | size = str(net_utils.get_ping_payload_size( |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 275 | mtu=mtu, ip_version=ip_version)) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 276 | cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host) |
| 277 | return source.exec_command(cmd) |
| 278 | |
| 279 | def ping_remote(): |
| 280 | try: |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 281 | result = ping_host(source, dest, count, nic=nic, mtu=mtu, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame] | 282 | fragmentation=fragmentation) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 283 | |
| 284 | except lib_exc.SSHExecCommandFailed: |
| 285 | LOG.warning('Failed to ping IP: %s via a ssh connection ' |
| 286 | 'from: %s.', dest, source.host) |
| 287 | return not should_succeed |
| 288 | LOG.debug('ping result: %s', result) |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 289 | |
| 290 | if validators.validate_ip_address(dest) is None: |
| 291 | # Assert that the return traffic was from the correct |
| 292 | # source address. |
| 293 | from_source = 'from %s' % dest |
| 294 | self.assertIn(from_source, result) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 295 | return should_succeed |
| 296 | |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 297 | return test_utils.call_until_true( |
| 298 | ping_remote, timeout or CONF.validation.ping_timeout, 1) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 299 | |
| 300 | def check_remote_connectivity(self, source, dest, should_succeed=True, |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 301 | nic=None, mtu=None, fragmentation=True, |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 302 | servers=None, timeout=None, |
| 303 | ping_count=CONF.validation.ping_count): |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 304 | try: |
| 305 | self.assertTrue(self._check_remote_connectivity( |
LIU Yulong | 68ab245 | 2019-05-18 10:19:49 +0800 | [diff] [blame] | 306 | source, dest, ping_count, should_succeed, nic, mtu, |
| 307 | fragmentation, |
Assaf Muller | 92fdc78 | 2018-05-31 10:32:47 -0400 | [diff] [blame] | 308 | timeout=timeout)) |
Slawek Kaplonski | b07251f | 2018-05-16 12:21:50 +0200 | [diff] [blame] | 309 | except lib_exc.SSHTimeout as ssh_e: |
| 310 | LOG.debug(ssh_e) |
| 311 | self._log_console_output(servers) |
| 312 | raise |
| 313 | except AssertionError: |
| 314 | self._log_console_output(servers) |
| 315 | raise |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 316 | |
| 317 | def ping_ip_address(self, ip_address, should_succeed=True, |
| 318 | ping_timeout=None, mtu=None): |
| 319 | # the code is taken from tempest/scenario/manager.py in tempest git |
| 320 | timeout = ping_timeout or CONF.validation.ping_timeout |
| 321 | cmd = ['ping', '-c1', '-w1'] |
| 322 | |
| 323 | if mtu: |
| 324 | cmd += [ |
| 325 | # don't fragment |
| 326 | '-M', 'do', |
| 327 | # ping receives just the size of ICMP payload |
| 328 | '-s', str(net_utils.get_ping_payload_size(mtu, 4)) |
| 329 | ] |
| 330 | cmd.append(ip_address) |
| 331 | |
| 332 | def ping(): |
| 333 | proc = subprocess.Popen(cmd, |
| 334 | stdout=subprocess.PIPE, |
| 335 | stderr=subprocess.PIPE) |
| 336 | proc.communicate() |
| 337 | |
| 338 | return (proc.returncode == 0) == should_succeed |
| 339 | |
| 340 | caller = test_utils.find_test_caller() |
| 341 | LOG.debug('%(caller)s begins to ping %(ip)s in %(timeout)s sec and the' |
| 342 | ' expected result is %(should_succeed)s', { |
| 343 | 'caller': caller, 'ip': ip_address, 'timeout': timeout, |
| 344 | 'should_succeed': |
| 345 | 'reachable' if should_succeed else 'unreachable' |
| 346 | }) |
| 347 | result = test_utils.call_until_true(ping, timeout, 1) |
Manjeet Singh Bhatia | 8bbf899 | 2019-03-04 11:59:57 -0800 | [diff] [blame] | 348 | |
| 349 | # To make sure ping_ip_address called by test works |
| 350 | # as expected. |
| 351 | self.assertTrue(result) |
| 352 | |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 353 | LOG.debug('%(caller)s finishes ping %(ip)s in %(timeout)s sec and the ' |
| 354 | 'ping result is %(result)s', { |
| 355 | 'caller': caller, 'ip': ip_address, 'timeout': timeout, |
| 356 | 'result': 'expected' if result else 'unexpected' |
| 357 | }) |
| 358 | return result |
Federico Ressi | e7417b7 | 2018-05-30 05:50:58 +0200 | [diff] [blame] | 359 | |
| 360 | def wait_for_server_status(self, server, status, client=None, **kwargs): |
| 361 | """Waits for a server to reach a given status. |
| 362 | |
| 363 | :param server: mapping having schema {'id': <server_id>} |
| 364 | :param status: string status to wait for (es: 'ACTIVE') |
| 365 | :param clien: servers client (self.os_primary.servers_client as |
| 366 | default value) |
| 367 | """ |
| 368 | |
| 369 | client = client or self.os_primary.servers_client |
| 370 | waiters.wait_for_server_status(client, server['id'], status, **kwargs) |
| 371 | |
| 372 | def wait_for_server_active(self, server, client=None): |
| 373 | """Waits for a server to reach active status. |
| 374 | |
| 375 | :param server: mapping having schema {'id': <server_id>} |
| 376 | :param clien: servers client (self.os_primary.servers_client as |
| 377 | default value) |
| 378 | """ |
| 379 | self.wait_for_server_status( |
| 380 | server, constants.SERVER_STATUS_ACTIVE, client) |