예제 #1
0
def test_build_simple(cmd: str, tmp_path):

    rcls = DockerRenderer if cmd == "docker" else SingularityRenderer

    # Create a Dockerfile.
    r = rcls("apt")
    if isinstance(r, DockerRenderer):
        r.from_("debian:buster-slim", as_="builder")
    else:
        r.from_("debian:buster-slim")
    r.arg("FOO")
    r.copy(["foo.txt", "tst/baz.txt"], "/opt/")
    r.env(PATH="$PATH:/opt/foo/bin")
    r.label(ORG="myorg")
    r.run("echo foobar")
    r.user("nonroot")
    r.workdir("/opt/foobar")
    r.entrypoint(["echo", "hi there"])

    # Create the paths that are copied in the Dockerfile.
    (tmp_path / "tst").mkdir(exist_ok=True)
    (tmp_path / "foo.txt").write_text("")
    (tmp_path / "tst" / "baz.txt").write_text("")

    spec = "Dockerfile" if cmd == "docker" else "Singularity"
    (tmp_path / spec).write_text(str(r))

    build_fn, run_fn = get_build_and_run_fns(cmd)
    with build_fn(tmp_path) as img:
        stdout, _ = run_fn(img)
        assert stdout == "hi there"
        stdout, _ = run_fn(img, entrypoint=["ls"], args=["/opt/"])
        assert set(stdout.splitlines()) == {"baz.txt", "foo.txt", "foobar"}
def test_build_image_from_registered(tmp_path: Path, cmd: str,
                                     pkg_manager: str, base_image: str):
    # Templates are in this directory.
    template_path = Path(__file__).parent
    runner = CliRunner(env={"REPROENV_TEMPLATE_PATH": str(template_path)})
    _TemplateRegistry._reset()
    result = runner.invoke(
        generate,
        [
            "--template-path",
            str(template_path),
            cmd,
            "--base-image",
            base_image,
            "--pkg-manager",
            pkg_manager,
            "--jq",
            "version=1.5",
        ],
    )
    assert result.exit_code == 0, result.output
    assert "jq-1.5/jq-linux64" in result.output

    spec = "Dockerfile" if cmd == "docker" else "Singularity"
    (tmp_path / spec).write_text(result.output)

    build_fn, run_fn = get_build_and_run_fns(cmd)
    with build_fn(tmp_path) as img:
        stdout, _ = run_fn(img, args=["jq", "--help"])
        assert "jq is a tool for processing JSON" in stdout
예제 #3
0
def test_build_using_renderer_from_dict(
    cmd: str,
    pkg_manager: str,
    base_image: str,
    jq_version: str,
    jq_version_output: str,
    fd_version_startswith: str,
    tmp_path: Path,
):

    _TemplateRegistry._reset()
    _TemplateRegistry.register(_template_filepath)

    d = {
        "pkg_manager":
        pkg_manager,
        "instructions": [
            {
                "name": "from_",
                "kwds": {
                    "base_image": base_image
                }
            },
            {
                "name": "run",
                "kwds": {
                    "command": "echo hello there"
                }
            },
            {
                "name": "jq",
                "kwds": {
                    "version": jq_version,
                    "method": "binaries"
                }
            },
        ],
    }

    fd_exe = "fdfind" if pkg_manager == "apt" else "fd"

    rcls = DockerRenderer if cmd == "docker" else SingularityRenderer
    specf = "Dockerfile" if cmd == "docker" else "Singularity"

    r = rcls.from_dict(d)
    (tmp_path / specf).write_text(str(r))

    build_fn, run_fn = get_build_and_run_fns(cmd)
    with build_fn(tmp_path) as img:
        stdout, _ = run_fn(img, args=["jq", "--help"])
        assert stdout.startswith("jq - commandline JSON processor")
        stdout, _ = run_fn(img, args=["jq", "--version"])
        assert stdout == jq_version_output
        # Test that deb was installed
        stdout, _ = run_fn(img, args=[fd_exe, "--version"])
        assert stdout.startswith(fd_version_startswith)
def test_json_roundtrip(cmd: str, inputs: str, tmp_path: Path):
    """Test that we can generate a JSON representation of a container and build an
    identical container with it.
    """
    _TemplateRegistry._reset()
    runner = CliRunner()
    result = runner.invoke(
        generate,
        [
            cmd,
            "--json",
            "--base-image",
            "debian:buster-slim",
            "--pkg-manager",
            "apt",
            "--install",
            "git",
            "--env",
            "CAT=FOO",
            "DOG=BAR",
        ],
    )
    assert result.exit_code == 0, result.output
    (tmp_path / "specs.json").write_text(result.output)

    if inputs == "file":
        result = runner.invoke(genfromjson,
                               [cmd, str(tmp_path / "specs.json")])
    elif inputs == "stdin":
        json_input = (tmp_path / "specs.json").read_text()
        result = runner.invoke(genfromjson, [cmd, "-"], input=json_input)
    else:
        raise ValueError(f"unknown inputs: {inputs}")

    spec = "Dockerfile" if cmd == "docker" else "Singularity"
    (tmp_path / spec).write_text(result.output)

    build_fn, run_fn = get_build_and_run_fns(cmd)
    with build_fn(tmp_path) as img:
        stdout, _ = run_fn(img, args=["git", "--help"])
        assert "commit" in stdout
        stdout, _ = run_fn(img, args=["env"])
        assert "CAT=FOO" in stdout
        assert "DOG=BAR" in stdout
예제 #5
0
def test_build_using_renderer_instance_methods(
    cmd: str,
    pkg_manager: str,
    base_image: str,
    method: str,
    jq_version: str,
    jq_version_output: str,
    fd_version_startswith: str,
    tmp_path: Path,
):
    _TemplateRegistry._reset()
    _TemplateRegistry.register(_template_filepath)

    pkg_manager = ty.cast(pkg_managers_type, pkg_manager)
    method = ty.cast(installation_methods_type, method)

    fd_exe = "fdfind" if pkg_manager == "apt" else "fd"

    rcls = DockerRenderer if cmd == "docker" else SingularityRenderer
    r = rcls(pkg_manager=pkg_manager)
    r.from_(base_image)
    r.add_registered_template("jq", method=method, version=jq_version)
    specf = "Dockerfile" if cmd == "docker" else "Singularity"
    (tmp_path / specf).write_text(str(r))

    build_fn, run_fn = get_build_and_run_fns(cmd)
    with build_fn(tmp_path) as img:
        stdout, _ = run_fn(img, args=["jq", "--help"])
        assert stdout.startswith("jq - commandline JSON processor")
        stdout, _ = run_fn(img, args=["jq", "--version"])
        if method == "source" and jq_version == "1.5":
            assert stdout == "jq-"
        else:
            assert stdout == jq_version_output
        # Test that deb was installed
        if method == "binaries":
            stdout, _ = run_fn(img, args=[fd_exe, "--version"])
            assert stdout.startswith(fd_version_startswith)