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