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