예제 #1
0
async def test_temporary_failures_with_expired_delays_are_reindexed(
        resource, settings, registry, memories, indexers, index, caplog,
        event_type, handlers):
    caplog.set_level(logging.DEBUG)
    body = {'metadata': {'namespace': 'ns1', 'name': 'name1'}}
    delayed = datetime.datetime(2020, 12, 31, 23, 59, 59, 0)
    memory = await memories.recall(raw_body=body)
    memory.indexing_memory.indexing_state = State(
        {'index_fn': HandlerState(delayed=delayed)})
    await process_resource_event(
        lifecycle=all_at_once,
        registry=registry,
        settings=settings,
        resource=resource,
        indexers=indexers,
        memories=memories,
        memobase=Memo(),
        raw_event={
            'type': event_type,
            'object': body
        },
        event_queue=asyncio.Queue(),
        resource_indexed=Toggle(),  # used! only to enable indexing.
    )
    assert handlers.index_mock.call_count == 1
예제 #2
0
async def test_permanent_failures_are_not_reindexed(resource, settings,
                                                    registry, memories,
                                                    indexers, index, caplog,
                                                    event_type, handlers):
    caplog.set_level(logging.DEBUG)
    body = {'metadata': {'namespace': 'ns1', 'name': 'name1'}}
    memory = await memories.recall(raw_body=body)
    memory.indexing_memory.indexing_state = State(
        {'index_fn': HandlerState(failure=True)})
    await process_resource_event(
        lifecycle=all_at_once,
        registry=registry,
        settings=settings,
        resource=resource,
        indexers=indexers,
        memories=memories,
        memobase=Memo(),
        raw_event={
            'type': event_type,
            'object': body
        },
        event_queue=asyncio.Queue(),
        resource_indexed=Toggle(),  # used! only to enable indexing.
    )
    assert handlers.index_mock.call_count == 0
예제 #3
0
async def test_successes_are_removed_from_the_indexing_state(
        resource, settings, registry, memories, indexers, caplog, event_type,
        handlers):
    caplog.set_level(logging.DEBUG)
    body = {'metadata': {'namespace': 'ns1', 'name': 'name1'}}
    memory = await memories.recall(raw_body=body)
    memory.indexing_memory.indexing_state = State(
        {'unrelated': HandlerState(success=True)})
    handlers.index_mock.side_effect = 123
    await process_resource_event(
        lifecycle=all_at_once,
        registry=registry,
        settings=settings,
        resource=resource,
        indexers=indexers,
        memories=memories,
        memobase=Memo(),
        raw_event={
            'type': event_type,
            'object': body
        },
        event_queue=asyncio.Queue(),
        resource_indexed=Toggle(),  # used! only to enable indexing.
    )
    assert handlers.index_mock.call_count == 1
    assert memory.indexing_memory.indexing_state is None
예제 #4
0
async def test_delayed_handlers_sleep(registry, settings, handlers, resource,
                                      cause_mock, cause_reason, caplog,
                                      assert_logs, k8s_mocked, now,
                                      delayed_iso, delay):
    caplog.set_level(logging.DEBUG)

    # Simulate the original persisted state of the resource.
    # Make sure the finalizer is added since there are mandatory deletion handlers.
    started_dt = datetime.datetime.fromisoformat(
        '2000-01-01T00:00:00')  # long time ago is fine.
    delayed_dt = datetime.datetime.fromisoformat(delayed_iso)
    event_type = None if cause_reason == Reason.RESUME else 'irrelevant'
    event_body = {
        'metadata': {
            'finalizers': [settings.persistence.finalizer]
        },
        'status': {
            'kopf': {
                'progress': {
                    'create_fn':
                    HandlerState(started=started_dt,
                                 delayed=delayed_dt).as_in_storage(),
                    'update_fn':
                    HandlerState(started=started_dt,
                                 delayed=delayed_dt).as_in_storage(),
                    'delete_fn':
                    HandlerState(started=started_dt,
                                 delayed=delayed_dt).as_in_storage(),
                    'resume_fn':
                    HandlerState(started=started_dt,
                                 delayed=delayed_dt).as_in_storage(),
                }
            }
        }
    }
    cause_mock.reason = cause_reason

    with freezegun.freeze_time(now):
        await process_resource_event(
            lifecycle=kopf.lifecycles.all_at_once,
            registry=registry,
            settings=settings,
            resource=resource,
            indexers=OperatorIndexers(),
            memories=ResourceMemories(),
            memobase=Memo(),
            raw_event={
                'type': event_type,
                'object': event_body
            },
            event_queue=asyncio.Queue(),
        )

    assert not handlers.create_mock.called
    assert not handlers.update_mock.called
    assert not handlers.delete_mock.called
    assert not handlers.resume_mock.called

    # The dummy patch is needed to trigger the further changes. The value is irrelevant.
    assert k8s_mocked.patch.called
    assert 'dummy' in k8s_mocked.patch.call_args_list[-1][1]['payload'][
        'status']['kopf']

    # The duration of sleep should be as expected.
    assert k8s_mocked.sleep.called
    assert k8s_mocked.sleep.call_args_list[0][0][0] == delay

    assert_logs([
        r"Sleeping for ([\d\.]+|[\d\.]+ \(capped [\d\.]+\)) seconds",
    ])