示例#1
0
async def test_skipped_with_no_handlers(registry, 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(
        ResourceHandler(
            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,
        resource=resource,
        memories=ResourceMemories(),
        raw_event={
            'type': event_type,
            'object': event_body
        },
        replenished=asyncio.Event(),
        event_queue=asyncio.Queue(),
    )

    assert not k8s_mocked.asyncio_sleep.called
    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=[
                    "All handlers succeeded",
                ])
示例#2
0
def _matches_filter_callback(
        handler: handlers.ResourceHandler,
        cause: causation.ResourceCause,
) -> bool:
    if not handler.when:
        return True
    return handler.when(**invocation.build_kwargs(cause=cause))
示例#3
0
文件: conftest.py 项目: zhutony/kopf
def parent_handler():

    def parent_fn(**_):
        pass

    return ResourceHandler(
        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,
    )
示例#4
0
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 = ResourceHandler(
        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
示例#5
0
def test_resource_handler_with_deprecated_cooldown_instead_of_backoff(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()
    labels = mocker.Mock()
    annotations = mocker.Mock()
    requires_finalizer = mocker.Mock()

    with pytest.deprecated_call(match=r"use backoff="):
        handler = ResourceHandler(
            fn=fn,
            id=id,
            reason=reason,
            field=field,
            errors=errors,
            timeout=timeout,
            retries=retries,
            backoff=None,
            cooldown=backoff,  # deprecated, but still required
            initial=initial,
            labels=labels,
            annotations=annotations,
            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.labels is labels
    assert handler.annotations is annotations
    assert handler.requires_finalizer is requires_finalizer
示例#6
0
 def factory(**kwargs):
     handler = ResourceHandler(**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,
             reason=None,
         ), **kwargs))
     registry.resource_changing_handlers[resource].append(handler)
     return handler