def virtual_machine_image_resource(
    release_manifest,
    cicd_cfg,
):
    resource_type = 'virtual_machine_image'
    image_file_suffix = glci.util.virtual_image_artifact_for_platform(
        release_manifest.platform)
    image_file_path = release_manifest.path_by_suffix(image_file_suffix)

    bucket_name = cicd_cfg.build.s3_bucket_name

    labels = [
        cm.Label(name='gardener.cloud/gardenlinux/ci/build-metadata',
                 value={
                     'modifiers':
                     release_manifest.modifiers,
                     'buildTimestamp':
                     release_manifest.build_timestamp,
                     'debianPackages': [
                         p for p in _virtual_image_packages(
                             release_manifest, cicd_cfg)
                     ],
                 }),
    ]

    if (published_image_metadata := release_manifest.published_image_metadata):
        labels.append(
            cm.Label(
                name='gardener.cloud/gardenlinux/ci/published-image-metadata',
                value=published_image_metadata,
            ), )
Exemple #2
0
def test_diff_label():
    label_foo = cm.Label(name='foo', value='bar v1')

    left_labels = [label_foo]
    right_labels = [label_foo]

    # check identical label in both lists
    label_diff = cnudie.util.diff_labels(left_labels=left_labels,
                                         right_labels=right_labels)
    assert len(label_diff.label_pairs_changed) == 0
    assert len(label_diff.labels_only_left) == 0
    assert len(label_diff.labels_only_right) == 0

    # check left exclusive label
    label_only_left = cm.Label(name='left', value='only')
    left_labels.append(label_only_left)
    label_diff = cnudie.util.diff_labels(left_labels=left_labels,
                                         right_labels=right_labels)
    assert len(label_diff.label_pairs_changed) == 0
    assert len(label_diff.labels_only_left) == 1
    assert label_diff.labels_only_left[0] == label_only_left
    assert len(label_diff.labels_only_right) == 0

    # check right exclusive label
    label_only_right = cm.Label(name='right', value='only')
    right_labels.append(label_only_right)
    label_diff = cnudie.util.diff_labels(left_labels=left_labels,
                                         right_labels=right_labels)
    assert len(label_diff.label_pairs_changed) == 0
    assert len(label_diff.labels_only_left) == 1
    assert len(label_diff.labels_only_right) == 1
    assert label_diff.labels_only_right[0] == label_only_right

    # check removal of one label
    right_labels.remove(label_foo)
    label_diff = cnudie.util.diff_labels(left_labels=left_labels,
                                         right_labels=right_labels)
    assert len(label_diff.label_pairs_changed) == 0
    assert len(label_diff.labels_only_left) == 2
    assert label_diff.labels_only_left[0] == label_foo
    assert len(label_diff.labels_only_right) == 1

    # check different label value with the same name
    label_foo_updated = cm.Label(name='foo', value='bar v2')
    right_labels.append(label_foo_updated)
    label_diff = cnudie.util.diff_labels(left_labels=left_labels,
                                         right_labels=right_labels)
    assert len(label_diff.label_pairs_changed) == 1
    assert label_diff.label_pairs_changed[0] == (label_foo, label_foo_updated)
    assert len(label_diff.labels_only_left) == 1
    assert len(label_diff.labels_only_right) == 1

    # check that duplicate label name in one list cause exception
    right_labels.append(label_foo_updated)
    with pytest.raises(RuntimeError) as re:
        label_diff = cnudie.util.diff_labels(left_labels=left_labels,
                                             right_labels=right_labels)
    assert re != None
def oci_image_resources(
    releases: typing.List[glci.model.OnlineReleaseManifest],
    effective_version: str,
):
    for release_manifest in releases:
        if (release_manifest.platform == 'oci'
                and (meta := release_manifest.published_image_metadata)):
            yield cm.Resource(
                name='gardenlinux',
                version=effective_version,
                type=cm.ResourceType.OCI_IMAGE,
                relation=cm.ResourceRelation.LOCAL,
                access=cm.OciAccess(
                    type=cm.AccessType.OCI_REGISTRY,
                    imageReference=meta.image_reference,
                ),
                labels=[
                    cm.Label(
                        name='gardener.cloud/gardenlinux/ci/build-metadata',
                        value={
                            'modifiers': release_manifest.modifiers,
                        }),
                ])
Exemple #4
0
def test_label_usage():
    component_name = 'c'
    component_version = '1.2.3'
    sources = [
        cm.ComponentSource(
            name='repo_aux_source',
            access=cm.GithubAccess(type=cm.AccessType.GITHUB,
                                   ref='refs/heads/master',
                                   repoUrl='github.com/otherOrg/otherRepo'),
            labels=[
                cm.Label(
                    name='cloud.gardener/cicd/source',
                    value={'repository-classification': 'auxiliary'},
                ),
            ],
        ),
        cm.ComponentSource(
            name='repo_main_source',
            access=cm.GithubAccess(type=cm.AccessType.GITHUB,
                                   ref='refs/heads/master',
                                   repoUrl='github.com/org/repo'),
            labels=[
                cm.Label(
                    name='cloud.gardener/cicd/source',
                    value={'repository-classification': 'main'},
                ),
            ],
        ),
    ]
    component_with_source_label = cm.Component(
        name=component_name,
        version=component_version,
        sources=sources,
        componentReferences=[],
        labels=[],
        repositoryContexts=[
            cm.RepositoryContext(
                baseUrl=
                'eu.gcr.io/sap-se-gcr-k8s-private/cnudie/gardener/landscapes',
                type='ociRegistry',
            ),
        ],
        resources=[],
        provider=[],
    )

    main_source = cnudie.util.determine_main_source_for_component(
        component_with_source_label, )
    assert main_source.labels[0].value == {'repository-classification': 'main'}
    assert main_source.name == 'repo_main_source'

    component_without_source_label = cm.Component(
        name=component_name,
        version=component_version,
        sources=[
            cm.ComponentSource(
                name='repo_main_source',
                access=cm.GithubAccess(type=cm.AccessType.GITHUB,
                                       ref='refs/heads/master',
                                       repoUrl='github.com/org/repo'),
            ),
            cm.ComponentSource(
                name='repo_aux_source',
                access=cm.GithubAccess(
                    type=cm.AccessType.GITHUB,
                    ref='refs/heads/master',
                    repoUrl='github.com/otherOrg/otherRepo'),
            ),
        ],
        componentReferences=[],
        labels=[],
        repositoryContexts=[
            cm.RepositoryContext(
                baseUrl=
                'eu.gcr.io/sap-se-gcr-k8s-private/cnudie/gardener/landscapes',
                type='ociRegistry',
            ),
        ],
        resources=[],
        provider=[],
    )

    main_source = cnudie.util.determine_main_source_for_component(
        component_without_source_label)

    assert main_source.name == 'repo_main_source'
Exemple #5
0
def test_set_label():
    lssd_label_name = 'cloud.gardener.cnudie/sdo/lssd'
    processing_rule_name = 'test-processing-rule'

    @dataclasses.dataclass
    class TestCase(unittest.TestCase):
        name: str
        input_labels: typing.List[cm.Label]
        label_to_set: cm.Label
        raise_if_present: bool
        expected_labels: typing.List[cm.Label]
        expected_err_msg: str

    testcases = [
        TestCase(
            name='appends label to empty input_labels list with raise_if_present == True',
            input_labels=[],
            label_to_set=cm.Label(
                name=lssd_label_name,
                value={
                    'processingRules': [
                        processing_rule_name,
                    ],
                },
            ),
            raise_if_present=True,
            expected_labels=[
                cm.Label(
                    name=lssd_label_name,
                    value={
                        'processingRules': [
                            processing_rule_name,
                        ],
                    },
                ),
            ],
            expected_err_msg=''
        ),
        TestCase(
            name='appends label to empty input_labels list with raise_if_present == False',
            input_labels=[],
            label_to_set=cm.Label(
                name=lssd_label_name,
                value={
                    'processingRules': [
                        processing_rule_name,
                    ],
                },
            ),
            raise_if_present=False,
            expected_labels=[
                cm.Label(
                    name=lssd_label_name,
                    value={
                        'processingRules': [
                            processing_rule_name,
                        ],
                    },
                ),
            ],
            expected_err_msg=''
        ),
        TestCase(
            name='throws exception if label exists and raise_if_present == True',
            input_labels=[
                cm.Label(
                    name=lssd_label_name,
                    value={
                        'processingRules': [
                            'first-pipeline',
                        ],
                    },
                ),
            ],
            label_to_set=cm.Label(
                name=lssd_label_name,
                value={
                    'processingRules': [
                        processing_rule_name,
                    ],
                },
            ),
            raise_if_present=True,
            expected_labels=None,
            expected_err_msg=f'label {lssd_label_name} is already present'
        ),
        TestCase(
            name='throws no exception if label exists and raise_if_present == False',
            input_labels=[
                cm.Label(
                    name='test-label',
                    value='test-val',
                ),
                cm.Label(
                    name=lssd_label_name,
                    value={
                        'processingRules': [
                            'first-pipeline',
                        ],
                        'otherOperations': 'test',
                    },
                ),
            ],
            label_to_set=cm.Label(
                name=lssd_label_name,
                value={
                    'processingRules': [
                        processing_rule_name,
                    ],
                },
            ),
            raise_if_present=False,
            expected_labels=[
                cm.Label(
                    name='test-label',
                    value='test-val',
                ),
                cm.Label(
                    name=lssd_label_name,
                    value={
                        'processingRules': [
                            processing_rule_name,
                        ],
                    },
                ),
            ],
            expected_err_msg='',
        ),
    ]

    for testcase in testcases:
        test_resource = cm.Resource(
            name='test-resource',
            version='v0.1.0',
            type=cm.ResourceType.OCI_IMAGE,
            access=cm.OciAccess(
                cm.AccessType.OCI_REGISTRY,
                imageReference='test-repo.com/test-resource:v0.1.0',
            ),
            labels=testcase.input_labels,
        )

        if testcase.expected_err_msg != '':
            with testcase.assertRaises(ValueError) as ctx:
                patched_resource = test_resource.set_label(
                    label=testcase.label_to_set,
                    raise_if_present=testcase.raise_if_present,
                )
            assert str(ctx.exception) == testcase.expected_err_msg
        else:
            patched_resource = test_resource.set_label(
                label=testcase.label_to_set,
                raise_if_present=testcase.raise_if_present,
            )
            testcase.assertListEqual(
                list1=patched_resource.labels,
                list2=testcase.expected_labels,
            )