Example #1
0
def test_on_field_with_all_kwargs(mocker, cause_factory):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    old = {'field': {'subfield': 'old'}}
    new = {'field': {'subfield': 'new'}}
    cause = cause_factory(resource=resource, reason=Reason.UPDATE, old=old, new=new, body=new)
    mocker.patch('kopf.reactor.registries.match', return_value=True)

    when = lambda **_: False

    @kopf.on.field('group', 'version', 'plural', field='field.subfield',
                   id='id', timeout=123, registry=registry,
                   labels={'somelabel': 'somevalue'},
                   annotations={'someanno': 'somevalue'},
                   when=when)
    def fn(**_):
        pass

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].reason is None
    assert handlers[0].field ==('field', 'subfield')
    assert handlers[0].id == 'id/field.subfield'
    assert handlers[0].timeout == 123
    assert handlers[0].labels == {'somelabel': 'somevalue'}
    assert handlers[0].annotations == {'someanno': 'somevalue'}
    assert handlers[0].when == when
Example #2
0
def test_on_field_with_all_kwargs(mocker, cause_factory):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    diff = [('op', ('field', 'subfield'), 'old', 'new')]
    cause = cause_factory(resource=resource, reason=Reason.UPDATE, diff=diff)
    mocker.patch('kopf.reactor.registries.match', return_value=True)

    when = lambda **_: False

    @kopf.on.field('group',
                   'version',
                   'plural',
                   'field.subfield',
                   id='id',
                   timeout=123,
                   registry=registry,
                   labels={'somelabel': 'somevalue'},
                   annotations={'someanno': 'somevalue'},
                   when=when)
    def fn(**_):
        pass

    with pytest.deprecated_call(
            match=r"use OperatorRegistry.get_resource_changing_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].reason is None
    assert handlers[0].field == ('field', 'subfield')
    assert handlers[0].id == 'id/field.subfield'
    assert handlers[0].timeout == 123
    assert handlers[0].labels == {'somelabel': 'somevalue'}
    assert handlers[0].annotations == {'someanno': 'somevalue'}
    assert handlers[0].when == when
def test_on_create_with_all_kwargs(mocker):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, reason=Reason.CREATE)
    mocker.patch('kopf.reactor.registries.match', return_value=True)

    @kopf.on.create('group',
                    'version',
                    'plural',
                    id='id',
                    timeout=123,
                    registry=registry,
                    labels={'somelabel': 'somevalue'},
                    annotations={'someanno': 'somevalue'})
    def fn(**_):
        pass

    handlers = registry.get_cause_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].reason == Reason.CREATE
    assert handlers[0].field is None
    assert handlers[0].id == 'id'
    assert handlers[0].timeout == 123
    assert handlers[0].labels == {'somelabel': 'somevalue'}
    assert handlers[0].annotations == {'someanno': 'somevalue'}
def test_on_field_with_all_kwargs(mocker):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    diff = [('op', ('field', 'subfield'), 'old', 'new')]
    cause = mocker.MagicMock(resource=resource,
                             reason=Reason.UPDATE,
                             diff=diff)
    mocker.patch('kopf.reactor.registries.match', return_value=True)

    @kopf.on.field('group',
                   'version',
                   'plural',
                   'field.subfield',
                   id='id',
                   timeout=123,
                   registry=registry,
                   labels={'somelabel': 'somevalue'},
                   annotations={'someanno': 'somevalue'})
    def fn(**_):
        pass

    handlers = registry.get_cause_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].reason is None
    assert handlers[0].field == ('field', 'subfield')
    assert handlers[0].id == 'id/field.subfield'
    assert handlers[0].timeout == 123
    assert handlers[0].labels == {'somelabel': 'somevalue'}
    assert handlers[0].annotations == {'someanno': 'somevalue'}
Example #5
0
def test_on_delete_with_all_kwargs(mocker, optional, cause_factory):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = cause_factory(resource=resource, reason=Reason.DELETE)
    mocker.patch('kopf.reactor.registries.match', return_value=True)

    when = lambda **_: False

    @kopf.on.delete('group', 'version', 'plural',
                    id='id', timeout=123, registry=registry, optional=optional,
                    labels={'somelabel': 'somevalue'},
                    annotations={'someanno': 'somevalue'},
                    when=when)
    def fn(**_):
        pass

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].reason == Reason.DELETE
    assert handlers[0].field is None
    assert handlers[0].id == 'id'
    assert handlers[0].timeout == 123
    assert handlers[0].labels == {'somelabel': 'somevalue'}
    assert handlers[0].annotations == {'someanno': 'somevalue'}
    assert handlers[0].when == when
Example #6
0
def test_on_update_with_all_kwargs(mocker):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, reason=Reason.UPDATE)
    mocker.patch('kopf.reactor.registries.match', return_value=True)

    when = lambda **_: False

    @kopf.on.update('group',
                    'version',
                    'plural',
                    id='id',
                    timeout=123,
                    registry=registry,
                    labels={'somelabel': 'somevalue'},
                    annotations={'someanno': 'somevalue'},
                    when=when)
    def fn(**_):
        pass

    with pytest.deprecated_call(
            match=r"use OperatorRegistry.get_resource_changing_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].reason == Reason.UPDATE
    assert handlers[0].field is None
    assert handlers[0].id == 'id'
    assert handlers[0].timeout == 123
    assert handlers[0].labels == {'somelabel': 'somevalue'}
    assert handlers[0].annotations == {'someanno': 'somevalue'}
    assert handlers[0].when == when
Example #7
0
def test_global_registry_with_minimal_signature(mocker, resource):
    cause = mocker.Mock(resource=resource, event=None, diff=None)

    registry = GlobalRegistry()
    registry.register(resource.group, resource.version, resource.plural, some_fn)
    handlers = registry.get_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
Example #8
0
def test_global_registry_via_list(mocker, resource):
    cause = mocker.Mock(resource=resource, event=None, diff=None)

    registry = GlobalRegistry()
    handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
def test_requires_finalizer_no_deletion_handler():
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.create('group', 'version', 'plural', registry=registry)
    def fn1(**_):
        pass

    requires_finalizer = registry.requires_finalizer(resource=resource,
                                                     body=OBJECT_BODY)
    assert requires_finalizer is False
def test_requires_finalizer_deletion_handler(optional, expected):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.delete('group', 'version', 'plural',
                    registry=registry, optional=optional)
    def fn(**_):
        pass

    requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
    assert requires_finalizer == expected
def test_global_registry_with_minimal_signature(mocker, resource):
    cause = mocker.Mock(resource=resource, event=None, diff=None)

    registry = GlobalRegistry()
    with pytest.deprecated_call(match=r"use OperatorRegistry.register_resource_changing_handler\(\)"):
        registry.register_cause_handler(resource.group, resource.version, resource.plural, some_fn)
    with pytest.deprecated_call(match=r"use OperatorRegistry.get_resource_changing_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
def test_global_registry_via_list(mocker, resource):
    cause = mocker.Mock(resource=resource, event=None, diff=None)

    registry = GlobalRegistry()
    with pytest.deprecated_call(match=r"use OperatorRegistry.get_resource_changing_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
Example #13
0
def test_global_registry_via_list(cause_factory):

    cause = cause_factory()
    registry = GlobalRegistry()
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
def test_requires_finalizer_no_deletion_handler():
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.create('group', 'version', 'plural', registry=registry)
    def fn1(**_):
        pass

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        requires_finalizer = registry.requires_finalizer(resource=resource,
                                                         cause=CAUSE)
    assert requires_finalizer is False
Example #15
0
def test_global_registry_via_list(cause_factory):

    cause = cause_factory()
    registry = GlobalRegistry()
    with pytest.deprecated_call(
            match=r"use OperatorRegistry.get_resource_changing_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
Example #16
0
def test_global_registry_with_minimal_signature(cause_factory, resource):

    cause = cause_factory()
    registry = GlobalRegistry()
    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register_cause_handler(resource.group, resource.version,
                                        resource.plural, some_fn)
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
Example #17
0
def test_global_registry_via_iter(mocker, resource):
    cause = mocker.Mock(resource=resource, event=None, diff=None)

    registry = GlobalRegistry()
    iterator = registry.iter_cause_handlers(cause)

    assert isinstance(iterator, collections.abc.Iterator)
    assert not isinstance(iterator, collections.abc.Collection)
    assert not isinstance(iterator, collections.abc.Container)
    assert not isinstance(iterator, (list, tuple))

    handlers = list(iterator)
    assert not handlers
Example #18
0
def test_resources():
    registry = GlobalRegistry()
    registry.register('group1', 'version1', 'plural1', some_fn)
    registry.register('group2', 'version2', 'plural2', some_fn)

    resources = registry.resources

    assert isinstance(resources, collections.abc.Collection)
    assert len(resources) == 2

    resource1 = Resource('group1', 'version1', 'plural1')
    resource2 = Resource('group2', 'version2', 'plural2')
    assert resource1 in resources
    assert resource2 in resources
Example #19
0
def test_global_registry_via_iter(cause_factory):

    cause = cause_factory()
    registry = GlobalRegistry()
    iterator = registry.iter_cause_handlers(cause)

    assert isinstance(iterator, collections.abc.Iterator)
    assert not isinstance(iterator, collections.abc.Collection)
    assert not isinstance(iterator, collections.abc.Container)
    assert not isinstance(iterator, (list, tuple))

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = list(iterator)
    assert not handlers
Example #20
0
def test_global_registry_via_iter(cause_factory):

    cause = cause_factory()
    registry = GlobalRegistry()
    iterator = registry.iter_cause_handlers(cause)

    assert isinstance(iterator, collections.abc.Iterator)
    assert not isinstance(iterator, collections.abc.Collection)
    assert not isinstance(iterator, collections.abc.Container)
    assert not isinstance(iterator, (list, tuple))

    with pytest.deprecated_call(
            match=r"use OperatorRegistry.iter_resource_changing_handlers\(\)"):
        handlers = list(iterator)
    assert not handlers
def test_requires_finalizer_deletion_handler(optional, expected):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.delete('group',
                    'version',
                    'plural',
                    registry=registry,
                    optional=optional)
    def fn(**_):
        pass

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        requires_finalizer = registry.requires_finalizer(resource=resource,
                                                         cause=CAUSE)
    assert requires_finalizer == expected
def test_requires_finalizer_deletion_handler_matches_labels(
        labels, optional, expected):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.delete('group',
                    'version',
                    'plural',
                    labels=labels,
                    registry=registry,
                    optional=optional)
    def fn(**_):
        pass

    requires_finalizer = registry.requires_finalizer(resource=resource,
                                                     body=OBJECT_BODY)
    assert requires_finalizer == expected
def test_requires_finalizer_deletion_handler_mismatches_annotations(
        annotations, optional, expected):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.delete('group',
                    'version',
                    'plural',
                    annotations=annotations,
                    registry=registry,
                    optional=optional)
    def fn(**_):
        pass

    with pytest.deprecated_call(
            match=r"use registry.resource_changing_handlers"):
        requires_finalizer = registry.requires_finalizer(resource=resource,
                                                         cause=CAUSE)
    assert requires_finalizer == expected
Example #24
0
def test_requires_finalizer_deletion_handler_matches_labels(
        labels, optional, expected, cause_factory):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = cause_factory(resource=resource, body=OBJECT_BODY)

    @kopf.on.delete('group',
                    'version',
                    'plural',
                    labels=labels,
                    registry=registry,
                    optional=optional)
    def fn(**_):
        pass

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        requires_finalizer = registry.requires_finalizer(resource=resource,
                                                         cause=cause)
    assert requires_finalizer == expected
def test_requires_finalizer_multiple_handlers(optional, expected):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.create('group', 'version', 'plural', registry=registry)
    def fn1(**_):
        pass

    @kopf.on.delete('group',
                    'version',
                    'plural',
                    registry=registry,
                    optional=optional)
    def fn2(**_):
        pass

    requires_finalizer = registry.requires_finalizer(resource=resource,
                                                     body=OBJECT_BODY)
    assert requires_finalizer == expected
def test_requires_finalizer_multiple_handlers(optional, expected):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')

    @kopf.on.create('group', 'version', 'plural', registry=registry)
    def fn1(**_):
        pass

    @kopf.on.delete('group',
                    'version',
                    'plural',
                    registry=registry,
                    optional=optional)
    def fn2(**_):
        pass

    with pytest.deprecated_call(
            match=r"use registry.resource_changing_handlers"):
        requires_finalizer = registry.requires_finalizer(resource=resource,
                                                         cause=CAUSE)
    assert requires_finalizer == expected
Example #27
0
def test_resources():
    registry = GlobalRegistry()

    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register_cause_handler('group1', 'version1', 'plural1', some_fn)
    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register_cause_handler('group2', 'version2', 'plural2', some_fn)

    resources = registry.resources

    assert isinstance(resources, collections.abc.Collection)
    assert len(resources) == 2

    resource1 = Resource('group1', 'version1', 'plural1')
    resource2 = Resource('group2', 'version2', 'plural2')
    assert resource1 in resources
    assert resource2 in resources
Example #28
0
def test_creation_of_global():
    registry = GlobalRegistry()
    assert isinstance(registry, BaseRegistry)
    assert isinstance(registry, GlobalRegistry)
Example #29
0
def registry(request):
    if request.param == 'simple':
        return SimpleRegistry()
    if request.param == 'global':
        return GlobalRegistry()
    raise Exception(f"Unsupported registry type: {request.param}")