| 1 |
# package is named tests, not test, so it won't be confused with test in stdlib |
| 2 |
|
| 3 |
import contextlib |
| 4 |
import errno |
| 5 |
import functools |
| 6 |
import gc |
| 7 |
import io |
| 8 |
import json |
| 9 |
import os |
| 10 |
try: |
| 11 |
import resource |
| 12 |
except ImportError: |
| 13 |
resource = None |
| 14 |
import signal |
| 15 |
import subprocess |
| 16 |
import sys |
| 17 |
import unittest |
| 18 |
import warnings |
| 19 |
|
| 20 |
from unittest import SkipTest |
| 21 |
|
| 22 |
import eventlet |
| 23 |
from eventlet import tpool |
| 24 |
import socket |
| 25 |
from threading import Thread |
| 26 |
import struct |
| 27 |
|
| 28 |
|
| 29 |
# convenience for importers |
| 30 |
main = unittest.main |
| 31 |
|
| 32 |
|
| 33 |
@contextlib.contextmanager |
| 34 |
def assert_raises(exc_type): |
| 35 |
try: |
| 36 |
yield |
| 37 |
except exc_type: |
| 38 |
pass |
| 39 |
else: |
| 40 |
name = str(exc_type) |
| 41 |
try: |
| 42 |
name = exc_type.__name__ |
| 43 |
except AttributeError: |
| 44 |
pass |
| 45 |
assert False, 'Expected exception {}'.format(name) |
| 46 |
|
| 47 |
|
| 48 |
def skipped(func, *decorator_args): |
| 49 |
"""Decorator that marks a function as skipped. |
| 50 |
""" |
| 51 |
@functools.wraps(func) |
| 52 |
def wrapped(*a, **k): |
| 53 |
raise SkipTest(*decorator_args) |
| 54 |
|
| 55 |
return wrapped |
| 56 |
|
| 57 |
|
| 58 |
def skip_if(condition): |
| 59 |
""" Decorator that skips a test if the *condition* evaluates True. |
| 60 |
*condition* can be a boolean or a callable that accepts one argument. |
| 61 |
The callable will be called with the function to be decorated, and |
| 62 |
should return True to skip the test. |
| 63 |
""" |
| 64 |
def skipped_wrapper(func): |
| 65 |
@functools.wraps(func) |
| 66 |
def wrapped(*a, **kw): |
| 67 |
if isinstance(condition, bool): |
| 68 |
result = condition |
| 69 |
else: |
| 70 |
result = condition(func) |
| 71 |
if result: |
| 72 |
raise SkipTest() |
| 73 |
else: |
| 74 |
return func(*a, **kw) |
| 75 |
return wrapped |
| 76 |
return skipped_wrapper |
| 77 |
|
| 78 |
|
| 79 |
def skip_unless(condition): |
| 80 |
""" Decorator that skips a test if the *condition* does not return True. |
| 81 |
*condition* can be a boolean or a callable that accepts one argument. |
| 82 |
The callable will be called with the function to be decorated, and |
| 83 |
should return True if the condition is satisfied. |
| 84 |
""" |
| 85 |
def skipped_wrapper(func): |
| 86 |
@functools.wraps(func) |
| 87 |
def wrapped(*a, **kw): |
| 88 |
if isinstance(condition, bool): |
| 89 |
result = condition |
| 90 |
else: |
| 91 |
result = condition(func) |
| 92 |
if not result: |
| 93 |
raise SkipTest() |
| 94 |
else: |
| 95 |
return func(*a, **kw) |
| 96 |
return wrapped |
| 97 |
return skipped_wrapper |
| 98 |
|
| 99 |
|
| 100 |
def skip_on_windows(func): |
| 101 |
""" Decorator that skips a test on Windows.""" |
| 102 |
return skip_if(sys.platform.startswith('win'))(func) |
| 103 |
|
| 104 |
|
| 105 |
def skip_if_no_itimer(func): |
| 106 |
""" Decorator that skips a test if the `itimer` module isn't found """ |
| 107 |
has_itimer = False |
| 108 |
try: |
| 109 |
import itimer |
| 110 |
has_itimer = True |
| 111 |
except ImportError: |
| 112 |
pass |
| 113 |
return skip_unless(has_itimer)(func) |
| 114 |
|
| 115 |
|
| 116 |
def skip_if_CRLock_exist(func): |
| 117 |
""" Decorator that skips a test if the `_thread.RLock` class exists """ |
| 118 |
try: |
| 119 |
from _thread import RLock |
| 120 |
return skipped(func) |
| 121 |
except ImportError: |
| 122 |
return func |
| 123 |
|
| 124 |
|
| 125 |
def skip_if_no_ssl(func): |
| 126 |
""" Decorator that skips a test if SSL is not available.""" |
| 127 |
try: |
| 128 |
import eventlet.green.ssl |
| 129 |
return func |
| 130 |
except ImportError: |
| 131 |
try: |
| 132 |
import eventlet.green.OpenSSL |
| 133 |
return func |
| 134 |
except ImportError: |
| 135 |
return skipped(func) |
| 136 |
|
| 137 |
|
| 138 |
def skip_if_no_ipv6(func): |
| 139 |
if os.environ.get('eventlet_test_ipv6') != '1': |
| 140 |
return skipped(func) |
| 141 |
return func |
| 142 |
|
| 143 |
|
| 144 |
class TestIsTakingTooLong(Exception): |
| 145 |
""" Custom exception class to be raised when a test's runtime exceeds a limit. """ |
| 146 |
pass |
| 147 |
|
| 148 |
|
| 149 |
class LimitedTestCase(unittest.TestCase): |
| 150 |
""" Unittest subclass that adds a timeout to all tests. Subclasses must |
| 151 |
be sure to call the LimitedTestCase setUp and tearDown methods. The default |
| 152 |
timeout is 1 second, change it by setting TEST_TIMEOUT to the desired |
| 153 |
quantity.""" |
| 154 |
|
| 155 |
TEST_TIMEOUT = 2 |
| 156 |
|
| 157 |
def setUp(self): |
| 158 |
self.previous_alarm = None |
| 159 |
self.timer = eventlet.Timeout(self.TEST_TIMEOUT, |
| 160 |
TestIsTakingTooLong(self.TEST_TIMEOUT)) |
| 161 |
|
| 162 |
def reset_timeout(self, new_timeout): |
| 163 |
"""Changes the timeout duration; only has effect during one test. |
| 164 |
`new_timeout` can be int or float. |
| 165 |
""" |
| 166 |
self.timer.cancel() |
| 167 |
self.timer = eventlet.Timeout(new_timeout, |
| 168 |
TestIsTakingTooLong(new_timeout)) |
| 169 |
|
| 170 |
def set_alarm(self, new_timeout): |
| 171 |
"""Call this in the beginning of your test if you expect busy loops. |
| 172 |
Only has effect during one test. |
| 173 |
`new_timeout` must be int. |
| 174 |
""" |
| 175 |
def sig_alarm_handler(sig, frame): |
| 176 |
# Could arm previous alarm but test is failed anyway |
| 177 |
# seems to be no point in restoring previous state. |
| 178 |
raise TestIsTakingTooLong(new_timeout) |
| 179 |
|
| 180 |
self.previous_alarm = ( |
| 181 |
signal.signal(signal.SIGALRM, sig_alarm_handler), |
| 182 |
signal.alarm(new_timeout), |
| 183 |
) |
| 184 |
|
| 185 |
def tearDown(self): |
| 186 |
self.timer.cancel() |
| 187 |
if self.previous_alarm: |
| 188 |
signal.signal(signal.SIGALRM, self.previous_alarm[0]) |
| 189 |
signal.alarm(self.previous_alarm[1]) |
| 190 |
|
| 191 |
tpool.killall() |
| 192 |
gc.collect() |
| 193 |
eventlet.sleep(0) |
| 194 |
verify_hub_empty() |
| 195 |
|
| 196 |
def assert_less_than(self, a, b, msg=None): |
| 197 |
msg = msg or "%s not less than %s" % (a, b) |
| 198 |
assert a < b, msg |
| 199 |
|
| 200 |
assertLessThan = assert_less_than |
| 201 |
|
| 202 |
def assert_less_than_equal(self, a, b, msg=None): |
| 203 |
msg = msg or "%s not less than or equal to %s" % (a, b) |
| 204 |
assert a <= b, msg |
| 205 |
|
| 206 |
assertLessThanEqual = assert_less_than_equal |
| 207 |
|
| 208 |
|
| 209 |
def check_idle_cpu_usage(duration, allowed_part): |
| 210 |
if resource is None: |
| 211 |
# TODO: use https://code.google.com/p/psutil/ |
| 212 |
raise SkipTest('CPU usage testing not supported (`import resource` failed)') |
| 213 |
|
| 214 |
r1 = resource.getrusage(resource.RUSAGE_SELF) |
| 215 |
eventlet.sleep(duration) |
| 216 |
r2 = resource.getrusage(resource.RUSAGE_SELF) |
| 217 |
utime = r2.ru_utime - r1.ru_utime |
| 218 |
stime = r2.ru_stime - r1.ru_stime |
| 219 |
|
| 220 |
# This check is reliably unreliable on Travis/Github Actions, presumably because of CPU |
| 221 |
# resources being quite restricted by the build environment. The workaround |
| 222 |
# is to apply an arbitrary factor that should be enough to make it work nicely. |
| 223 |
if os.environ.get('CI') == 'true': |
| 224 |
allowed_part *= 5 |
| 225 |
|
| 226 |
assert utime + stime < duration * allowed_part, \ |
| 227 |
"CPU usage over limit: user %.0f%% sys %.0f%% allowed %.0f%%" % ( |
| 228 |
utime / duration * 100, stime / duration * 100, |
| 229 |
allowed_part * 100) |
| 230 |
|
| 231 |
|
| 232 |
def verify_hub_empty(): |
| 233 |
|
| 234 |
def format_listener(listener): |
| 235 |
return 'Listener %r for greenlet %r with run callback %r' % ( |
| 236 |
listener, listener.greenlet, getattr(listener.greenlet, 'run', None)) |
| 237 |
|
| 238 |
from eventlet import hubs |
| 239 |
hub = hubs.get_hub() |
| 240 |
readers = hub.get_readers() |
| 241 |
writers = hub.get_writers() |
| 242 |
num_readers = len(readers) |
| 243 |
num_writers = len(writers) |
| 244 |
num_timers = hub.get_timers_count() |
| 245 |
assert num_readers == 0 and num_writers == 0, \ |
| 246 |
"Readers: %s (%d) Writers: %s (%d)" % ( |
| 247 |
', '.join(map(format_listener, readers)), num_readers, |
| 248 |
', '.join(map(format_listener, writers)), num_writers, |
| 249 |
) |
| 250 |
|
| 251 |
|
| 252 |
def find_command(command): |
| 253 |
for dir in os.getenv('PATH', '/usr/bin:/usr/sbin').split(os.pathsep): |
| 254 |
p = os.path.join(dir, command) |
| 255 |
if os.access(p, os.X_OK): |
| 256 |
return p |
| 257 |
raise OSError(errno.ENOENT, 'Command not found: %r' % command) |
| 258 |
|
| 259 |
|
| 260 |
def silence_warnings(func): |
| 261 |
def wrapper(*args, **kw): |
| 262 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 263 |
try: |
| 264 |
return func(*args, **kw) |
| 265 |
finally: |
| 266 |
warnings.simplefilter('default', DeprecationWarning) |
| 267 |
wrapper.__name__ = func.__name__ |
| 268 |
return wrapper |
| 269 |
|
| 270 |
|
| 271 |
def get_database_auth(): |
| 272 |
"""Retrieves a dict of connection parameters for connecting to test databases. |
| 273 |
|
| 274 |
Authentication parameters are highly-machine specific, so |
| 275 |
get_database_auth gets its information from either environment |
| 276 |
variables or a config file. The environment variable is |
| 277 |
"EVENTLET_DB_TEST_AUTH" and it should contain a json object. If |
| 278 |
this environment variable is present, it's used and config files |
| 279 |
are ignored. If it's not present, it looks in the local directory |
| 280 |
(tests) and in the user's home directory for a file named |
| 281 |
".test_dbauth", which contains a json map of parameters to the |
| 282 |
connect function. |
| 283 |
""" |
| 284 |
retval = { |
| 285 |
'MySQLdb': {'host': 'localhost', 'user': 'root', 'passwd': ''}, |
| 286 |
'psycopg2': {'user': 'test'}, |
| 287 |
} |
| 288 |
|
| 289 |
if 'EVENTLET_DB_TEST_AUTH' in os.environ: |
| 290 |
return json.loads(os.environ.get('EVENTLET_DB_TEST_AUTH')) |
| 291 |
|
| 292 |
files = [os.path.join(os.path.dirname(__file__), '.test_dbauth'), |
| 293 |
os.path.join(os.path.expanduser('~'), '.test_dbauth')] |
| 294 |
for f in files: |
| 295 |
try: |
| 296 |
auth_utf8 = json.load(open(f)) |
| 297 |
# Have to convert unicode objects to str objects because |
| 298 |
# mysqldb is dumb. Using a doubly-nested list comprehension |
| 299 |
# because we know that the structure is a two-level dict. |
| 300 |
return { |
| 301 |
str(modname): { |
| 302 |
str(k): str(v) for k, v in connectargs.items()} |
| 303 |
for modname, connectargs in auth_utf8.items()} |
| 304 |
except OSError: |
| 305 |
pass |
| 306 |
return retval |
| 307 |
|
| 308 |
|
| 309 |
def run_python(path, env=None, args=None, timeout=None, pythonpath_extend=None, expect_pass=False): |
| 310 |
new_argv = [sys.executable] |
| 311 |
new_env = os.environ.copy() |
| 312 |
new_env.setdefault('eventlet_test_in_progress', 'yes') |
| 313 |
src_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 314 |
if path: |
| 315 |
path = os.path.abspath(path) |
| 316 |
new_argv.append(path) |
| 317 |
new_env['PYTHONPATH'] = os.pathsep.join(sys.path + [src_dir]) |
| 318 |
if env: |
| 319 |
new_env.update(env) |
| 320 |
if pythonpath_extend: |
| 321 |
new_path = [p for p in new_env.get('PYTHONPATH', '').split(os.pathsep) if p] |
| 322 |
new_path.extend( |
| 323 |
p if os.path.isabs(p) else os.path.join(src_dir, p) for p in pythonpath_extend |
| 324 |
) |
| 325 |
new_env['PYTHONPATH'] = os.pathsep.join(new_path) |
| 326 |
if args: |
| 327 |
new_argv.extend(args) |
| 328 |
p = subprocess.Popen( |
| 329 |
new_argv, |
| 330 |
env=new_env, |
| 331 |
stderr=subprocess.STDOUT, |
| 332 |
stdin=subprocess.PIPE, |
| 333 |
stdout=subprocess.PIPE, |
| 334 |
) |
| 335 |
if timeout is None: |
| 336 |
timeout = 10 |
| 337 |
try: |
| 338 |
output, _ = p.communicate(timeout=timeout) |
| 339 |
except subprocess.TimeoutExpired: |
| 340 |
p.kill() |
| 341 |
output, _ = p.communicate(timeout=timeout) |
| 342 |
if expect_pass: |
| 343 |
sys.stderr.write('Program {} output:\n---\n{}\n---\n'.format(path, output.decode())) |
| 344 |
assert False, 'timed out' |
| 345 |
return '{}\nFAIL - timed out'.format(output).encode() |
| 346 |
|
| 347 |
if expect_pass: |
| 348 |
if output.startswith(b'skip'): |
| 349 |
parts = output.rstrip().split(b':', 1) |
| 350 |
skip_args = [] |
| 351 |
if len(parts) > 1: |
| 352 |
skip_args.append(parts[1]) |
| 353 |
raise SkipTest(*skip_args) |
| 354 |
lines = output.splitlines() |
| 355 |
ok = lines[-1].rstrip() == b'pass' |
| 356 |
if not ok or len(lines) > 1: |
| 357 |
sys.stderr.write('Program {} output:\n---\n{}\n---\n'.format(path, output.decode(errors="backslashreplace"))) |
| 358 |
assert ok, 'Expected single line "pass" in stdout' |
| 359 |
|
| 360 |
return output |
| 361 |
|
| 362 |
|
| 363 |
def run_isolated(path, prefix='tests/isolated/', **kwargs): |
| 364 |
kwargs.setdefault('expect_pass', True) |
| 365 |
run_python(prefix + path, **kwargs) |
| 366 |
|
| 367 |
|
| 368 |
def check_is_timeout(obj): |
| 369 |
value_text = getattr(obj, 'is_timeout', '(missing)') |
| 370 |
assert eventlet.is_timeout(obj), 'type={} str={} .is_timeout={}'.format(type(obj), str(obj), value_text) |
| 371 |
|
| 372 |
|
| 373 |
@contextlib.contextmanager |
| 374 |
def capture_stderr(): |
| 375 |
stream = io.StringIO() |
| 376 |
original = sys.stderr |
| 377 |
try: |
| 378 |
sys.stderr = stream |
| 379 |
yield stream |
| 380 |
finally: |
| 381 |
sys.stderr = original |
| 382 |
stream.seek(0) |
| 383 |
|
| 384 |
|
| 385 |
certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt') |
| 386 |
private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key') |
| 387 |
|
| 388 |
|
| 389 |
@contextlib.contextmanager |
| 390 |
def dns_tcp_server(ip_to_give, request_count=1): |
| 391 |
state = [0] # request count storage writable by thread |
| 392 |
host = "localhost" |
| 393 |
death_pill = b"DEATH_PILL" |
| 394 |
|
| 395 |
def extract_domain(data): |
| 396 |
domain = b'' |
| 397 |
kind = (data[4] >> 3) & 15 # Opcode bits |
| 398 |
if kind == 0: # Standard query |
| 399 |
ini = 14 |
| 400 |
length = data[ini] |
| 401 |
while length != 0: |
| 402 |
domain += data[ini + 1:ini + length + 1] + b'.' |
| 403 |
ini += length + 1 |
| 404 |
length = data[ini] |
| 405 |
return domain |
| 406 |
|
| 407 |
def answer(data, domain): |
| 408 |
domain_length = len(domain) |
| 409 |
packet = b'' |
| 410 |
if domain: |
| 411 |
# If an ip was given we return it in the answer |
| 412 |
if ip_to_give: |
| 413 |
packet += data[2:4] + b'\x81\x80' |
| 414 |
packet += data[6:8] + data[6:8] + b'\x00\x00\x00\x00' # Questions and answers counts |
| 415 |
packet += data[14: 14 + domain_length + 1] # Original domain name question |
| 416 |
packet += b'\x00\x01\x00\x01' # Type and class |
| 417 |
packet += b'\xc0\x0c\x00\x01' # TTL |
| 418 |
packet += b'\x00\x01' |
| 419 |
packet += b'\x00\x00\x00\x08' |
| 420 |
packet += b'\x00\x04' # Resource data length -> 4 bytes |
| 421 |
packet += bytearray(int(x) for x in ip_to_give.split(".")) |
| 422 |
else: |
| 423 |
packet += data[2:4] + b'\x85\x80' |
| 424 |
packet += data[6:8] + b'\x00\x00' + b'\x00\x00\x00\x00' # Questions and answers counts |
| 425 |
packet += data[14: 14 + domain_length + 1] # Original domain name question |
| 426 |
packet += b'\x00\x01\x00\x01' # Type and class |
| 427 |
|
| 428 |
sz = struct.pack('>H', len(packet)) |
| 429 |
return sz + packet |
| 430 |
|
| 431 |
def serve(server_socket): # thread target |
| 432 |
client_sock, address = server_socket.accept() |
| 433 |
state[0] += 1 |
| 434 |
if state[0] <= request_count: |
| 435 |
data = bytearray(client_sock.recv(1024)) |
| 436 |
if data == death_pill: |
| 437 |
client_sock.close() |
| 438 |
return |
| 439 |
|
| 440 |
domain = extract_domain(data) |
| 441 |
client_sock.sendall(answer(data, domain)) |
| 442 |
client_sock.close() |
| 443 |
|
| 444 |
# Server starts |
| 445 |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 446 |
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 447 |
server_socket.bind((host, 0)) |
| 448 |
server_socket.listen(5) |
| 449 |
server_addr = server_socket.getsockname() |
| 450 |
|
| 451 |
thread = Thread(target=serve, args=(server_socket, )) |
| 452 |
thread.start() |
| 453 |
|
| 454 |
yield server_addr |
| 455 |
|
| 456 |
# Stop the server |
| 457 |
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 458 |
client.connect(server_addr) |
| 459 |
client.send(death_pill) |
| 460 |
client.close() |
| 461 |
thread.join() |
| 462 |
server_socket.close() |
| 463 |
|
| 464 |
|
| 465 |
def read_file(path, mode="rb"): |
| 466 |
with open(path, mode) as f: |
| 467 |
result = f.read() |
| 468 |
return result |