예제 #1
0
async def test_empty_stream_yields_nothing(resource, stream):
    stream.return_value = iter([])

    events = []
    async for event in streaming_watch(resource=resource, namespace=None):
        events.append(event)

    assert len(events) == 0
예제 #2
0
async def test_exception_escalates(resource, stream):
    stream.side_effect = SampleException()

    events = []
    with pytest.raises(SampleException):
        async for event in streaming_watch(resource=resource, namespace=None):
            events.append(event)

    assert stream.call_count == 1
    assert len(events) == 0
예제 #3
0
async def test_event_stream_yields_everything(resource, stream):
    stream.return_value = iter(STREAM_WITH_NORMAL_EVENTS)

    events = []
    async for event in streaming_watch(resource=resource, namespace=None):
        events.append(event)

    assert len(events) == 2
    assert events[0]['object']['spec'] == 'a'
    assert events[1]['object']['spec'] == 'b'
예제 #4
0
async def test_unknown_error_raises_exception(resource, stream):
    stream.return_value = iter(STREAM_WITH_ERROR_CODE)

    events = []
    with pytest.raises(WatchingError) as e:
        async for event in streaming_watch(resource=resource, namespace=None):
            events.append(event)

    assert stream.call_count == 1
    assert len(events) == 1
    assert events[0]['object']['spec'] == 'a'
    assert '666' in str(e.value)
예제 #5
0
async def test_error_410gone_exits_normally(resource, stream, caplog):
    caplog.set_level(logging.DEBUG)
    stream.return_value = iter(STREAM_WITH_ERROR_410GONE)

    events = []
    async for event in streaming_watch(resource=resource, namespace=None):
        events.append(event)

    assert stream.call_count == 1
    assert len(events) == 1
    assert events[0]['object']['spec'] == 'a'
    assert "Restarting the watch-stream" in caplog.text
예제 #6
0
async def test_unknown_event_type_ignored(resource, stream, caplog):
    caplog.set_level(logging.DEBUG)
    stream.return_value = iter(STREAM_WITH_UNKNOWN_EVENT)

    events = []
    async for event in streaming_watch(resource=resource, namespace=None):
        events.append(event)

    assert stream.call_count == 1
    assert len(events) == 2
    assert events[0]['object']['spec'] == 'a'
    assert events[1]['object']['spec'] == 'b'
    assert "Ignoring an unsupported event type" in caplog.text
    assert "UNKNOWN" in caplog.text
예제 #7
0
async def test_watching_over_cluster(resource, stream_fn, apicls_fn):
    apicls_fn.return_value.list_cluster_custom_object = object()
    stream_fn.return_value = iter(EMPTY_EVENT_STREAM)

    itr = streaming_watch(
        resource=resource,
        namespace=None,
    )

    assert isinstance(itr, collections.abc.AsyncIterator)
    assert isinstance(itr, collections.abc.AsyncGenerator)
    async for _ in itr: pass  # fully deplete it

    assert stream_fn.called
    assert stream_fn.call_count == 1
    assert stream_fn.call_args_list == [
        call(apicls_fn.return_value.list_cluster_custom_object,
             group=resource.group, version=resource.version, plural=resource.plural),
    ]