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