From 69d31914952dd33082ac7019c6f76b43c45b9d06 Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Sat, 20 Jan 2024 10:39:49 +1300 Subject: [PATCH] [test] Skip source address tests if the address cannot be bound to (#8900) Fixes https://github.com/yt-dlp/yt-dlp/issues/8890 Authored by: coletdjnz --- test/helper.py | 7 ++++++- test/test_networking.py | 5 ++++- test/test_socks.py | 4 +++- test/test_websockets.py | 3 +++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/test/helper.py b/test/helper.py index e5ace8fe2..4aca47025 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 dc60ca699..62325aa8e 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 71f783e13..cb22b61dc 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 af6142ea3..91bac3442 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')