File size: 4,496 Bytes
5631464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pytest

from haddock.libs.libgrid import (
    GridJob,
    CompositeGridJob,
    JobStatus,
    Tag,
    ping_dirac,
    validate_dirac,
)


@pytest.fixture
def dummy_paths(tmp_path):
    toppar = tmp_path / "toppar"
    toppar.mkdir()
    module = tmp_path / "module"
    module.mkdir()
    return toppar, module


def test_gridjob_create_jdl_and_job_script(dummy_paths):
    toppar, module = dummy_paths
    job = GridJob(input="input", toppar_path=toppar, module_path=module)
    job.name = "testjob"
    job.expected_outputs = ["foo.out"]
    job.jdl = job.loc / "job.jdl"
    job.job_script = job.loc / "job.sh"
    job.create_jdl()
    job.create_job_script()
    assert job.jdl.exists()
    assert job.job_script.exists()
    content = job.jdl.read_text()
    assert f'JobName = "{job.name}";' in content
    assert '"foo.out"' in content


def test_gridjob_process_input_f(tmp_path, dummy_paths):
    toppar, module = dummy_paths
    input_str = '$output_test = "result.txt";\n'
    job = GridJob(input=input_str, toppar_path=toppar, module_path=module)
    job.name = "testjob"
    job.loc = tmp_path
    job.input_str = input_str
    job.process_input_f()
    assert "result.txt" in job.expected_outputs
    inp_file = tmp_path / f"{job.name}.inp"
    assert inp_file.exists()


def test_compositegridjob_parse_input_and_process(dummy_paths):
    toppar, module = dummy_paths
    input_list = ["line1\nstop\n", "line2\nstop\n"]
    job = CompositeGridJob(input=input_list, toppar_path=toppar, module_path=module)
    assert len(job.input_str_list) == 2
    job.process_input_f()
    for idx in range(2):
        inp_file = job.loc / f"{idx}_{job.name}.inp"
        assert inp_file.exists()


def test_gridjob_repr(dummy_paths):
    toppar, module = dummy_paths
    job = GridJob(input="input", toppar_path=toppar, module_path=module)
    s = repr(job)
    assert "ID:" in s and "Name:" in s and "Status:" in s


def test_jobstatus_from_string():
    assert JobStatus.from_string("Running") == JobStatus.RUNNING
    assert JobStatus.from_string("unknown") == JobStatus.UNKNOWN


def test_tag_enum():
    assert Tag.PROBING.value == "Probing"
    assert Tag.DEFAULT.value == "Default"


def test_parse_output():
    s = "JobID=123 Status=Running Site=TestSite;"
    d = GridJob.parse_output(s)
    assert d["JobID"] == "123"
    assert d["Status"] == "Running"
    assert d["Site"] == "TestSite"


def test_process_line_and_find_output():
    line = '$output_test = "foo.txt";'
    new_line, found = GridJob._process_line(line)
    assert new_line == line
    output = GridJob._find_output(line)
    assert output == "foo.txt"


def test_clean_timings(dummy_paths):
    toppar, module = dummy_paths
    job = GridJob(input="input", toppar_path=toppar, module_path=module)
    job.timings = {"foo": 123}
    job.clean_timings()
    assert job.timings == {}


def test_clean_removes_tmpdir(dummy_paths):
    toppar, module = dummy_paths
    job = GridJob(input="input", toppar_path=toppar, module_path=module)
    loc = job.loc
    assert loc.exists()
    job.clean()
    assert not loc.exists()


def test_ping_and_validate_dirac(monkeypatch):
    monkeypatch.setattr("shutil.which", lambda cmd: "/usr/bin/" + cmd)
    monkeypatch.setattr(
        "subprocess.run",
        lambda *a, **k: type(
            "R", (), {"returncode": 0, "stderr": b"", "stdout": b""}
        )(),
    )
    assert validate_dirac()
    assert ping_dirac()


def test_process_line(tmp_path, monkeypatch):
    # No match
    line = "no pattern here"
    new_line, found = GridJob._process_line(line)
    assert new_line == line
    assert found is None

    # AT_PATTERN match and file exists
    test_file = tmp_path / "foo.txt"
    test_file.write_text("data")
    line = f"@@{test_file}"
    monkeypatch.chdir(tmp_path)
    new_line, found = GridJob._process_line(line)
    assert new_line == line.replace(str(test_file), "foo.txt")
    assert found == str(test_file)

    # VAR_PATTERN match and file does not exist
    line = '($ test="bar.txt")'
    new_line, found = GridJob._process_line(line)
    assert new_line == line
    assert found is None


def test_find_output():
    line = '$output_test = "foo.txt";'
    output = GridJob._find_output(line)
    assert output == "foo.txt"

    line2 = "$output_test = bar.txt;"
    output2 = GridJob._find_output(line2)
    assert output2 == "bar.txt"

    line3 = "no output here"
    output3 = GridJob._find_output(line3)
    assert output3 is None