Skip to content

Commit

Permalink
Add compat.py and utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdoepke committed Feb 25, 2018
1 parent 80508f1 commit 588e2af
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
8 changes: 8 additions & 0 deletions pytest_localstack/compat.py
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
25 changes: 25 additions & 0 deletions pytest_localstack/utils.py
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 )"
)
7 changes: 0 additions & 7 deletions tests/test_dummy.py

This file was deleted.

Empty file added tests/unit/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions tests/unit/test_utils.py
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()

0 comments on commit 588e2af

Please sign in to comment.