|
5 | 5 | import shutil
|
6 | 6 | import subprocess
|
7 | 7 | import unittest
|
8 |
| -from unittest.mock import patch |
9 | 8 |
|
10 | 9 | from simple_slurm import Slurm
|
11 | 10 |
|
@@ -209,34 +208,15 @@ def test_13_output_env_vars(self):
|
209 | 208 |
|
210 | 209 | def test_14_srun_returncode(self):
|
211 | 210 | slurm = Slurm(contiguous=True)
|
212 |
| - if shutil.which("srun") is not None: |
213 |
| - code = slurm.srun("echo Hello!") |
214 |
| - else: |
215 |
| - with patch("subprocess.run", subprocess_srun): |
216 |
| - code = slurm.srun("echo Hello!") |
| 211 | + code = slurm.srun("echo Hello!") |
217 | 212 | self.assertEqual(code, 0)
|
218 | 213 |
|
219 | 214 | def test_15_sbatch_execution(self):
|
220 |
| - with io.StringIO() as buffer: |
221 |
| - with contextlib.redirect_stdout(buffer): |
222 |
| - slurm = Slurm(contiguous=True) |
223 |
| - if shutil.which("sbatch") is not None: |
224 |
| - job_id = slurm.sbatch("echo Hello!") |
225 |
| - else: |
226 |
| - with patch("subprocess.run", subprocess_sbatch): |
227 |
| - job_id = slurm.sbatch("echo Hello!") |
228 |
| - stdout = buffer.getvalue() |
| 215 | + slurm = Slurm(contiguous=True) |
| 216 | + job_id, stdout = self.__run_sbatch(slurm) |
229 | 217 |
|
230 |
| - out_file = f"slurm-{job_id}.out" |
231 |
| - while True: # wait for job to finalize |
232 |
| - if os.path.isfile(out_file): |
233 |
| - break |
234 |
| - with open(out_file, "r") as fid: |
235 |
| - contents = fid.read() |
236 |
| - os.remove(out_file) |
237 | 218 | self.assertFalse(slurm.is_parsable)
|
238 | 219 | self.assertIsInstance(job_id, int)
|
239 |
| - self.assertIn("Hello!", contents) |
240 | 220 | self.assertIn(f"Submitted batch job {job_id}", stdout)
|
241 | 221 |
|
242 | 222 | def test_16_parse_timedelta(self):
|
@@ -281,36 +261,19 @@ def test_18_false_boolean_arguments(self):
|
281 | 261 |
|
282 | 262 | def test_19_sbatch_execution_with_job_file(self):
|
283 | 263 | job_file = "script.sh"
|
284 |
| - with io.StringIO() as buffer: |
285 |
| - with contextlib.redirect_stdout(buffer): |
286 |
| - slurm = Slurm(contiguous=True) |
287 |
| - if shutil.which("sbatch") is not None: |
288 |
| - job_id = slurm.sbatch("echo Hello!", job_file=job_file) |
289 |
| - else: |
290 |
| - with patch("subprocess.run", subprocess_sbatch): |
291 |
| - job_id = slurm.sbatch("echo Hello!", job_file=job_file) |
292 |
| - stdout = buffer.getvalue() |
293 | 264 |
|
| 265 | + slurm = Slurm(contiguous=True) |
| 266 | + job_id, stdout = self.__run_sbatch(slurm, job_file=job_file) |
| 267 | + |
| 268 | + self.assertFalse(slurm.is_parsable) |
294 | 269 | self.assertIsInstance(job_id, int)
|
| 270 | + self.assertIn(f"Submitted batch job {job_id}", stdout) |
295 | 271 |
|
296 |
| - out_file = f"slurm-{job_id}.out" |
297 |
| - while True: # wait for job to finalize |
298 |
| - if os.path.isfile(out_file): |
299 |
| - break |
300 |
| - # Assert the script was written correctly |
301 | 272 | with open(job_file, "r") as fid:
|
302 | 273 | job_contents = fid.read()
|
303 | 274 | os.remove(job_file)
|
304 | 275 |
|
305 |
| - self.assertEqual(job_contents, self.job_file_test_19) |
306 |
| - |
307 |
| - # Assert the script was executed correctly |
308 |
| - with open(out_file, "r") as fid: |
309 |
| - contents = fid.read() |
310 |
| - os.remove(out_file) |
311 |
| - |
312 |
| - self.assertIn("Hello!", contents) |
313 |
| - self.assertIn(f"Submitted batch job {job_id}", stdout) |
| 276 | + self.assertEqual(self.job_file_test_19, job_contents) |
314 | 277 |
|
315 | 278 | def test_20_add_cmd_single(self):
|
316 | 279 | slurm = Slurm(
|
@@ -367,54 +330,19 @@ def test_21_add_cmd_multiple(self):
|
367 | 330 | self.assertEqual(self.script + "\n" + self.commands, str(slurm))
|
368 | 331 |
|
369 | 332 | def test_22_parsable_sbatch_execution(self):
|
370 |
| - with io.StringIO() as buffer: |
371 |
| - with contextlib.redirect_stdout(buffer): |
372 |
| - slurm = Slurm(contiguous=True, parsable=True) |
373 |
| - if shutil.which("sbatch") is not None: |
374 |
| - job_id = slurm.sbatch("echo Hello!") |
375 |
| - else: |
376 |
| - with patch("subprocess.run", subprocess_sbatch_parsable): |
377 |
| - job_id = slurm.sbatch("echo Hello!") |
378 |
| - stdout = buffer.getvalue() |
| 333 | + slurm = Slurm(contiguous=True, parsable=True) |
| 334 | + job_id, stdout = self.__run_sbatch(slurm) |
379 | 335 |
|
380 |
| - out_file = f"slurm-{job_id}.out" |
381 |
| - while True: # wait for job to finalize |
382 |
| - if os.path.isfile(out_file): |
383 |
| - break |
384 |
| - with open(out_file, "r") as fid: |
385 |
| - contents = fid.read() |
386 |
| - os.remove(out_file) |
387 | 336 | self.assertTrue(slurm.is_parsable)
|
388 | 337 | self.assertIsInstance(job_id, int)
|
389 |
| - self.assertIn("Hello!", contents) |
390 | 338 | self.assertEqual(f"{job_id}\n", stdout)
|
391 | 339 |
|
392 |
| - |
393 |
| -def subprocess_srun(*args, **kwargs): |
394 |
| - print("Hello!!!") |
395 |
| - return subprocess.CompletedProcess(*args, returncode=0) |
396 |
| - |
397 |
| - |
398 |
| -def subprocess_sbatch(*args, **kwargs): |
399 |
| - job_id = 1234 |
400 |
| - out_file = f"slurm-{job_id}.out" |
401 |
| - with open(out_file, "w") as fid: |
402 |
| - fid.write("Hello!!!\n") |
403 |
| - stdout = f"Submitted batch job {job_id}" |
404 |
| - return subprocess.CompletedProcess( |
405 |
| - *args, returncode=1, stdout=stdout.encode("utf-8") |
406 |
| - ) |
407 |
| - |
408 |
| - |
409 |
| -def subprocess_sbatch_parsable(*args, **kwargs): |
410 |
| - job_id = 1234 |
411 |
| - out_file = f"slurm-{job_id}.out" |
412 |
| - with open(out_file, "w") as fid: |
413 |
| - fid.write("Hello!!!\n") |
414 |
| - stdout = str(job_id) |
415 |
| - return subprocess.CompletedProcess( |
416 |
| - *args, returncode=0, stdout=stdout.encode("utf-8") |
417 |
| - ) |
| 340 | + def __run_sbatch(self, slurm, *args, **kwargs): |
| 341 | + with io.StringIO() as buffer: |
| 342 | + with contextlib.redirect_stdout(buffer): |
| 343 | + job_id = slurm.sbatch("echo Hello!", *args, **kwargs) |
| 344 | + stdout = buffer.getvalue() |
| 345 | + return job_id, stdout |
418 | 346 |
|
419 | 347 |
|
420 | 348 | if __name__ == "__main__":
|
|
0 commit comments