Completed
Pull Request — master (#166)
by
unknown
27s
created

TestSlurm   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 66
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
B _test_param() 0 19 6
A test_priority() 0 6 1
A test_constraint() 0 6 1
A test_nb_cpus() 0 7 1
A tearDown() 0 3 2
A test_gres() 0 7 1
A test_memory() 0 6 1
1
from glob import glob
2
import os
3
import time
4
import unittest
5
6
from subprocess import Popen, PIPE
7
8
pbs_string = """\
9
#!/usr/bin/env /bin/bash
10
11
#PBS -N arrayJob
12
#PBS -o arrayJob_%A_%a.out
13
#PBS -l walltime=01:00:00
14
{}
15
16
######################
17
# Begin work section #
18
######################
19
20
echo "My SLURM_ARRAY_JOB_ID:" $SLURM_ARRAY_JOB_ID
21
echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID
22
nvidia-smi
23
"""
24
25
sbatch_string = """\
26
#!/usr/bin/env -i /bin/zsh
27
28
#SBATCH --job-name=arrayJob
29
#SBATCH --output=arrayJob_%A_%a.out
30
#SBATCH --time=01:00:00
31
{}
32
33
######################
34
# Begin work section #
35
######################
36
37
echo "My SLURM_ARRAY_JOB_ID:" $SLURM_ARRAY_JOB_ID
38
echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID
39
nvidia-smi
40
"""
41
42
43
class TestSlurm(unittest.TestCase):
44
45
    def tearDown(self):
46
        for file_name in (glob('*.out') + ["test.pbs"]):
47
            os.remove(file_name)
48
49
    def _test_param(self, param_array, command, flag, string=pbs_string, output_array=None):
50
        output_array = output_array or param_array
51
        for param, output in zip(param_array, output_array):
52
            com = pbs_string.format(
53
                string.format(command.format(param))
54
            )
55
            with open("test.pbs", "w") as text_file:
56
                text_file.write(com)
57
            process = Popen("sbatch test.pbs", stdout=PIPE, stderr=PIPE, shell=True)
58
            stdout, _ = process.communicate()
59
            stdout = stdout.decode()
60
            self.assertIn("Submitted batch job", stdout)
61
            job_id = stdout.split(" ")[-1].strip()
62
63
            time.sleep(0.25)
64
            process = Popen("squeue -u $USER -j {} -O {}".format(job_id, flag), stdout=PIPE, stderr=PIPE, shell=True)
65
            stdout, _ = process.communicate()
66
            job_params = [c.strip() for c in stdout.decode().split("\n")[1:] if c != '']
67
            self.assertSequenceEqual(job_params, [output for _ in range(len(job_params))])
68
69
    def test_priority(self):
70
        self._test_param(
71
            ['high', 'low'],
72
            "#SBATCH --qos={}",
73
            "qos",
74
            pbs_string
75
        )
76
77
    def test_gres(self):
78
        self._test_param(
79
            ["1", "2"],
80
            "#PBS -l naccelerators={}",
81
            "gres",
82
            pbs_string,
83
            ["gpu:1", "gpu:2"]
84
        )
85
86
    def test_memory(self):
87
        self._test_param(
88
            ["2G", "4G"],
89
            "#PBS -l mem={}",
90
            "minmemory",
91
            pbs_string
92
        )
93
94
    def test_nb_cpus(self):
95
        self._test_param(
96
            ["2", "3"],
97
            "#PBS -l mppdepth={}",
98
            # "#SBATCH --cpus-per-task={}",
99
            "numcpus",
100
            pbs_string
101
        )
102
103
    def test_constraint(self):
104
        self._test_param(
105
            ["gpu6gb", "gpu8gb"],
106
            "#PBS -l proc={}",
107
            "feature",
108
            pbs_string
109
        )
110
111
if __name__ == '__main__':
112
    unittest.main()
113