示例#1
0
文件: test_build.py 项目: lcarva/iib
def test_handle_add_request(
    mock_vlpc,
    mock_glsp,
    mock_capml,
    mock_srs,
    mock_elp,
    mock_frpb,
    mock_vii,
    mock_pi,
    mock_bi,
    mock_oia,
    mock_prfb,
    mock_vl,
    mock_cleanup,
):
    arches = {'amd64', 's390x'}
    mock_prfb.return_value = {
        'arches': arches,
        'binary_image_resolved': 'binary-image@sha256:abcdef',
        'from_index_resolved': 'from-index@sha256:bcdefg',
    }
    legacy_packages = {'some_package'}
    mock_glsp.return_value = legacy_packages
    output_pull_spec = 'quay.io/namespace/some-image:3'
    mock_capml.return_value = output_pull_spec

    bundles = ['some-bundle:2.3-1']
    cnr_token = 'token'
    organization = 'org'
    build.handle_add_request(
        bundles,
        'binary-image:latest',
        3,
        'from-index:latest',
        ['s390x'],
        cnr_token,
        organization,
    )

    mock_cleanup.assert_called_once()
    mock_vl.assert_called_once()
    mock_prfb.assert_called_once()

    add_args = mock_oia.call_args[0]
    assert bundles in add_args
    mock_oia.assert_called_once()

    assert mock_bi.call_count == len(arches)
    assert mock_pi.call_count == len(arches)

    mock_elp.assert_called_once()
    export_args = mock_elp.call_args[0]
    assert legacy_packages in export_args
    assert cnr_token in export_args
    assert organization in export_args

    mock_frpb.assert_called_once()
    mock_vii.assert_called_once()
    mock_capml.assert_called_once()
    mock_srs.assert_called_once()
示例#2
0
def test_handle_add_request_gating_failure(mock_grb, mock_vl, mock_gb,
                                           mock_srs, mock_srs2, mock_cleanup):
    error_msg = 'Gating failure!'
    mock_gb.side_effect = IIBError(error_msg)
    mock_grb.return_value = ['some-bundle@sha']
    bundles = ['some-bundle:2.3-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    with pytest.raises(IIBError, match=error_msg):
        build.handle_add_request(
            bundles,
            'binary-image:latest',
            3,
            'from-index:latest',
            ['s390x'],
            cnr_token,
            organization,
            None,
            False,
            None,
            None,
            greenwave_config,
        )
    mock_cleanup.assert_called_once_with()
    mock_srs2.assert_called_once()
    mock_vl.assert_called_once()
    mock_gb.assert_called_once_with(['some-bundle@sha'], greenwave_config)
示例#3
0
def test_handle_add_request_backport_failure_no_overwrite(
    mock_srt,
    mock_gmb,
    mock_gpb,
    mock_aolti,
    mock_grb,
    mock_gb,
    mock_vlpc,
    mock_glsp,
    mock_capml,
    mock_srs,
    mock_elp,
    mock_uiips,
    mock_vii,
    mock_pi,
    mock_bi,
    mock_oia,
    mock_uiibs,
    mock_prfb,
    mock_vl,
    mock_cleanup,
):
    error_msg = 'Backport failure!'
    mock_elp.side_effect = IIBError(error_msg)
    arches = {'amd64', 's390x'}
    mock_prfb.return_value = {
        'arches': arches,
        'binary_image_resolved': 'binary-image@sha256:abcdef',
        'from_index_resolved': 'from-index@sha256:bcdefg',
        'ocp_version': 'v4.6',
        'distribution_scope': 'prod',
    }
    mock_grb.return_value = ['some-bundle@sha']
    legacy_packages = {'some_package'}
    mock_glsp.return_value = legacy_packages
    output_pull_spec = 'quay.io/namespace/some-image:3'
    mock_capml.return_value = output_pull_spec

    bundles = ['some-bundle:2.3-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    with pytest.raises(IIBError, match=error_msg):
        build.handle_add_request(
            bundles,
            'binary-image:latest',
            3,
            'from-index:latest',
            ['s390x'],
            cnr_token,
            organization,
            False,
            None,
            greenwave_config,
        )
    mock_elp.assert_called_once()
    mock_uiips.assert_not_called()
示例#4
0
def test_handle_add_request_raises(mock_iifbc, mock_runcmd, mock_c):
    mock_iifbc.return_value = True
    with pytest.raises(IIBError):
        build.handle_add_request(
            bundles=['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1'],
            request_id=3,
            binary_image='binary-image:latest',
            from_index='from-index:latest',
            add_arches=['s390x'],
            cnr_token='token',
            organization='org',
            force_backport=True,
            overwrite_from_index=False,
            overwrite_from_index_token=None,
            distribution_scope=None,
            greenwave_config={'some_key': 'other_value'},
            binary_image_config={'prod': {
                'v4.5': 'some_image'
            }},
            deprecation_list=[],
        )
示例#5
0
def test_handle_add_request_bundle_resolution_failure(mock_grb, mock_srs):
    error_msg = 'Bundle Resolution failure!'
    mock_grb.side_effect = IIBError(error_msg)
    bundles = ['some-bundle:2.3-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    with pytest.raises(IIBError, match=error_msg):
        build.handle_add_request(
            bundles,
            'binary-image:latest',
            3,
            'from-index:latest',
            ['s390x'],
            cnr_token,
            organization,
            False,
            None,
            greenwave_config,
        )
    mock_srs.assert_called_once()
    mock_grb.assert_called_once_with(bundles)
示例#6
0
def test_handle_add_request_check_index_label_behavior(
    mock_srt,
    mock_gpb,
    mock_alti,
    mock_grb,
    mock_gb,
    mock_capml,
    mock_srs,
    mock_uiips,
    mock_vii,
    mock_pi,
    mock_bi,
    mock_oia,
    mock_uiibs,
    mock_prfb,
    mock_vl,
    mock_cleanup,
    mock_ugrb,
    mock_dep_b,
    mock_sir,
    mock_run_cmd,
):
    arches = {'amd64', 's390x'}
    binary_image_config = {'prod': {'v4.5': 'some_image'}}
    mock_prfb.return_value = {
        'arches': arches,
        'binary_image': 'binary-image:latest',
        'binary_image_resolved': 'binary-image@sha256:abcdef',
        'from_index_resolved': 'from-index@sha256:bcdefg',
        'ocp_version': 'v4.5',
        'distribution_scope': 'stage',
    }
    mock_grb.return_value = ['some-bundle@sha', 'some-deprecation-bundle@sha']
    output_pull_spec = 'quay.io/namespace/some-image:3'
    mock_capml.return_value = output_pull_spec
    mock_gpb.return_value = [{
        'bundlePath': 'random_bundle@sha'
    }], ['random_bundle@sha']
    bundles = ['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    mock_ugrb.return_value = [
        'random_bundle@sha', 'some-deprecation-bundle@sha'
    ]
    deprecation_list = ['random_bundle@sha', 'some-deprecation-bundle@sha']
    # Assume default labels are set on the index
    label_state = {'LABEL_SET': 'default_labels_set'}

    def _add_label_to_index(*args):
        # Set the labels in the index again making sure they were wiped out
        if label_state['LABEL_SET'] == 'wiping_out_labels':
            label_state['LABEL_SET'] = 'setting_label_in_add_label_to_index'

    mock_alti.side_effect = _add_label_to_index

    def deprecate_bundles_mock(*args, **kwargs):
        # Wipe out the labels on the index
        label_state['LABEL_SET'] = 'wiping_out_labels'

    mock_dep_b.side_effect = deprecate_bundles_mock

    port = 0
    my_mock = mock.MagicMock()
    mock_sir.return_value = (port, my_mock)
    mock_run_cmd.return_value = '{"packageName": "package1", "version": "v1.0", \
        "bundlePath": "bundle1"\n}'

    build.handle_add_request(
        bundles,
        3,
        'binary-image:latest',
        'from-index:latest',
        ['s390x'],
        cnr_token,
        organization,
        True,
        False,
        None,
        None,
        greenwave_config,
        binary_image_config=binary_image_config,
        deprecation_list=deprecation_list,
    )

    mock_sir.assert_called_once()
    mock_run_cmd.assert_called_once()
    mock_run_cmd.assert_has_calls([
        mock.call(
            [
                'grpcurl', '-plaintext', f'localhost:{port}',
                'api.Registry/ListBundles'
            ],
            exc_msg=mock.ANY,
        ),
    ])

    mock_cleanup.assert_called_once()
    mock_vl.assert_called_once()
    mock_prfb.assert_called_once_with(
        3,
        RequestConfigAddRm(
            _binary_image='binary-image:latest',
            from_index='from-index:latest',
            overwrite_from_index_token=None,
            add_arches=['s390x'],
            bundles=['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1'],
            distribution_scope=None,
            binary_image_config=binary_image_config,
        ),
    )
    mock_dep_b.assert_called_once_with(
        ['random_bundle@sha', 'some-deprecation-bundle@sha'],
        mock.ANY,
        'binary-image:latest',
        'containers-storage:localhost/iib-build:3-amd64',
        None,
        container_tool='podman',
    )
    # Assert the labels are set again once they were wiped out
    assert label_state['LABEL_SET'] == 'setting_label_in_add_label_to_index'
    assert mock_alti.call_count == 2
示例#7
0
def test_handle_add_request(
    mock_srt,
    mock_gpb,
    mock_alti,
    mock_grb,
    mock_gb,
    mock_capml,
    mock_srs,
    mock_srs2,
    mock_uiips,
    mock_vii,
    mock_pi,
    mock_bi,
    mock_oia,
    mock_uiibs,
    mock_prfb,
    mock_vl,
    mock_cleanup,
    mock_ugrb,
    mock_dep_b,
    mock_sir,
    mock_run_cmd,
    force_backport,
    binary_image,
    distribution_scope,
    deprecate_bundles,
):
    arches = {'amd64', 's390x'}
    binary_image_config = {'prod': {'v4.5': 'some_image'}}
    mock_prfb.return_value = {
        'arches': arches,
        'binary_image': binary_image or 'some_image',
        'binary_image_resolved': 'binary-image@sha256:abcdef',
        'from_index_resolved': 'from-index@sha256:bcdefg',
        'ocp_version': 'v4.5',
        'distribution_scope': distribution_scope,
    }
    mock_grb.return_value = ['some-bundle@sha', 'some-deprecation-bundle@sha']
    output_pull_spec = 'quay.io/namespace/some-image:3'
    mock_capml.return_value = output_pull_spec
    mock_gpb.return_value = [{
        'bundlePath': 'random_bundle@sha'
    }], ['random_bundle@sha']
    bundles = ['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    deprecation_list = []
    if deprecate_bundles:
        mock_ugrb.return_value = [
            'random_bundle@sha', 'some-deprecation-bundle@sha'
        ]
        deprecation_list = ['random_bundle@sha', 'some-deprecation-bundle@sha']

    # Simulate opm's behavior of creating files that cannot be deleted
    def side_effect(_, temp_dir, *args, **kwargs):
        read_only_dir = os.path.join(temp_dir, 'read-only-dir')
        os.mkdir(read_only_dir)
        with open(os.path.join(read_only_dir, 'read-only-file'), 'w') as f:
            os.chmod(f.fileno(), stat.S_IRUSR | stat.S_IRGRP)
        # Make the dir read-only *after* populating it
        os.chmod(read_only_dir, mode=stat.S_IRUSR | stat.S_IRGRP)

    mock_dep_b.side_effect = side_effect

    port = 0
    my_mock = mock.MagicMock()
    mock_sir.return_value = (port, my_mock)
    mock_run_cmd.return_value = '{"packageName": "package1", "version": "v1.0", \
        "bundlePath": "bundle1"\n}'

    build.handle_add_request(
        bundles,
        3,
        binary_image,
        'from-index:latest',
        ['s390x'],
        cnr_token,
        organization,
        force_backport,
        False,
        None,
        None,
        greenwave_config,
        binary_image_config=binary_image_config,
        deprecation_list=deprecation_list,
    )

    mock_sir.assert_called_once()
    mock_run_cmd.assert_called_once()
    mock_run_cmd.assert_has_calls([
        mock.call(
            [
                'grpcurl', '-plaintext', f'localhost:{port}',
                'api.Registry/ListBundles'
            ],
            exc_msg=mock.ANY,
        ),
    ])

    mock_cleanup.assert_called_once()
    mock_vl.assert_called_once()
    mock_prfb.assert_called_once_with(
        3,
        RequestConfigAddRm(
            _binary_image=binary_image,
            from_index='from-index:latest',
            overwrite_from_index_token=None,
            add_arches=['s390x'],
            bundles=['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1'],
            distribution_scope=None,
            binary_image_config=binary_image_config,
        ),
    )
    mock_gb.assert_called_once()
    assert 2 == mock_alti.call_count

    mock_oia.assert_called_once()

    if distribution_scope in ['dev', 'stage']:
        assert mock_oia.call_args[0][5]
    else:
        assert not mock_oia.call_args[0][5]

    mock_srt.assert_called_once()

    if deprecate_bundles:
        # Take into account the temporarily created index image
        assert mock_bi.call_count == len(arches) + 1
    else:
        assert mock_bi.call_count == len(arches)

    assert mock_pi.call_count == len(arches)

    mock_uiips.assert_called_once()
    mock_vii.assert_not_called()
    mock_capml.assert_called_once()
    assert mock_srs.call_count == 4
    if deprecate_bundles:
        mock_dep_b.assert_called_once_with(
            ['random_bundle@sha', 'some-deprecation-bundle@sha'],
            mock.ANY,
            binary_image or 'some_image',
            'containers-storage:localhost/iib-build:3-amd64',
            None,
            container_tool='podman',
        )
    else:
        mock_dep_b.assert_not_called()
示例#8
0
def test_handle_add_request(
    mock_grb,
    mock_gb,
    mock_vlpc,
    mock_glsp,
    mock_capml,
    mock_srs,
    mock_elp,
    mock_uiips,
    mock_vii,
    mock_pi,
    mock_bi,
    mock_oia,
    mock_prfb,
    mock_vl,
    mock_cleanup,
    force_backport,
):
    arches = {'amd64', 's390x'}
    mock_prfb.return_value = {
        'arches': arches,
        'binary_image_resolved': 'binary-image@sha256:abcdef',
        'from_index_resolved': 'from-index@sha256:bcdefg',
    }
    mock_grb.return_value = ['some-bundle@sha']
    legacy_packages = {'some_package'}
    mock_glsp.return_value = legacy_packages
    output_pull_spec = 'quay.io/namespace/some-image:3'
    mock_capml.return_value = output_pull_spec

    bundles = ['some-bundle:2.3-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    build.handle_add_request(
        bundles,
        'binary-image:latest',
        3,
        'from-index:latest',
        ['s390x'],
        cnr_token,
        organization,
        force_backport,
        False,
        None,
        greenwave_config,
    )

    mock_cleanup.assert_called_once()
    mock_vl.assert_called_once()
    mock_prfb.assert_called_once()
    mock_gb.assert_called_once()
    mock_glsp.assert_called_once_with(['some-bundle@sha'],
                                      3,
                                      force_backport=force_backport)

    add_args = mock_oia.call_args[0]
    assert ['some-bundle@sha'] in add_args
    mock_oia.assert_called_once()

    assert mock_bi.call_count == len(arches)
    assert mock_pi.call_count == len(arches)

    mock_elp.assert_called_once()
    export_args = mock_elp.call_args[0]
    assert legacy_packages in export_args
    assert cnr_token in export_args
    assert organization in export_args

    mock_uiips.assert_called_once()
    mock_vii.assert_called_once()
    mock_capml.assert_called_once()
    assert mock_srs.call_count == 3
示例#9
0
def test_handle_add_request_check_index_label_behavior(
    mock_iifbc,
    mock_srt,
    mock_gpb,
    mock_alti,
    mock_grb,
    mock_gb,
    mock_capml,
    mock_srs,
    mock_uiips,
    mock_vii,
    mock_pi,
    mock_bi,
    mock_oia,
    mock_uiibs,
    mock_prfb,
    mock_vl,
    mock_cleanup,
    mock_ugrb,
    mock_dep_b,
    mock_ors,
    mock_run_cmd,
    mock_sqlite,
    mock_gwc,
):
    arches = {'amd64', 's390x'}
    binary_image_config = {'prod': {'v4.5': 'some_image'}}
    mock_iifbc.return_value = False
    mock_prfb.return_value = {
        'arches': arches,
        'binary_image': 'binary-image:latest',
        'binary_image_resolved': 'binary-image@sha256:abcdef',
        'from_index_resolved': 'from-index@sha256:bcdefg',
        'ocp_version': 'v4.5',
        'distribution_scope': 'stage',
    }
    mock_grb.return_value = ['some-bundle@sha', 'some-deprecation-bundle@sha']
    output_pull_spec = 'quay.io/namespace/some-image:3'
    mock_capml.return_value = output_pull_spec
    mock_gpb.return_value = [{
        'bundlePath': 'random_bundle@sha'
    }], ['random_bundle@sha']
    bundles = ['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1']
    cnr_token = 'token'
    organization = 'org'
    greenwave_config = {'some_key': 'other_value'}
    mock_ugrb.return_value = [
        'random_bundle@sha', 'some-deprecation-bundle@sha'
    ]
    deprecation_list = ['random_bundle@sha', 'some-deprecation-bundle@sha']
    # Assume default labels are set on the index
    label_state = {'LABEL_SET': 'default_labels_set'}
    mock_gwc.return_value = {
        'iib_registry': 'quay.io',
        'iib_image_push_template': '{registry}/iib-build:{request_id}',
    }

    def _add_label_to_index(*args):
        # Set the labels in the index again making sure they were wiped out
        if label_state['LABEL_SET'] == 'wiping_out_labels':
            label_state['LABEL_SET'] = 'setting_label_in_add_label_to_index'

    mock_alti.side_effect = _add_label_to_index

    def deprecate_bundles_mock(*args, **kwargs):
        # Wipe out the labels on the index
        label_state['LABEL_SET'] = 'wiping_out_labels'

    mock_dep_b.side_effect = deprecate_bundles_mock

    port = 0
    my_mock = mock.MagicMock()
    mock_ors.return_value = (port, my_mock)
    mock_run_cmd.side_effect = [
        '{"packageName": "package1", "version": "v1.0", "csvName": "random-csv", \
        "bundlePath": "some-bundle@sha"\n}',
        '{"passed":false, "outputs": [{"message": "olm.maxOpenShiftVersion not present"}]}',
    ]
    mock_sqlite.execute.return_value = 200

    build.handle_add_request(
        bundles,
        3,
        'binary-image:latest',
        'from-index:latest',
        ['s390x'],
        cnr_token,
        organization,
        True,
        False,
        None,
        None,
        greenwave_config,
        binary_image_config=binary_image_config,
        deprecation_list=deprecation_list,
    )

    mock_ors.assert_called_once()
    assert mock_run_cmd.call_count == 2

    mock_run_cmd.assert_has_calls([
        mock.call(
            [
                'grpcurl', '-plaintext', f'localhost:{port}',
                'api.Registry/ListBundles'
            ],
            exc_msg=mock.ANY,
        ),
        mock.call(
            [
                'operator-sdk',
                'bundle',
                'validate',
                'some-bundle@sha',
                '--select-optional',
                'name=community',
                '--output=json-alpha1',
                '--image-builder',
                'none',
            ],
            strict=False,
        ),
    ])

    mock_cleanup.assert_called_once()
    mock_vl.assert_called_once()
    mock_prfb.assert_called_once_with(
        3,
        RequestConfigAddRm(
            _binary_image='binary-image:latest',
            from_index='from-index:latest',
            overwrite_from_index_token=None,
            add_arches=['s390x'],
            bundles=['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1'],
            distribution_scope=None,
            binary_image_config=binary_image_config,
        ),
    )
    mock_dep_b.assert_called_once_with(
        bundles=['random_bundle@sha', 'some-deprecation-bundle@sha'],
        base_dir=mock.ANY,
        binary_image='binary-image:latest',
        from_index='quay.io/iib-build:3-amd64',
        overwrite_target_index_token=None,
        container_tool='podman',
    )
    # Assert the labels are set again once they were wiped out
    assert label_state['LABEL_SET'] == 'setting_label_in_add_label_to_index'
    assert mock_alti.call_count == 2