Exemplo n.º 1
0
    def test_non_root_users_are_rejected(self, getuid, FakeConfig):
        """Check that a UID != 0 will receive a message and exit non-zero"""
        getuid.return_value = 1

        cfg = FakeConfig.for_attached_machine()
        with pytest.raises(exceptions.NonRootUserError):
            action_refresh(mock.MagicMock(), cfg)
    def test_refresh_messages_error(self, m_update_motd, getuid, FakeConfig):
        """On failure in update_apt_and_motd_messages emit an error."""
        m_update_motd.side_effect = Exception("test")

        with pytest.raises(exceptions.UserFacingError) as excinfo:
            action_refresh(mock.MagicMock(target="messages"), cfg=FakeConfig())

        assert messages.REFRESH_MESSAGES_FAILURE == excinfo.value.msg
    def test_refresh_config_error_on_failure_to_process_config(
        self, _m_process_config, _m_logging_error, getuid, FakeConfig
    ):
        """On failure in process_config emit an error."""

        cfg = FakeConfig.for_attached_machine()

        with pytest.raises(exceptions.UserFacingError) as excinfo:
            action_refresh(mock.MagicMock(target="config"), cfg=cfg)

        assert messages.REFRESH_CONFIG_FAILURE == excinfo.value.msg
    def test_refresh_contract_error_on_failure_to_update_contract(
            self, request_updated_contract, logging_error, getuid):
        """On failure in request_updates_contract emit an error."""
        request_updated_contract.side_effect = exceptions.UserFacingError(
            "Failure to refresh")

        cfg = FakeConfig.for_attached_machine()

        with pytest.raises(exceptions.UserFacingError) as excinfo:
            action_refresh(mock.MagicMock(), cfg)

        assert "Failure to refresh" == excinfo.value.msg
 def test_lock_file_exists(self, m_subp, _getuid, FakeConfig):
     """Check inability to refresh if operation holds lock file."""
     cfg = FakeConfig().for_attached_machine()
     with open(cfg.data_path("lock"), "w") as stream:
         stream.write("123:ua disable")
     with pytest.raises(exceptions.LockHeldError) as err:
         action_refresh(mock.MagicMock(), cfg)
     assert [mock.call(["ps", "123"])] == m_subp.call_args_list
     assert (
         "Unable to perform: ua refresh.\n"
         "Operation in progress: ua disable (pid:123)"
     ) == err.value.msg
    def test_refresh_messages_systemctl_error(
        self, m_update_motd, m_subp, logging_error, _m_path, getuid, FakeConfig
    ):
        subp_exc = Exception("test")
        m_subp.side_effect = ["", subp_exc]

        with pytest.raises(exceptions.UserFacingError) as excinfo:
            action_refresh(mock.MagicMock(target="messages"), cfg=FakeConfig())

        assert 1 == logging_error.call_count
        assert [mock.call(subp_exc)] == logging_error.call_args_list
        assert messages.REFRESH_MESSAGES_FAILURE == excinfo.value.msg
    def test_not_attached_errors(
        self, _m_write_cfg, getuid, target, expect_unattached_error, FakeConfig
    ):
        """Check that an unattached machine emits message and exits 1"""
        cfg = FakeConfig()

        cfg.update_messaging_timer = 0
        cfg.update_status_timer = 0
        cfg.metering_timer = 0

        if expect_unattached_error:
            with pytest.raises(exceptions.UnattachedError):
                action_refresh(mock.MagicMock(target=target), cfg=cfg)
        else:
            action_refresh(mock.MagicMock(target=target), cfg=cfg)
    def test_not_attached_errors(self, getuid, stdout):
        """Check that an unattached machine emits message and exits 1"""
        cfg = FakeConfig()

        ret = action_refresh(mock.MagicMock(), cfg)

        assert 1 == ret
        expected_msg = status.MESSAGE_UNATTACHED
        assert mock.call(expected_msg) in stdout.write.call_args_list
    def test_non_root_users_are_rejected(self, getuid, stdout):
        """Check that a UID != 0 will receive a message and exit non-zero"""
        getuid.return_value = 1

        cfg = FakeConfig.for_attached_machine()
        ret = action_refresh(mock.MagicMock(), cfg)

        assert 1 == ret
        assert (mock.call(status.MESSAGE_NONROOT_USER)
                in stdout.write.call_args_list)
    def test_refresh_contract_error_on_failure_to_update_contract(
            self, request_updated_contract, logging_error, getuid, stdout):
        """On failure in request_updates_contract emit an error."""
        request_updated_contract.return_value = False  # failure to refresh

        cfg = FakeConfig.for_attached_machine()
        ret = action_refresh(mock.MagicMock(), cfg)

        assert 1 == ret
        assert (mock.call(status.MESSAGE_REFRESH_FAILURE)
                in logging_error.call_args_list)
    def test_refresh_config_happy_path(
        self, m_process_config, getuid, capsys, FakeConfig
    ):
        """On success from process_config root user gets success message."""

        cfg = FakeConfig.for_attached_machine()
        ret = action_refresh(mock.MagicMock(target="config"), cfg=cfg)

        assert 0 == ret
        assert messages.REFRESH_CONFIG_SUCCESS in capsys.readouterr()[0]
        assert [mock.call()] == m_process_config.call_args_list
    def test_refresh_messages_happy_path(
        self, m_update_motd, m_refresh_motd, getuid, capsys, FakeConfig
    ):
        """On success from request_updates_contract root user can refresh."""
        cfg = FakeConfig()
        ret = action_refresh(mock.MagicMock(target="messages"), cfg=cfg)

        assert 0 == ret
        assert messages.REFRESH_MESSAGES_SUCCESS in capsys.readouterr()[0]
        assert [mock.call(cfg)] == m_update_motd.call_args_list
        assert [mock.call()] == m_refresh_motd.call_args_list
    def test_refresh_contract_happy_path(self, request_updated_contract,
                                         getuid, capsys):
        """On success from request_updates_contract root user can refresh."""
        request_updated_contract.return_value = True

        cfg = FakeConfig.for_attached_machine()
        ret = action_refresh(mock.MagicMock(), cfg)

        assert 0 == ret
        assert status.MESSAGE_REFRESH_SUCCESS in capsys.readouterr()[0]
        assert [mock.call(cfg)] == request_updated_contract.call_args_list
    def test_refresh_contract_error_on_failure_to_update_contract(
        self,
        m_remove_notice,
        request_updated_contract,
        logging_error,
        getuid,
        FakeConfig,
    ):
        """On failure in request_updates_contract emit an error."""
        request_updated_contract.side_effect = exceptions.UrlError(
            mock.MagicMock()
        )

        cfg = FakeConfig.for_attached_machine()

        with pytest.raises(exceptions.UserFacingError) as excinfo:
            action_refresh(mock.MagicMock(target="contract"), cfg=cfg)

        assert messages.REFRESH_CONTRACT_FAILURE == excinfo.value.msg
        assert [
            mock.call("", messages.NOTICE_REFRESH_CONTRACT_WARNING)
        ] != m_remove_notice.call_args_list
    def test_refresh_messages_doesnt_fail_if_update_notifier_does(
        self,
        m_update_motd,
        m_subp,
        logging_error,
        _m_path,
        getuid,
        capsys,
        FakeConfig,
    ):
        subp_exc = Exception("test")
        m_subp.side_effect = [subp_exc, ""]

        ret = action_refresh(
            mock.MagicMock(target="messages"), cfg=FakeConfig()
        )

        assert 0 == ret
        assert 1 == logging_error.call_count
        assert [mock.call(subp_exc)] == logging_error.call_args_list
        assert messages.REFRESH_MESSAGES_SUCCESS in capsys.readouterr()[0]
    def test_refresh_contract_happy_path(
        self,
        m_remove_notice,
        request_updated_contract,
        getuid,
        capsys,
        FakeConfig,
    ):
        """On success from request_updates_contract root user can refresh."""
        request_updated_contract.return_value = True

        cfg = FakeConfig.for_attached_machine()
        ret = action_refresh(mock.MagicMock(target="contract"), cfg=cfg)

        assert 0 == ret
        assert messages.REFRESH_CONTRACT_SUCCESS in capsys.readouterr()[0]
        assert [mock.call(cfg)] == request_updated_contract.call_args_list
        assert [
            mock.call("", messages.NOTICE_REFRESH_CONTRACT_WARNING),
            mock.call("", "Operation in progress.*"),
        ] == m_remove_notice.call_args_list
    def test_refresh_all_happy_path(
        self,
        m_remove_notice,
        m_process_config,
        m_request_updated_contract,
        getuid,
        capsys,
        FakeConfig,
    ):
        """On success from process_config root user gets success message."""

        cfg = FakeConfig.for_attached_machine()
        ret = action_refresh(mock.MagicMock(target=None), cfg=cfg)
        out, err = capsys.readouterr()

        assert 0 == ret
        assert messages.REFRESH_CONFIG_SUCCESS in out
        assert messages.REFRESH_CONTRACT_SUCCESS in out
        assert [mock.call()] == m_process_config.call_args_list
        assert [mock.call(cfg)] == m_request_updated_contract.call_args_list
        assert [
            mock.call("", messages.NOTICE_REFRESH_CONTRACT_WARNING),
            mock.call("", "Operation in progress.*"),
        ] == m_remove_notice.call_args_list
Exemplo n.º 18
0
    def test_not_attached_errors(self, getuid, FakeConfig):
        """Check that an unattached machine emits message and exits 1"""
        cfg = FakeConfig()

        with pytest.raises(exceptions.UnattachedError):
            action_refresh(mock.MagicMock(), cfg)