Example #1
0
def owner(request, resource):
    if request.param == 'state-changing-cause':
        cause = ResourceChangingCause(
            logger=logging.getLogger('kopf.test.fake.logger'),
            resource=resource,
            patch=Patch(),
            memo=ObjectDict(),
            body=OWNER,
            initial=False,
            reason=Reason.NOOP,
        )
        with context([(cause_var, cause)]):
            yield
    elif request.param == 'event-watching-cause':
        cause = ResourceWatchingCause(
            logger=logging.getLogger('kopf.test.fake.logger'),
            resource=resource,
            patch=Patch(),
            memo=ObjectDict(),
            body=OWNER,
            type='irrelevant',
            raw=Event(type='irrelevant', object=OWNER),
        )
        with context([(cause_var, cause)]):
            yield
    else:
        raise RuntimeError(
            f"Wrong param for `owner` fixture: {request.param!r}")
Example #2
0
def test_resource_watching_cause(mocker):
    logger = mocker.Mock()
    indices = mocker.Mock()
    resource = mocker.Mock()
    body = mocker.Mock()
    patch = mocker.Mock()
    memo = mocker.Mock()
    type = mocker.Mock()
    raw = mocker.Mock()
    cause = ResourceWatchingCause(
        resource=resource,
        indices=indices,
        logger=logger,
        body=body,
        patch=patch,
        memo=memo,
        type=type,
        raw=raw,
    )
    assert cause.resource is resource
    assert cause.indices is indices
    assert cause.logger is logger
    assert cause.body is body
    assert cause.patch is patch
    assert cause.memo is memo
    assert cause.type is type
    assert cause.raw is raw
Example #3
0
def owner(request, resource):
    body = Body(copy.deepcopy(OWNER))
    if request.param == 'state-changing-cause':
        cause = ResourceChangingCause(
            logger=logging.getLogger('kopf.test.fake.logger'),
            indices=OperatorIndexers().indices,
            resource=resource,
            patch=Patch(),
            memo=Memo(),
            body=body,
            initial=False,
            reason=Reason.NOOP,
        )
        with context([(cause_var, cause)]):
            yield body
    elif request.param == 'event-watching-cause':
        cause = ResourceWatchingCause(
            logger=logging.getLogger('kopf.test.fake.logger'),
            indices=OperatorIndexers().indices,
            resource=resource,
            patch=Patch(),
            memo=Memo(),
            body=body,
            type='irrelevant',
            raw=RawEvent(type='irrelevant', object=OWNER),
        )
        with context([(cause_var, cause)]):
            yield body
    else:
        raise RuntimeError(
            f"Wrong param for `owner` fixture: {request.param!r}")
Example #4
0
def test_resource_watching_kwargs(resource, indices):
    body = {
        'metadata': {
            'uid': 'uid1',
            'name': 'name1',
            'namespace': 'ns1',
            'labels': {
                'l1': 'v1'
            },
            'annotations': {
                'a1': 'v1'
            }
        },
        'spec': {
            'field': 'value'
        },
        'status': {
            'info': 'payload'
        }
    }
    cause = ResourceWatchingCause(
        logger=logging.getLogger('kopf.test.fake.logger'),
        indices=indices,
        resource=resource,
        patch=Patch(),
        memo=Memo(),
        body=Body(body),
        type='ADDED',
        raw={
            'type': 'ADDED',
            'object': {}
        },
    )
    kwargs = build_kwargs(cause=cause, extrakwarg=123)
    assert set(kwargs) == {
        'extrakwarg', 'logger', 'index1', 'index2', 'resource', 'patch',
        'event', 'type', 'memo', 'body', 'spec', 'status', 'meta', 'uid',
        'name', 'namespace', 'labels', 'annotations'
    }
    assert kwargs['extrakwarg'] == 123
    assert kwargs['resource'] is cause.resource
    assert kwargs['index1'] is indices['index1']
    assert kwargs['index2'] is indices['index2']
    assert kwargs['logger'] is cause.logger
    assert kwargs['patch'] is cause.patch
    assert kwargs['event'] is cause.raw
    assert kwargs['memo'] is cause.memo
    assert kwargs['type'] is cause.type
    assert kwargs['body'] is cause.body
    assert kwargs['spec'] is cause.body.spec
    assert kwargs['meta'] is cause.body.metadata
    assert kwargs['status'] is cause.body.status
    assert kwargs['labels'] is cause.body.metadata.labels
    assert kwargs['annotations'] is cause.body.metadata.annotations
    assert kwargs['uid'] == cause.body.metadata.uid
    assert kwargs['name'] == cause.body.metadata.name
    assert kwargs['namespace'] == cause.body.metadata.namespace
Example #5
0
 def make_cause(
     cls=ResourceChangingCause,
     *,
     resource=resource,
     type=None,
     raw=None,
     body=None,
     diff=(),
     reason='some-reason',
     initial=False,
     activity=None,
     settings=None,
 ):
     if cls is ActivityCause or cls is ActivityRegistry:
         return ActivityCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             activity=activity,
             settings=settings,
         )
     if cls is ResourceCause or cls is ResourceRegistry or cls is SimpleRegistry:
         return ResourceCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
         )
     if cls is ResourceWatchingCause or cls is ResourceWatchingRegistry:
         return ResourceWatchingCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
             type=type,
             raw=raw,
         )
     if cls is ResourceChangingCause or cls is ResourceChangingRegistry:
         return ResourceChangingCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
             diff=Diff(DiffItem(*d) for d in diff),
             initial=initial,
             reason=reason,
         )
     raise TypeError(
         f"Cause/registry type {cls} is not supported by this fixture.")
Example #6
0
 def make_cause(
     cls=ResourceChangingCause,
     *,
     resource=resource,
     type=None,
     raw=None,
     body=None,
     diff=(),
     old=None,
     new=None,
     reason='some-reason',
     initial=False,
     activity=None,
     settings=None,
 ):
     if cls is ActivityCause or cls is ActivityRegistry:
         return ActivityCause(
             memo=Memo(),
             logger=logging.getLogger('kopf.test.fake.logger'),
             indices=OperatorIndexers().indices,
             activity=activity,
             settings=settings,
         )
     if cls is ResourceCause or cls is ResourceRegistry:
         return ResourceCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             indices=OperatorIndexers().indices,
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
         )
     if cls is ResourceWatchingCause or cls is ResourceWatchingRegistry:
         return ResourceWatchingCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             indices=OperatorIndexers().indices,
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
             type=type,
             raw=raw,
         )
     if cls is ResourceChangingCause or cls is ResourceChangingRegistry:
         return ResourceChangingCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             indices=OperatorIndexers().indices,
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
             diff=Diff(DiffItem(*d) for d in diff),
             old=old,
             new=new,
             initial=initial,
             reason=reason,
         )
     if cls is ResourceSpawningCause or cls is ResourceSpawningRegistry:
         return ResourceSpawningCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             indices=OperatorIndexers().indices,
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
             reset=False,
         )
     if cls is ResourceWebhookCause or cls is ResourceWebhooksRegistry:
         return ResourceWebhookCause(
             logger=logging.getLogger('kopf.test.fake.logger'),
             indices=OperatorIndexers().indices,
             resource=resource,
             patch=Patch(),
             memo=Memo(),
             body=Body(body if body is not None else {}),
             dryrun=False,
             sslpeer={},
             headers={},
             userinfo={},
             warnings=[],
             reason=None,
             webhook=None,
             operation=None,
         )
     raise TypeError(
         f"Cause/registry type {cls} is not supported by this fixture.")