"""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'))

[((439, 543), 'neurodocker.docker.client.containers.run', 'client.containers.run', (['"""debian:stretch"""'], {'detach': '(True)', 'tty': '(True)', 'security_opt': "['seccomp:unconfined']"}), "('debian:stretch', detach=True, tty=True, security_opt\n    =['seccomp:unconfined'])\n", (460, 543), False, 'from neurodocker.docker import client\n'), ((909, 927), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ([], {}), '()\n', (925, 927), False, 'import tempfile\n'), ((1188, 1217), 'os.path.join', 'os.path.join', (['tmpdir', '"""*.rpz"""'], {}), "(tmpdir, '*.rpz')\n", (1200, 1217), False, 'import os\n'), ((1234, 1247), 'glob.glob', 'glob', (['pattern'], {}), '(pattern)\n', (1238, 1247), False, 'from glob import glob\n'), ((1307, 1341), 'os.path.join', 'os.path.join', (['tmpdir', '"""merged.rpz"""'], {}), "(tmpdir, 'merged.rpz')\n", (1319, 1341), False, 'import os\n'), ((1346, 1400), 'neurodocker.reprozip.merge.merge_pack_files', 'merge_pack_files', ([], {'outfile': 'outfile', 'packfiles': 'packfiles'}), '(outfile=outfile, packfiles=packfiles)\n', (1362, 1400), False, 'from neurodocker.reprozip.merge import merge_pack_files\n'), ((606, 670), 'neurodocker.reprozip.trace.ReproZipMinimizer', 'ReproZipMinimizer', (['container.id', 'commands'], {'packfile_save_dir': 'dir'}), '(container.id, commands, packfile_save_dir=dir)\n', (623, 670), False, 'from neurodocker.reprozip.trace import ReproZipMinimizer\n'), ((1069, 1099), 'os.path.join', 'os.path.join', (['tmpdir', 'new_name'], {}), '(tmpdir, new_name)\n', (1081, 1099), False, 'import os\n'), ((1411, 1432), 'tarfile.open', 'tarfile.open', (['outfile'], {}), '(outfile)\n', (1423, 1432), False, 'import tarfile\n'), ((1496, 1531), 'os.path.join', 'os.path.join', (['tmpdir', '"""DATA.tar.gz"""'], {}), "(tmpdir, 'DATA.tar.gz')\n", (1508, 1531), False, 'import os\n'), ((1545, 1567), 'tarfile.open', 'tarfile.open', (['datafile'], {}), '(datafile)\n', (1557, 1567), False, 'import tarfile\n'), ((1647, 1689), 'os.path.join', 'os.path.join', (['tmpdir', '"""DATA"""', '"""usr"""', '"""bin"""'], {}), "(tmpdir, 'DATA', 'usr', 'bin')\n", (1659, 1689), False, 'import os\n'), ((1724, 1752), 'os.path.join', 'os.path.join', (['usr_path', '"""du"""'], {}), "(usr_path, 'du')\n", (1736, 1752), False, 'import os\n'), ((1788, 1818), 'os.path.join', 'os.path.join', (['usr_path', '"""grep"""'], {}), "(usr_path, 'grep')\n", (1800, 1818), False, 'import os\n'), ((1854, 1882), 'os.path.join', 'os.path.join', (['usr_path', '"""ls"""'], {}), "(usr_path, 'ls')\n", (1866, 1882), False, 'import os\n'), ((1918, 1946), 'os.path.join', 'os.path.join', (['usr_path', '"""rm"""'], {}), "(usr_path, 'rm')\n", (1930, 1946), False, 'import os\n'), ((1986, 2015), 'os.path.join', 'os.path.join', (['usr_path', '"""sed"""'], {}), "(usr_path, 'sed')\n", (1998, 2015), False, 'import os\n'), ((2055, 2084), 'os.path.join', 'os.path.join', (['usr_path', '"""tar"""'], {}), "(usr_path, 'tar')\n", (2067, 2084), False, 'import os\n')]