Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 1 | # Copyright 2011, VMware, 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. |
| 15 | # |
| 16 | # Borrowed from nova code base, more utilities will be added/borrowed as and |
| 17 | # when needed. |
| 18 | |
| 19 | """Utilities and helper functions.""" |
| 20 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 21 | import threading |
| 22 | import time |
zheng.yong | 74e760a | 2019-05-22 14:16:14 +0800 | [diff] [blame] | 23 | try: |
| 24 | import urlparse |
| 25 | except ImportError: |
| 26 | from urllib import parse as urlparse |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 27 | |
Brian Haley | 33ef460 | 2018-04-26 14:37:49 -0400 | [diff] [blame] | 28 | import eventlet |
Rodolfo Alonso Hernandez | 0adf8a2 | 2020-06-11 11:28:25 +0000 | [diff] [blame] | 29 | |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 30 | from oslo_log import log |
Maciej Józefczyk | 328edc8 | 2019-09-16 14:05:48 +0000 | [diff] [blame] | 31 | from tempest.lib import exceptions |
Brian Haley | 33ef460 | 2018-04-26 14:37:49 -0400 | [diff] [blame] | 32 | |
Eduardo Olivares | 1f665d8 | 2022-02-14 17:22:54 +0100 | [diff] [blame] | 33 | from neutron_tempest_plugin import config |
| 34 | |
Rodolfo Alonso Hernandez | 0adf8a2 | 2020-06-11 11:28:25 +0000 | [diff] [blame] | 35 | |
zheng.yong | 74e760a | 2019-05-22 14:16:14 +0800 | [diff] [blame] | 36 | SCHEMA_PORT_MAPPING = { |
| 37 | "http": 80, |
| 38 | "https": 443, |
| 39 | } |
Eduardo Olivares | 1f665d8 | 2022-02-14 17:22:54 +0100 | [diff] [blame] | 40 | CONF = config.CONF |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 41 | LOG = log.getLogger(__name__) |
zheng.yong | 74e760a | 2019-05-22 14:16:14 +0800 | [diff] [blame] | 42 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 43 | |
| 44 | class classproperty(object): |
| 45 | def __init__(self, f): |
| 46 | self.func = f |
| 47 | |
| 48 | def __get__(self, obj, owner): |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 49 | return self.func(owner) |
| 50 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 51 | |
| 52 | class WaitTimeout(Exception): |
| 53 | """Default exception coming from wait_until_true() function.""" |
| 54 | |
| 55 | |
| 56 | class LockWithTimer(object): |
| 57 | def __init__(self, threshold): |
| 58 | self._threshold = threshold |
| 59 | self.timestamp = 0 |
| 60 | self._lock = threading.Lock() |
| 61 | |
| 62 | def acquire(self): |
| 63 | return self._lock.acquire(False) |
| 64 | |
| 65 | def release(self): |
| 66 | return self._lock.release() |
| 67 | |
| 68 | def time_to_wait(self): |
| 69 | return self.timestamp - time.time() + self._threshold |
| 70 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 71 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 72 | def wait_until_true(predicate, timeout=60, sleep=1, exception=None): |
Brian Haley | ae328b9 | 2018-10-09 19:51:54 -0400 | [diff] [blame] | 73 | """Wait until callable predicate is evaluated as True |
| 74 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 75 | :param predicate: Callable deciding whether waiting should continue. |
Elod Illes | f2e985e | 2023-11-06 19:30:29 +0100 | [diff] [blame] | 76 | Best practice is to instantiate predicate with functools.partial() |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 77 | :param timeout: Timeout in seconds how long should function wait. |
| 78 | :param sleep: Polling interval for results in seconds. |
| 79 | :param exception: Exception instance to raise on timeout. If None is passed |
| 80 | (default) then WaitTimeout exception is raised. |
| 81 | """ |
| 82 | try: |
| 83 | with eventlet.Timeout(timeout): |
| 84 | while not predicate(): |
| 85 | eventlet.sleep(sleep) |
| 86 | except eventlet.Timeout: |
| 87 | if exception is not None: |
Brian Haley | 8aaa73f | 2018-10-09 19:55:44 -0400 | [diff] [blame] | 88 | # pylint: disable=raising-bad-type |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 89 | raise exception |
| 90 | raise WaitTimeout("Timed out after %d seconds" % timeout) |
Brian Haley | ba80045 | 2017-12-14 10:30:48 -0500 | [diff] [blame] | 91 | |
| 92 | |
Federico Ressi | 0e04f8f | 2018-10-24 12:19:05 +0200 | [diff] [blame] | 93 | def override_class(overriden_class, overrider_class): |
| 94 | """Override class definition with a MixIn class |
| 95 | |
| 96 | If overriden_class is not a subclass of overrider_class then it creates |
| 97 | a new class that has as bases overrider_class and overriden_class. |
| 98 | """ |
| 99 | |
| 100 | if not issubclass(overriden_class, overrider_class): |
| 101 | name = overriden_class.__name__ |
| 102 | bases = (overrider_class, overriden_class) |
| 103 | overriden_class = type(name, bases, {}) |
| 104 | return overriden_class |
zheng.yong | 74e760a | 2019-05-22 14:16:14 +0800 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def normalize_url(url): |
| 108 | """Normalize url without port with schema default port |
| 109 | |
| 110 | """ |
| 111 | parse_result = urlparse.urlparse(url) |
| 112 | (scheme, netloc, url, params, query, fragment) = parse_result |
| 113 | port = parse_result.port |
| 114 | if scheme in SCHEMA_PORT_MAPPING and not port: |
| 115 | netloc = netloc + ":" + str(SCHEMA_PORT_MAPPING[scheme]) |
| 116 | return urlparse.urlunparse((scheme, netloc, url, params, query, fragment)) |
Maciej Józefczyk | 328edc8 | 2019-09-16 14:05:48 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | def kill_nc_process(ssh_client): |
| 120 | cmd = "killall -q nc" |
| 121 | try: |
| 122 | ssh_client.exec_command(cmd) |
| 123 | except exceptions.SSHExecCommandFailed: |
| 124 | pass |
| 125 | |
| 126 | |
Slawek Kaplonski | fd4141f | 2020-03-14 14:34:00 +0100 | [diff] [blame] | 127 | def process_is_running(ssh_client, process_name): |
| 128 | try: |
| 129 | ssh_client.exec_command("pidof %s" % process_name) |
| 130 | return True |
| 131 | except exceptions.SSHExecCommandFailed: |
| 132 | return False |
| 133 | |
| 134 | |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 135 | class StatefulConnection: |
| 136 | """Class to test connection that should remain opened |
| 137 | |
| 138 | Can be used to perform some actions while the initiated connection |
| 139 | remain opened |
| 140 | """ |
| 141 | |
| 142 | def __init__(self, client_ssh, server_ssh, target_ip, target_port): |
| 143 | self.client_ssh = client_ssh |
| 144 | self.server_ssh = server_ssh |
| 145 | self.ip = target_ip |
| 146 | self.port = target_port |
| 147 | self.connection_started = False |
| 148 | self.test_attempt = 0 |
Alex Katz | 305ea4a | 2022-08-10 19:47:03 +0300 | [diff] [blame] | 149 | self.test_timeout = 10 |
| 150 | self.test_sleep = 1 |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 151 | |
| 152 | def __enter__(self): |
| 153 | return self |
| 154 | |
| 155 | @property |
| 156 | def test_str(self): |
| 157 | return 'attempt_{}'.format(str(self.test_attempt).zfill(3)) |
| 158 | |
| 159 | def _start_connection(self): |
Eduardo Olivares | 1f665d8 | 2022-02-14 17:22:54 +0100 | [diff] [blame] | 160 | if CONF.neutron_plugin_options.default_image_is_advanced: |
| 161 | server_exec_method = self.server_ssh.execute_script |
| 162 | client_exec_method = self.client_ssh.execute_script |
| 163 | else: |
| 164 | server_exec_method = self.server_ssh.exec_command |
| 165 | client_exec_method = self.client_ssh.exec_command |
| 166 | |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 167 | self.server_ssh.exec_command( |
| 168 | 'echo "{}" > input.txt'.format(self.test_str)) |
Alex Katz | 305ea4a | 2022-08-10 19:47:03 +0300 | [diff] [blame] | 169 | server_exec_method('tail -f input.txt | sudo nc -lp ' |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 170 | '{} &> output.txt &'.format(self.port)) |
| 171 | self.client_ssh.exec_command( |
| 172 | 'echo "{}" > input.txt'.format(self.test_str)) |
Alex Katz | 305ea4a | 2022-08-10 19:47:03 +0300 | [diff] [blame] | 173 | client_exec_method('tail -f input.txt | sudo nc {} {} &>' |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 174 | 'output.txt &'.format(self.ip, self.port)) |
| 175 | |
Alex Katz | 305ea4a | 2022-08-10 19:47:03 +0300 | [diff] [blame] | 176 | def _nc_is_running(self): |
| 177 | server = process_is_running(self.server_ssh, 'nc') |
| 178 | client = process_is_running(self.client_ssh, 'nc') |
| 179 | if client and server: |
| 180 | return True |
| 181 | else: |
| 182 | return False |
| 183 | |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 184 | def _test_connection(self): |
| 185 | if not self.connection_started: |
| 186 | self._start_connection() |
| 187 | else: |
| 188 | self.server_ssh.exec_command( |
| 189 | 'echo "{}" >> input.txt'.format(self.test_str)) |
| 190 | self.client_ssh.exec_command( |
| 191 | 'echo "{}" >> input.txt & sleep 1'.format(self.test_str)) |
Alex Katz | 305ea4a | 2022-08-10 19:47:03 +0300 | [diff] [blame] | 192 | wait_until_true(self._nc_is_running, |
| 193 | timeout=self.test_timeout, |
| 194 | sleep=self.test_sleep) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 195 | try: |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 196 | LOG.info("Checking connectivity between server and client -" |
| 197 | " attempt {}".format(self.test_attempt)) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 198 | self.server_ssh.exec_command( |
| 199 | 'grep {} output.txt'.format(self.test_str)) |
| 200 | self.client_ssh.exec_command( |
| 201 | 'grep {} output.txt'.format(self.test_str)) |
| 202 | if not self.should_pass: |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 203 | LOG.warning("attempt {} succeed while it should fail".format( |
| 204 | self.test_attempt)) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 205 | return False |
| 206 | else: |
| 207 | if not self.connection_started: |
| 208 | self.connection_started = True |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 209 | LOG.info("attempt {} succeed as it expected".format( |
| 210 | self.test_attempt)) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 211 | return True |
| 212 | except exceptions.SSHExecCommandFailed: |
| 213 | if self.should_pass: |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 214 | LOG.warning("attempt {} failed while it should pass".format( |
| 215 | self.test_attempt)) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 216 | return False |
| 217 | else: |
Alex Katz | 5684d5e | 2022-08-15 23:50:24 +0300 | [diff] [blame] | 218 | LOG.info("attempt {} failed as it expected".format( |
| 219 | self.test_attempt)) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 220 | return True |
| 221 | finally: |
| 222 | self.test_attempt += 1 |
| 223 | |
| 224 | def test_connection(self, should_pass=True, timeout=10, sleep_timer=1): |
| 225 | self.should_pass = should_pass |
Alex Katz | 305ea4a | 2022-08-10 19:47:03 +0300 | [diff] [blame] | 226 | self.test_timeout = timeout |
| 227 | self.test_sleep = sleep_timer |
| 228 | wait_until_true(self._test_connection, |
| 229 | timeout=self.test_timeout, |
| 230 | sleep=self.test_sleep) |
Alex Katz | bd2bfd4 | 2021-05-26 18:12:36 +0300 | [diff] [blame] | 231 | |
| 232 | def __exit__(self, type, value, traceback): |
Eduardo Olivares | 1f665d8 | 2022-02-14 17:22:54 +0100 | [diff] [blame] | 233 | self.server_ssh.exec_command('sudo killall nc || killall nc || ' |
| 234 | 'echo "True"') |
Alex Katz | 5d1043b | 2021-08-03 10:21:43 +0300 | [diff] [blame] | 235 | self.server_ssh.exec_command( |
| 236 | 'sudo killall tail || killall tail || echo "True"') |
Eduardo Olivares | 1f665d8 | 2022-02-14 17:22:54 +0100 | [diff] [blame] | 237 | self.client_ssh.exec_command('sudo killall nc || killall nc || ' |
| 238 | 'echo "True"') |
Alex Katz | 5d1043b | 2021-08-03 10:21:43 +0300 | [diff] [blame] | 239 | self.client_ssh.exec_command( |
| 240 | 'sudo killall tail || killall tail || echo "True"') |