forked from miketheman/pytest-socket
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pytest_socket.py
185 lines (141 loc) · 5 KB
/
pytest_socket.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
import ipaddress
import socket
import pytest
import warnings
_true_socket = socket.socket
_true_connect = socket.socket.connect
class SocketBlockedError(RuntimeError):
def __init__(self, *args, **kwargs):
super(SocketBlockedError, self).__init__("A test tried to use socket.socket.")
class SocketConnectBlockedError(RuntimeError):
def __init__(self, allowed, host, *args, **kwargs):
if allowed:
allowed = ",".join(allowed)
super(SocketConnectBlockedError, self).__init__(
'A test tried to use socket.socket.connect() with host "{0}" (allowed: "{1}").'.format(
host, allowed
)
)
def pytest_addoption(parser):
group = parser.getgroup("socket")
group.addoption(
"--disable-socket",
action="store_true",
dest="disable_socket",
help="Disable socket.socket by default to block network calls.",
)
group.addoption(
"--allow-hosts",
dest="allow_hosts",
metavar="ALLOWED_HOSTS_CSV",
help="Only allow specified hosts through socket.socket.connect((host, port)).",
)
@pytest.fixture(autouse=True)
def _socket_marker(request):
if request.node.get_closest_marker("disable_socket"):
request.getfixturevalue("socket_disabled")
if request.node.get_closest_marker("enable_socket"):
request.getfixturevalue("socket_enabled")
if request.config.getoption("--disable-socket"):
request.getfixturevalue("socket_disabled")
@pytest.fixture
def socket_disabled():
""" disable socket.socket for duration of this test function """
disable_socket()
yield
enable_socket()
@pytest.fixture
def socket_enabled():
""" enable socket.socket for duration of this test function """
enable_socket()
yield
disable_socket()
def disable_socket():
""" disable socket.socket to disable the Internet. useful in testing.
"""
def guarded(*args, **kwargs):
raise SocketBlockedError()
socket.socket = guarded
def enable_socket():
""" re-enable socket.socket to enable the Internet. useful in testing.
"""
socket.socket = _true_socket
def pytest_configure(config):
config.addinivalue_line(
"markers", "disable_socket(): Disable socket connections for a specific test"
)
config.addinivalue_line(
"markers", "enable_socket(): Enable socket connections for a specific test"
)
config.addinivalue_line(
"markers",
"allow_hosts([hosts]): Restrict socket connection to defined list of hosts",
)
def pytest_runtest_setup(item):
mark_restrictions = item.get_closest_marker("allow_hosts")
cli_restrictions = item.config.getoption("--allow-hosts")
hosts = None
if mark_restrictions:
hosts = mark_restrictions.args[0]
elif cli_restrictions:
hosts = cli_restrictions
socket_allow_hosts(hosts)
def pytest_runtest_teardown():
remove_host_restrictions()
def host_from_address_py2(address):
host = address[0]
if isinstance(host, str) or isinstance(host, unicode): # noqa F821
return host
def host_from_connect_args(args):
address = args[0]
if isinstance(address, tuple):
host = address[0]
if isinstance(host, str):
return host
def parse_allowed_host(host):
"""host may be an IP address or a hostname.
Returns a list of IP addresses.
Note that the host names are resolved at the start of the test run, so
will be static after `pytest_runtest_setup` has run.
"""
# 1. see if it's an IP address
try:
ipaddress.ip_address(host)
# Validated ip address
return [host]
except ValueError:
pass
# 2. See if it resolves to an IP address, or return none
try:
address_info = socket.getaddrinfo(host, None)
# It is possible that there will be more than one IP address
addresses = [info[4][0] for info in address_info]
return addresses
except socket.gaierror:
pass
warnings.warn(
"[pytest-socket] {host} did not resolve to any IP addresses".format(host=host)
)
return []
def socket_allow_hosts(allowed=None):
""" disable socket.socket.connect() to disable the Internet. useful in testing.
"""
if isinstance(allowed, str):
allowed = allowed.split(",")
if not isinstance(allowed, list):
return
# Parse each hostname, create an expanded list, then simplify
resolved_allowed_hosts = set(
sum([parse_allowed_host(host) for host in allowed], [])
)
def guarded_connect(inst, *args):
host = host_from_connect_args(args)
if host and host in resolved_allowed_hosts:
return _true_connect(inst, *args)
raise SocketConnectBlockedError(resolved_allowed_hosts, host)
socket.socket.connect = guarded_connect
def remove_host_restrictions():
""" restore socket.socket.connect() to allow access to the Internet. useful in testing.
"""
socket.socket.connect = _true_connect