Example #1
0
def mock_env(
    workflow,
    *,
    df_content: str,
    base_inspect: ImageInspectionData,
    # (dict[str, str] | str | None), but some tests intentionally pass the wrong type
    labels_plugin_arg: Optional[Any] = None,
    labels_reactor_conf: Optional[Dict[str, str]] = None,
    # list[list[str]], but same as above
    eq_conf: Optional[Any] = None,
    dont_overwrite: Optional[List[str]] = None,
    dont_overwrite_if_in_dockerfile: Optional[List[str]] = None,
    aliases: Optional[Dict[str, str]] = None,
    auto_labels: Optional[List[str]] = None,
) -> MockEnv:

    env = MockEnv(workflow).for_plugin("prebuild", AddLabelsPlugin.key)

    args = {
        'labels': labels_plugin_arg,
        'dont_overwrite': dont_overwrite,
        'auto_labels': auto_labels,
        'aliases': aliases,
        'equal_labels': eq_conf,
    }
    if dont_overwrite_if_in_dockerfile is not None:
        args[
            'dont_overwrite_if_in_dockerfile'] = dont_overwrite_if_in_dockerfile

    env.set_plugin_args(args)

    df_path = Path(workflow.source.path) / "Dockerfile"
    df_path.write_text(df_content)

    workflow.build_dir.init_build_dirs(["aarch64", "x86_64"], workflow.source)

    env.set_dockerfile_images(
        workflow.build_dir.any_platform.dockerfile.parent_images)

    flexmock(workflow.imageutil).should_receive(
        'base_image_inspect').and_return(base_inspect)
    flexmock(workflow.source).should_receive('get_vcs_info').and_return(
        VcsInfo(vcs_type="git",
                vcs_url=DOCKERFILE_GIT,
                vcs_ref=DOCKERFILE_SHA1))

    if labels_reactor_conf is not None:
        env.reactor_config.conf["image_labels"] = deepcopy(labels_reactor_conf)
    if eq_conf is not None:
        env.reactor_config.conf["image_equal_labels"] = eq_conf

    return env
def mock_env(
    workflow,
    *,
    df_content: str,
    base_inspect: ImageInspectionData,
    # (dict[str, str] | str | None), but some tests intentionally pass the wrong type
    labels_plugin_arg: Optional[Any] = None,
    labels_reactor_conf: Optional[Dict[str, str]] = None,
    # list[list[str]], but same as above
    eq_conf: Optional[Any] = None,
    dont_overwrite: Optional[List[str]] = None,
    dont_overwrite_if_in_dockerfile: Optional[List[str]] = None,
    aliases: Optional[Dict[str, str]] = None,
    auto_labels: Optional[List[str]] = None,
) -> MockEnv:

    env = MockEnv(workflow).for_plugin(AddLabelsPlugin.key)

    args = {
        'labels': labels_plugin_arg,
        'dont_overwrite': dont_overwrite,
        'auto_labels': auto_labels,
        'aliases': aliases,
        'equal_labels': eq_conf,
    }
    if dont_overwrite_if_in_dockerfile is not None:
        args[
            'dont_overwrite_if_in_dockerfile'] = dont_overwrite_if_in_dockerfile

    env.set_plugin_args(args)

    df_path = Path(workflow.source.path) / "Dockerfile"
    df_path.write_text(df_content)

    def get_build(pipeline_run_name):
        start_time_json = {'status': {'startTime': TIME}}
        return start_time_json

    flexmock(OSBS, get_build=get_build)
    config_kwargs = {
        'namespace': workflow.namespace,
        'verify_ssl': True,
        'openshift_url': 'http://example.com/',
        'use_auth': True,
        'conf_file': None,
    }
    (flexmock(osbs.conf.Configuration).should_call("__init__").with_args(
        **config_kwargs))

    openshift_map = {
        'url': 'http://example.com/',
        'insecure': False,
        'auth': {
            'enable': True
        },
    }

    rcm = {'version': 1, 'openshift': openshift_map}
    if labels_reactor_conf is not None:
        rcm["image_labels"] = deepcopy(labels_reactor_conf)
    if eq_conf is not None:
        rcm["image_equal_labels"] = eq_conf
    workflow.conf.conf = rcm

    workflow.build_dir.init_build_dirs(["aarch64", "x86_64"], workflow.source)

    env.set_dockerfile_images(
        workflow.build_dir.any_platform.dockerfile.parent_images)

    flexmock(workflow.imageutil).should_receive(
        'base_image_inspect').and_return(base_inspect)
    flexmock(workflow.source).should_receive('get_vcs_info').and_return(
        VcsInfo(vcs_type="git",
                vcs_url=DOCKERFILE_GIT,
                vcs_ref=DOCKERFILE_SHA1))

    return env