import pytest
import sys
import os
import subprocess
from .util import get_data, get_main_output
import tempfile
import shutil
from psutil.tests import TRAVIS

LINUX = sys.platform in ('linux', 'linux2')


@pytest.mark.skipif(not LINUX, reason="LINUX only")
class TestUdocker:
    udocker_path = None

    @classmethod
    def setup_class(cls):
        install_cmds = [
            "curl https://raw.githubusercontent.com/indigo-dc/udocker/master/udocker.py -o ./udocker",
            "chmod u+rx ./udocker",
            "./udocker install"]

        test_cwd = os.getcwd()
        test_environ = os.environ.copy()

        cls.docker_install_dir = tempfile.mkdtemp()
        os.chdir(cls.docker_install_dir)

        os.environ['UDOCKER_DIR'] = os.path.join(cls.docker_install_dir, ".udocker")
        os.environ['HOME'] = cls.docker_install_dir

        results = []
        for _ in range(3):
              results = [subprocess.call(cmd.split()) for cmd in install_cmds]
              if sum(results) == 0:
                  break
              subprocess.call(["rm", "./udocker"])

        assert sum(results) == 0

        cls.udocker_path = os.path.join(cls.docker_install_dir, 'udocker')
        os.chdir(test_cwd)
        os.environ = test_environ
        print('Udocker install dir: ' + cls.docker_install_dir)

    @classmethod
    def teardown_class(cls):
        shutil.rmtree(cls.docker_install_dir)

    def test_udocker_usage_should_not_write_cid_file(self, tmpdir):
        cwd = tmpdir.chdir()

        test_file = "tests/wf/wc-tool.cwl"
        job_file = "tests/wf/wc-job.json"
        error_code, stdout, stderr = get_main_output(
            ["--debug", "--default-container", "debian", "--user-space-docker-cmd=" + self.udocker_path,
             get_data(test_file), get_data(job_file)])
        cwd.chdir()
        cidfiles_count = sum(1 for _ in tmpdir.visit(fil="*.cid"))

        tmpdir.remove(ignore_errors=True)

        assert "completed success" in stderr, stderr
        assert cidfiles_count == 0

    @pytest.mark.skipif(TRAVIS, reason='Not reliable on single threaded test on travis.')
    def test_udocker_should_display_memory_usage(self, tmpdir):
        cwd = tmpdir.chdir()
        error_code, stdout, stderr = get_main_output(
            ["--enable-ext", "--default-container=debian",  "--user-space-docker-cmd=" + self.udocker_path,
             get_data("tests/wf/timelimit.cwl"), "--sleep_time", "10"])
        cwd.chdir()
        tmpdir.remove(ignore_errors=True)

        assert "completed success" in stderr, stderr
        assert "Max memory" in stderr, stderr

@@ -342,206 +342,8 @@
 s):%0A
-        install_cmds = %5B%0A            %22curl https://raw.githubusercontent.com/indigo-dc/udocker/master/udocker.py -o ./udocker%22,%0A            %22chmod u+rx ./udocker%22,%0A            %22./udocker install%22%5D%0A%0A
     
@@ -369,16 +369,16 @@
 etcwd()%0A
+
         
@@ -410,17 +410,16 @@
 .copy()%0A
-%0A
         
@@ -500,24 +500,371 @@
 stall_dir)%0A%0A
+        url=%22https://download.ncg.ingrid.pt/webdav/udocker/udocker-1.1.3.tar.gz%22%0A        install_cmds = %5B%0A            %5B%22curl%22, url, %22-o%22, %22./udocker-tarball.tgz%22%5D,%0A            %5B%22tar%22, %22xzvf%22, %22udocker-tarball.tgz%22, %22udocker%22%5D,%0A            %5B%22bash%22, %22-c%22, %22UDOCKER_TARBALL=%7B%7D/udocker-tarball.tgz ./udocker install%22.format(cls.docker_install_dir)%5D%5D%0A%0A
         os.e
@@ -1085,16 +1085,9 @@
 (cmd
-.split()
+s
 ) fo
@@ -1091,16 +1091,17 @@
  for cmd
+s
  in inst

improve reliability