def test_created_empty_from_scratch(): state = State.from_scratch() assert len(state) == 0 assert state.purpose is None assert state.done == True assert state.delay is None assert state.delays == [] assert state.counts == StateCounters(success=0, failure=0, running=0) assert state.extras == {}
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().with_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().with_handlers([handler1, handler2, handler3]) state = state.with_outcomes({handler1.id: Outcome(final=False)}) state = state.with_outcomes({handler1.id: Outcome(final=False)}) state = state.with_outcomes({handler3.id: Outcome(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
def test_created_from_handlers_as_active(storage, handler): state = State.from_scratch() state = state.with_handlers([handler]) assert len(state) == 1 assert state['some-id'].active is True
def test_started_from_scratch(storage, handler): patch = Patch() state = State.from_scratch() state = state.with_handlers([handler]) state.store(body=Body({}), patch=patch, storage=storage) assert patch['status']['kopf']['progress']['some-id']['started'] == TS0_ISO
def test_repurposed_not_affecting_the_existing_handlers_from_scratch(handler, reason): state = State.from_scratch() state = state.with_handlers([handler]).with_purpose(reason).with_handlers([handler]) assert len(state) == 1 assert state.purpose == reason assert state['some-id'].purpose is None
def test_repurposed_with_handlers(handler, reason): state = State.from_scratch() state = state.with_handlers([handler]).with_purpose(reason, handlers=[handler]) assert len(state) == 1 assert state.purpose == reason assert state['some-id'].purpose == reason
def test_enriched_with_outcomes_keeps_the_original_purpose(reason): state = State.from_scratch() state = state.with_purpose(reason) state = state.with_outcomes({}) assert state.purpose == reason
def test_enriched_with_handlers_keeps_the_original_purpose(handler, reason): state = State.from_scratch() state = state.with_purpose(reason) state = state.with_handlers([handler]) assert state.purpose == reason
def test_passed_through_with_outcomes_when_active(storage, handler): state = State.from_scratch() state = state.with_handlers([handler]) state = state.with_outcomes({'some-id': Outcome(final=True)}) assert len(state) == 1 assert state['some-id'].active is True
def test_with_empty_input(lifecycle): state = State.from_scratch() handlers = [] selected = lifecycle(handlers, state=state) assert isinstance(selected, (tuple, list)) assert len(selected) == 0