示例#1
0
def test_version(capsys):
    """
    Test passing '--version' to repo2docker
    """
    with pytest.raises(SystemExit):
        make_r2d(['--version'])
    assert capsys.readouterr().out == "{}\n".format(__version__)
示例#2
0
def test_mem_limit():
    """
    Test various ways of passing --build-memory-limit
    """
    r2d = make_r2d(["--build-memory-limit", "1024", "."])
    assert int(r2d.build_memory_limit) == 1024

    r2d = make_r2d(["--build-memory-limit", "3K", "."])
    assert int(r2d.build_memory_limit) == 1024 * 3
示例#3
0
def test_mem_limit():
    """
    Test various ways of passing --build-memory-limit
    """
    r2d = make_r2d(['--build-memory-limit', '1024', '.'])
    assert int(r2d.build_memory_limit) == 1024

    r2d = make_r2d(['--build-memory-limit', '3K', '.'])
    assert int(r2d.build_memory_limit) == 1024 * 3
示例#4
0
def test_clean():
    """
    Test checkout is cleaned appropriately
    """

    # Don't clean when repo isn't local and we explicitly ask it to not clean
    assert not make_r2d(['--no-clean', 'https://github.com/blah.git']).cleanup_checkout
    # Do clean repo when repo isn't localj
    assert make_r2d(['https://github.com/blah.git']).cleanup_checkout

    # Don't clean by default when repo exists locally
    assert not make_r2d(['.']).cleanup_checkout
    # Don't clean when repo exists locally and we explicitly ask it to not clean
    assert not make_r2d(['--no-clean', '.']).cleanup_checkout
示例#5
0
def test_editable():
    """
    Test --editable behavior
    """
    r2d = make_r2d(["--editable", "."])
    assert r2d.repo == "."
    assert r2d.volumes[os.getcwd()] == "."
def test_editable_by_host():
    """Test whether a new file created by the host environment, is
    detected in the container"""

    app = make_r2d(['--editable', DIR])
    app.initialize()
    app.build()
    container = app.start_container()
    # give the container a chance to start
    time.sleep(1)
    try:
        with tempfile.NamedTemporaryFile(dir=DIR,
                                         prefix='testfile',
                                         suffix='.txt'):
            status, output = container.exec_run(
                ['sh', '-c', 'ls testfile????????.txt'])
            assert status == 0
            assert re.match(br'^testfile\w{8}\.txt\n$', output) is not None
        # File should be removed in the container as well
        status, output = container.exec_run(
            ['sh', '-c', 'ls testfile????????.txt'])
        assert status != 1
        assert re.match(br'^testfile\w{8}\.txt\n$', output) is None

    finally:
        # stop the container
        container.stop()
        app.wait_for_container(container)
def test_editable_by_host():
    """Test whether a new file created by the host environment, is
    detected in the container"""

    app = make_r2d(["--editable", DIR])
    app.initialize()
    app.build()
    container = app.start_container()

    # give the container a chance to start
    while container.status != "running":
        time.sleep(1)

    try:
        with tempfile.NamedTemporaryFile(dir=DIR,
                                         prefix="testfile",
                                         suffix=".txt"):
            status, output = container._c.exec_run(
                ["sh", "-c", "ls testfile????????.txt"])
            assert status == 0
            assert re.match(br"^testfile\w{8}\.txt\n$", output) is not None
        # After exiting the with block the file should stop existing
        # in the container as well as locally
        status, output = container._c.exec_run(
            ["sh", "-c", "ls testfile????????.txt"])
        assert status == 2
        assert re.match(br"^testfile\w{8}\.txt\n$", output) is None

    finally:
        # stop the container, we don't care how it stops or
        # what the exit code is.
        container.stop(timeout=1)
        container.reload()
        assert container.status == "exited", container.status
        container.remove()
示例#8
0
def test_build_args(tmpdir):

    with chdir(tmpdir):
        with open("postBuild", "w") as f:
            f.write("echo FOO=${FOO} > /tmp/foo.txt")

        with patch("repo2docker.buildpacks.BuildPack.get_build_args") as gba:
            gba.return_value = ["FOO"]
            r2d = make_r2d([
                "--build-arg",
                "FOO=BAR",
                "--debug",
                str(tmpdir),
                "cat",
                "/tmp/foo.txt",
            ])
            r2d.initialize()
            r2d.start()

            # Originally used capsys, but this failed when run with other tests
            log = r2d.log.handlers[0].stream
            log.seek(0)
            output = log.read()
            # ARG should be set in debug output
            assert "ARG FOO" in output
            # ARG and value should be present in cat output
            assert "FOO=BAR" in output
示例#9
0
def deploy_run_command(
    workspace: Workspace, image_name: Optional[str], no_mount_ssh_keys: bool
) -> None:
    target_repo_dir = "/home/%s/%s" % (os.environ["USER"], workspace.name)
    if image_name is None:
        image_name = workspace.name
    argv = [
        "--target-repo-dir",
        target_repo_dir,
        "--image-name",
        image_name,
    ]
    if not no_mount_ssh_keys:
        dot_ssh = abspath(expanduser("~/.ssh"))
        argv.append("-v")
        argv.append("%s:/home/%s/.ssh" % (dot_ssh, os.environ["USER"]))
    if isinstance(workspace, git_backend.Workspace):
        workspace_dir = workspace.get_workspace_local_path_if_any()
        assert workspace_dir is not None
        argv.append("dws+" + get_remote_origin_url(workspace_dir, verbose=workspace.verbose))
    else:
        # need to figure out how the clone url works for a non-git workspace
        assert 0, "run build not yet implemented for non-git workspaces"
    if workspace.verbose:
        click.echo("Command args for repo2docker: %s" % repr(argv))
    r2d = make_r2d(argv=argv)
    r2d.initialize()
    r2d.run_image()
    click.echo("Run of image %s was successful." % image_name)
示例#10
0
def test_editable():
    """
    Test --editable behavior
    """
    r2d = make_r2d(['--editable', '.'])
    assert r2d.repo == '.'
    assert r2d.volumes[os.getcwd()] == '.'
示例#11
0
def test_dry_run():
    """
    Test passing --no-build implies --no-run and lack of --push
    """
    r2d = make_r2d(['--no-build', '.'])
    assert r2d.dry_run
    assert not r2d.run
    assert not r2d.push
def test_local_dir_image_name(repo_with_content):
    upstream, sha1 = repo_with_content
    app = Repo2Docker()
    argv = ["--no-build", upstream]
    app = make_r2d(argv)

    app.start()

    assert app.output_image_spec.startswith(
        "r2d" + escapism.escape(upstream, escape_char="-").lower())
def test_image_name_remains_unchanged():
    # if we specify an image name, it should remain unmodified
    with TemporaryDirectory() as src:
        app = Repo2Docker()
        argv = ["--image-name", "a-special-name", "--no-build", src]
        app = make_r2d(argv)

        app.start()

        assert app.output_image_spec == "a-special-name"
def test_image_name_contains_sha1(repo_with_content):
    upstream, sha1 = repo_with_content
    app = Repo2Docker()
    # force selection of the git content provider by prefixing path with
    # file://. This is important as the Local content provider does not
    # store the SHA1 in the repo spec
    argv = ["--no-build", "file://" + upstream]
    app = make_r2d(argv)

    app.start()

    assert app.output_image_spec.endswith(sha1[:7])
def test_run_kwargs(repo_with_content):
    upstream, sha1 = repo_with_content
    argv = [upstream]
    app = make_r2d(argv)
    app.extra_run_kwargs = {"somekey": "somevalue"}

    with patch.object(docker.DockerClient, "containers") as containers:
        app.start_container()
    containers.run.assert_called_once()
    args, kwargs = containers.run.call_args
    assert "somekey" in kwargs
    assert kwargs["somekey"] == "somevalue"
def test_build_kwargs(repo_with_content):
    upstream, sha1 = repo_with_content
    argv = [upstream]
    app = make_r2d(argv)
    app.extra_build_kwargs = {"somekey": "somevalue"}

    with patch.object(docker.APIClient, "build") as builds:
        builds.return_value = []
        app.build()
    builds.assert_called_once()
    args, kwargs = builds.call_args
    assert "somekey" in kwargs
    assert kwargs["somekey"] == "somevalue"
def deploy_build_command(
    workspace: Workspace,
    image_name: Optional[str],
    force_rebuild: bool,
    git_user_email: Optional[str],
    git_user_name: Optional[str],
) -> None:
    try:
        from repo2docker.__main__ import make_r2d  # type: ignore
    except ImportError as e:
        raise ConfigurationError(R2D_IMPORT_ERROR) from e
    target_repo_dir = "/home/%s/%s" % (os.environ["USER"], workspace.name)
    if image_name is None:
        image_name = workspace.name
    argv = [
        "--target-repo-dir", target_repo_dir, "--image-name", image_name,
        "--no-run"
    ]
    if isinstance(workspace, git_backend.Workspace):
        workspace_dir = workspace.get_workspace_local_path_if_any()
        assert workspace_dir is not None
        user_email = (
            git_user_email if git_user_email else get_git_config_param(
                workspace_dir, "user.email", verbose=workspace.verbose))
        user_name = (git_user_name if git_user_name else get_git_config_param(
            workspace_dir, "user.name", verbose=workspace.verbose))
        argv.append(
            '--appendix=RUN git config --global user.email %s; git config --global user.name "%s"'
            % (user_email, user_name))
        argv.append(
            "dws+" +
            get_remote_origin_url(workspace_dir, verbose=workspace.verbose))
    else:
        # need to figure out how the clone url works for a non-git workspace
        assert 0, "build not yet implemented for non-git workspaces"

    if force_rebuild:
        click.echo("Forcing remove of image %s." % image_name)
        call_subprocess(
            ["docker", "image", "rm", "-f", "--no-prune", image_name],
            cwd=curdir,
            verbose=workspace.verbose,
        )
    if workspace.verbose:
        click.echo("Command args for repo2docker: %s" % repr(argv))
    r2d = make_r2d(argv=argv)
    r2d.initialize()
    r2d.start()
    click.echo("Build of image %s was successful." % image_name)
示例#18
0
def test_root_not_allowed():
    with TemporaryDirectory() as src, patch("os.geteuid") as geteuid:
        geteuid.return_value = 0
        argv = [src]
        app = make_r2d(argv)
        with pytest.raises(SystemExit) as exc:
            app.build()
            assert exc.code == errno.EPERM

        app = Repo2Docker(repo=src, user_id=1000, user_name="jovyan", run=False)
        app.initialize()
        with patch.object(docker.APIClient, "build") as builds:
            builds.return_value = []
            app.build()
        builds.assert_called_once()
示例#19
0
def test_run_required():
    """
    Test all the things that should fail if we pass in --no-run
    """
    # Can't use volumes without running
    with pytest.raises(SystemExit):
        make_r2d(['--no-run', '--editable', '.'])

    # Can't publish all ports without running
    with pytest.raises(SystemExit):
        make_r2d(['--no-run', '-P', '.'])

    # Can't publish any ports without running
    with pytest.raises(SystemExit):
        make_r2d(['--no-run', '-p', '8000:8000', '.'])

    # Can't publish any ports while running if we don't specify a command explicitly
    with pytest.raises(SystemExit):
        make_r2d(['-p', '8000:8000', '.'])
示例#20
0
 def test():
     app = make_r2d(args)
     app.initialize()
     if app.run_cmd:
         # verify test, run it
         app.start()
         return
     # no run_cmd given, starting notebook server
     app.run = False
     app.start()  # This just build the image and does not run it.
     container = app.start_container()
     port = app.port
     # wait a bit for the container to be ready
     container_url = "http://localhost:%s/api" % port
     # give the container a chance to start
     time.sleep(1)
     try:
         # try a few times to connect
         success = False
         for i in range(1, 4):
             container.reload()
             assert container.status == "running"
             try:
                 info = requests.get(container_url).json()
             except Exception as e:
                 print("Error: %s" % e)
                 time.sleep(i * 3)
             else:
                 print(info)
                 success = True
                 break
         assert success, "Notebook never started in %s" % container
     finally:
         # stop the container
         container.stop()
         app.wait_for_container(container)
示例#21
0
def test_invalid_image_name():
    """
    Test validating image names
    """
    with pytest.raises(SystemExit):
        make_r2d(["--image-name", "_invalid", "."])
示例#22
0
def test_simple():
    """
    Test simplest possible invocation to r2d
    """
    r2d = make_r2d(['.'])
    assert r2d.repo == '.'
示例#23
0
def test_invalid_image_name():
    """
    Test validating image names
    """
    with pytest.raises(SystemExit):
        make_r2d(['--image-name', '_invalid', '.'])