diff --git a/osbuild/testutil/net.py b/osbuild/testutil/net.py index d2abb565..6dc8acca 100644 --- a/osbuild/testutil/net.py +++ b/osbuild/testutil/net.py @@ -7,6 +7,20 @@ import http.server import socket import threading +try: + from http.server import ThreadingHTTPServer +except ImportError: + # This fallback is only needed on py3.6. Py3.7+ has ThreadingHTTPServer. + # We just import ThreadingHTTPServer here so that the import of "net.py" + # on py36 works, the helpers are not usable because the "directory" arg + # for SimpleHTTPRequestHandler is also not supported. + class ThreadingHTTPServer: # type: ignore + def __init__(self, *args, **kwargs): # pylint: disable=unused-argument + # pylint: disable=import-outside-toplevel + import pytest # type: ignore + pytest.skip("python too old to suport ThreadingHTTPServer") + + from .atomic import AtomicCounter @@ -21,7 +35,7 @@ class SilentHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): pass -class DirHTTPServer(http.server.ThreadingHTTPServer): +class DirHTTPServer(ThreadingHTTPServer): def __init__(self, *args, directory=None, simulate_failures=0, **kwargs): super().__init__(*args, **kwargs) self.directory = directory diff --git a/sources/test/test_curl_source.py b/sources/test/test_curl_source.py index 31308b9b..49813ad0 100644 --- a/sources/test/test_curl_source.py +++ b/sources/test/test_curl_source.py @@ -9,7 +9,7 @@ from unittest.mock import patch import pytest -import osbuild.testutil.net +from osbuild.testutil.net import http_serve_directory SOURCES_NAME = "org.osbuild.curl" @@ -118,7 +118,7 @@ def test_curl_download_many_with_retry(tmp_path, sources_service): fake_httpd_root = tmp_path / "fake-httpd-root" simulate_failures = 2 - with osbuild.testutil.net.http_serve_directory(fake_httpd_root, simulate_failures=simulate_failures) as httpd: + with http_serve_directory(fake_httpd_root, simulate_failures=simulate_failures) as httpd: test_sources = make_test_sources(fake_httpd_root, httpd.server_port, 5) sources_service.cache = tmp_path / "curl-download-dir" @@ -134,7 +134,7 @@ def test_curl_download_many_with_retry(tmp_path, sources_service): def test_curl_download_many_chksum_validate(tmp_path, sources_service): fake_httpd_root = tmp_path / "fake-httpd-root" - with osbuild.testutil.net.http_serve_directory(fake_httpd_root) as httpd: + with http_serve_directory(fake_httpd_root) as httpd: test_sources = make_test_sources(fake_httpd_root, httpd.server_port, 5) # "break" the hash of file "1" by replacing the content to no longer # match the checksum @@ -150,7 +150,7 @@ def test_curl_download_many_chksum_validate(tmp_path, sources_service): def test_curl_download_many_retries(tmp_path, sources_service): fake_httpd_root = tmp_path / "fake-httpd-root" - with osbuild.testutil.net.http_serve_directory(fake_httpd_root) as httpd: + with http_serve_directory(fake_httpd_root) as httpd: test_sources = make_test_sources(fake_httpd_root, httpd.server_port, 5) # remove all the sources shutil.rmtree(fake_httpd_root)