From 67352c9fe8d056fe3c733eb8728c60c80e84891c Mon Sep 17 00:00:00 2001 From: Alex Laird Date: Tue, 31 Dec 2024 14:37:25 -0600 Subject: [PATCH] Finish implementation to provision default v3 config, if requested, and test. Remaining missing implementation is to interpret "endpoints" tunnel definition. --- pyngrok/conf.py | 3 ++- pyngrok/installer.py | 17 +++++++++++------ tests/test_installer.py | 25 ++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/pyngrok/conf.py b/pyngrok/conf.py index ab481f8..95a595d 100644 --- a/pyngrok/conf.py +++ b/pyngrok/conf.py @@ -60,7 +60,7 @@ def __init__(self, start_new_session: bool = False, ngrok_version: str = "v3", api_key: Optional[str] = None, - config_version: int = "2") -> None: + config_version: int = 2) -> None: #: The path to the ``ngrok`` binary, defaults to being placed in the same directory as #: `ngrok's configs `_. self.ngrok_path: str = DEFAULT_NGROK_PATH if ngrok_path is None else ngrok_path @@ -89,6 +89,7 @@ def __init__(self, self.ngrok_version: str = ngrok_version #: A ``ngrok`` API key. self.api_key: Optional[str] = api_key + #: The ``ngrok`` config version. self.config_version = config_version diff --git a/pyngrok/installer.py b/pyngrok/installer.py index 11487fd..b6570b9 100644 --- a/pyngrok/installer.py +++ b/pyngrok/installer.py @@ -149,13 +149,14 @@ def _install_ngrok_zip(ngrok_path: str, def get_ngrok_config(config_path: str, use_cache: bool = True, ngrok_version: Optional[str] = "v3", - config_version: Optional[int] = "2") -> Dict[str, Any]: + config_version: Optional[int] = 2) -> Dict[str, Any]: """ Get the ``ngrok`` config from the given path. :param config_path: The ``ngrok`` config path to read. :param use_cache: Use the cached version of the config (if populated). :param ngrok_version: The major version of ``ngrok`` installed. + :param config_version: The ``ngrok`` config version. :return: The ``ngrok`` config. """ if config_path not in _config_cache or not use_cache: @@ -170,9 +171,9 @@ def get_ngrok_config(config_path: str, def get_default_config(ngrok_version: Optional[str], - config_version: Optional[str]) -> Dict[str, Any]: + config_version: Optional[int]) -> Dict[str, Any]: """ - Get the default config params for the given major version of ``ngrok``. + Get the default config params for the given major version of ``ngrok`` and config version. :param ngrok_version: The major version of ``ngrok`` installed. :param config_version: The ``ngrok`` config version. @@ -183,7 +184,7 @@ def get_default_config(ngrok_version: Optional[str], return {} elif ngrok_version == "v3": config = {"version": config_version} - if str(config_version) == "2": + if int(config_version) == 2: config["region"] = "us" return config else: @@ -193,7 +194,7 @@ def get_default_config(ngrok_version: Optional[str], def install_default_config(config_path: str, data: Optional[Dict[str, Any]] = None, ngrok_version: Optional[str] = "v3", - config_version: Optional[str] = "2") -> None: + config_version: Optional[int] = 2) -> None: """ Install the given data to the ``ngrok`` config. If a config is not already present for the given path, create one. Before saving new data to the default config, validate that they are compatible with ``pyngrok``. @@ -201,6 +202,7 @@ def install_default_config(config_path: str, :param config_path: The path to where the ``ngrok`` config should be installed. :param data: A dictionary of things to add to the default config. :param ngrok_version: The major version of ``ngrok`` installed. + :param config_version: The ``ngrok`` config version. """ if data is None: data = {} @@ -215,7 +217,10 @@ def install_default_config(config_path: str, if not os.path.exists(config_path): open(config_path, "w").close() - config = get_ngrok_config(config_path, use_cache=False, ngrok_version=ngrok_version) + config = get_ngrok_config(config_path, + use_cache=False, + ngrok_version=ngrok_version, + config_version=config_version) config.update(data) diff --git a/tests/test_installer.py b/tests/test_installer.py index 816ddb2..fd77187 100644 --- a/tests/test_installer.py +++ b/tests/test_installer.py @@ -68,16 +68,35 @@ def test_config_provisioned(self): # THEN self.assertTrue(os.path.exists(self.pyngrok_config_v3.config_path)) - def test_get_default_config(self): + def test_get_default_v2_config(self): # GIVEN - installer.install_default_config(self.pyngrok_config_v3.config_path, {}, self.pyngrok_config_v3.ngrok_version) + installer.install_default_config(self.pyngrok_config_v3.config_path, + {}, + self.pyngrok_config_v3.ngrok_version, + 2) # WHEN ngrok_config = installer.get_ngrok_config(self.pyngrok_config_v3.config_path) # THEN self.assertEqual(2, len(ngrok_config)) - self.assertEqual("2", ngrok_config["version"]) + self.assertEqual(2, ngrok_config["version"]) + self.assertEqual("us", ngrok_config["region"]) + self.assertTrue(os.path.exists(self.pyngrok_config_v3.config_path)) + + def test_get_default_v3_config(self): + # GIVEN + installer.install_default_config(self.pyngrok_config_v3.config_path, + {}, + self.pyngrok_config_v3.ngrok_version, + 3) + + # WHEN + ngrok_config = installer.get_ngrok_config(self.pyngrok_config_v3.config_path) + + # THEN + self.assertEqual(1, len(ngrok_config)) + self.assertEqual(3, ngrok_config["version"]) self.assertTrue(os.path.exists(self.pyngrok_config_v3.config_path)) ################################################################################