Debian Codegraph @

src/sluurp/tests/test_job.py

raw symbols SCIP
1 import pytest
2 import logging
3 from sluurp.job import SBatchScriptJob, ScriptJob, submit_sbatch_job
4 import os
5 import platform as _platform_mod
6 from concurrent import futures
7 from sluurp.utils import (
8 SubProcessCommand,
9 has_sbatch_available,
10 has_scontrol_available,
11 )
12
13
14 def run_async(job):
15 job.run()
16 return job.result
17
18
19 @pytest.mark.skipif(
20 _platform_mod.system() != "Linux", reason="job script only works on linux platform"
21 )
22 def test_job_script(tmp_path):
23 test_dir = tmp_path / "test_image_key_upgrader"
24 script_path = test_dir / "my_script.sh"
25
26 with pytest.raises(TypeError):
27 SBatchScriptJob(script="echo test", script_path=script_path, clean_script=False)
28
29 script_job = SBatchScriptJob(
30 script=("echo test",), script_path=script_path, clean_script=False
31 )
32 script_job.dry_run = True
33 assert not os.path.exists(script_path)
34 script_job.run()
35 assert os.path.exists(script_path)
36 assert len(os.listdir(test_dir)) == 1
37
38
39 @pytest.mark.asyncio
40 @pytest.mark.skipif(
41 _platform_mod.system() != "Linux", reason="job script only works on linux platform"
42 )
43 async def test_job_script_submission(tmp_path):
44 test_dir = tmp_path / "test_image_key_upgrader"
45 script_path = test_dir / "my_script.sh"
46
47 sleep_time = 4
48 assert isinstance(sleep_time, int)
49
50 class MyBashScriptJob(ScriptJob):
51 def run(self):
52 script_path = self._save_script()
53 if not os.path.exists(script_path):
54 raise OSError(
55 f"{script_path} doesn't exists. Unable to write script file. Can be write rights error."
56 )
57 self._result = SubProcessCommand(command=f"bash {script_path}").run()
58 if self.clean_script:
59 self._do_script_cleaning()
60
61 script_job = MyBashScriptJob(
62 script=(f"sleep {sleep_time}s", "echo 'succeed'"),
63 script_path=script_path,
64 clean_script=False,
65 )
66
67 with futures.ThreadPoolExecutor() as executor:
68 future = executor.submit(run_async, script_job)
69 result = future.result(timeout=10)
70 assert result == "succeed\n"
71
72
73 @pytest.mark.asyncio
74 @pytest.mark.skipif(
75 not (has_sbatch_available() and has_scontrol_available()),
76 reason="slurm command not available",
77 )
78 @pytest.mark.skipif(
79 _platform_mod.system() != "Linux", reason="job script only works on linux platform"
80 )
81 async def test_sbatchjob_script_submission(tmp_path):
82 test_dir = tmp_path / "test_image_key_upgrader"
83 script_path = test_dir / "my_script_for_slurm.sh"
84
85 sleep_time = 20
86 assert isinstance(sleep_time, int)
87 script_job = SBatchScriptJob(
88 script=(f"sleep {sleep_time}s", "echo 'succeed'"),
89 script_path=script_path,
90 clean_script=False,
91 )
92
93 with futures.ThreadPoolExecutor() as executor:
94 future = executor.submit(submit_sbatch_job, script_job)
95 future.result(timeout=10)
96
97
98 @pytest.mark.asyncio
99 @pytest.mark.skipif(
100 not (has_sbatch_available() and has_scontrol_available()),
101 reason="slurm command not available",
102 )
103 async def test_sbatchjob_script_submission_with_slurm_config(tmp_path):
104 """
105 test sbatch job submission in dry_mode
106 """
107 test_dir = tmp_path / "test_image_key_upgrader"
108 script_path = test_dir / "my_script_for_slurm.sh"
109
110 sleep_time = 20
111 assert isinstance(sleep_time, int)
112 script_job = SBatchScriptJob(
113 script=(f"sleep {sleep_time}s", "echo 'succeed'"),
114 script_path=script_path,
115 clean_script=False,
116 slurm_config={
117 "memory": "15GB",
118 "partition": "my_partition",
119 "n_gpus": 2,
120 "python_venv": "path/to/my/python/venv",
121 },
122 )
123 script_job.dry_run = True
124
125 with futures.ThreadPoolExecutor() as executor:
126 future = executor.submit(submit_sbatch_job, script_job)
127 future.result(timeout=10)
128
129 lines = []
130 with open(script_path, mode="r") as file_object:
131 [lines.append(line) for line in file_object]
132
133 assert lines == [
134 "#!/bin/bash -l\n",
135 "#SBATCH --mem=15GB\n",
136 "#SBATCH -p my_partition\n",
137 "#SBATCH --gres=gpu:2\n",
138 "source path/to/my/python/venv\n",
139 f"sleep {sleep_time}s\n",
140 "echo 'succeed'\n",
141 ]
142
143
144 def test_interpret_slurm_config(caplog):
145 """
146 insure interpretation done by 'interpret_slurm_config' works
147 """
148 caplog.set_level(logging.INFO)
149 SBatchScriptJob.interpret_slurm_config({"new_key": "test"})
150 assert "Unknow slurm configuration key: new_key" in caplog.text
151
152 assert SBatchScriptJob.interpret_slurm_config(
153 {"cpu-per-task": 2, "n_tasks": 1, "memory": None}
154 ) == (
155 (
156 "#SBATCH --cpus-per-task=2",
157 "#SBATCH --ntasks=1",
158 ),
159 (),
160 )
161
162 assert SBatchScriptJob.interpret_slurm_config(
163 {
164 "python_venv": "my_python",
165 "walltime": None,
166 "memory": "10",
167 "job_name": "my_project",
168 }
169 ) == (
170 (
171 "#SBATCH --mem=10GB",
172 "#SBATCH -J 'my_project'",
173 ),
174 ("source my_python",),
175 )
176
177 assert SBatchScriptJob.interpret_slurm_config(
178 {"n_gpus": "3", "partition": "my_partition"}
179 ) == (
180 (
181 "#SBATCH --gres=gpu:3",
182 "#SBATCH -p my_partition",
183 ),
184 (),
185 )
186
187 assert SBatchScriptJob.interpret_slurm_config(
188 {
189 "walltime": None,
190 "memory": "10",
191 "modules": (
192 "tomotools",
193 "pycharm/11.7.1",
194 ),
195 },
196 sbatch_extra_params={
197 "gpu_card": "a40"
198 }, # just to make sure this doesn't add gpu options of no gpu requested
199 ) == (
200 ("#SBATCH --mem=10GB",),
201 ("module load tomotools", "module load pycharm/11.7.1"),
202 )
203
204 assert SBatchScriptJob.interpret_slurm_config(
205 slurm_config={"n_gpus": "3", "partition": "my_partition"},
206 sbatch_extra_params={"gpu_card": "a40"},
207 ) == (
208 (
209 "#SBATCH --gres=gpu:3 -C a40",
210 "#SBATCH -p my_partition",
211 ),
212 (),
213 )
214
215
216 def test_strip_gpu_card_name():
217 """test `strip_gpu_card_name` function"""
218 assert SBatchScriptJob.strip_gpu_card_name("a40") == "a40"
219 assert SBatchScriptJob.strip_gpu_card_name("nvidia_a40") == "a40"
220 assert SBatchScriptJob.strip_gpu_card_name("tesla_56") == "56"
221 assert SBatchScriptJob.strip_gpu_card_name("tesla_v100-pcie-32gb") == "v100"