-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"""Handle compatibility issues between Python 2 and Python 3.""" | ||
|
||
import six | ||
|
||
if six.PY3: | ||
from unittest import mock | ||
else: | ||
import mock # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Misc utilities.""" | ||
import six | ||
|
||
|
||
def check_no_proxy(): | ||
"""Raise warnings about improperly set no_proxy env var.""" | ||
proxy_settings = six.moves.urllib.request.getproxies() | ||
if 'http' not in proxy_settings and 'https' not in proxy_settings: | ||
return | ||
if 'no' not in proxy_settings: | ||
raise UserWarning( | ||
"You have proxy settings, but no_proxy isn't set. " | ||
"If you try to connect to localhost (i.e. like pytest-localstack does) " | ||
"it's going to try to go through the proxy and fail. " | ||
"Set the no_proxy environment variable to something like " | ||
"'localhost,127.0.0.1' (and maybe add your local network as well? ;D )" | ||
) | ||
if '127.0.0.1' not in proxy_settings['no']: | ||
raise UserWarning( | ||
"You have proxy settings (including no_proxy) set, " | ||
"but no_proxy doens't contain '127.0.0.1'. " | ||
"This is needed for Localstack. " | ||
"Please set the no_proxy environment variable to something like " | ||
"'localhost,127.0.0.1' (and maybe add your local network as well? ;D )" | ||
) |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Unit tests for pytest_localstack.utils.""" | ||
import os | ||
|
||
import pytest | ||
from hypothesis import ( | ||
given, | ||
strategies as st, | ||
) | ||
|
||
from pytest_localstack import ( | ||
compat, | ||
utils, | ||
) | ||
|
||
|
||
def _get_env_var(name): | ||
return os.environ.get(name) or os.environ.get(name.upper(), '') | ||
|
||
|
||
@given( | ||
test_environ=st.fixed_dictionaries({ | ||
'http_proxy': st.sampled_from(['', 'http://proxy:3128']), | ||
'https_proxy': st.sampled_from(['', 'http://proxy:3128']), | ||
'HTTP_PROXY': st.sampled_from(['', 'http://proxy:3128']), | ||
'HTTPS_PROXY': st.sampled_from(['', 'http://proxy:3128']), | ||
'no_proxy': st.sampled_from(['', 'localhost,127.0.0.1', 'localhost', 'foobar']), | ||
'NO_PROXY': st.sampled_from(['', 'localhost,127.0.0.1', 'localhost', 'foobar']), | ||
}) | ||
) | ||
def test_check_no_proxy(test_environ): | ||
"""Test pytest_localstack.utils.check_no_proxy.""" | ||
with compat.mock.patch.dict(os.environ): | ||
# mock.patch.dict can't delete keys. | ||
# Patch os.environ manually. | ||
for key, value in test_environ.items(): | ||
if value: | ||
os.environ[key] = value | ||
else: | ||
os.environ.pop(key, None) | ||
|
||
has_http_proxy = bool(_get_env_var('http_proxy')) | ||
has_https_proxy = bool(_get_env_var('https_proxy')) | ||
good_no_proxy = '127.0.0.1' in _get_env_var('no_proxy') | ||
|
||
if has_http_proxy or has_https_proxy: | ||
if good_no_proxy: | ||
utils.check_no_proxy() | ||
else: | ||
with pytest.raises(UserWarning): | ||
utils.check_no_proxy() | ||
else: | ||
utils.check_no_proxy() |