diff --git a/test/helper.py b/test/helper.py index e5ace8fe2c..4aca47025e 100644 --- a/test/helper.py +++ b/test/helper.py @@ -10,7 +10,7 @@ import types import yt_dlp.extractor from yt_dlp import YoutubeDL from yt_dlp.compat import compat_os_name -from yt_dlp.utils import preferredencoding, try_call, write_string +from yt_dlp.utils import preferredencoding, try_call, write_string, find_available_port if 'pytest' in sys.modules: import pytest @@ -329,3 +329,8 @@ def http_server_port(httpd): else: sock = httpd.socket return sock.getsockname()[1] + + +def verify_address_availability(address): + if find_available_port(address) is None: + pytest.skip(f'Unable to bind to source address {address} (address may not exist)') diff --git a/test/test_networking.py b/test/test_networking.py index dc60ca6994..62325aa8e0 100644 --- a/test/test_networking.py +++ b/test/test_networking.py @@ -26,7 +26,7 @@ import zlib from email.message import Message from http.cookiejar import CookieJar -from test.helper import FakeYDL, http_server_port +from test.helper import FakeYDL, http_server_port, verify_address_availability from yt_dlp.cookies import YoutubeDLCookieJar from yt_dlp.dependencies import brotli, requests, urllib3 from yt_dlp.networking import ( @@ -538,6 +538,9 @@ class TestHTTPRequestHandler(TestRequestHandlerBase): @pytest.mark.parametrize('handler', ['Urllib', 'Requests'], indirect=True) def test_source_address(self, handler): source_address = f'127.0.0.{random.randint(5, 255)}' + # on some systems these loopback addresses we need for testing may not be available + # see: https://github.com/yt-dlp/yt-dlp/issues/8890 + verify_address_availability(source_address) with handler(source_address=source_address) as rh: data = validate_and_send( rh, Request(f'http://127.0.0.1:{self.http_port}/source_address')).read().decode() diff --git a/test/test_socks.py b/test/test_socks.py index 71f783e132..cb22b61dc8 100644 --- a/test/test_socks.py +++ b/test/test_socks.py @@ -25,7 +25,7 @@ from socketserver import ( ThreadingTCPServer, ) -from test.helper import http_server_port +from test.helper import http_server_port, verify_address_availability from yt_dlp.networking import Request from yt_dlp.networking.exceptions import ProxyError, TransportError from yt_dlp.socks import ( @@ -326,6 +326,7 @@ class TestSocks4Proxy: def test_ipv4_client_source_address(self, handler, ctx): with ctx.socks_server(Socks4ProxyHandler) as server_address: source_address = f'127.0.0.{random.randint(5, 255)}' + verify_address_availability(source_address) with handler(proxies={'all': f'socks4://{server_address}'}, source_address=source_address) as rh: response = ctx.socks_info_request(rh) @@ -441,6 +442,7 @@ class TestSocks5Proxy: def test_ipv4_client_source_address(self, handler, ctx): with ctx.socks_server(Socks5ProxyHandler) as server_address: source_address = f'127.0.0.{random.randint(5, 255)}' + verify_address_availability(source_address) with handler(proxies={'all': f'socks5://{server_address}'}, source_address=source_address) as rh: response = ctx.socks_info_request(rh) assert response['client_address'][0] == source_address diff --git a/test/test_websockets.py b/test/test_websockets.py index af6142ea3b..91bac3442e 100644 --- a/test/test_websockets.py +++ b/test/test_websockets.py @@ -6,6 +6,8 @@ import sys import pytest +from test.helper import verify_address_availability + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import http.client @@ -227,6 +229,7 @@ class TestWebsSocketRequestHandlerConformance: @pytest.mark.parametrize('handler', ['Websockets'], indirect=True) def test_source_address(self, handler): source_address = f'127.0.0.{random.randint(5, 255)}' + verify_address_availability(source_address) with handler(source_address=source_address) as rh: ws = validate_and_send(rh, Request(self.ws_base_url)) ws.send('source_address')