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
Beispiel #2
0
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, event=UPDATE, diff=diff)
    mocker.patch('kopf.structs.filters.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].event 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'}
Beispiel #3
0
def test_on_create_with_all_kwargs(mocker):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, event=CREATE)
    mocker.patch('kopf.structs.filters.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].event == 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'}
Beispiel #4
0
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
Beispiel #5
0
def test_all_args(mocker):
    group = mocker.Mock()
    version = mocker.Mock()
    plural = mocker.Mock()
    resource = Resource(
        group=group,
        version=version,
        plural=plural,
    )
    assert resource.group is group
    assert resource.version is version
    assert resource.plural is plural
Beispiel #6
0
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,
                                                     body=OBJECT_BODY)
    assert requires_finalizer == expected
def test_on_create_minimal(mocker):
    registry = kopf.get_default_registry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, event=CREATE)

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

    handlers = registry.get_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].event == CREATE
    assert handlers[0].field is None
    assert handlers[0].timeout is None
Beispiel #8
0
def test_on_delete_minimal(mocker):
    registry = kopf.get_default_registry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, event=DELETE)

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

    handlers = registry.get_cause_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].event == DELETE
    assert handlers[0].field is None
    assert handlers[0].timeout is None
    assert registry.requires_finalizer(resource=resource) is True
def test_on_field_minimal(mocker):
    registry = kopf.get_default_registry()
    resource = Resource('group', 'version', 'plural')
    diff = [('op', ('field', 'subfield'), 'old', 'new')]
    cause = mocker.MagicMock(resource=resource, event=UPDATE, diff=diff)

    @kopf.on.field('group', 'version', 'plural', 'field.subfield')
    def fn(**_):
        pass

    handlers = registry.get_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].event is None
    assert handlers[0].field == ('field', 'subfield')
    assert handlers[0].timeout is None
def test_on_create_with_all_kwargs(mocker):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, event=CREATE)

    @kopf.on.create('group', 'version', 'plural',
                    id='id', timeout=123, registry=registry)
    def fn(**_):
        pass

    handlers = registry.get_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].event == CREATE
    assert handlers[0].field is None
    assert handlers[0].id == 'id'
    assert handlers[0].timeout == 123
Beispiel #11
0
def test_resource_requires_finalizer_mixed_handlers(mocker):
    registry = kopf.get_default_registry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, event=CREATE)

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

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

    @kopf.on.update('group', 'version', 'plural')
    def fn3(**_):
        pass

    assert registry.requires_finalizer(resource=resource) is True
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, event=UPDATE, diff=diff)

    @kopf.on.field('group', 'version', 'plural', 'field.subfield',
                   id='id', timeout=123, registry=registry)
    def fn(**_):
        pass

    handlers = registry.get_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].event is None
    assert handlers[0].field ==('field', 'subfield')
    assert handlers[0].id == 'id/field.subfield'
    assert handlers[0].timeout == 123
Beispiel #13
0
def test_on_delete_with_all_kwargs(mocker, optional):
    registry = GlobalRegistry()
    resource = Resource('group', 'version', 'plural')
    cause = mocker.MagicMock(resource=resource, event=DELETE)

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

    handlers = registry.get_cause_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
    assert handlers[0].event == DELETE
    assert handlers[0].field is None
    assert handlers[0].id == 'id'
    assert handlers[0].timeout == 123
    assert registry.requires_finalizer(resource=resource) is not optional
Beispiel #14
0
def test_no_args():
    with pytest.raises(TypeError):
        Resource()
Beispiel #15
0
def resource():
    """ The resource used in the tests. Usually mocked, so it does not matter. """
    return Resource('zalando.org', 'v1', 'kopfexamples')