Skip to content

Commit

Permalink
Mark tests as requiring Internet
Browse files Browse the repository at this point in the history
So that we can self-contained tests, when offline.

The use-case here is packaging truststore in Debian.
We like to run tests during our package builds, where possible. These
build environments don't permit Internet access.
We do have CI test environments that can access the Internet, but those
happen after build. We'd run the full test suite there (inclnuding
mkcert, which messes with the system certificate store).

I see that the vast majority of the tests require Internet access, so I
understand if you're not interested in this patch, because you aren't
expecting much low-level test coverage.
  • Loading branch information
stefanor committed Oct 18, 2023
1 parent 459c750 commit f95184c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ filterwarnings = [
# See: aio-libs/aiohttp#7545
"ignore:.*datetime.utcfromtimestamp().*:DeprecationWarning",
]
markers = [
"internet: test reqires Internet access"
]

[tool.flit.sdist]
include = ["docs", "tests"]
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
original_SSLContext = ssl.SSLContext


successful_hosts = pytest.mark.parametrize("host", ["example.com", "1.1.1.1"])
def decorator_requires_internet(decorator):
"""Mark a decorator with the "internet" mark"""
def wrapper(f):
return pytest.mark.internet(decorator(f))
return wrapper


successful_hosts = decorator_requires_internet(
pytest.mark.parametrize("host", ["example.com", "1.1.1.1"]))

logger = logging.getLogger("aiohttp.web")

Expand Down
18 changes: 13 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

import truststore
from tests import SSLContextAdapter
from tests.conftest import decorator_requires_internet

pytestmark = pytest.mark.flaky

# Make sure the httpserver doesn't hang
# if the client drops the connection due to a cert verification error
socket.setdefaulttimeout(10)

successful_hosts = pytest.mark.parametrize("host", ["example.com", "1.1.1.1"])

successful_hosts = decorator_requires_internet(
pytest.mark.parametrize("host", ["example.com", "1.1.1.1"]))


@dataclass
Expand Down Expand Up @@ -118,8 +121,10 @@ class FailureHost:
),
]

failure_hosts_no_revocation = pytest.mark.parametrize(
"failure", failure_hosts_list.copy(), ids=attrgetter("host")
failure_hosts_no_revocation = decorator_requires_internet(
pytest.mark.parametrize(
"failure", failure_hosts_list.copy(), ids=attrgetter("host")
)
)

if platform.system() != "Linux":
Expand All @@ -139,8 +144,10 @@ class FailureHost:
)
)

failure_hosts = pytest.mark.parametrize(
"failure", failure_hosts_list, ids=attrgetter("host")
failure_hosts = decorator_requires_internet(
pytest.mark.parametrize(
"failure", failure_hosts_list, ids=attrgetter("host")
)
)


Expand Down Expand Up @@ -318,6 +325,7 @@ def test_trustme_cert_loaded_via_capath(trustme_ca, httpserver):
assert len(resp.data) > 0


@pytest.mark.internet
def test_trustme_cert_still_uses_system_certs(trustme_ca):
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
trustme_ca.configure_trust(ctx)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sslcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import truststore


@pytest.mark.internet
def test_minimum_maximum_version():
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.maximum_version = ssl.TLSVersion.TLSv1_2
Expand All @@ -24,6 +25,7 @@ def test_minimum_maximum_version():
assert ctx.maximum_version == ssl.TLSVersion.TLSv1_2


@pytest.mark.internet
def test_check_hostname_false():
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
assert ctx.check_hostname is True
Expand All @@ -35,6 +37,7 @@ def test_check_hostname_false():
assert "match" in str(e.value)


@pytest.mark.internet
def test_verify_mode_cert_none():
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
assert ctx.check_hostname is True
Expand Down

0 comments on commit f95184c

Please sign in to comment.