From ee0a4a935a95a9472655f85a04e1323f3d328898 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Thu, 17 Feb 2022 16:31:42 +0100 Subject: automatically pick up a free port to run the HTTP proxy on It is now possible to run multiple `make test` commands in parallel (e.g. testing abrowser and librewolf simultaneously after running `configure` for each of them in 2 different directories). --- test/haketilo_test/__main__.py | 14 +++----------- test/haketilo_test/conftest.py | 6 +++--- test/haketilo_test/misc_constants.py | 1 - test/haketilo_test/profiles.py | 12 +++++------- test/haketilo_test/server.py | 7 ++++++- 5 files changed, 17 insertions(+), 23 deletions(-) (limited to 'test') diff --git a/test/haketilo_test/__main__.py b/test/haketilo_test/__main__.py index 3664e8c..18ef5b5 100644 --- a/test/haketilo_test/__main__.py +++ b/test/haketilo_test/__main__.py @@ -40,7 +40,7 @@ from .extension_crafting import get_extension_base_url def fail(msg, error_code): print('Error:', msg) - print('Usage:', sys.argv[0], '[--load-haketilo]', '[certificates_directory] [proxy_port]') + print('Usage:', sys.argv[0], '[--load-haketilo]', '[certificates_directory]') sys.exit(error_code) load_haketilo = False @@ -55,16 +55,8 @@ certdir = Path(sys.argv[argv_idx]).resolve() if len(sys.argv) > argv_idx \ if not certdir.is_dir(): fail('selected certificate directory does not exist.', 2) -argv_idx += 1 - -port = sys.argv[argv_idx] if len(sys.argv) > argv_idx \ - else str(default_proxy_port) - -if not port.isnumeric(): - fail('port must be an integer.', 3) - -httpd = do_an_internet(certdir, int(port)) -driver = firefox_safe_mode(proxy_port=int(port)) +httpd = do_an_internet(certdir) +driver = firefox_safe_mode(proxy_port=httpd.server_port) if load_haketilo: driver.install_addon(str(Path.cwd() / 'mozilla-build.zip'), temporary=True) diff --git a/test/haketilo_test/conftest.py b/test/haketilo_test/conftest.py index 9103ac8..834f026 100644 --- a/test/haketilo_test/conftest.py +++ b/test/haketilo_test/conftest.py @@ -46,7 +46,7 @@ def proxy(): @pytest.fixture(scope="session") def _driver(proxy): - with firefox_safe_mode() as driver: + with firefox_safe_mode(proxy.server_port) as driver: yield driver driver.quit() @@ -57,14 +57,14 @@ def close_all_but_one_window(driver): driver.switch_to.window(driver.window_handles[0]) @pytest.fixture() -def driver(_driver, request): +def driver(proxy, _driver, request): nav_target = request.node.get_closest_marker('get_page') nav_target = nav_target.args[0] if nav_target else 'about:blank' second_driver = request.node.get_closest_marker('second_driver') if second_driver: - with firefox_safe_mode() as _driver: + with firefox_safe_mode(proxy.server_port) as _driver: _driver.get(nav_target) yield _driver _driver.quit() diff --git a/test/haketilo_test/misc_constants.py b/test/haketilo_test/misc_constants.py index ac34f32..d552208 100644 --- a/test/haketilo_test/misc_constants.py +++ b/test/haketilo_test/misc_constants.py @@ -46,7 +46,6 @@ with open(Path.cwd() / 'record.conf', 'rt') as conf: conf_settings[match.group(1).strip()] = match.group(2) default_proxy_host = '127.0.0.1' -default_proxy_port = 1337 default_cert_dir = Path.cwd() / 'certs' diff --git a/test/haketilo_test/profiles.py b/test/haketilo_test/profiles.py index ae997fc..e1fc368 100755 --- a/test/haketilo_test/profiles.py +++ b/test/haketilo_test/profiles.py @@ -80,9 +80,8 @@ def set_webextension_uuid(profile, extension_id, uuid=default_extension_uuid): profile.set_preference('extensions.webextensions.uuids', json.dumps({extension_id: uuid})) -def firefox_safe_mode(firefox_binary=conf_settings['BROWSER_BINARY'], - proxy_host=default_proxy_host, - proxy_port=default_proxy_port): +def firefox_safe_mode(proxy_port, proxy_host=default_proxy_host, + firefox_binary=conf_settings['BROWSER_BINARY']): """ Initialize a Firefox instance controlled by selenium. The instance is started in safe mode. @@ -97,10 +96,9 @@ def firefox_safe_mode(firefox_binary=conf_settings['BROWSER_BINARY'], return HaketiloFirefox(options=options, firefox_profile=profile, firefox_binary=firefox_binary) -def firefox_with_profile(firefox_binary=conf_settings['BROWSER_BINARY'], - profile_dir=conf_settings['CLEAN_PROFILE'], - proxy_host=default_proxy_host, - proxy_port=default_proxy_port): +def firefox_with_profile(proxy_port, proxy_host=default_proxy_host, + firefox_binary=conf_settings['BROWSER_BINARY'], + profile_dir=conf_settings['CLEAN_PROFILE']): """ Initialize a Firefox instance controlled by selenium. The instance is started using an empty profile (either the default one or the one passed to diff --git a/test/haketilo_test/server.py b/test/haketilo_test/server.py index 0963b5b..19d4a01 100755 --- a/test/haketilo_test/server.py +++ b/test/haketilo_test/server.py @@ -33,6 +33,8 @@ from urllib.parse import parse_qs from threading import Thread import traceback +from selenium.webdriver.common.utils import free_port + from .proxy_core import ProxyRequestHandler, ThreadingHTTPServer from .misc_constants import * from .world_wide_library import catalog as internet @@ -99,8 +101,11 @@ class RequestHijacker(ProxyRequestHandler): if resp_body: self.wfile.write(resp_body) -def do_an_internet(certdir=default_cert_dir, port=default_proxy_port): +def do_an_internet(certdir=default_cert_dir, port=None): """Start up the proxy/server""" + if port is None: + port = free_port() + class RequestHijackerWithCertdir(RequestHijacker): def __init__(self, *args, **kwargs): super().__init__(*args, certdir=certdir, **kwargs) -- cgit v1.2.3