def test_asap_takes_the_first_one_when_no_retries(mocker): handler1 = mocker.Mock(id='id1', spec_set=['id']) handler2 = mocker.Mock(id='id2', spec_set=['id']) handler3 = mocker.Mock(id='id3', spec_set=['id']) state = State.from_scratch(handlers=[handler1, handler2, handler3]) handlers = [handler1, handler2, handler3] selected = kopf.lifecycles.asap(handlers, state=state) assert isinstance(selected, (tuple, list)) assert len(selected) == 1 assert selected[0] is handler1
def test_asap_takes_the_least_retried(mocker): handler1 = mocker.Mock(id='id1', spec_set=['id']) handler2 = mocker.Mock(id='id2', spec_set=['id']) handler3 = mocker.Mock(id='id3', spec_set=['id']) # Set the pre-existing state, and verify that it was set properly. state = State.from_scratch(handlers=[handler1, handler2, handler3]) state = state.with_outcomes({handler1.id: HandlerOutcome(final=False)}) state = state.with_outcomes({handler1.id: HandlerOutcome(final=False)}) state = state.with_outcomes({handler3.id: HandlerOutcome(final=False)}) assert state[handler1.id].retries == 2 assert state[handler2.id].retries == 0 assert state[handler3.id].retries == 1 handlers = [handler1, handler2, handler3] selected = kopf.lifecycles.asap(handlers, state=state) assert isinstance(selected, (tuple, list)) assert len(selected) == 1 assert selected[0] is handler2
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=ObjectDict(), 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_always_started_when_created_from_scratch(handler): patch = {} state = State.from_scratch(handlers=[handler]) state.store(patch=patch) assert patch['status']['kopf']['progress']['some-id']['started'] == TS0_ISO
def test_with_empty_input(lifecycle): state = State.from_scratch(handlers=[]) handlers = [] selected = lifecycle(handlers, state=state) assert isinstance(selected, (tuple, list)) assert len(selected) == 0