Exemplo n.º 1
0
def test_main():
    args = [
        'generate', '-b', 'ubuntu:17.04', '-p', 'apt', '--ants',
        'version=2.1.0', '--fsl', 'version=5.0.10', '--miniconda',
        'env_name=default', 'python_version=3.5.1', '--mrtrix3',
        '--neurodebian', 'os_codename=zesty', 'download_server=usa-nh',
        '--spm', 'version=12', 'matlab_version=R2017a', '--no-check-urls'
    ]
    main(args)

    args = ['generate', '-b', 'ubuntu:17.04', '-p', 'apt', '--no-check-urls']
    main(args)

    with pytest.raises(SystemExit):
        args = ['-b', 'ubuntu:17.04']
        main(args)

    with pytest.raises(SystemExit):
        main()

    args = [
        'generate', '-b', 'ubuntu:17.04', '-p', 'apt', '--ants', 'option=value'
    ]
    with pytest.raises(ValueError):
        main(args)
Exemplo n.º 2
0
def test_generate_from_json(capsys, tmpdir):
    cmd = "generate docker -b debian:stretch -p apt --convert3d version=1.0.0"
    main(cmd.split())
    true, _ = capsys.readouterr()

    specs = {
        'instructions': [['base', 'debian:stretch'],
                         ['convert3d', {
                             'version': '1.0.0'
                         }]],
        'pkg_manager':
        'apt'
    }
    str_specs = json.dumps(specs)
    filepath = tmpdir.join("specs.json")
    filepath.write(str_specs)

    gen_cmd = "generate docker {}".format(filepath)
    main(gen_cmd.split())
    test, _ = capsys.readouterr()

    # Remove last RUN instruction (it contains saving to JSON and order is not guaranteed).
    true = "RUN".join(true.split("RUN")[:-1])
    test = "RUN".join(test.split("RUN")[:-1])

    # Remove comments.
    true = [t for t in true.splitlines() if not t.startswith('#')]
    test = [t for t in test.splitlines() if not t.startswith('#')]

    assert true == test
Exemplo n.º 3
0
def test_generate_from_json(capsys, tmpdir):
    cmd = "generate docker -b debian:stretch -p apt --convert3d version=1.0.0"
    main(cmd.split())
    true, _ = capsys.readouterr()

    specs = {
        "instructions": [
            ["base", "debian:stretch"],
            ["convert3d", {
                "version": "1.0.0"
            }],
        ],
        "pkg_manager":
        "apt",
    }
    str_specs = json.dumps(specs)
    filepath = tmpdir.join("specs.json")
    filepath.write(str_specs)

    gen_cmd = "generate docker {}".format(filepath)
    main(gen_cmd.split())
    test, _ = capsys.readouterr()

    # Remove last RUN instruction (it contains saving to JSON and order is not guaranteed).
    true = "RUN".join(true.split("RUN")[:-1])
    test = "RUN".join(test.split("RUN")[:-1])

    # Remove comments.
    true = [t for t in true.splitlines() if not t.startswith("#")]
    test = [t for t in test.splitlines() if not t.startswith("#")]

    assert true == test
Exemplo n.º 4
0
def test_generate_from_json(capsys, tmpdir):
    import json

    cmd = "generate -b debian:stretch -p apt --c3d version=1.0.0"
    main(cmd.split())
    true, _ = capsys.readouterr()

    specs = {
        'check_urls': True,
        'generation_timestamp': '2017-08-31 21:49:04',
        'instructions': [['base', 'debian:stretch'],
                         ['c3d', {
                             'version': '1.0.0'
                         }]],
        'neurodocker_version': '0.2.0-18-g9227b17',
        'pkg_manager': 'apt'
    }
    str_specs = json.dumps(specs)
    filepath = tmpdir.join("specs.json")
    filepath.write(str_specs)

    gen_cmd = "generate --file {}".format(filepath)
    main(gen_cmd.split())
    test, _ = capsys.readouterr()

    # These indices chop off the header (with timestamp) and the layer that
    # saves to JSON (with timestamp).
    sl = slice(8, -19)
    assert true.split('\n')[sl] == test.split('\n')[sl]
Exemplo n.º 5
0
def test_generate():
    args = ("generate -b ubuntu:17.04 -p apt"
            " --afni version=latest"
            " --ants version=2.2.0"
            " --freesurfer version=6.0.0"
            " --fsl version=5.0.10"
            " --user=neuro"
            " --miniconda env_name=neuro python_version=3.6"
            " --user=root"
            " --mrtrix3"
            " --neurodebian os_codename=zesty download_server=usa-nh"
            " --spm version=12 matlab_version=R2017a"
            " --no-check-urls"
            " --expose 1234 9000"
            " --copy relpath/to/file.txt /tmp/file.txt"
            " --add relpath/to/file2.txt /tmp/file2.txt"
            " --workdir /home"
            " --install git"
            " --user=neuro")
    main(args.split())

    with pytest.raises(SystemExit):
        args = "-b ubuntu"
        main(args.split())

    with pytest.raises(SystemExit):
        args = "-p apt"
        main(args.split())

    with pytest.raises(SystemExit):
        main()

    args = "generate -b ubuntu -p apt --ants option=value"
    with pytest.raises(ValueError):
        main(args.split())
Exemplo n.º 6
0
def test_generate_save(tmpdir):
    outfile = tmpdir.join("test.txt")
    args = ['generate', '-b', 'ubuntu:17.04', '-p', 'apt', '--mrtrix3',
            'use_binaries=false', '--no-print-df', '-o', outfile.strpath,
            '--no-check-urls']
    main(args)
    assert outfile.read(), "saved Dockerfile is empty"
    assert "git clone https://github.com/MRtrix3/mrtrix3.git" in outfile.read()
Exemplo n.º 7
0
def test_generate_no_print(capsys):
    args = ['generate', '-b', 'ubuntu:17.04', '-p', 'apt', '--no-check-urls']
    main(args)
    out, _ = capsys.readouterr()
    assert "FROM" in out and "RUN" in out

    args.append('--no-print-df')
    main(args)
    out, _ = capsys.readouterr()
    assert not out
Exemplo n.º 8
0
def test_generate():
    args = ("generate docker -b ubuntu:17.04 -p apt"
            " --arg FOO=BAR BAZ"
            " --ndfreeze date=20180312"
            " --afni version=latest method=source"
            " --ants version=2.2.0 method=source"
            " --freesurfer version=6.0.0"
            " --fsl version=5.0.10"
            " --user=neuro"
            " --miniconda create_env=neuro conda_install=python=3.6.2"
            " --user=root"
            " --mrtrix3 version=3.0_RC3"
            " --neurodebian os_codename=zesty server=usa-nh"
            " --spm12 version=r7219 matlab_version=R2017a"
            " --expose 1234 9000"
            " --volume /var /usr/bin"
            " --label FOO=BAR BAZ=CAT"
            " --copy relpath/to/file.txt /tmp/file.txt"
            " --add relpath/to/file2.txt /tmp/file2.txt"
            " --cmd '--arg1' '--arg2'"
            " --workdir /home"
            " --install git"
            " --user=neuro")
    main(args.split())

    with pytest.raises(SystemExit):
        args = "-b ubuntu"
        main(args.split())

    with pytest.raises(SystemExit):
        args = "-p apt"
        main(args.split())

    with pytest.raises(SystemExit):
        main()
Exemplo n.º 9
0
def test_generate_from_json_stdin(capsys, monkeypatch):
    specs = {
        "instructions": [["base", "debian:stretch"], ["install", ["git", "vim"]]],
        "pkg_manager": "apt",
    }
    monkeypatch.setattr("sys.stdin", io.StringIO(json.dumps(specs)))
    main("generate docker -".split())
    test, _ = capsys.readouterr()

    true_cmd = "generate docker -b debian:stretch -p apt --install git vim"
    main(true_cmd.split())
    true, _ = capsys.readouterr()

    # Remove last RUN instruction (it contains saving to JSON and order is not guaranteed).
    true = "RUN".join(true.split("RUN")[:-1])
    test = "RUN".join(test.split("RUN")[:-1])

    # Remove comments.
    true = [t for t in true.splitlines() if not t.startswith("#")]
    test = [t for t in test.splitlines() if not t.startswith("#")]

    assert true == test
Exemplo n.º 10
0
def test_generate_from_json_stdin(capsys, monkeypatch):
    specs = {
        'instructions': [['base', 'debian:stretch'],
                         ['install', ['git', 'vim']]],
        'pkg_manager': 'apt',
    }
    monkeypatch.setattr('sys.stdin', io.StringIO(json.dumps(specs)))
    main("generate docker -".split())
    test, _ = capsys.readouterr()

    true_cmd = "generate docker -b debian:stretch -p apt --install git vim"
    main(true_cmd.split())
    true, _ = capsys.readouterr()

    # Remove last RUN instruction (it contains saving to JSON and order is not guaranteed).
    true = "RUN".join(true.split("RUN")[:-1])
    test = "RUN".join(test.split("RUN")[:-1])

    # Remove comments.
    true = [t for t in true.splitlines() if not t.startswith('#')]
    test = [t for t in test.splitlines() if not t.startswith('#')]

    assert true == test
Exemplo n.º 11
0
def test_dockerfile_opts(capsys):
    args = "generate -b ubuntu:17.04 -p apt --no-check-urls {}"
    main(args.format('--user=neuro').split())
    out, _ = capsys.readouterr()
    assert "USER neuro" in out

    main(args.format('--env KEY=VAL KEY2=VAL').split())
    out, _ = capsys.readouterr()
    assert 'ENV KEY="VAL" \\' in out
    assert '  KEY2="VAL"' in out

    main(args.format('--expose 1230 1231').split())
    out, _ = capsys.readouterr()
    assert "EXPOSE 1230 1231" in out
Exemplo n.º 12
0
def test_generate_opts(capsys):
    args = "generate docker -b ubuntu:17.04 -p apt {}"
    main(args.format('--user=neuro').split())
    out, _ = capsys.readouterr()
    assert "USER neuro" in out

    main(args.format('--add path/to/file.txt /tmp/file.txt').split())
    out, _ = capsys.readouterr()
    assert 'ADD ["path/to/file.txt", "/tmp/file.txt"]' in out

    main(args.format('--copy path/to/file.txt /tmp/file.txt').split())
    out, _ = capsys.readouterr()
    assert 'COPY ["path/to/file.txt", "/tmp/file.txt"]' in out

    main(args.format('--env KEY=VAL KEY2=VAL').split())
    out, _ = capsys.readouterr()
    assert (('ENV KEY="VAL" \\' in out and 'KEY="VAL"' in out)
            or ('ENV KEY2="VAL" \\' in out and 'KEY="VAL"'))

    main(args.format('--expose 1230 1231').split())
    out, _ = capsys.readouterr()
    assert "EXPOSE 1230 1231" in out

    main(args.format('--workdir /home').split())
    out, _ = capsys.readouterr()
    assert "WORKDIR /home" in out

    main(args.format('--install vi').split())
    out, _ = capsys.readouterr()
    assert "vi" in out

    # Test that nd_freeze comes before the header.
    main(args.format('--ndfreeze date=20180312').split())
    out, _ = capsys.readouterr()
    assert out.find("nd_freeze") < out.find("ND_ENTRYPOINT")
Exemplo n.º 13
0
def test_generate_opts(capsys):
    args = "generate docker -b ubuntu:17.04 -p apt {}"
    main(args.format("--user=neuro").split())
    out, _ = capsys.readouterr()
    assert "USER neuro" in out

    main(args.format("--add path/to/file.txt /tmp/file.txt").split())
    out, _ = capsys.readouterr()
    assert 'ADD ["path/to/file.txt", "/tmp/file.txt"]' in out

    main(args.format("--copy path/to/file.txt /tmp/file.txt").split())
    out, _ = capsys.readouterr()
    assert 'COPY ["path/to/file.txt", "/tmp/file.txt"]' in out

    main(args.format("--env KEY=VAL KEY2=VAL").split())
    out, _ = capsys.readouterr()
    assert ('ENV KEY="VAL" \\' in out
            and 'KEY="VAL"' in out) or ('ENV KEY2="VAL" \\' in out
                                        and 'KEY="VAL"')

    main(args.format("--expose 1230 1231").split())
    out, _ = capsys.readouterr()
    assert "EXPOSE 1230 1231" in out

    main(args.format("--workdir /home").split())
    out, _ = capsys.readouterr()
    assert "WORKDIR /home" in out

    main(args.format("--install vi").split())
    out, _ = capsys.readouterr()
    assert "vi" in out

    # Test that nd_freeze comes before the header.
    main(args.format("--ndfreeze date=20180312").split())
    out, _ = capsys.readouterr()
    assert out.find("nd_freeze") < out.find("ND_ENTRYPOINT")

    with mock.patch("neurodocker.interfaces.Miniconda._installed", False):
        main(
            args.format("--miniconda create_env=newenv version=4.7.10 "
                        "conda_install=python=3.7").split())
    out, _ = capsys.readouterr()
    assert (
        "https://repo.continuum.io/miniconda/Miniconda3-4.7.10-Linux-x86_64.sh"
        in out)
Exemplo n.º 14
0
def test_generate_opts(capsys):
    args = "generate -b ubuntu:17.04 -p apt --no-check-urls {}"
    main(args.format('--user=neuro').split())
    out, _ = capsys.readouterr()
    assert "USER neuro" in out

    main(args.format('--add path/to/file.txt /tmp/file.txt').split())
    out, _ = capsys.readouterr()
    assert 'ADD ["path/to/file.txt", "/tmp/file.txt"]' in out

    main(args.format('--copy path/to/file.txt /tmp/file.txt').split())
    out, _ = capsys.readouterr()
    assert 'COPY ["path/to/file.txt", "/tmp/file.txt"]' in out

    main(args.format('--env KEY=VAL KEY2=VAL').split())
    out, _ = capsys.readouterr()
    assert 'ENV KEY="VAL" \\' in out
    assert '  KEY2="VAL"' in out

    main(args.format('--expose 1230 1231').split())
    out, _ = capsys.readouterr()
    assert "EXPOSE 1230 1231" in out

    main(args.format('--workdir /home').split())
    out, _ = capsys.readouterr()
    assert "WORKDIR /home" in out

    main(args.format('--install vi').split())
    out, _ = capsys.readouterr()
    assert "vi" in out

    main(args.format('--instruction RUNecho').split())
    out, _ = capsys.readouterr()
    assert "RUNecho" in out
Exemplo n.º 15
0
 def _get_dockerfile_from_stdout(args):
     f = io.StringIO()
     with redirect_stdout(f):
         main(args)
     return f.getvalue()