-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_gent.py
204 lines (178 loc) · 6.6 KB
/
test_gent.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# coding: utf-8
# Copyright (c) Jan Janssen
import os
import pandas
import unittest
from pysqa import QueueAdapter
__author__ = "Jan Janssen"
__copyright__ = "Copyright 2019, Jan Janssen"
__version__ = "0.0.1"
__maintainer__ = "Jan Janssen"
__email__ = "janssen@mpie.de"
__status__ = "production"
__date__ = "Feb 9, 2019"
df_queue_status = pandas.DataFrame(
{
"cluster": ["Mycluster", "Mycluster", "Mycluster", "Mycluster", "Mycluster"],
"jobid": [5322019, 5322016, 5322017, 5322018, 5322013],
"user": ["janj", "janj", "janj", "janj", "janj"],
"jobname": ["pi_19576488", "pi_19576485", "pi_19576486", "pi_19576487", "pi_19576482"],
"status": ["r", "r", "r", "r", "r"],
}
)
class TestGentQueueAdapter(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.path = os.path.dirname(os.path.abspath(__file__))
cls.gent = QueueAdapter(directory=os.path.join(cls.path, "config/gent"))
def test_config(self):
self.assertEqual(self.gent.config["queue_type"], "GENT")
self.assertEqual(self.gent.config["queue_primary"], "slurm")
def test_list_clusters(self):
self.assertEqual(self.gent.list_clusters(), ['default'])
def test_ssh_delete_file_on_remote(self):
self.assertEqual(self.gent.ssh_delete_file_on_remote, True)
def test_interfaces(self):
self.assertEqual(
self.gent._adapter._commands.get_queue_status_command,
["squeue", "--format", "%A|%u|%t|%.15j|%Z", "--noheader"],
)
def test__list_command_to_be_executed(self):
with self.subTest("gent"):
self.assertEqual(
self.gent._adapter._list_command_to_be_executed(None, "here"),
["sbatch", "--parsable", "here"],
)
with self.subTest("gent with dependency"):
self.assertRaises(
NotImplementedError,
self.gent._adapter._list_command_to_be_executed,
[],
"here",
)
def test_get_job_id_from_output(self):
self.assertEqual(self.gent._adapter._commands.get_job_id_from_output("123;MyQueue"), 123)
def test_get_queue_from_output(self):
self.assertEqual(self.gent._adapter._commands.get_queue_from_output("123;MyQueue"), "MyQueue")
def test_convert_queue_status_slurm(self):
with open(os.path.join(self.path, "config/gent", "gent_output"), "r") as f:
content = f.read()
self.assertTrue(
df_queue_status.equals(
self.gent._adapter._commands.convert_queue_status(
queue_status_output=content
)
)
)
def test_switch_cluster_command(self):
self.assertEqual(
self.gent._adapter._switch_cluster_command(cluster_module="module1"),
['module', '--quiet', 'swap', 'cluster/module1;']
)
def test_resolve_queue_id(self):
self.assertEqual(
self.gent._adapter._resolve_queue_id(process_id=20120, cluster_dict={0: "cluster1"}),
("cluster1", 2012)
)
def test_submit_job_no_output(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
pass
gent_tmp = QueueAdapter(
directory=os.path.join(self.path, "config/gent"),
execute_command=execute_command
)
self.assertIsNone(gent_tmp.submit_job(
queue="slurm",
job_name="test",
working_directory=".",
command="echo hello"
))
os.remove("run_queue.sh")
def test_submit_job_with_output(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
return "123;cluster0"
gent_tmp = QueueAdapter(
directory=os.path.join(self.path, "config/gent"),
execute_command=execute_command
)
self.assertEqual(gent_tmp.submit_job(
queue="slurm",
job_name="test",
working_directory=".",
command="echo hello"
), 1230)
os.remove("run_queue.sh")
def test_delete_job_no_output(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
pass
gent_tmp = QueueAdapter(
directory=os.path.join(self.path, "config/gent"),
execute_command=execute_command
)
self.assertIsNone(gent_tmp.delete_job(process_id=1))
def test_delete_job_with_output(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
return 0, 1
gent_tmp = QueueAdapter(
directory=os.path.join(self.path, "config/gent"),
execute_command=execute_command
)
self.assertEqual(gent_tmp.delete_job(process_id=1), 0)
def test_get_queue_status(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
with open(os.path.join(self.path, "config", "gent", "gent_output")) as f:
return f.read()
gent_tmp = QueueAdapter(
directory=os.path.join(self.path, "config/gent"),
execute_command=execute_command
)
self.assertTrue(pandas.concat([df_queue_status] * 3).reset_index(drop=True).equals(
gent_tmp.get_queue_status())
)
def test_get_queue_status_user(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
with open(os.path.join(self.path, "config", "gent", "gent_output")) as f:
return f.read()
gent_tmp = QueueAdapter(
directory=os.path.join(self.path, "config/gent"),
execute_command=execute_command
)
self.assertTrue(pandas.concat([df_queue_status] * 3).reset_index(drop=True).equals(
gent_tmp.get_queue_status(user="janj"))
)