"""Tests for merge.py."""

from __future__ import absolute_import, division, print_function

from glob import glob
import os
import tarfile
import tempfile

from neurodocker.docker import client
from neurodocker.reprozip.trace import ReproZipMinimizer
from neurodocker.reprozip.merge import merge_pack_files


def _create_packfile(commands, dir):
    """Create packfile from list `commands` in debian:stretch container."""
    container = client.containers.run('debian:stretch', detach=True, tty=True,
                                      security_opt=['seccomp:unconfined'])
    try:
        minimizer = ReproZipMinimizer(container.id, commands,
                                      packfile_save_dir=dir)
        packfile_path = minimizer.run()
    except:
        raise
    finally:
        container.stop()
        container.remove()
    return packfile_path


def test_merge_pack_files():
    tmpdir = tempfile.mkdtemp()

    cmd = ["du -sh /usr", "rm --help"]
    packpath = _create_packfile(cmd, tmpdir)
    new_name = "first-pack.rpz"
    os.rename(packpath, os.path.join(tmpdir, new_name))

    cmd = ["ls -l /", "grep --help"]
    _create_packfile(cmd, tmpdir)

    pattern = os.path.join(tmpdir, '*.rpz')
    packfiles = glob(pattern)
    assert packfiles, "packfiles not found"

    outfile = os.path.join(tmpdir, 'merged.rpz')
    merge_pack_files(outfile=outfile, packfiles=packfiles)

    with tarfile.open(outfile) as tar:
        tar.extractall(path=tmpdir)
        datafile = os.path.join(tmpdir, 'DATA.tar.gz')
        with tarfile.open(datafile) as tardata:
            tardata.extractall(path=tmpdir)
            usr_path = os.path.join(tmpdir, 'DATA', 'usr', 'bin')
            assert os.path.isfile(os.path.join(usr_path, 'du'))
            assert os.path.isfile(os.path.join(usr_path, 'grep'))
            assert os.path.isfile(os.path.join(usr_path, 'ls'))
            assert os.path.isfile(os.path.join(usr_path, 'rm'))
            assert not os.path.isfile(os.path.join(usr_path, 'sed'))
            assert not os.path.isfile(os.path.join(usr_path, 'tar'))

[((17, 16, 18, 74), 'neurodocker.docker.client.containers.run', 'client.containers.run', (), '', False, 'from neurodocker.docker import client\n'), ((32, 13, 32, 31), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ({}, {}), '()', False, 'import tempfile\n'), ((42, 14, 42, 43), 'os.path.join', 'os.path.join', ({(42, 27, 42, 33): 'tmpdir', (42, 35, 42, 42): '"""*.rpz"""'}, {}), "(tmpdir, '*.rpz')", False, 'import os\n'), ((43, 16, 43, 29), 'glob.glob', 'glob', ({(43, 21, 43, 28): 'pattern'}, {}), '(pattern)', False, 'from glob import glob\n'), ((46, 14, 46, 48), 'os.path.join', 'os.path.join', ({(46, 27, 46, 33): 'tmpdir', (46, 35, 46, 47): '"""merged.rpz"""'}, {}), "(tmpdir, 'merged.rpz')", False, 'import os\n'), ((47, 4, 47, 58), 'neurodocker.reprozip.merge.merge_pack_files', 'merge_pack_files', (), '', False, 'from neurodocker.reprozip.merge import merge_pack_files\n'), ((20, 20, 21, 60), 'neurodocker.reprozip.trace.ReproZipMinimizer', 'ReproZipMinimizer', (), '', False, 'from neurodocker.reprozip.trace import ReproZipMinimizer\n'), ((37, 24, 37, 54), 'os.path.join', 'os.path.join', ({(37, 37, 37, 43): 'tmpdir', (37, 45, 37, 53): 'new_name'}, {}), '(tmpdir, new_name)', False, 'import os\n'), ((49, 9, 49, 30), 'tarfile.open', 'tarfile.open', ({(49, 22, 49, 29): 'outfile'}, {}), '(outfile)', False, 'import tarfile\n'), ((51, 19, 51, 54), 'os.path.join', 'os.path.join', ({(51, 32, 51, 38): 'tmpdir', (51, 40, 51, 53): '"""DATA.tar.gz"""'}, {}), "(tmpdir, 'DATA.tar.gz')", False, 'import os\n'), ((52, 13, 52, 35), 'tarfile.open', 'tarfile.open', ({(52, 26, 52, 34): 'datafile'}, {}), '(datafile)', False, 'import tarfile\n'), ((54, 23, 54, 65), 'os.path.join', 'os.path.join', ({(54, 36, 54, 42): 'tmpdir', (54, 44, 54, 50): '"""DATA"""', (54, 52, 54, 57): '"""usr"""', (54, 59, 54, 64): '"""bin"""'}, {}), "(tmpdir, 'DATA', 'usr', 'bin')", False, 'import os\n'), ((55, 34, 55, 62), 'os.path.join', 'os.path.join', ({(55, 47, 55, 55): 'usr_path', (55, 57, 55, 61): '"""du"""'}, {}), "(usr_path, 'du')", False, 'import os\n'), ((56, 34, 56, 64), 'os.path.join', 'os.path.join', ({(56, 47, 56, 55): 'usr_path', (56, 57, 56, 63): '"""grep"""'}, {}), "(usr_path, 'grep')", False, 'import os\n'), ((57, 34, 57, 62), 'os.path.join', 'os.path.join', ({(57, 47, 57, 55): 'usr_path', (57, 57, 57, 61): '"""ls"""'}, {}), "(usr_path, 'ls')", False, 'import os\n'), ((58, 34, 58, 62), 'os.path.join', 'os.path.join', ({(58, 47, 58, 55): 'usr_path', (58, 57, 58, 61): '"""rm"""'}, {}), "(usr_path, 'rm')", False, 'import os\n'), ((59, 38, 59, 67), 'os.path.join', 'os.path.join', ({(59, 51, 59, 59): 'usr_path', (59, 61, 59, 66): '"""sed"""'}, {}), "(usr_path, 'sed')", False, 'import os\n'), ((60, 38, 60, 67), 'os.path.join', 'os.path.join', ({(60, 51, 60, 59): 'usr_path', (60, 61, 60, 66): '"""tar"""'}, {}), "(usr_path, 'tar')", False, 'import os\n')]