From c782dd641035e0bfbf5d464c9d9ff4eeba699bf3 Mon Sep 17 00:00:00 2001 From: Dheyay Date: Thu, 7 Mar 2024 14:58:38 -0800 Subject: [PATCH] Handle empty value for config set Fixes: [#2925] --- uaclient/cli/__init__.py | 3 +++ uaclient/cli/tests/test_cli_config_set.py | 22 ++++++++-------------- uaclient/exceptions.py | 5 ++++- uaclient/messages/__init__.py | 5 +++++ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/uaclient/cli/__init__.py b/uaclient/cli/__init__.py index 103bdc5367..2f5ea751ae 100644 --- a/uaclient/cli/__init__.py +++ b/uaclient/cli/__init__.py @@ -690,6 +690,9 @@ def action_config_set(args, *, cfg, **kwargs): raise exceptions.InvalidArgChoice( arg="", choices=", ".join(config.UA_CONFIGURABLE_KEYS) ) + if not set_value.strip(): + subparser.print_help() + raise exceptions.InvalidArgValue(arg=set_key) if set_key in ("http_proxy", "https_proxy"): protocol_type = set_key.split("_")[0] if protocol_type == "http": diff --git a/uaclient/cli/tests/test_cli_config_set.py b/uaclient/cli/tests/test_cli_config_set.py index 1a37ff288b..bfc1b7032f 100644 --- a/uaclient/cli/tests/test_cli_config_set.py +++ b/uaclient/cli/tests/test_cli_config_set.py @@ -55,6 +55,14 @@ class TestMainConfigSet: " global_apt_https_proxy, update_messaging_timer," " metering_timer", ), + ( + "http_proxy=", + "Empty value provided for http_proxy.", + ), + ( + "https_proxy= ", + "Empty value provided for https_proxy.", + ), ), ) @mock.patch("uaclient.cli.contract.get_available_resources") @@ -274,13 +282,6 @@ def test_set_apt_http_proxy_and_apt_https_proxy_prints_warning( "https://proxy", "https://proxy", ), - ( - "global_apt_http_proxy", - "", - apt.AptProxyScope.GLOBAL, - "https://proxy", - "https://proxy", - ), ), ) @mock.patch("uaclient.cli.configure_apt_proxy") @@ -384,13 +385,6 @@ def test_set_global_apt_http_and_global_apt_https_proxy( "https://proxy", "https://proxy", ), - ( - "ua_apt_https_proxy", - "", - apt.AptProxyScope.UACLIENT, - "https://proxy", - "https://proxy", - ), ), ) @mock.patch("uaclient.cli.configure_apt_proxy") diff --git a/uaclient/exceptions.py b/uaclient/exceptions.py index 10df85c059..4e5b693e5b 100644 --- a/uaclient/exceptions.py +++ b/uaclient/exceptions.py @@ -335,7 +335,6 @@ class EntitlementNotFoundError(UbuntuProError): class EntitlementsNotEnabledError(UbuntuProError): - exit_code = 4 _msg = messages.E_ENTITLEMENTS_NOT_ENABLED_ERROR @@ -496,6 +495,10 @@ class InvalidArgChoice(UbuntuProError): _formatted_msg = messages.E_CLI_VALID_CHOICES +class InvalidArgValue(UbuntuProError): + _formatted_msg = messages.E_CLI_VALID_VALUE + + class GenericInvalidFormat(UbuntuProError): _formatted_msg = messages.E_CLI_EXPECTED_FORMAT diff --git a/uaclient/messages/__init__.py b/uaclient/messages/__init__.py index d449554242..1a512ab643 100644 --- a/uaclient/messages/__init__.py +++ b/uaclient/messages/__init__.py @@ -2421,6 +2421,11 @@ def __repr__(self): "invalid-arg-choice", "\n" + t.gettext("{arg} must be one of: {choices}") ) +E_CLI_VALID_VALUE = FormattedNamedMessage( + "empty-value", + t.gettext("Empty value provided for {arg}."), +) + E_CLI_EXPECTED_FORMAT = FormattedNamedMessage( "generic-invalid-format", "\n" + t.gettext("Expected {expected} but found: {actual}"),