def owner(request, resource): if request.param == 'state-changing-cause': cause = ResourceChangingCause( logger=logging.getLogger('kopf.test.fake.logger'), resource=resource, patch=Patch(), memo=Memo(), body=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=Memo(), body=Body(OWNER), type='irrelevant', raw=RawEvent(type='irrelevant', object=OWNER), ) with context([(cause_var, cause)]): yield else: raise RuntimeError(f"Wrong param for `owner` fixture: {request.param!r}")
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.")
def test_resource_changing_kwargs(resource): body = { 'metadata': { 'uid': 'uid1', 'name': 'name1', 'namespace': 'ns1', 'labels': { 'l1': 'v1' }, 'annotations': { 'a1': 'v1' } }, 'spec': { 'field': 'value' }, 'status': { 'info': 'payload' } } cause = ResourceChangingCause( logger=logging.getLogger('kopf.test.fake.logger'), resource=resource, patch=Patch(), initial=False, reason=Reason.NOOP, memo=Memo(), body=Body(body), diff=Diff([]), old=BodyEssence(), new=BodyEssence(), ) kwargs = build_kwargs(cause=cause, extrakwarg=123) assert set(kwargs) == { 'extrakwarg', 'logger', 'resource', 'patch', 'reason', 'memo', 'body', 'spec', 'status', 'meta', 'uid', 'name', 'namespace', 'labels', 'annotations', 'diff', 'old', 'new' } assert kwargs['extrakwarg'] == 123 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 test_resource_watching_kwargs(resource): 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'), 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', '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['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
def test_daemon_async_stopper(resource): cause = DaemonCause( logger=logging.getLogger('kopf.test.fake.logger'), resource=resource, patch=Patch(), memo=Memo(), body=Body({}), stopper=DaemonStopper(), ) kwargs = build_kwargs(cause=cause, _sync=False) assert kwargs['stopped'] is cause.stopper.async_checker
def test_daemon_kwargs(resource): body = { 'metadata': { 'uid': 'uid1', 'name': 'name1', 'namespace': 'ns1', 'labels': { 'l1': 'v1' }, 'annotations': { 'a1': 'v1' } }, 'spec': { 'field': 'value' }, 'status': { 'info': 'payload' } } cause = DaemonCause( logger=logging.getLogger('kopf.test.fake.logger'), resource=resource, patch=Patch(), memo=Memo(), body=Body(body), stopper=DaemonStopper(), ) kwargs = build_kwargs(cause=cause, extrakwarg=123) assert set(kwargs) == { 'extrakwarg', 'logger', 'resource', 'patch', 'memo', 'body', 'spec', 'status', 'meta', 'uid', 'name', 'namespace', 'labels', 'annotations' } assert kwargs['extrakwarg'] == 123 assert kwargs['resource'] is cause.resource assert kwargs['logger'] is cause.logger assert kwargs['patch'] is cause.patch assert kwargs['memo'] is cause.memo 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 assert 'stopped' not in kwargs
async def test_protocol_invocation(lifecycle, resource): """ To be sure that all kwargs are accepted properly. Especially when the new kwargs are added or an invocation protocol changed. """ # The values are irrelevant, they can be anything. state = State.from_scratch(handlers=[]) cause = ResourceChangingCause( logger=logging.getLogger('kopf.test.fake.logger'), resource=resource, patch=Patch(), memo=Memo(), body=Body({}), initial=False, reason=Reason.NOOP, ) handlers = [] selected = await invoke(lifecycle, handlers, cause=cause, state=state) assert isinstance(selected, (tuple, list)) assert len(selected) == 0
def test_object_dict_raises_attribute_errors_on_del(): obj = Memo() with pytest.raises(AttributeError): del obj.unexistent
def test_object_dict_raises_key_errors_on_del(): obj = Memo() with pytest.raises(KeyError): del obj['unexistent']
def test_object_dict_fields_deleted(): obj = Memo() obj.xyz = 100 del obj.xyz assert obj == {}
def test_object_dict_keys_deleted(): obj = Memo() obj['xyz'] = 100 del obj['xyz'] assert obj == {}
def test_object_dict_keys_are_fields(): obj = Memo() obj['xyz'] = 100 assert obj.xyz == 100
def test_object_dict_creation(): obj = Memo() assert isinstance(obj, collections.abc.MutableMapping)