diff --git a/pytest_localstack/compat.py b/pytest_localstack/compat.py new file mode 100644 index 0000000..64c92f5 --- /dev/null +++ b/pytest_localstack/compat.py @@ -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 diff --git a/pytest_localstack/utils.py b/pytest_localstack/utils.py new file mode 100644 index 0000000..84c6a74 --- /dev/null +++ b/pytest_localstack/utils.py @@ -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 )" + ) diff --git a/tests/test_dummy.py b/tests/test_dummy.py deleted file mode 100644 index 5687270..0000000 --- a/tests/test_dummy.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Dummy tests""" -import pytest_localstack - - -def test_dummy(): - """Dummy to make sure tests work.""" - assert pytest_localstack.__version__ == '0.1.0-dev' diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py new file mode 100644 index 0000000..d19c049 --- /dev/null +++ b/tests/unit/test_utils.py @@ -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()