def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = { self.refresh_route: exceptions.UserFacingError("Machine token refresh fail") } return fake_client
def test_user_facing_error_due_to_unexpected_process_entitlement_delta( self, client, get_machine_id, process_entitlement_delta, first_error, second_error, ux_error_msg, FakeConfig, ): """Unexpected errors from process_entitlement_delta are raised. Remaining entitlements are processed regardless of error and error is raised at the end. Unexpected exceptions take priority over the handled UserFacingErrors. """ # Fail first and succeed second call to process_entitlement_delta process_entitlement_delta.side_effect = ( first_error, second_error, None, ) # resourceEntitlements specifically ordered reverse alphabetically # to ensure proper sorting for process_contract_delta calls below machine_token = { "machineToken": "mToken", "machineTokenInfo": { "contractInfo": { "id": "cid", "resourceEntitlements": [ {"entitled": False, "type": "ent3"}, {"entitled": False, "type": "ent2"}, {"entitled": True, "type": "ent1"}, ], } }, } cfg = FakeConfig.for_attached_machine(machine_token=machine_token) fake_client = FakeContractClient(cfg) fake_client._responses = { self.refresh_route: machine_token, self.access_route_ent1: { "entitlement": { "entitled": True, "type": "ent1", "new": "newval", } }, } client.return_value = fake_client with pytest.raises(exceptions.UserFacingError) as exc: assert None is request_updated_contract(cfg) assert 3 == process_entitlement_delta.call_count assert ux_error_msg == str(exc.value)
def fake_contract_client(cfg): client = FakeContractClient(cfg) # Note ent2 access route is not called client._responses = { refresh_route: machine_token, access_route_ent1: { 'entitlement': { 'entitled': True, 'type': 'ent1', 'new': 'newval'}}} return client
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = { self.refresh_route: machine_token, self.access_route_ent1: exceptions.UserFacingError("Broken ent1 route"), self.access_route_ent2: exceptions.UserFacingError("Broken ent2 route"), } return fake_client
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = { API_V1_CONTEXT_MACHINE_TOKEN: ContractAPIError( util.UrlError( "Server error", code=500, url="http://me", headers={} ), error_response={ "message": "invalid token: checksum error" }, ) } return fake_client
def fake_contract_client(cfg): client = FakeContractClient(cfg) # Note ent2 access route is not called client._responses = { self.refresh_route: machine_token, self.access_route_ent1: { "entitlement": { "entitled": True, "type": "ent1", "new": "newval", } }, } return client
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = { API_V1_CONTEXT_MACHINE_TOKEN: ContractAPIError( util.UrlError( "Server error", code=error_code, url="http://me", headers={}, ), error_response={"message": "unauthorized"}, ) } return fake_client
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = { API_V1_CONTEXT_MACHINE_TOKEN: exceptions.ContractAPIError( exceptions.UrlError( "Server error", code=error_code, url="http://me", headers={}, ), error_response=json.loads( error_response, cls=util.DatetimeAwareJSONDecoder), ) } return fake_client
def test_correct_message_emitted( self, m_update_apt_and_motd_msgs, m_client, m_entitlements, m_getuid, _m_prompt, capsys, FakeConfig, tmpdir, ): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client m_cfg = mock.MagicMock() m_cfg.check_lock_info.return_value = (-1, "") m_cfg.data_path.return_value = tmpdir.join("lock").strpath action_detach(mock.MagicMock(), m_cfg) out, _err = capsys.readouterr() assert messages.DETACH_SUCCESS + "\n" == out assert [mock.call(m_cfg)] == m_update_apt_and_motd_msgs.call_args_list
def test_informational_message_emitted( self, m_client, m_entitlements, m_getuid, _m_prompt, capsys, classes, expected_message, FakeConfig, tmpdir, ): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = classes fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client m_cfg = mock.MagicMock() m_cfg.data_path.return_value = tmpdir.join("lock").strpath action_detach(mock.MagicMock(), m_cfg) out, _err = capsys.readouterr() assert expected_message in out
def test_entitlements_disabled_appropriately( self, m_client, m_entitlements, m_getuid, m_prompt, prompt_response, assume_yes, expect_disable, FakeConfig, ): # The three parameters: # prompt_response: the user's response to the prompt # assume_yes: the value of the --assume-yes flag in the args passed # to the action # expect_disable: whether or not the enabled entitlement is expected # to be disabled by the action m_getuid.return_value = 0 cfg = FakeConfig.for_attached_machine() fake_client = FakeContractClient(cfg) m_client.return_value = fake_client m_prompt.return_value = prompt_response m_entitlements.ENTITLEMENT_CLASSES = [ entitlement_cls_mock_factory(False), entitlement_cls_mock_factory(True), entitlement_cls_mock_factory(False), ] args = mock.MagicMock(assume_yes=assume_yes) return_code = action_detach(args, cfg) # Check that can_disable is called correctly for ent_cls in m_entitlements.ENTITLEMENT_CLASSES: assert [mock.call(silent=True) ] == ent_cls.return_value.can_disable.call_args_list # Check that disable is only called when can_disable is true for undisabled_cls in [ m_entitlements.ENTITLEMENT_CLASSES[0], m_entitlements.ENTITLEMENT_CLASSES[2], ]: assert 0 == undisabled_cls.return_value.disable.call_count disabled_cls = m_entitlements.ENTITLEMENT_CLASSES[1] if expect_disable: assert [mock.call(silent=True) ] == disabled_cls.return_value.disable.call_args_list assert 0 == return_code else: assert 0 == disabled_cls.return_value.disable.call_count assert 1 == return_code assert [mock.call(assume_yes=assume_yes)] == m_prompt.call_args_list
def test_returns_zero(self, m_client, m_entitlements, m_getuid, _m_prompt, FakeConfig): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client ret = action_detach(mock.MagicMock(), mock.MagicMock()) assert 0 == ret
def test_config_cache_deleted(self, m_client, m_entitlements, m_getuid, _m_prompt, FakeConfig): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client cfg = mock.MagicMock() action_detach(mock.MagicMock(), cfg) assert [mock.call()] == cfg.delete_cache.call_args_list
def test_returns_zero(self, m_client, m_entitlements, m_getuid, _m_prompt, FakeConfig, tmpdir): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client m_cfg = mock.MagicMock() m_cfg.data_path.return_value = tmpdir.join("lock").strpath ret = action_detach(mock.MagicMock(), m_cfg) assert 0 == ret
def test_correct_message_emitted(self, m_client, m_entitlements, m_getuid, _m_prompt, capsys, FakeConfig): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client action_detach(mock.MagicMock(), mock.MagicMock()) out, _err = capsys.readouterr() assert status.MESSAGE_DETACH_SUCCESS + "\n" == out
def test_config_cache_deleted(self, m_client, m_entitlements, m_getuid, _m_prompt, FakeConfig, tmpdir): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client m_cfg = mock.MagicMock() m_cfg.data_path.return_value = tmpdir.join("lock").strpath action_detach(mock.MagicMock(), m_cfg) assert [mock.call()] == m_cfg.delete_cache.call_args_list
def test_informational_message_emitted( self, m_update_apt_and_motd_msgs, m_client, m_entitlements, m_getuid, _m_prompt, capsys, classes, expected_message, disabled_services, FakeConfig, tmpdir, event, ): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = classes fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client m_cfg = mock.MagicMock() m_cfg.check_lock_info.return_value = (-1, "") m_cfg.data_path.return_value = tmpdir.join("lock").strpath args = mock.MagicMock() action_detach(args, m_cfg) out, _err = capsys.readouterr() assert expected_message in out assert [mock.call(m_cfg)] == m_update_apt_and_motd_msgs.call_args_list cfg = FakeConfig.for_attached_machine() fake_stdout = io.StringIO() with contextlib.redirect_stdout(fake_stdout): with mock.patch.object(event, "_event_logger_mode", event_logger.EventLoggerMode.JSON): main_error_handler(action_detach)(args, cfg) expected = { "_schema_version": event_logger.JSON_SCHEMA_VERSION, "result": "success", "errors": [], "failed_services": [], "needs_reboot": False, "processed_services": disabled_services, "warnings": [], } assert expected == json.loads(fake_stdout.getvalue())
def test_returns_zero( self, m_update_apt_and_motd_msgs, m_client, m_entitlements, m_getuid, _m_prompt, FakeConfig, tmpdir, ): m_getuid.return_value = 0 m_entitlements.ENTITLEMENT_CLASSES = [] fake_client = FakeContractClient(FakeConfig.for_attached_machine()) m_client.return_value = fake_client m_cfg = mock.MagicMock() m_cfg.check_lock_info.return_value = (-1, "") m_cfg.data_path.return_value = tmpdir.join("lock").strpath ret = action_detach(mock.MagicMock(), m_cfg) assert 0 == ret assert [mock.call(m_cfg)] == m_update_apt_and_motd_msgs.call_args_list
def fake_contract_client(cfg): client = FakeContractClient(cfg) client._responses = {self.refresh_route: new_token} return client
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = {self.refresh_route: machine_token} return fake_client
def test_entitlements_disabled_appropriately( self, m_update_apt_and_motd_msgs, m_client, m_entitlements, m_getuid, m_prompt, prompt_response, assume_yes, expect_disable, FakeConfig, event, capsys, ): # The three parameters: # prompt_response: the user's response to the prompt # assume_yes: the value of the --assume-yes flag in the args passed # to the action # expect_disable: whether or not the enabled entitlement is expected # to be disabled by the action m_getuid.return_value = 0 cfg = FakeConfig.for_attached_machine() fake_client = FakeContractClient(cfg) m_client.return_value = fake_client m_prompt.return_value = prompt_response m_entitlements.ENTITLEMENT_CLASSES = [ entitlement_cls_mock_factory(False), entitlement_cls_mock_factory(True, name="test"), entitlement_cls_mock_factory(False), ] args = mock.MagicMock(assume_yes=assume_yes) return_code = action_detach(args, cfg=cfg) # Check that can_disable is called correctly for ent_cls in m_entitlements.ENTITLEMENT_CLASSES: assert [mock.call(ignore_dependent_services=True) ] == ent_cls.return_value.can_disable.call_args_list assert [mock.call(cfg=cfg, assume_yes=assume_yes)] == ent_cls.call_args_list # Check that disable is only called when can_disable is true for undisabled_cls in [ m_entitlements.ENTITLEMENT_CLASSES[0], m_entitlements.ENTITLEMENT_CLASSES[2], ]: assert 0 == undisabled_cls.return_value.disable.call_count disabled_cls = m_entitlements.ENTITLEMENT_CLASSES[1] if expect_disable: assert [mock.call() ] == disabled_cls.return_value.disable.call_args_list assert 0 == return_code else: assert 0 == disabled_cls.return_value.disable.call_count assert 1 == return_code assert [mock.call(assume_yes=assume_yes)] == m_prompt.call_args_list if expect_disable: assert [mock.call(cfg) ] == m_update_apt_and_motd_msgs.call_args_list cfg = FakeConfig.for_attached_machine() fake_stdout = io.StringIO() # On json response, we will never prompt the user m_prompt.return_value = True with contextlib.redirect_stdout(fake_stdout): with mock.patch.object(event, "_event_logger_mode", event_logger.EventLoggerMode.JSON): main_error_handler(action_detach)(args, cfg) expected = { "_schema_version": event_logger.JSON_SCHEMA_VERSION, "result": "success", "errors": [], "failed_services": [], "needs_reboot": False, "processed_services": ["test"], "warnings": [], } assert expected == json.loads(fake_stdout.getvalue())
def fake_contract_client(cfg): return FakeContractClient(cfg)
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = {url: information} return fake_client
def fake_contract_client(cfg): fake_client = FakeContractClient(cfg) fake_client._responses = {url: {"resources": new_resources}} return fake_client