コード例 #1
0
def test_storing_to_status_storage_overwrites_old_content(cls):
    storage = cls(field='status.my-operator')
    patch = Patch()
    body = Body({})
    storage.store(body=body,
                  patch=patch,
                  key=HandlerId('id1'),
                  record=CONTENT_DATA_1)
    storage.store(body=body,
                  patch=patch,
                  key=HandlerId('id1'),
                  record=CONTENT_DATA_2)

    assert patch
    assert patch['status']['my-operator']['id1'] == CONTENT_DATA_2
コード例 #2
0
def test_activity_error_exception():
    outcome = Outcome(final=True)
    outcomes: Mapping[HandlerId, Outcome]
    outcomes = {HandlerId('id'): outcome}
    error = ActivityError("message", outcomes=outcomes)
    assert str(error) == "message"
    assert error.outcomes == outcomes
コード例 #3
0
def parent_handler(selector):
    def parent_fn(**_):
        pass

    return ChangingHandler(
        fn=parent_fn,
        id=HandlerId('parent_fn'),
        param=None,
        errors=None,
        retries=None,
        timeout=None,
        backoff=None,
        selector=selector,
        labels=None,
        annotations=None,
        when=None,
        field=None,
        value=None,
        old=None,
        new=None,
        field_needs_change=None,
        initial=None,
        deleted=None,
        requires_finalizer=None,
        reason=None,
    )
コード例 #4
0
def test_storing_to_status_storage_appends_keys(cls):
    storage = cls(field='status.my-operator')
    patch = Patch()
    body = Body({})
    storage.store(body=body,
                  patch=patch,
                  key=HandlerId('id1'),
                  record=CONTENT_DATA_1)
    storage.store(body=body,
                  patch=patch,
                  key=HandlerId('id2'),
                  record=CONTENT_DATA_1)

    assert patch
    assert patch['status']['my-operator']['id1'] == CONTENT_DATA_1
    assert patch['status']['my-operator']['id2'] == CONTENT_DATA_1
コード例 #5
0
def test_storing_to_annotations_storage_overwrites_old_content(cls):
    storage = cls(prefix='my-operator.example.com', verbose=True)
    patch = Patch()
    body = Body({})
    storage.store(body=body,
                  patch=patch,
                  key=HandlerId('id1'),
                  record=CONTENT_DATA_1)
    storage.store(body=body,
                  patch=patch,
                  key=HandlerId('id1'),
                  record=CONTENT_DATA_2)

    assert patch
    assert patch['metadata']['annotations'][
        'my-operator.example.com/id1'] == CONTENT_JSON_2
コード例 #6
0
def test_purging_of_status_storage_nullifies_content(cls):
    storage = cls(field='status.my-operator')
    patch = Patch()
    body = Body({'status': {'my-operator': {'id1': CONTENT_DATA_1}}})
    storage.purge(body=body, patch=patch, key=HandlerId('id1'))

    assert patch
    assert patch['status']['my-operator']['id1'] is None
コード例 #7
0
def test_fetching_from_annotations_storage(cls):
    storage = cls(prefix='my-operator.example.com', verbose=True)
    body = Body({
        'metadata': {
            'annotations': {
                'my-operator.example.com/id1': CONTENT_JSON_1,
            }
        }
    })
    content = storage.fetch(body=body, key=HandlerId('id1'))

    assert content == CONTENT_DATA_1
コード例 #8
0
def test_fetching_from_status_storage(cls):
    storage = cls(field='status.my-operator')
    body = Body({
        'status': {
            'my-operator': {
                'id1': CONTENT_DATA_1,
                'id2': CONTENT_DATA_2
            }
        }
    })
    content = storage.fetch(body=body, key=HandlerId('id1'))

    assert content == CONTENT_DATA_1
コード例 #9
0
def test_purging_of_annotations_storage_nullifies_content(cls):
    storage = cls(prefix='my-operator.example.com', verbose=True)
    patch = Patch()
    body = Body({
        'metadata': {
            'annotations': {
                'my-operator.example.com/id1': CONTENT_JSON_1,
            }
        }
    })
    storage.purge(body=body, patch=patch, key=HandlerId('id1'))

    assert patch
    assert patch['metadata']['annotations'][
        'my-operator.example.com/id1'] is None
コード例 #10
0
def test_storing_to_annotations_storage_cleans_content(cls):
    storage = cls(prefix='my-operator.example.com')  # no verbose=
    patch = Patch()
    body = Body({})
    content = ProgressRecord(
        started=None,
        stopped=None,
        delayed=None,
        retries=None,
        success=None,
        failure=None,
        message=None,
        subrefs=None,
    )
    storage.store(body=body, patch=patch, key=HandlerId('id1'), record=content)

    assert patch
    assert patch['metadata']['annotations'][
        'my-operator.example.com/id1'] == json.dumps({})
コード例 #11
0
async def test_handlers_with_reason_and_webhook_requested(
        settings, registry, resource, memories, insights, indices, adm_request,
        reason):

    mock1 = Mock()
    mock2 = Mock()
    mock3 = Mock()
    mock4 = Mock()

    @kopf.on.validate(*resource, id='fnX')
    def fn1(**kwargs):
        mock1(**kwargs)

    @kopf.on.validate(*resource)
    def fn2(**kwargs):
        mock2(**kwargs)

    @kopf.on.mutate(*resource)
    def fn3(**kwargs):
        mock3(**kwargs)

    @kopf.on.mutate(*resource, id='fnX')
    def fn4(**kwargs):
        mock4(**kwargs)

    response = await serve_admission_request(
        adm_request,
        webhook=HandlerId('fnX'),
        reason=reason,
        settings=settings,
        registry=registry,
        insights=insights,
        memories=memories,
        memobase=object(),
        indices=indices,
    )
    assert response['response']['allowed'] is True
    assert mock1.call_count == (1 if reason == WebhookType.VALIDATING else 0)
    assert mock2.call_count == 0
    assert mock3.call_count == 0
    assert mock4.call_count == (1 if reason == WebhookType.MUTATING else 0)
コード例 #12
0
def test_purging_already_empty_body_does_nothing(cls: Type[ProgressStorage]):
    storage = cls()
    patch = Patch()
    body = Body({})
    storage.purge(body=body, patch=patch, key=HandlerId('id1'))
    assert not patch
コード例 #13
0
def test_fetching_from_empty_body_returns_none(cls: Type[ProgressStorage]):
    storage = cls()
    body = Body({})
    data = storage.fetch(body=body, key=HandlerId('id1'))
    assert data is None