-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpytest_blockage.py
105 lines (80 loc) · 3.07 KB
/
pytest_blockage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
if sys.version_info[0] < 3:
import httplib
else:
import http.client as httplib
import logging
import smtplib
logger = logging.getLogger(__name__)
class MockHttpCall(Exception):
pass
class MockSmtpCall(Exception):
pass
def get_string_type():
try:
return basestring
except NameError: # python3
return str
def block_http(whitelist):
def whitelisted(self, host, *args, **kwargs):
if isinstance(host, get_string_type()) and host not in whitelist:
logger.warning('Denied HTTP connection to: %s' % host)
raise MockHttpCall(host)
logger.debug('Allowed HTTP connection to: %s' % host)
return self.old(host, *args, **kwargs)
whitelisted.blockage = True
if not getattr(httplib.HTTPConnection, 'blockage', False):
logger.debug('Monkey patching httplib')
httplib.HTTPConnection.old = httplib.HTTPConnection.__init__
httplib.HTTPConnection.__init__ = whitelisted
def block_smtp(whitelist):
def whitelisted(self, host, *args, **kwargs):
if isinstance(host, get_string_type()) and host not in whitelist:
logger.warning('Denied SMTP connection to: %s' % host)
raise MockSmtpCall(host)
logger.debug('Allowed SMTP connection to: %s' % host)
return self.old(host, *args, **kwargs)
whitelisted.blockage = True
if not getattr(smtplib.SMTP, 'blockage', False):
logger.debug('Monkey patching smtplib')
smtplib.SMTP.old = smtplib.SMTP.__init__
smtplib.SMTP.__init__ = whitelisted
def pytest_addoption(parser):
group = parser.getgroup('blockage')
group.addoption('--blockage', action='store_true',
help='Block network requests during test run')
parser.addini(
'blockage', 'Block network requests during test run', default=False)
group.addoption(
'--blockage-http-whitelist',
action='store',
help='Do not block HTTP requests to this comma separated list of '
'hostnames',
default=''
)
parser.addini(
'blockage-http-whitelist',
'Do not block HTTP requests to this comma separated list of hostnames',
default=''
)
group.addoption(
'--blockage-smtp-whitelist',
action='store',
help='Do not block SMTP requests to this comma separated list of '
'hostnames',
default=''
)
parser.addini(
'blockage-smtp-whitelist',
'Do not block SMTP requests to this comma separated list of hostnames',
default=''
)
def pytest_sessionstart(session):
config = session.config
if config.option.blockage or config.getini('blockage'):
http_whitelist_str = config.option.blockage_http_whitelist or config.getini('blockage-http-whitelist')
http_whitelist = http_whitelist_str.split(',')
smtp_whitelist_str = config.option.blockage_smtp_whitelist or config.getini('blockage-smtp-whitelist')
smtp_whitelist = smtp_whitelist_str.split(',')
block_http(http_whitelist)
block_smtp(smtp_whitelist)