def mock_env(dockerfile_path, docker_tasker, labels=(), flatpak=False, dockerfile_f=mock_dockerfile, isolated=None): """Mock test environment :param dockerfile_path: the path to the fake dockerfile to be created, not including the dockerfile filename. :type dockerfile_path: py.path.LocalPath :param docker_tasker: docker_tasker fixture from conftest. Passed to ``MockEnv.create_runner`` directly to create a corresponding plugin runner instance. :param labels: an iterable labels set for testing operator bundle or appregistry build. :type labels: iterable[str] :param bool flatpak: a flag to indicate whether the test is for a flatpak build. :param callable dockerfile_f: a function to create fake dockerfile. Different test could pass a specific function for itself. :param bool isolated: a flag to indicated if build is isolated """ if not flatpak: # flatpak build has no Dockefile dockerfile_f(dockerfile_path, labels) env = MockEnv().for_plugin('prebuild', CheckUserSettingsPlugin.key, {'flatpak': flatpak}) env.workflow.source = FakeSource(dockerfile_path) if isolated is not None: env.set_isolated(isolated) dfp = df_parser(str(dockerfile_path)) env.workflow.builder.set_df_path(str(dockerfile_path)) env.workflow.builder.set_dockerfile_images([] if flatpak else dfp.parent_images) return env.create_runner(docker_tasker)
def mock_env(workflow, source_dir: Path, labels=None, flatpak=False, dockerfile_f=mock_dockerfile, isolated=None): """Mock test environment :param workflow: a DockerBuildWorkflow object for a specific test. :type workflow: DockerBuildWorkflow :param source_dir: path to the source directory holding the dockerfile to be created. :type source_dir: pathlib.Path :param labels: an iterable labels set for testing operator bundle or appregistry build. :type labels: iterable[str] :param bool flatpak: a flag to indicate whether the test is for a flatpak build. :param callable dockerfile_f: a function to create fake dockerfile. Different test could pass a specific function for itself. :param bool isolated: a flag to indicated if build is isolated """ # Make sure the version label will be presented in labels if not labels: labels = ['version="1.0"'] elif not any([label.startswith('version') for label in labels]): labels.append('version="1.0"') if not flatpak: # flatpak build has no Dockefile dockerfile_f(source_dir, labels) env = MockEnv(workflow).for_plugin(CheckUserSettingsPlugin.key, {'flatpak': flatpak}) env.workflow.source = FakeSource(source_dir) env.workflow.build_dir.init_build_dirs(["aarch64", "x86_64"], env.workflow.source) if isolated is not None: env.set_isolated(isolated) dfp = env.workflow.build_dir.any_platform.dockerfile env.workflow.data.dockerfile_images = DockerfileImages( [] if flatpak else dfp.parent_images) flexmock(env.workflow.imageutil).should_receive( "base_image_inspect").and_return({}) return env.create_runner()