def test_bodies_for_deletion_via_timestamp(registry):
    b1 = RawBody(metadata={'name': 'ns1'})
    b2 = RawBody(metadata={'name': 'ns1', 'deletionTimestamp': '...'})
    insights = Insights()
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_bodies=[b1])
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_bodies=[b2])
    assert not insights.namespaces
def test_bodies_for_additional_population(registry):
    b1 = RawBody(metadata={'name': 'ns1'})
    b2 = RawBody(metadata={'name': 'ns2'})
    insights = Insights()
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_bodies=[b1])
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_bodies=[b2])
    assert insights.namespaces == {'ns1', 'ns2'}
def test_events_for_additional_population(registry):
    e1 = RawEvent(type=None, object=RawBody(metadata={'name': 'ns1'}))
    e2 = RawEvent(type=None, object=RawBody(metadata={'name': 'ns2'}))
    insights = Insights()
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_events=[e1])
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_events=[e2])
    assert insights.namespaces == {'ns1', 'ns2'}
def test_events_for_deletion_via_event_type(registry):
    e1 = RawEvent(type=None, object=RawBody(metadata={'name': 'ns1'}))
    e2 = RawEvent(type='DELETED', object=RawBody(metadata={'name': 'ns1'}))
    insights = Insights()
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_events=[e1])
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_events=[e2])
    assert not insights.namespaces
Esempio n. 5
0
async def insights(settings, resource):
    val_resource = Resource('admissionregistration.k8s.io', 'v1',
                            'validatingwebhookconfigurations')
    mut_resource = Resource('admissionregistration.k8s.io', 'v1',
                            'mutatingwebhookconfigurations')
    insights = Insights()
    insights.resources.add(resource)
    await insights.backbone.fill(resources=[val_resource, mut_resource])
    insights.ready_resources.set()
    return insights
async def insights(request, settings):
    insights = Insights()
    await insights.backbone.fill(resources=[
        request.getfixturevalue(name) for name in [
            'peering_resource', 'cluster_peering_resource',
            'namespaced_peering_resource'
        ] if name in request.fixturenames
    ])
    settings.peering.mandatory = True
    return insights
Esempio n. 7
0
def test_initial_population(registry):
    r1 = Resource(group='group1',
                  version='version1',
                  plural='plural1',
                  verbs=VERBS)
    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1])
    assert insights.resources == {r1}
Esempio n. 8
0
def test_no_ambiguity_in_generic_selector(registry, decorator, caplog,
                                          assert_logs):
    r1 = Resource(group='g1', version='v1', plural='plural', verbs=VERBS)
    r2 = Resource(group='g2', version='v2', plural='plural', verbs=VERBS)

    @decorator(EVERYTHING)
    def fn(**_):
        ...

    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1, r2])
    assert insights.resources == {r1, r2}
    assert_logs([], prohibited=[r"Ambiguous resources will not be served"])
Esempio n. 9
0
def test_ambiguity_in_specific_selectors(registry, decorator, caplog,
                                         assert_logs):
    r1 = Resource(group='g1', version='v1', plural='plural', verbs=VERBS)
    r2 = Resource(group='g2', version='v2', plural='plural', verbs=VERBS)

    @decorator(plural='plural')
    def fn(**_):
        ...

    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1, r2])
    assert not insights.resources
    assert_logs([r"Ambiguous resources will not be served"])
async def test_initial_listing_is_ignored():
    insights = Insights()
    e1 = RawEvent(type=None, object=RawBody(metadata={'name': 'ns1'}))

    async def delayed_injection(delay: float):
        await asyncio.sleep(delay)
        await process_discovered_namespace_event(insights=insights,
                                                 raw_event=e1,
                                                 namespaces=['ns*'])

    task = asyncio.create_task(delayed_injection(0))
    with pytest.raises(asyncio.TimeoutError):
        async with insights.revised:
            await asyncio.wait_for(insights.revised.wait(), timeout=0.1)
    await task
    assert not insights.namespaces
async def test_followups_for_addition(timer, etype):
    insights = Insights()
    e1 = RawEvent(type=etype, object=RawBody(metadata={'name': 'ns1'}))

    async def delayed_injection(delay: float):
        await asyncio.sleep(delay)
        await process_discovered_namespace_event(insights=insights,
                                                 raw_event=e1,
                                                 namespaces=['ns*'])

    task = asyncio.create_task(delayed_injection(0.1))
    async with timer, async_timeout.timeout(1):
        async with insights.revised:
            await insights.revised.wait()
    await task
    assert 0.1 < timer.seconds < 0.11
    assert insights.namespaces == {'ns1'}
Esempio n. 12
0
def test_corev1_overrides_ambuigity(registry, decorator, caplog, assert_logs):
    r1 = Resource(group='', version='v1', plural='pods', verbs=VERBS)
    r2 = Resource(group='metrics.k8s.io',
                  version='v1',
                  plural='pods',
                  verbs=VERBS)

    @decorator(plural='pods')
    def fn(**_):
        ...

    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1, r2])
    assert insights.resources == {r1}
    assert_logs([], prohibited=[r"Ambiguous resources will not be served"])
Esempio n. 13
0
def test_replacing_a_new_group(registry):
    r1 = Resource(group='group1',
                  version='version1',
                  plural='plural1',
                  verbs=VERBS)
    r2 = Resource(group='group2',
                  version='version2',
                  plural='plural2',
                  verbs=VERBS)
    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1])
    revise_resources(registry=registry,
                     insights=insights,
                     group='group2',
                     resources=[r2])
    assert insights.resources == {r1, r2}
Esempio n. 14
0
async def test_backbone_is_filled(registry, core_mock, corev1_mock, timer,
                                  etype):
    e1 = RawEvent(type=etype, object=RawBody(spec={'group': ''}))
    insights = Insights()

    async def delayed_injection(delay: float):
        await asyncio.sleep(delay)
        await process_discovered_resource_event(insights=insights,
                                                raw_event=e1,
                                                registry=registry)

    task = asyncio.create_task(delayed_injection(0.1))
    async with timer, async_timeout.timeout(1.0):
        await insights.backbone.wait_for(NAMESPACES)
    await task
    assert 0.1 < timer.seconds < 1.0
    assert NAMESPACES in insights.backbone
    assert core_mock.called
    assert corev1_mock.called
Esempio n. 15
0
def test_nonpatchable_excluded(registry, decorator, caplog, assert_logs):
    r1 = Resource(group='group1',
                  version='version1',
                  plural='plural1',
                  verbs=['watch', 'list'])

    @decorator('group1', 'version1', 'plural1')  # because it patches!
    def fn(**_):
        ...

    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1])
    assert not insights.resources
    assert_logs([
        r"Non-patchable resources will not be served: {plural1.version1.group1}"
    ])
async def test_paused_with_mandatory_peering_but_absent_peering_resource(
        settings, ensemble: Ensemble):

    settings.peering.mandatory = True
    insights = Insights()

    await ensemble.peering_missing.turn_to(False)  # prerequisite
    assert ensemble.peering_missing.is_off()  # prerequisite
    assert ensemble.operator_paused.is_off()  # prerequisite

    await adjust_tasks(
        processor=processor,
        identity=Identity('...'),
        settings=settings,
        insights=insights,
        ensemble=ensemble,
    )

    assert ensemble.peering_missing.is_on()
    assert ensemble.operator_paused.is_on()
Esempio n. 17
0
async def test_initial_listing_is_ignored(registry, apis_mock, group1_mock):
    insights = Insights()
    e1 = RawEvent(type=None, object=RawBody(spec={'group': 'group1'}))

    async def delayed_injection(delay: float):
        await asyncio.sleep(delay)
        await process_discovered_resource_event(insights=insights,
                                                raw_event=e1,
                                                registry=registry)

    task = asyncio.create_task(delayed_injection(0))
    with pytest.raises(asyncio.TimeoutError):
        async with async_timeout.timeout(0.1) as timeout:
            async with insights.revised:
                await insights.revised.wait()
    await task
    assert timeout.expired
    assert not insights.resources
    assert not apis_mock.called
    assert not group1_mock.called
Esempio n. 18
0
def test_selectors_with_no_resources(registry, decorator, caplog, assert_logs):
    r1 = Resource(group='group1',
                  version='version1',
                  plural='plural1',
                  verbs=VERBS)
    r2 = Resource(group='group2',
                  version='version2',
                  plural='plural2',
                  verbs=VERBS)

    @decorator(plural='plural3')
    def fn(**_):
        ...

    insights = Insights()
    revise_resources(registry=registry,
                     insights=insights,
                     group=None,
                     resources=[r1, r2])
    assert not insights.resources
    assert_logs([r"Unresolved resources cannot be served"])
Esempio n. 19
0
async def test_followups_for_addition(registry, apis_mock, group1_mock, timer,
                                      etype):
    e1 = RawEvent(type=etype, object=RawBody(spec={'group': 'group1'}))
    r1 = Resource(group='group1', version='version1', plural='plural1')
    insights = Insights()

    async def delayed_injection(delay: float):
        await asyncio.sleep(delay)
        await process_discovered_resource_event(insights=insights,
                                                raw_event=e1,
                                                registry=registry)

    task = asyncio.create_task(delayed_injection(0.1))
    async with timer, async_timeout.timeout(1.0):
        async with insights.revised:
            await insights.revised.wait()
    await task
    assert 0.1 < timer.seconds < 1.0
    assert insights.resources == {r1}
    assert apis_mock.called
    assert group1_mock.called
async def test_no_peering_tasks_with_no_peering_resources(settings, ensemble):

    settings.peering.mandatory = False
    insights = Insights()
    r1 = Resource(group='group1',
                  version='version1',
                  plural='plural1',
                  namespaced=True)
    insights.watched_resources.add(r1)
    insights.namespaces.add('ns1')

    await adjust_tasks(
        processor=processor,
        identity=Identity('...'),
        settings=settings,
        insights=insights,
        ensemble=ensemble,
    )

    assert ensemble.watcher_tasks
    assert not ensemble.peering_tasks
    assert not ensemble.pinging_tasks
    assert not ensemble.conflicts_found
Esempio n. 21
0
async def insights():
    return Insights()
def test_events_ignored_for_mismatching(registry):
    e1 = RawEvent(type=None, object=RawBody(metadata={'name': 'def1'}))
    insights = Insights()
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_events=[e1])
    assert not insights.namespaces
Esempio n. 23
0
async def insights(settings, peering_resource):
    insights = Insights()
    await insights.backbone.fill(resources=[peering_resource])
    settings.peering.mandatory = True
    return insights
def test_bodies_ignored_for_mismatching(registry):
    b1 = RawBody(metadata={'name': 'def1'})
    insights = Insights()
    revise_namespaces(insights=insights, namespaces=['ns*'], raw_bodies=[b1])
    assert not insights.namespaces