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

TestSlurm.test_gres()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
import time
2
import unittest
3
4
5
from subprocess import Popen, PIPE
6
from nose.tools import assert_equal, assert_true
7
8
pbs_string = """\
9
#!/usr/bin/env /bin/bash
10
11
#PBS -N arrayJob
12
#PBS -o arrayJob_%A_%a.out
13
#PBS -t 1-2
14
#PBS -l walltime=01:00:00
15
{}
16
17
######################
18
# Begin work section #
19
######################
20
21
echo "My SLURM_ARRAY_JOB_ID:" $SLURM_ARRAY_JOB_ID
22
echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID
23
nvidia-smi
24
"""
25
26
sbatch_string = """\
27
#!/usr/bin/env -i /bin/zsh
28
29
#SBATCH --job-name=arrayJob
30
#SBATCH --output=arrayJob_%A_%a.out
31
#SBATCH --array=0-5
32
#SBATCH --time=01:00:00
33
#SBATCH --gres=gpu
34
#SBATCH --constraint=gpu6gb
35
36
######################
37
# Begin work section #
38
######################
39
40
echo "My SLURM_ARRAY_JOB_ID:" $SLURM_ARRAY_JOB_ID
41
echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID
42
nvidia-smi
43
"""
44
45
def test_param(param_array, com, flag, string=pbs_string):
46
    for param in param_array:
47
        command = pbs_string.format(
48
            string.format(com.format(param))
49
        )
50
        with open("test.pbs", "w") as text_file:
51
            text_file.write(command)
52
        process = Popen("sbatch test.pbs", stdout=PIPE, stderr=PIPE, shell=True)
53
        stdout, stderr = process.communicate()
54
        assert_true("Submitted batch job" in stdout)
55
        job_id = stdout.split(" ")[-1].strip()
56
57
        time.sleep(0.25)
58
        process = Popen("squeue -u $USER -j {} -O {}".format(job_id, flag), stdout=PIPE, stderr=PIPE, shell=True)
59
        stdout, stderr = process.communicate()
60
        job_params = [c.strip() for c in stdout.split("\n")[1:] if c != '']
61
        # import ipdb; ipdb.set_trace()
62
        assert_true(all(p == param for p in job_params))
63
64
class TestSlurm(unittest.TestCase):
65
66
    def tearDown(self):
67
        process = Popen("rm *.out test.pbs", stdout=PIPE, stderr=PIPE, shell=True)
68
        stdout, stderr = process.communicate()
69
70
    def test_priority(self):
71
        test_param(
72
            ['high', 'low'],
73
            "#SBATCH --qos={}",
74
            "qos",
75
            pbs_string
76
        )
77
78
    def test_gres(self):
79
        test_param(
80
            ['titanblack'],
81
            "#PBS -l naccelerators={}",
82
            "gres",
83
            pbs_string
84
        )
85
86
    def test_memory(self):
87
        test_param(
88
            ['2G', '4G'],
89
            "#PBS -l mem={}",
90
            "minmemory",
91
            pbs_string
92
        )
93
94
    def test_nb_cpus(self):
95
        test_param(
96
            ["1", "2"],
97
            "PBS -l ncpus={}",
98
            "numcpus",
99
            pbs_string
100
        )
101
102
    def test_constraint(self):
103
        test_param(
104
            ["gpu6gb", "gpu8gb"],
105
            "PBS -l proc={}",
106
            "feature",
107
            pbs_string
108
        )
109
110
#
111
# pbs_string2 = """\
112
# #!/usr/bin/env /bin/bash
113
#
114
# #PBS -N arrayJob
115
# #PBS -o arrayJob_%A_%a.out
116
# #PBS -t 1-5
117
# #PBS -l walltime=01:00:00
118
# #PBS -l naccelerators=1
119
# #PBS -l proc={cons}
120
# #PBS -M {mail}
121
# #SBATCH --qos {priority}
122
#
123
# ######################
124
# # Begin work section #
125
# ######################
126
#
127
# echo "My SLURM_ARRAY_JOB_ID:" $SLURM_ARRAY_JOB_ID
128
# echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID
129
# nvidia-smi
130
# """
131
132
if __name__ == '__main__':
133
    unittest.main()
134