async def test_special_kwargs_added(fn, resource): body = {'metadata': {'uid': 'uid', 'name': 'name', 'namespace': 'ns'}, 'spec': {'field': 'value'}, 'status': {'info': 'payload'}} # Values can be any. cause = ChangingCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), initial=False, reason=Reason.NOOP, memo=object(), body=Body(body), diff=object(), old=object(), new=object(), ) fn = MagicMock(fn) await invoke(fn, kwargsrc=cause) assert fn.called assert fn.call_count == 1 # Only check that kwargs are passed at all. The exact kwargs per cause are tested separately. assert 'logger' in fn.call_args[1] assert 'resource' in fn.call_args[1]
def owner(request, resource): body = Body(copy.deepcopy(OWNER)) if request.param == 'state-changing-cause': cause = ChangingCause( 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 = WatchingCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), memo=Memo(), body=body, type='irrelevant', event=RawEvent(type='irrelevant', object=OWNER), ) with context([(cause_var, cause)]): yield body else: raise RuntimeError(f"Wrong param for `owner` fixture: {request.param!r}")
def test_changing_cause_with_only_required_args(mocker): logger = mocker.Mock() indices = mocker.Mock() resource = mocker.Mock() reason = mocker.Mock() initial = mocker.Mock() body = mocker.Mock() patch = mocker.Mock() memo = mocker.Mock() cause = ChangingCause( resource=resource, indices=indices, logger=logger, reason=reason, initial=initial, body=body, patch=patch, memo=memo, ) assert cause.resource is resource assert cause.indices is indices assert cause.logger is logger assert cause.reason is reason assert cause.initial is initial assert cause.body is body assert cause.patch is patch assert cause.memo is memo assert cause.diff is not None assert not cause.diff assert cause.old is None assert cause.new is None
def test_changing_kwargs(resource, attr): body = { 'metadata': { 'uid': 'uid1', 'name': 'name1', 'namespace': 'ns1', 'labels': { 'l1': 'v1' }, 'annotations': { 'a1': 'v1' } }, 'spec': { 'field': 'value' }, 'status': { 'info': 'payload' } } cause = ChangingCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), initial=False, reason=Reason.NOOP, memo=Memo(), body=Body(body), diff=Diff([]), old=BodyEssence(), new=BodyEssence(), ) kwargs = getattr( cause, attr) # cause.kwargs / cause.sync_kwargs / cause.async_kwargs assert set(kwargs) == { 'logger', 'resource', 'patch', 'reason', 'memo', 'body', 'spec', 'status', 'meta', 'uid', 'name', 'namespace', 'labels', 'annotations', 'diff', 'old', 'new' } assert kwargs['resource'] is cause.resource assert kwargs['reason'] is cause.reason assert kwargs['logger'] is cause.logger assert kwargs['patch'] is cause.patch assert kwargs['memo'] is cause.memo assert kwargs['diff'] is cause.diff assert kwargs['old'] is cause.old assert kwargs['new'] is cause.new 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
def new_detect_fn(*, finalizer, diff, new, old, **kwargs): # For change detection, we ensure that there is no extra cycle of adding a finalizer. raw_event = kwargs.pop('raw_event', None) raw_body = raw_event['object'] raw_body.setdefault('metadata', {}).setdefault('finalizers', [finalizer]) # Pass through kwargs: resource, logger, patch, diff, old, new. # I.e. everything except what we mock -- for them, use the mocked values (if not None). return ChangingCause(reason=mock.reason, diff=mock.diff if mock.diff is not None else diff, new=mock.new if mock.new is not None else new, old=mock.old if mock.old is not None else old, **kwargs)
def test_changing_cause_with_all_args(mocker): logger = mocker.Mock() indices = mocker.Mock() resource = mocker.Mock() reason = mocker.Mock() initial = mocker.Mock() body = mocker.Mock() patch = mocker.Mock() memo = mocker.Mock() diff = mocker.Mock() old = mocker.Mock() new = mocker.Mock() cause = ChangingCause( resource=resource, indices=indices, logger=logger, reason=reason, initial=initial, body=body, patch=patch, memo=memo, diff=diff, old=old, new=new, ) assert cause.resource is resource assert cause.indices is indices assert cause.logger is logger assert cause.reason is reason assert cause.initial is initial assert cause.body is body assert cause.patch is patch assert cause.memo is memo assert cause.diff is diff assert cause.old is old assert cause.new is new
def make_cause( cls=ChangingCause, *, resource=resource, event=None, type=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 WatchingCause or cls is WatchingRegistry: return WatchingCause( 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, event=event, ) if cls is ChangingCause or cls is ChangingRegistry: return ChangingCause( 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 SpawningCause or cls is SpawningRegistry: return SpawningCause( 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 WebhookCause or cls is WebhooksRegistry: return WebhookCause( 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.")