Skip to content

Commit

Permalink
tests: Update tests which call config status
Browse files Browse the repository at this point in the history
Fixes: #2996
  • Loading branch information
dheyay authored and renanrodrigo committed Mar 18, 2024
1 parent 09c05c3 commit f6a4f07
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
7 changes: 7 additions & 0 deletions uaclient/cli/tests/test_cli_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions uaclient/cli/tests/test_cli_enable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CanEnableFailure,
CanEnableFailureReason,
)
from uaclient.files.user_config_file import UserConfigData

HELP_OUTPUT = """\
usage: pro enable <service> [<service>] [flags]
Expand All @@ -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")
Expand All @@ -47,6 +53,7 @@ def test_enable_help(
_m_resources,
_m_setup_logging,
_refresh,
_m_public_config,
capsys,
FakeConfig,
):
Expand All @@ -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,
Expand Down Expand Up @@ -139,6 +147,7 @@ def test_lock_file_exists(
m_subp,
m_sleep,
_refresh,
_m_public_config,
capsys,
event,
FakeConfig,
Expand Down Expand Up @@ -205,6 +214,7 @@ def test_unattached_error_message(
self,
m_we_are_currently_root,
_refresh,
_m_public_config,
root,
expected_error_template,
capsys,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
):
Expand Down Expand Up @@ -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,
):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions uaclient/cli/tests/test_cli_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions uaclient/tests/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -566,15 +567,21 @@ 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,
_m_remove_notice,
m_on_supported_kernel,
FakeConfig,
):
m_public_config.return_value = UserConfigData()
m_get_available_resources.return_value = [
{"name": "esm-infra", "available": True}
]
Expand All @@ -590,15 +597,21 @@ 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,
_m_remove_notice,
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)
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f6a4f07

Please sign in to comment.