def test_purge_progress_when_already_empty_in_body_but_not_in__patch(handler): body = {} patch = {'status': {'kopf': {'progress': {'some-id': {'retries': 5}}}}} origbody = copy.deepcopy(body) state = State.from_body(body=body, handlers=[handler]) state.purge(patch=patch, body=body) assert not patch assert body == origbody # not modified
def test_purge_progress_when_exists_in_body(handler): body = {'status': {'kopf': {'progress': {'some-id': {'retries': 5}}}}} patch = {} origbody = copy.deepcopy(body) state = State.from_body(body=body, handlers=[handler]) state.purge(patch=patch, body=body) assert patch == {'status': {'kopf': {'progress': None}}} assert body == origbody # not modified
def test_purge_progress_when_already_empty_in_body_and_patch(handler): body = {} patch = {} origbody = copy.deepcopy(body) state = State.from_body(body=body, handlers=[handler]) state.purge(patch=patch, body=body) assert not patch assert body == origbody # not modified
def test_always_started_when_created_from_body(handler, body, expected): origbody = copy.deepcopy(body) patch = {} state = State.from_body(body=body, handlers=[handler]) state.store(patch=patch) assert patch['status']['kopf']['progress']['some-id'][ 'started'] == expected assert body == origbody # not modified
def test_set_awake_time(handler, expected, body, delay): origbody = copy.deepcopy(body) patch = {} state = State.from_body(body=body, handlers=[handler]) state = state.with_outcomes( outcomes={handler.id: HandlerOutcome(final=False, delay=delay)}) state.store(patch=patch) assert patch['status']['kopf']['progress']['some-id'].get( 'delayed') == expected assert body == origbody # not modified
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_store_success(handler, expected_retries, expected_stopped, body): origbody = copy.deepcopy(body) patch = {} state = State.from_body(body=body, handlers=[handler]) state = state.with_outcomes( outcomes={handler.id: HandlerOutcome(final=True)}) state.store(patch=patch) assert patch['status']['kopf']['progress']['some-id']['success'] is True assert patch['status']['kopf']['progress']['some-id']['failure'] is False assert patch['status']['kopf']['progress']['some-id'][ 'retries'] == expected_retries assert patch['status']['kopf']['progress']['some-id'][ 'stopped'] == expected_stopped assert patch['status']['kopf']['progress']['some-id']['message'] is None assert body == origbody # not modified
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_get_retry_count(handler, expected, body): origbody = copy.deepcopy(body) state = State.from_body(body=body, handlers=[handler]) result = state[handler.id].retries assert result == expected assert body == origbody # not modified
def test_awakening_time(handler, expected, body): origbody = copy.deepcopy(body) state = State.from_body(body=body, handlers=[handler]) result = state[handler.id].delayed assert result == expected assert body == origbody # not modified
def test_sleeping_flag(handler, expected, body): origbody = copy.deepcopy(body) state = State.from_body(body=body, handlers=[handler]) result = state[handler.id].sleeping assert result == expected assert body == origbody # not modified
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