Beispiel #1
0
def test_repurposed_not_affecting_the_existing_handlers_from_storage(storage, handler, reason):
    body = {'status': {'kopf': {'progress': {'some-id': {'purpose': None}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    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
Beispiel #2
0
def test_set_awake_time(storage, handler, expected, body, delay):
    patch = Patch()
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    state = state.with_outcomes(outcomes={handler.id: Outcome(final=False, delay=delay)})
    state.store(patch=patch, body=Body(body), storage=storage)
    assert patch['status']['kopf']['progress']['some-id'].get('delayed') == expected
Beispiel #3
0
def test_issue_601_deletion_supersedes_other_processing(storage, reason):

    body = {'status': {'kopf': {'progress': {
        'fn1': {'purpose': reason.value, 'failure': True},
        'fn2': {'purpose': reason.value, 'success': True},
        'fn3': {'purpose': reason.value, 'delayed': TS1_ISO},
    }}}}
    create_handler1 = Mock(id='fn1', spec_set=['id'])
    create_handler2 = Mock(id='fn2', spec_set=['id'])
    create_handler3 = Mock(id='fn3', spec_set=['id'])
    delete_handler9 = Mock(id='delete_fn', spec_set=['id'])
    owned_handlers = [create_handler1, create_handler2, create_handler3, delete_handler9]
    cause_handlers = [delete_handler9]

    state = State.from_storage(body=Body(body), handlers=owned_handlers, storage=storage)
    state = state.with_purpose(Reason.DELETE)
    state = state.with_handlers(cause_handlers)

    assert len(state) == 4
    assert state.extras == {reason: StateCounters(success=1, failure=1, running=1)}
    assert state.counts == StateCounters(success=0, failure=0, running=1)
    assert state.done == False
    assert state.delays == [0.0]

    state = state.with_outcomes({'delete_fn': Outcome(final=True)})

    assert state.extras == {reason: StateCounters(success=1, failure=1, running=1)}
    assert state.counts == StateCounters(success=1, failure=0, running=0)
    assert state.done == True
    assert state.delays == []
Beispiel #4
0
def test_started_from_storage_is_preferred_over_from_scratch(storage, handler, body, expected):
    with freezegun.freeze_time(TS0):
        state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    with freezegun.freeze_time(TS1):
        state = state.with_handlers([handler])
    patch = Patch()
    state.store(body=Body({}), patch=patch, storage=storage)
    assert patch['status']['kopf']['progress']['some-id']['started'] == expected
Beispiel #5
0
def test_created_empty_from_filled_storage_without_handlers(storage, handler, body):
    state = State.from_storage(body=Body(body), handlers=[], storage=storage)
    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 == {}
Beispiel #6
0
def test_subrefs_ignored_when_not_specified(storage, handler):
    body = {}
    patch = Patch()
    outcome = Outcome(final=True, subrefs=[])
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    state = state.with_outcomes(outcomes={handler.id: outcome})
    state.store(patch=patch, body=Body(body), storage=storage)
    assert patch['status']['kopf']['progress']['some-id']['subrefs'] is None
Beispiel #7
0
def test_with_handlers_relevant_to_the_purpose(
        storage, handler, body, expected_counts, expected_done, expected_delays, reason):
    body['status']['kopf']['progress']['some-id']['purpose'] = reason.value
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_purpose(reason).with_handlers([handler])
    assert len(state) == 1
    assert state.extras == {}
    assert state.counts == expected_counts
    assert state.done == expected_done
    assert state.delays == expected_delays
Beispiel #8
0
def test_with_handlers_irrelevant_to_the_purpose(
        storage, handler, body, expected_extras, stored_reason, processed_reason):
    body['status']['kopf']['progress']['some-id']['purpose'] = stored_reason.value
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_purpose(processed_reason)
    assert len(state) == 1
    assert state.extras[stored_reason] == expected_extras
    assert state.counts == StateCounters(success=0, failure=0, running=0)
    assert state.done == True
    assert state.delays == []
Beispiel #9
0
def test_subrefs_added_to_preexisting_subrefs(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {'subrefs': ['sub9/2', 'sub9/1']}}}}}
    patch = Patch()
    outcome_subrefs = ['sub2/b', 'sub2/a', 'sub2', 'sub1', 'sub3']
    expected_subrefs = ['sub1', 'sub2', 'sub2/a', 'sub2/b', 'sub3', 'sub9/1', 'sub9/2']
    outcome = Outcome(final=True, subrefs=outcome_subrefs)
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    state = state.with_outcomes(outcomes={handler.id: outcome})
    state.store(patch=patch, body=Body(body), storage=storage)
    assert patch['status']['kopf']['progress']['some-id']['subrefs'] == expected_subrefs
Beispiel #10
0
def test_store_success(storage, handler, expected_retries, expected_stopped, body):
    patch = Patch()
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    state = state.with_outcomes(outcomes={handler.id: Outcome(final=True)})
    state.store(patch=patch, body=Body(body), storage=storage)
    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
Beispiel #11
0
def test_purge_progress_cascades_to_subrefs(storage, handler):
    body = {'status': {'kopf': {'progress': {
        'some-id': {'subrefs': ['sub1', 'sub2', 'sub3']},
        'sub1': {},
        'sub2': {},
        # 'sub3' is intentionally absent -- should not be purged as already non-existent.
        'sub-unrelated': {},  # should be ignored, as not related to the involved handlers.
    }}}}
    patch = Patch()
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state.purge(patch=patch, body=Body(body), storage=storage, handlers=[handler])
    assert patch == {'status': {'kopf': {'progress': {
        'some-id': None,
        'sub1': None,
        'sub2': None,
    }}}}
Beispiel #12
0
def test_passive_states_are_not_used_in_done_calculation(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    assert len(state) == 1
    assert state.done is True  # because the unfinished handler state is ignored
Beispiel #13
0
def test_get_retry_count(storage, handler, expected, body):
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    result = state[handler.id].retries
    assert result == expected
Beispiel #14
0
def test_awakening_time(storage, handler, expected, body):
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    result = state[handler.id].delayed
    assert result == expected
Beispiel #15
0
def test_passed_through_with_outcomes_when_passive(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {'purpose': None}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_outcomes({'some-id': Outcome(final=True)})
    assert len(state) == 1
    assert state['some-id'].active is False
Beispiel #16
0
def test_purge_progress_when_already_empty_in_body_but_not_in_patch(storage, handler):
    body = {}
    patch = Patch({'status': {'kopf': {'progress': {'some-id': {'retries': 5}}}}})
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state.purge(patch=patch, body=Body(body), storage=storage, handlers=[handler])
    assert not patch
Beispiel #17
0
def test_active_states_are_used_in_done_calculation(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    assert len(state) == 1
    assert state.done is False
Beispiel #18
0
def test_created_from_purposeful_storage(storage, handler, reason):
    body = {'status': {'kopf': {'progress': {'some-id': {'purpose': reason.value}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    assert len(state) == 1
    assert state.purpose is None
    assert state['some-id'].purpose == reason
Beispiel #19
0
def test_active_states_are_used_in_delays_calculation(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {'delayed': TS1_ISO}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    assert len(state) == 1
    assert state.delays == [1.0]
Beispiel #20
0
def test_purge_progress_when_already_empty_in_body_and_patch(storage, handler):
    body = {}
    patch = Patch()
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state.purge(patch=patch, body=Body(body), storage=storage, handlers=[handler])
    assert not patch
Beispiel #21
0
def test_started_from_storage(storage, handler, body, expected):
    patch = Patch()
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state.store(body=Body({}), patch=patch, storage=storage)
    assert patch['status']['kopf']['progress']['some-id']['started'] == expected
Beispiel #22
0
def test_purge_progress_when_known_at_purge_only(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {'retries': 5}}}}}
    patch = Patch()
    state = State.from_storage(body=Body(body), handlers=[], storage=storage)
    state.purge(patch=patch, body=Body(body), storage=storage, handlers=[handler])
    assert patch == {'status': {'kopf': {'progress': {'some-id': None}}}}
Beispiel #23
0
def test_runtime(storage, handler, expected, body):
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    result = state[handler.id].runtime
    assert result == expected
Beispiel #24
0
def test_original_body_is_not_modified_by_storing(storage, handler):
    body = Body({})
    patch = Patch()
    state = State.from_storage(body=body, handlers=[handler], storage=storage)
    state.store(body=body, patch=patch, storage=storage)
    assert dict(body) == {}
Beispiel #25
0
def test_sleeping_flag(storage, handler, expected, body):
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    result = state[handler.id].sleeping
    assert result == expected
Beispiel #26
0
def test_created_from_storage_as_passive(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    assert len(state) == 1
    assert state['some-id'].active is False
Beispiel #27
0
def test_switched_from_passive_to_active(storage, handler):
    body = {'status': {'kopf': {'progress': {'some-id': {'purpose': None}}}}}
    state = State.from_storage(body=Body(body), handlers=[handler], storage=storage)
    state = state.with_handlers([handler])
    assert len(state) == 1
    assert state['some-id'].active is True