예제 #1
0
def test_diff_comparison_to_the_same():
    d1 = Diff([
        DiffItem(DiffOperation.ADD   , ('key1',), None, 'new1'),
        DiffItem(DiffOperation.CHANGE, ('key2',), 'old2', 'new2'),
        DiffItem(DiffOperation.REMOVE, ('key3',), 'old3', None),
    ])
    d2 = Diff([
        DiffItem(DiffOperation.ADD   , ('key1',), None, 'new1'),
        DiffItem(DiffOperation.CHANGE, ('key2',), 'old2', 'new2'),
        DiffItem(DiffOperation.REMOVE, ('key3',), 'old3', None),
    ])
    assert d1 == d2
예제 #2
0
def test_resource_changing_kwargs(resource, indices):
    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'),
        indices=indices,
        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', 'index1', 'index2', '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['index1'] is indices['index1']
    assert kwargs['index2'] is indices['index2']
    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
예제 #3
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.")
예제 #4
0
import pytest

from kopf.structs.diffs import reduce, Diff, DiffItem, DiffOperation

DIFF = Diff([
    DiffItem(DiffOperation.ADD, ('key1', ), None, 'new1'),
    DiffItem(DiffOperation.CHANGE, ('key2', ), 'old2', 'new2'),
    DiffItem(DiffOperation.ADD, ('key2', 'suba'), 'olda', 'newa'),
    DiffItem(DiffOperation.REMOVE, ('key2', 'subb'), 'oldb', 'newb'),
    DiffItem(DiffOperation.REMOVE, ('key3', ), 'old3', None),
    DiffItem(DiffOperation.CHANGE, ('key4', ), {
        'suba': 'olda',
        'subc': 'oldc'
    }, {
        'subb': 'newb',
        'subc': 'newc'
    }),
])


@pytest.mark.parametrize('diff', [
    [['op', ['key', 'sub'], 'old', 'new']],
    [['op', ('key', 'sub'), 'old', 'new']],
    [('op', ['key', 'sub'], 'old', 'new')],
    [('op', ('key', 'sub'), 'old', 'new')],
    (['op', ['key', 'sub'], 'old', 'new'], ),
    (['op', ('key', 'sub'), 'old', 'new'], ),
    (('op', ['key', 'sub'], 'old', 'new'), ),
    (('op', ('key', 'sub'), 'old', 'new'), ),
],
                         ids=[
예제 #5
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.")