From f6a4f077110eaa927f111b192a342a9e019cb888 Mon Sep 17 00:00:00 2001 From: Dheyay Date: Thu, 14 Mar 2024 12:53:27 -0700 Subject: [PATCH] tests: Update tests which call config status Fixes: #2996 --- uaclient/cli/tests/test_cli_attach.py | 7 ++++++ uaclient/cli/tests/test_cli_enable.py | 22 +++++++++++++++++-- uaclient/cli/tests/test_cli_status.py | 18 ++++++++++++++++ uaclient/tests/test_status.py | 31 +++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/uaclient/cli/tests/test_cli_attach.py b/uaclient/cli/tests/test_cli_attach.py index 097e2a5bd0..98e57695eb 100644 --- a/uaclient/cli/tests/test_cli_attach.py +++ b/uaclient/cli/tests/test_cli_attach.py @@ -24,6 +24,7 @@ NonRootUserError, UbuntuProError, ) +from uaclient.files.user_config_file import UserConfigData from uaclient.testing.fakes import FakeFile, FakeUbuntuProError from uaclient.yaml import safe_dump @@ -538,8 +539,14 @@ def test_attach_config_enable_services( @mock.patch("uaclient.contract.apply_contract_overrides") @mock.patch("uaclient.contract.UAContractClient.request_url") @mock.patch("uaclient.timer.update_messaging.update_motd_messages") + @mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + return_value=UserConfigData(), + ) def test_attach_when_one_service_fails_to_enable( self, + m_public_config, _m_update_messages, m_request_url, _m_apply_contract_overrides, diff --git a/uaclient/cli/tests/test_cli_enable.py b/uaclient/cli/tests/test_cli_enable.py index f344d204e6..eb7cd8d653 100644 --- a/uaclient/cli/tests/test_cli_enable.py +++ b/uaclient/cli/tests/test_cli_enable.py @@ -13,6 +13,7 @@ CanEnableFailure, CanEnableFailureReason, ) +from uaclient.files.user_config_file import UserConfigData HELP_OUTPUT = """\ usage: pro enable [] [flags] @@ -38,6 +39,11 @@ """ +@mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + return_value=UserConfigData(), +) @mock.patch("uaclient.contract.refresh") class TestActionEnable: @mock.patch("uaclient.cli.setup_logging") @@ -47,6 +53,7 @@ def test_enable_help( _m_resources, _m_setup_logging, _refresh, + _m_public_config, capsys, FakeConfig, ): @@ -69,6 +76,7 @@ def test_non_root_users_are_rejected( _refresh, we_are_currently_root, m_setup_logging, + _m_public_config, capsys, event, FakeConfig, @@ -139,6 +147,7 @@ def test_lock_file_exists( m_subp, m_sleep, _refresh, + _m_public_config, capsys, event, FakeConfig, @@ -205,6 +214,7 @@ def test_unattached_error_message( self, m_we_are_currently_root, _refresh, + _m_public_config, root, expected_error_template, capsys, @@ -274,6 +284,7 @@ def test_invalid_service_error_message( self, m_we_are_currently_root, _refresh, + _m_public_config, root, expected_error_template, is_attached, @@ -370,6 +381,7 @@ def test_unattached_invalid_and_valid_service_error_message( self, m_we_are_currently_root, _refresh, + _m_public_config, root, expected_error_template, event, @@ -445,6 +457,7 @@ def test_assume_yes_passed_to_service_init( _m_get_available_resources, _m_update_activity_token, m_refresh, + _m_public_config, assume_yes, FakeConfig, ): @@ -489,6 +502,7 @@ def test_entitlements_not_found_disabled_and_enabled( m_entitlement_factory, _m_get_available_resources, _m_refresh, + _m_public_config, event, FakeConfig, ): @@ -614,6 +628,7 @@ def test_entitlements_not_found_and_beta( m_entitlement_factory, _m_get_available_resources, _m_refresh, + _m_public_config, beta_flag, event, FakeConfig, @@ -766,6 +781,7 @@ def test_print_message_when_can_enable_fails( _m_get_available_resources, _m_update_activity_token, _m_refresh, + _m_public_config, event, FakeConfig, ): @@ -841,6 +857,7 @@ def test_print_message_when_can_enable_fails( def test_invalid_service_names( self, _m_refresh, + _m_public_config, service, beta, event, @@ -922,6 +939,7 @@ def test_entitlement_instantiated_and_enabled( _m_get_available_resources, m_update_activity_token, _m_refresh, + _m_public_config, allow_beta, event, FakeConfig, @@ -993,7 +1011,7 @@ def test_entitlement_instantiated_and_enabled( assert expected_ret == ret def test_format_json_fails_when_assume_yes_flag_not_used( - self, _m_get_available_resources, event + self, _m_get_available_resources, _m_public_config, event ): cfg = mock.MagicMock() args_mock = mock.MagicMock() @@ -1028,7 +1046,7 @@ def test_format_json_fails_when_assume_yes_flag_not_used( assert expected == json.loads(fake_stdout.getvalue()) def test_access_only_cannot_be_used_together_with_variant( - self, _m_get_available_resources, FakeConfig + self, _m_get_available_resources, _m_public_config, FakeConfig ): cfg = FakeConfig.for_attached_machine() args_mock = mock.MagicMock() diff --git a/uaclient/cli/tests/test_cli_status.py b/uaclient/cli/tests/test_cli_status.py index 9cfb33a744..f778424ddc 100644 --- a/uaclient/cli/tests/test_cli_status.py +++ b/uaclient/cli/tests/test_cli_status.py @@ -15,6 +15,7 @@ from uaclient.conftest import FakeNotice from uaclient.event_logger import EventLoggerMode from uaclient.files.notices import Notice, NoticesManager +from uaclient.files.user_config_file import UserConfigData from uaclient.yaml import safe_load M_PATH = "uaclient.cli." @@ -341,11 +342,17 @@ "uaclient.status.get_contract_information", return_value=RESPONSE_CONTRACT_INFO, ) +@mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + return_value=UserConfigData(), +) class TestActionStatus: @mock.patch(M_PATH + "setup_logging") def test_status_help( self, _m_setup_logging, + _m_public_config, _m_get_contract_information, _m_get_available_resources, _m_should_reboot, @@ -393,6 +400,7 @@ def test_status_help( def test_attached( self, _m_format_expires, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -451,6 +459,7 @@ def test_attached( ) def test_unattached( self, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -478,6 +487,7 @@ def test_unattached( ) def test_simulated( self, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -505,6 +515,7 @@ def test_wait_blocks_until_lock_released( m_sleep, _m_subp, _m_get_version, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -557,6 +568,7 @@ def fake_sleep(seconds): ) def test_unattached_formats( self, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -667,6 +679,7 @@ def test_unattached_formats( @pytest.mark.parametrize("use_all", (True, False)) def test_attached_formats( self, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -800,6 +813,7 @@ def test_attached_formats( @pytest.mark.parametrize("use_all", (True, False)) def test_simulated_formats( self, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -951,6 +965,7 @@ def test_simulated_formats( def test_error_on_connectivity_errors( self, + _m_public_config, _m_get_contract_information, m_get_avail_resources, _m_should_reboot, @@ -979,6 +994,7 @@ def test_error_on_connectivity_errors( def test_unicode_dash_replacement_when_unprintable( self, _m_format_expires, + _m_public_config, _m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -1040,6 +1056,7 @@ def test_unicode_dash_replacement_when_unprintable( ) def test_errors_are_raised_appropriately( self, + _m_public_config, m_get_contract_information, _m_get_avail_resources, _m_should_reboot, @@ -1101,6 +1118,7 @@ def test_errors_are_raised_appropriately( ) def test_errors_for_token_dates( self, + _m_public_config, m_get_contract_information, _m_get_avail_resources, _m_should_reboot, diff --git a/uaclient/tests/test_status.py b/uaclient/tests/test_status.py index aa26d73433..e83cbdfcd4 100644 --- a/uaclient/tests/test_status.py +++ b/uaclient/tests/test_status.py @@ -24,6 +24,7 @@ from uaclient.entitlements.ros import ROSEntitlement from uaclient.entitlements.tests.test_base import ConcreteTestEntitlement from uaclient.files.notices import Notice, NoticesManager +from uaclient.files.user_config_file import UserConfigData from uaclient.status import ( DEFAULT_STATUS, TxtColor, @@ -566,8 +567,13 @@ def test_root_attached( @mock.patch("uaclient.util.we_are_currently_root") @mock.patch("uaclient.status.get_available_resources") + @mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + ) def test_nonroot_unattached_is_same_as_unattached_root( self, + m_public_config, m_get_available_resources, m_we_are_currently_root, _m_should_reboot, @@ -575,6 +581,7 @@ def test_nonroot_unattached_is_same_as_unattached_root( m_on_supported_kernel, FakeConfig, ): + m_public_config.return_value = UserConfigData() m_get_available_resources.return_value = [ {"name": "esm-infra", "available": True} ] @@ -590,8 +597,13 @@ def test_nonroot_unattached_is_same_as_unattached_root( @mock.patch("uaclient.util.we_are_currently_root") @mock.patch("uaclient.status.get_available_resources") + @mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + ) def test_root_and_non_root_are_same_attached( self, + m_public_config, m_get_available_resources, m_we_are_currently_root, _m_should_reboot, @@ -599,6 +611,7 @@ def test_root_and_non_root_are_same_attached( m_on_supported_kernel, FakeConfig, ): + m_public_config.return_value = UserConfigData() m_we_are_currently_root.return_value = True root_cfg = FakeConfig.for_attached_machine() root_status = status.status(cfg=root_cfg) @@ -608,14 +621,20 @@ def test_root_and_non_root_are_same_attached( assert normal_status == root_status @mock.patch("uaclient.status.get_available_resources", return_value=[]) + @mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + ) def test_cache_file_is_written_world_readable( self, + m_public_config, _m_get_available_resources, _m_should_reboot, m_remove_notice, m_on_supported_kernel, FakeConfig, ): + m_public_config.return_value = UserConfigData() cfg = FakeConfig() status.status(cfg=cfg) @@ -873,8 +892,13 @@ def test_attached_reports_contract_and_service_status( @pytest.mark.usefixtures("all_resources_available") @mock.patch("uaclient.util.we_are_currently_root") @mock.patch("uaclient.status.get_available_resources") + @mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + ) def test_expires_handled_appropriately( self, + m_public_config, _m_get_available_resources, m_we_are_currently_root, _m_should_reboot, @@ -883,6 +907,7 @@ def test_expires_handled_appropriately( all_resources_available, FakeConfig, ): + m_public_config.return_value = UserConfigData() m_we_are_currently_root.return_value = True token = { "availableResources": all_resources_available, @@ -927,16 +952,22 @@ def test_expires_handled_appropriately( "uaclient.files.state_files.reboot_cmd_marker_file", new_callable=mock.PropertyMock, ) + @mock.patch( + "uaclient.files.user_config_file.UserConfigFileObject.public_config", + new_callable=mock.PropertyMock, + ) @mock.patch("uaclient.status.get_available_resources", return_value={}) def test_nonroot_user_does_not_use_cache( self, _m_get_available_resources, + m_public_config, m_reboot_cmd_marker_file, _m_should_reboot, m_remove_notice, m_on_supported_kernel, FakeConfig, ): + m_public_config.return_value = UserConfigData() m_reboot_cmd_marker_file.is_present = True cached_status = {"pass": True} cfg = FakeConfig()