예제 #1
0
def test_rule_from_volume_with_claim(
    deltas_annotation_key,
    fx_deltas,
    volume_zone_label,
    provisioner_annotation,
):
    ctx = Context({'deltas_annotation_key': deltas_annotation_key})

    pv, pvc = make_volume_and_claim(
        ctx=ctx,
        volume_annotations=provisioner_annotation,
        claim_annotations={
            ctx.config['deltas_annotation_key']: fx_deltas,
        },
        volume_zone_label=volume_zone_label,
    )

    if pvc.namespace and not pvc.namespace == 'default':
        expected_rule_name = f'pvc-{pvc.namespace}-{pvc.name}'
    else:
        expected_rule_name = f'pvc-{pvc.name}'

    loop = asyncio.get_event_loop()
    with mock_kube([pv, pvc]) as _mocked:
        deltas_annotation_key = ctx.config['deltas_annotation_key']
        rule = loop.run_until_complete(
            rule_from_pv(ctx, pv, deltas_annotation_key))
        assert rule.source == pvc.obj['metadata']['selfLink']
        assert deltas_annotation_key in pvc.annotations
        assert rule.name == expected_rule_name
        assert rule.deltas == parse_deltas(fx_deltas)
예제 #2
0
def test_rule_name_from_pv(
    fx_context,
    fx_volume_zone_label,
    fx_volume_annotation_provisioned_by,
    fx_annotation_deltas,
):
    volume_name = 'source-pv'

    annotations = {}
    annotations.update(fx_volume_annotation_provisioned_by)
    annotations.update(fx_annotation_deltas)

    source_pv = make_resource(
        pykube.objects.PersistentVolume,
        volume_name,
        annotations=annotations,
        labels=fx_volume_zone_label,
        spec={'gcePersistentDisk': {
            'pdName': 'source-pd'
        }})

    expected_rule_name = f'pv-{volume_name}'

    loop = asyncio.get_event_loop()
    with mock_kube([source_pv]):

        async def _run():
            rule = await rule_from_pv(ctx=fx_context,
                                      volume=source_pv,
                                      deltas_annotation_key=fx_context.
                                      config['deltas_annotation_key'])

            assert rule.name == expected_rule_name

        loop.run_until_complete(_run())
예제 #3
0
def test_mock_kube(fx_context):
    n_resources = 5
    volume_names = [f'test-volume-{i}' for i in range(0, n_resources)]

    def _volume(name, namespace='default'):
        return pykube.objects.PersistentVolume(
            fx_context.kube_client(), {
                'apiVersion': 'v1',
                'kind': 'PersistentVolume',
                'metadata': {
                    'name': name,
                },
            })

    resources = [_volume(volume_name) for volume_name in volume_names]

    with mock_kube(resources) as _kube:
        for expected_resource, volume_name in zip(resources, volume_names):
            assert expected_resource.name == volume_name, \
                'Resources was not ceated properly'
            kube_resource = kube.get_resource_or_none_sync(
                fx_context.kube_client(),
                pykube.objects.PersistentVolume,
                name=volume_name)
            assert kube_resource == expected_resource

            assert len(kube_resource.name)
예제 #4
0
def test_volume_from_resource(
    fx_context,
    resource,
    resources,
    expected_volume_index,
):
    loop = asyncio.get_event_loop()

    with mock_kube(resources):
        result = loop.run_until_complete(
            volume_from_pvc(
                ctx=fx_context,
                resource=resource,
            ))

        if expected_volume_index is not None:
            assert result == resources[expected_volume_index]
예제 #5
0
def test_rule_name_from_pvc(fx_context, claim_namespace):
    claim_name = 'source-pvc'

    source_pv = make_resource(pykube.objects.PersistentVolume,
                              'source-pv',
                              annotations=ANNOTATION_PROVISIONED_BY,
                              labels=LABEL_ZONE,
                              spec={
                                  'claimRef': {
                                      'name': claim_name,
                                      'namespace': claim_namespace,
                                  },
                                  'gcePersistentDisk': {
                                      'pdName': 'source-pd',
                                  }
                              })

    source_pvc = make_resource(
        pykube.objects.PersistentVolumeClaim,
        claim_name,
        namespace=claim_namespace,
        annotations={fx_context.config['deltas_annotation_key']: 'PT1M PT2M'})

    resources = [source_pv, source_pvc]

    if claim_namespace == 'default':
        expected_rule_name = f'pvc-{claim_name}'
    else:
        expected_rule_name = f'{claim_namespace}-pvc-{claim_name}'

    loop = asyncio.get_event_loop()
    with mock_kube(resources):

        async def _run():
            rule = await rule_from_pv(ctx=fx_context,
                                      volume=source_pv,
                                      deltas_annotation_key=fx_context.
                                      config['deltas_annotation_key'])

            assert rule.name == expected_rule_name

        loop.run_until_complete(_run())
예제 #6
0
def test_rule_from_volume(fx_context: Context, fx_deltas: str,
                          label_zone: Optional[Dict[str, str]],
                          annotation_provisioned_by: Optional[Dict[str, str]],
                          _spec_gce_persistent_disk: Optional[Dict[str, Any]]):
    annotations = {}

    annotations.update({
        fx_context.config['deltas_annotation_key']: fx_deltas,
    })

    if annotation_provisioned_by is not None:
        annotations.update(annotation_provisioned_by)

    labels = {}

    if label_zone is not None:
        labels.update(label_zone)

    spec = {}

    if _spec_gce_persistent_disk is not None:
        spec.update(_spec_gce_persistent_disk)

    source_pv = make_resource(
        pykube.objects.PersistentVolume,
        'source-pv',
        annotations=annotations,
        labels=labels,
        spec=spec,
    )

    expected_rule_name = f'pv-{source_pv.name}'

    loop = asyncio.get_event_loop()

    with mock_kube([source_pv]):
        rule: Rule = loop.run_until_complete(
            rule_from_pv(fx_context, source_pv,
                         fx_context.config['deltas_annotation_key']))
        assert rule.name == expected_rule_name
        assert rule.deltas == parse_deltas(fx_deltas)