def factory(**kwargs): handler = ResourceChangingHandler(**dict( dict( fn=some_fn, id='a', errors=None, timeout=None, retries=None, backoff=None, cooldown=None, initial=None, deleted=None, requires_finalizer=None, annotations=None, labels=None, when=None, field=None, value=None, old=None, new=None, field_needs_change=None, reason=None, ), **kwargs)) registry.resource_changing_handlers[resource].append(handler) return handler
async def test_skipped_with_no_handlers(registry, settings, resource, cause_mock, cause_type, caplog, assert_logs, k8s_mocked): caplog.set_level(logging.DEBUG) event_type = None event_body = {'metadata': {'finalizers': []}} cause_mock.reason = cause_type assert not registry.resource_changing_handlers[resource] # prerequisite registry.resource_changing_handlers[resource].append( ResourceChangingHandler( reason='a-non-existent-cause-type', fn=lambda **_: None, id='id', errors=None, timeout=None, retries=None, backoff=None, cooldown=None, annotations=None, labels=None, when=None, field=None, deleted=None, initial=None, requires_finalizer=None, )) await process_resource_event( lifecycle=kopf.lifecycles.all_at_once, registry=registry, settings=settings, resource=resource, memories=ResourceMemories(), raw_event={ 'type': event_type, 'object': event_body }, replenished=asyncio.Event(), event_queue=asyncio.Queue(), ) assert not k8s_mocked.sleep_or_wait.called assert k8s_mocked.patch_obj.called # The patch must contain ONLY the last-seen update, and nothing else. patch = k8s_mocked.patch_obj.call_args_list[0][1]['patch'] assert set(patch.keys()) == {'metadata'} assert set(patch['metadata'].keys()) == {'annotations'} assert set( patch['metadata']['annotations'].keys()) == {LAST_SEEN_ANNOTATION} assert_logs([ ".* event:", "Patching with:", ], prohibited=[ "event is processed:", ])
def parent_handler(selector): def parent_fn(**_): pass return ResourceChangingHandler( fn=parent_fn, id=HandlerId('parent_fn'), param=None, errors=None, retries=None, timeout=None, backoff=None, selector=selector, labels=None, annotations=None, when=None, field=None, value=None, old=None, new=None, field_needs_change=None, initial=None, deleted=None, requires_finalizer=None, reason=None, )
async def test_stealth_mode_with_mismatching_handlers( registry, settings, selector, resource, cause_mock, cause_type, caplog, assert_logs, k8s_mocked, annotations, labels, when, deleted, initial): caplog.set_level(logging.DEBUG) event_type = None event_body = {'metadata': {'finalizers': []}} cause_mock.reason = cause_type assert not registry._resource_changing.has_handlers(resource=resource) # prerequisite registry._resource_changing.append(ResourceChangingHandler( reason=None, fn=lambda **_: None, id='id', errors=None, timeout=None, retries=None, backoff=None, selector=selector, annotations=annotations, labels=labels, when=when, field=None, value=None, old=None, new=None, field_needs_change=None, deleted=deleted, initial=initial, requires_finalizer=None, )) await process_resource_event( lifecycle=kopf.lifecycles.all_at_once, registry=registry, settings=settings, resource=resource, memories=ResourceMemories(), raw_event={'type': event_type, 'object': event_body}, replenished=asyncio.Event(), event_queue=asyncio.Queue(), ) assert not k8s_mocked.sleep_or_wait.called assert not k8s_mocked.patch_obj.called assert not caplog.messages # total stealth mode!
def test_resource_handler_with_all_args(mocker): fn = mocker.Mock() id = mocker.Mock() selector = mocker.Mock() reason = mocker.Mock() errors = mocker.Mock() timeout = mocker.Mock() retries = mocker.Mock() backoff = mocker.Mock() initial = mocker.Mock() deleted = mocker.Mock() labels = mocker.Mock() annotations = mocker.Mock() when = mocker.Mock() field = mocker.Mock() value = mocker.Mock() old = mocker.Mock() new = mocker.Mock() field_needs_change = mocker.Mock() requires_finalizer = mocker.Mock() handler = ResourceChangingHandler( fn=fn, id=id, selector=selector, reason=reason, errors=errors, timeout=timeout, retries=retries, backoff=backoff, initial=initial, deleted=deleted, labels=labels, annotations=annotations, when=when, field=field, value=value, old=old, new=new, field_needs_change=field_needs_change, requires_finalizer=requires_finalizer, ) assert handler.fn is fn assert handler.id is id assert handler.selector is selector assert handler.reason is reason assert handler.errors is errors assert handler.timeout is timeout assert handler.retries is retries assert handler.backoff is backoff assert handler.initial is initial assert handler.deleted is deleted assert handler.labels is labels assert handler.annotations is annotations assert handler.when is when assert handler.field is field assert handler.value is value assert handler.old is old assert handler.new is new assert handler.field_needs_change is field_needs_change assert handler.requires_finalizer is requires_finalizer
def parent_handler(): def parent_fn(**_): pass return ResourceChangingHandler( fn=parent_fn, id=HandlerId('parent_fn'), errors=None, retries=None, timeout=None, backoff=None, cooldown=None, labels=None, annotations=None, when=None, initial=None, deleted=None, requires_finalizer=None, reason=None, field=None, )
def test_resource_handler_with_all_args(mocker): fn = mocker.Mock() id = mocker.Mock() reason = mocker.Mock() field = mocker.Mock() errors = mocker.Mock() timeout = mocker.Mock() retries = mocker.Mock() backoff = mocker.Mock() initial = mocker.Mock() deleted = mocker.Mock() labels = mocker.Mock() annotations = mocker.Mock() when = mocker.Mock() requires_finalizer = mocker.Mock() handler = ResourceChangingHandler( fn=fn, id=id, reason=reason, field=field, errors=errors, timeout=timeout, retries=retries, backoff=backoff, cooldown=None, # deprecated, but still required initial=initial, deleted=deleted, labels=labels, annotations=annotations, when=when, requires_finalizer=requires_finalizer, ) assert handler.fn is fn assert handler.id is id assert handler.reason is reason assert handler.field is field assert handler.errors is errors assert handler.timeout is timeout assert handler.retries is retries assert handler.backoff is backoff assert handler.initial is initial assert handler.deleted is deleted assert handler.labels is labels assert handler.annotations is annotations assert handler.when is when assert handler.requires_finalizer is requires_finalizer with pytest.deprecated_call(match=r"use handler.reason"): assert handler.event is reason with pytest.deprecated_call(match=r"use handler.backoff"): assert handler.cooldown is backoff
def test_resource_handler_with_deprecated_cooldown_instead_of_backoff(mocker): fn = mocker.Mock() id = mocker.Mock() reason = mocker.Mock() errors = mocker.Mock() timeout = mocker.Mock() retries = mocker.Mock() backoff = mocker.Mock() initial = mocker.Mock() deleted = mocker.Mock() labels = mocker.Mock() annotations = mocker.Mock() when = mocker.Mock() field = mocker.Mock() value = mocker.Mock() old = mocker.Mock() new = mocker.Mock() field_needs_change = mocker.Mock() requires_finalizer = mocker.Mock() with pytest.deprecated_call(match=r"use backoff="): handler = ResourceChangingHandler( fn=fn, id=id, reason=reason, errors=errors, timeout=timeout, retries=retries, backoff=None, cooldown=backoff, # deprecated, but still required initial=initial, deleted=deleted, labels=labels, annotations=annotations, when=when, field=field, value=value, old=old, new=new, field_needs_change=field_needs_change, requires_finalizer=requires_finalizer, ) assert handler.fn is fn assert handler.id is id assert handler.reason is reason assert handler.errors is errors assert handler.timeout is timeout assert handler.retries is retries assert handler.backoff is backoff assert handler.initial is initial assert handler.deleted is deleted assert handler.labels is labels assert handler.annotations is annotations assert handler.when is when assert handler.field is field assert handler.value is value assert handler.old is old assert handler.new is new assert handler.field_needs_change is field_needs_change assert handler.requires_finalizer is requires_finalizer