|
| 1 | +"""Test optional udocker feature.""" |
| 2 | +import copy |
1 | 3 | import os
|
2 | 4 | import shutil
|
3 | 5 | import subprocess
|
4 | 6 | import sys
|
5 |
| -import tempfile |
6 | 7 |
|
7 | 8 | try:
|
8 | 9 | from psutil.tests import TRAVIS # type: ignore
|
9 | 10 | except ImportError:
|
10 | 11 | TRAVIS = True
|
11 | 12 |
|
12 | 13 |
|
13 |
| -import py.path |
| 14 | +from pathlib import Path |
| 15 | +from typing import Generator |
| 16 | + |
14 | 17 | import pytest
|
| 18 | +from _pytest.tmpdir import TempPathFactory |
15 | 19 |
|
16 | 20 | from .util import get_data, get_main_output
|
17 | 21 |
|
18 | 22 | LINUX = sys.platform in ("linux", "linux2")
|
19 | 23 |
|
20 | 24 |
|
| 25 | +@pytest.fixture(scope="session") |
| 26 | +def udocker(tmp_path_factory: TempPathFactory) -> str: |
| 27 | + """Udocker fixture, returns the path to the udocker script.""" |
| 28 | + test_cwd = os.getcwd() |
| 29 | + test_environ = copy.copy(os.environ) |
| 30 | + docker_install_dir = str(tmp_path_factory.mktemp("udocker")) |
| 31 | + os.chdir(docker_install_dir) |
| 32 | + |
| 33 | + url = "https://raw.githubusercontent.com/jorge-lip/udocker-builds/master/tarballs/udocker-1.1.4.tar.gz" |
| 34 | + install_cmds = [ |
| 35 | + ["curl", url, "-o", "./udocker-tarball.tgz"], |
| 36 | + ["tar", "xzvf", "udocker-tarball.tgz", "udocker"], |
| 37 | + [ |
| 38 | + "bash", |
| 39 | + "-c", |
| 40 | + "UDOCKER_TARBALL={}/udocker-tarball.tgz ./udocker install".format( |
| 41 | + docker_install_dir |
| 42 | + ), |
| 43 | + ], |
| 44 | + ] |
| 45 | + |
| 46 | + os.environ["UDOCKER_DIR"] = os.path.join(docker_install_dir, ".udocker") |
| 47 | + os.environ["HOME"] = docker_install_dir |
| 48 | + |
| 49 | + results = [] |
| 50 | + for _ in range(3): |
| 51 | + results = [subprocess.call(cmds) for cmds in install_cmds] |
| 52 | + if sum(results) == 0: |
| 53 | + break |
| 54 | + subprocess.call(["rm", "./udocker"]) |
| 55 | + |
| 56 | + assert sum(results) == 0 |
| 57 | + |
| 58 | + udocker_path = os.path.join(docker_install_dir, "udocker") |
| 59 | + os.chdir(test_cwd) |
| 60 | + os.environ = test_environ |
| 61 | + return udocker_path |
| 62 | + |
| 63 | + |
21 | 64 | @pytest.mark.skipif(not LINUX, reason="LINUX only")
|
22 |
| -class TestUdocker: |
23 |
| - udocker_path = "" |
24 |
| - docker_install_dir = "" |
25 |
| - |
26 |
| - @classmethod |
27 |
| - def setup_class(cls) -> None: |
28 |
| - test_cwd = os.getcwd() |
29 |
| - test_environ = os.environ.copy() |
30 |
| - cls.docker_install_dir = tempfile.mkdtemp() |
31 |
| - os.chdir(cls.docker_install_dir) |
32 |
| - |
33 |
| - url = "https://download.ncg.ingrid.pt/webdav/udocker/udocker-1.1.3.tar.gz" |
34 |
| - install_cmds = [ |
35 |
| - ["curl", url, "-o", "./udocker-tarball.tgz"], |
36 |
| - ["tar", "xzvf", "udocker-tarball.tgz", "udocker"], |
37 |
| - [ |
38 |
| - "bash", |
39 |
| - "-c", |
40 |
| - "UDOCKER_TARBALL={}/udocker-tarball.tgz ./udocker install".format( |
41 |
| - cls.docker_install_dir |
42 |
| - ), |
43 |
| - ], |
| 65 | +def test_udocker_usage_should_not_write_cid_file(udocker: str, tmp_path: Path) -> None: |
| 66 | + """Confirm that no cidfile is made when udocker is used.""" |
| 67 | + cwd = Path.cwd() |
| 68 | + os.chdir(tmp_path) |
| 69 | + |
| 70 | + test_file = "tests/wf/wc-tool.cwl" |
| 71 | + job_file = "tests/wf/wc-job.json" |
| 72 | + error_code, stdout, stderr = get_main_output( |
| 73 | + [ |
| 74 | + "--debug", |
| 75 | + "--default-container", |
| 76 | + "debian", |
| 77 | + "--user-space-docker-cmd=" + udocker, |
| 78 | + get_data(test_file), |
| 79 | + get_data(job_file), |
44 | 80 | ]
|
| 81 | + ) |
45 | 82 |
|
46 |
| - os.environ["UDOCKER_DIR"] = os.path.join(cls.docker_install_dir, ".udocker") |
47 |
| - os.environ["HOME"] = cls.docker_install_dir |
48 |
| - |
49 |
| - results = [] |
50 |
| - for _ in range(3): |
51 |
| - results = [subprocess.call(cmds) for cmds in install_cmds] |
52 |
| - if sum(results) == 0: |
53 |
| - break |
54 |
| - subprocess.call(["rm", "./udocker"]) |
55 |
| - |
56 |
| - assert sum(results) == 0 |
57 |
| - |
58 |
| - cls.udocker_path = os.path.join(cls.docker_install_dir, "udocker") |
59 |
| - os.chdir(test_cwd) |
60 |
| - os.environ = test_environ # type: ignore |
61 |
| - print("Udocker install dir: " + cls.docker_install_dir) |
62 |
| - |
63 |
| - @classmethod |
64 |
| - def teardown_class(cls) -> None: |
65 |
| - shutil.rmtree(cls.docker_install_dir) |
66 |
| - |
67 |
| - def test_udocker_usage_should_not_write_cid_file( |
68 |
| - self, tmpdir: py.path.local |
69 |
| - ) -> None: |
70 |
| - cwd = tmpdir.chdir() |
71 |
| - |
72 |
| - test_file = "tests/wf/wc-tool.cwl" |
73 |
| - job_file = "tests/wf/wc-job.json" |
74 |
| - error_code, stdout, stderr = get_main_output( |
75 |
| - [ |
76 |
| - "--debug", |
77 |
| - "--default-container", |
78 |
| - "debian", |
79 |
| - "--user-space-docker-cmd=" + self.udocker_path, |
80 |
| - get_data(test_file), |
81 |
| - get_data(job_file), |
82 |
| - ] |
83 |
| - ) |
84 |
| - cwd.chdir() |
85 |
| - cidfiles_count = sum(1 for _ in tmpdir.visit(fil="*.cid")) |
86 |
| - |
87 |
| - tmpdir.remove(ignore_errors=True) |
88 |
| - |
89 |
| - assert "completed success" in stderr, stderr |
90 |
| - assert cidfiles_count == 0 |
91 |
| - |
92 |
| - @pytest.mark.skipif( |
93 |
| - TRAVIS, reason="Not reliable on single threaded test on travis." |
| 83 | + cidfiles_count = sum(1 for _ in tmp_path.glob("*.cid")) |
| 84 | + os.chdir(cwd) |
| 85 | + |
| 86 | + assert "completed success" in stderr, stderr |
| 87 | + assert cidfiles_count == 0 |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.skipif( |
| 91 | + not LINUX or TRAVIS, |
| 92 | + reason="Linux only & not reliable on single threaded test on Travis-CI.", |
| 93 | +) |
| 94 | +def test_udocker_should_display_memory_usage(udocker: str, tmp_path: Path) -> None: |
| 95 | + """Confirm that memory ussage is logged even with udocker.""" |
| 96 | + cwd = Path.cwd() |
| 97 | + os.chdir(tmp_path) |
| 98 | + error_code, stdout, stderr = get_main_output( |
| 99 | + [ |
| 100 | + "--enable-ext", |
| 101 | + "--default-container=debian", |
| 102 | + "--user-space-docker-cmd=" + udocker, |
| 103 | + get_data("tests/wf/timelimit.cwl"), |
| 104 | + "--sleep_time", |
| 105 | + "10", |
| 106 | + ] |
94 | 107 | )
|
95 |
| - def test_udocker_should_display_memory_usage(self, tmpdir: py.path.local) -> None: |
96 |
| - cwd = tmpdir.chdir() |
97 |
| - error_code, stdout, stderr = get_main_output( |
98 |
| - [ |
99 |
| - "--enable-ext", |
100 |
| - "--default-container=debian", |
101 |
| - "--user-space-docker-cmd=" + self.udocker_path, |
102 |
| - get_data("tests/wf/timelimit.cwl"), |
103 |
| - "--sleep_time", |
104 |
| - "10", |
105 |
| - ] |
106 |
| - ) |
107 |
| - cwd.chdir() |
108 |
| - tmpdir.remove(ignore_errors=True) |
109 |
| - |
110 |
| - assert "completed success" in stderr, stderr |
111 |
| - assert "Max memory" in stderr, stderr |
| 108 | + os.chdir(cwd) |
| 109 | + |
| 110 | + assert "completed success" in stderr, stderr |
| 111 | + assert "Max memory" in stderr, stderr |
0 commit comments