|
| 1 | +# (c) Copyright IBM Corp. 2021 |
| 2 | +# (c) Copyright Instana Inc. 2020 |
| 3 | + |
| 4 | +import importlib |
| 5 | +import os |
| 6 | +import unittest |
| 7 | +import socket |
| 8 | + |
| 9 | +import gevent |
| 10 | +from gevent import monkey |
| 11 | +from instana import apply_gevent_monkey_patch |
| 12 | + |
| 13 | + |
| 14 | +class TestGEventAutoTrace(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + # Ensure that the test suite is operational even when Django is installed |
| 17 | + # but not running or configured |
| 18 | + os.environ['DJANGO_SETTINGS_MODULE'] = '' |
| 19 | + |
| 20 | + self.default_patched_modules = ('socket', 'time', 'select', 'os', |
| 21 | + 'threading', 'ssl', 'subprocess', 'signal', 'queue',) |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + if os.environ.get('INSTANA_GEVENT_MONKEY_OPTIONS'): |
| 25 | + os.environ.pop('INSTANA_GEVENT_MONKEY_OPTIONS') |
| 26 | + |
| 27 | + # Clean up after gevent monkey patches, by restore from the saved dict |
| 28 | + for modname in monkey.saved.keys(): |
| 29 | + try: |
| 30 | + mod = __import__(modname) |
| 31 | + importlib.reload(mod) |
| 32 | + for key in monkey.saved[modname].keys(): |
| 33 | + setattr(mod, key, monkey.saved[modname][key]) |
| 34 | + except ImportError: |
| 35 | + pass |
| 36 | + monkey.saved = {} |
| 37 | + |
| 38 | + |
| 39 | + def test_default_patch_all(self): |
| 40 | + apply_gevent_monkey_patch() |
| 41 | + for module_name in self.default_patched_modules: |
| 42 | + self.assertTrue(monkey.is_module_patched(module_name), |
| 43 | + f"{module_name} is not patched") |
| 44 | + |
| 45 | + def test_instana_monkey_options_only_time(self): |
| 46 | + os.environ['INSTANA_GEVENT_MONKEY_OPTIONS'] = ( |
| 47 | + 'time,no-socket,no-select,no-os,no-select,no-threading,no-os,' |
| 48 | + 'no-ssl,no-subprocess,''no-signal,no-queue') |
| 49 | + apply_gevent_monkey_patch() |
| 50 | + |
| 51 | + self.assertTrue(monkey.is_module_patched('time'), "time module is not patched") |
| 52 | + not_patched_modules = (m for m in self.default_patched_modules if m not in ('time', 'threading')) |
| 53 | + |
| 54 | + for module_name in not_patched_modules: |
| 55 | + self.assertFalse(monkey.is_module_patched(module_name), |
| 56 | + f"{module_name} is patched, when it shouldn't be") |
| 57 | + |
| 58 | + |
| 59 | + def test_instana_monkey_options_only_socket(self): |
| 60 | + os.environ['INSTANA_GEVENT_MONKEY_OPTIONS'] = ( |
| 61 | + '--socket, --no-time, --no-select, --no-os, --no-queue, --no-threading,' |
| 62 | + '--no-os, --no-ssl, no-subprocess, --no-signal, --no-select,') |
| 63 | + apply_gevent_monkey_patch() |
| 64 | + |
| 65 | + self.assertTrue(monkey.is_module_patched('socket'), "socket module is not patched") |
| 66 | + not_patched_modules = (m for m in self.default_patched_modules if m not in ('socket', 'threading')) |
| 67 | + |
| 68 | + for module_name in not_patched_modules: |
| 69 | + self.assertFalse(monkey.is_module_patched(module_name), |
| 70 | + f"{module_name} is patched, when it shouldn't be") |
| 71 | + |
0 commit comments