1
|
|
|
import os |
2
|
|
|
import shutil |
3
|
|
|
|
4
|
|
|
from StringIO import StringIO |
5
|
|
|
from os.path import join as pjoin |
6
|
|
|
|
7
|
|
|
import tempfile |
8
|
|
|
from nose.tools import assert_true, assert_equal |
9
|
|
|
from numpy.testing import assert_array_equal |
10
|
|
|
|
11
|
|
|
import smartdispatch |
12
|
|
|
from smartdispatch import utils |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def test_generate_name_from_command(): |
16
|
|
|
date_length = 20 |
17
|
|
|
|
18
|
|
|
command = "command arg1 arg2" |
19
|
|
|
expected = "_".join(command.split()) |
20
|
|
|
assert_equal(smartdispatch.generate_name_from_command(command)[date_length:], expected) |
21
|
|
|
|
22
|
|
|
max_length_arg = 7 |
23
|
|
|
long_arg = "veryverylongarg1" |
24
|
|
|
command = "command " + long_arg + " arg2" |
25
|
|
|
expected = command.split() |
26
|
|
|
expected[1] = long_arg[-max_length_arg:] |
27
|
|
|
expected = "_".join(expected) |
28
|
|
|
assert_equal(smartdispatch.generate_name_from_command(command, max_length_arg)[date_length:], expected) |
29
|
|
|
|
30
|
|
|
max_length = 23 |
31
|
|
|
command = "command veryverylongarg1 veryverylongarg1 veryverylongarg1 veryverylongarg1" |
32
|
|
|
expected = command[:max_length].replace(" ", "_") |
33
|
|
|
assert_equal(smartdispatch.generate_name_from_command(command, max_length=max_length + date_length)[date_length:], expected) |
34
|
|
|
|
35
|
|
|
# Test path arguments in command |
36
|
|
|
command = "command path/number/one path/number/two" |
37
|
|
|
expected = "command_pathnumberone_pathnumbertwo" |
38
|
|
|
assert_equal(smartdispatch.generate_name_from_command(command)[date_length:], expected) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def test_get_commands_from_file(): |
42
|
|
|
commands = ["command1 arg1 arg2", |
43
|
|
|
"command2", |
44
|
|
|
"command3 arg1 arg2 arg3 arg4"] |
45
|
|
|
fileobj = StringIO("\n".join(commands)) |
46
|
|
|
assert_array_equal(smartdispatch.get_commands_from_file(fileobj), commands) |
47
|
|
|
|
48
|
|
|
# Test stripping last line if empty |
49
|
|
|
fileobj = StringIO("\n".join(commands) + "\n") |
50
|
|
|
assert_array_equal(smartdispatch.get_commands_from_file(fileobj), commands) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_unfold_command(): |
54
|
|
|
# Test with one argument |
55
|
|
|
cmd = "ls" |
56
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["ls"]) |
57
|
|
|
|
58
|
|
|
cmd = "echo 1" |
59
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["echo 1"]) |
60
|
|
|
|
61
|
|
|
# Test two arguments |
62
|
|
|
cmd = "echo [1 2]" |
63
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["echo 1", "echo 2"]) |
64
|
|
|
|
65
|
|
|
cmd = "echo test [1 2] yay" |
66
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["echo test 1 yay", "echo test 2 yay"]) |
67
|
|
|
|
68
|
|
|
cmd = "echo test[1 2]" |
69
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["echo test1", "echo test2"]) |
70
|
|
|
|
71
|
|
|
cmd = "echo test[1 2]yay" |
72
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["echo test1yay", "echo test2yay"]) |
73
|
|
|
|
74
|
|
|
# Test multiple folded arguments |
75
|
|
|
cmd = "python my_command.py [0.01 0.000001 0.00000000001] -1 [omicron mu]" |
76
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["python my_command.py 0.01 -1 omicron", |
77
|
|
|
"python my_command.py 0.01 -1 mu", |
78
|
|
|
"python my_command.py 0.000001 -1 omicron", |
79
|
|
|
"python my_command.py 0.000001 -1 mu", |
80
|
|
|
"python my_command.py 0.00000000001 -1 omicron", |
81
|
|
|
"python my_command.py 0.00000000001 -1 mu"]) |
82
|
|
|
|
83
|
|
|
# Test multiple folded arguments and not unfoldable brackets |
84
|
|
|
cmd = "python my_command.py [0.01 0.000001 0.00000000001] -1 \[[42 133,666]\] slow [omicron mu]" |
85
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["python my_command.py 0.01 -1 [42] slow omicron", |
86
|
|
|
"python my_command.py 0.01 -1 [42] slow mu", |
87
|
|
|
"python my_command.py 0.01 -1 [133,666] slow omicron", |
88
|
|
|
"python my_command.py 0.01 -1 [133,666] slow mu", |
89
|
|
|
"python my_command.py 0.000001 -1 [42] slow omicron", |
90
|
|
|
"python my_command.py 0.000001 -1 [42] slow mu", |
91
|
|
|
"python my_command.py 0.000001 -1 [133,666] slow omicron", |
92
|
|
|
"python my_command.py 0.000001 -1 [133,666] slow mu", |
93
|
|
|
"python my_command.py 0.00000000001 -1 [42] slow omicron", |
94
|
|
|
"python my_command.py 0.00000000001 -1 [42] slow mu", |
95
|
|
|
"python my_command.py 0.00000000001 -1 [133,666] slow omicron", |
96
|
|
|
"python my_command.py 0.00000000001 -1 [133,666] slow mu"]) |
97
|
|
|
|
98
|
|
|
# Test multiple different folded arguments |
99
|
|
|
cmd = "python my_command.py [0.01 0.001] -[1:5] slow" |
100
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["python my_command.py 0.01 -1 slow", |
101
|
|
|
"python my_command.py 0.01 -2 slow", |
102
|
|
|
"python my_command.py 0.01 -3 slow", |
103
|
|
|
"python my_command.py 0.01 -4 slow", |
104
|
|
|
"python my_command.py 0.001 -1 slow", |
105
|
|
|
"python my_command.py 0.001 -2 slow", |
106
|
|
|
"python my_command.py 0.001 -3 slow", |
107
|
|
|
"python my_command.py 0.001 -4 slow"]) |
108
|
|
|
|
109
|
|
|
cmd = "python my_command.py -[1:5] slow [0.01 0.001]" |
110
|
|
|
assert_equal(smartdispatch.unfold_command(cmd), ["python my_command.py -1 slow 0.01", |
111
|
|
|
"python my_command.py -1 slow 0.001", |
112
|
|
|
"python my_command.py -2 slow 0.01", |
113
|
|
|
"python my_command.py -2 slow 0.001", |
114
|
|
|
"python my_command.py -3 slow 0.01", |
115
|
|
|
"python my_command.py -3 slow 0.001", |
116
|
|
|
"python my_command.py -4 slow 0.01", |
117
|
|
|
"python my_command.py -4 slow 0.001"]) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def test_replace_uid_tag(): |
121
|
|
|
command = "command without uid tag" |
122
|
|
|
assert_array_equal(smartdispatch.replace_uid_tag([command]), [command]) |
123
|
|
|
|
124
|
|
|
command = "command with one {UID} tag" |
125
|
|
|
uid = utils.generate_uid_from_string(command) |
126
|
|
|
assert_array_equal(smartdispatch.replace_uid_tag([command]), [command.replace("{UID}", uid)]) |
127
|
|
|
|
128
|
|
|
command = "command with two {UID} tag {UID}" |
129
|
|
|
uid = utils.generate_uid_from_string(command) |
130
|
|
|
assert_array_equal(smartdispatch.replace_uid_tag([command]), [command.replace("{UID}", uid)]) |
131
|
|
|
|
132
|
|
|
commands = ["a command with a {UID} tag"] * 10 |
133
|
|
|
uid = utils.generate_uid_from_string(commands[0]) |
134
|
|
|
assert_array_equal(smartdispatch.replace_uid_tag(commands), [commands[0].replace("{UID}", uid)] * len(commands)) |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def test_get_available_queues(): |
138
|
|
|
assert_equal(smartdispatch.get_available_queues(cluster_name=None), {}) |
139
|
|
|
assert_equal(smartdispatch.get_available_queues(cluster_name="unknown"), {}) |
140
|
|
|
|
141
|
|
|
queues_infos = smartdispatch.get_available_queues(cluster_name="guillimin") |
142
|
|
|
assert_true(len(queues_infos) > 0) |
143
|
|
|
|
144
|
|
|
queues_infos = smartdispatch.get_available_queues(cluster_name="mammouth") |
145
|
|
|
assert_true(len(queues_infos) > 0) |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
def test_get_job_folders(): |
149
|
|
|
temp_dir = tempfile.mkdtemp() |
150
|
|
|
jobname = "this_is_the_name_of_my_job" |
151
|
|
|
job_folders_paths = smartdispatch.get_job_folders(temp_dir, jobname) |
152
|
|
|
path_job, path_job_logs, path_job_commands = job_folders_paths |
153
|
|
|
|
154
|
|
|
assert_true(jobname in path_job) |
155
|
|
|
assert_true(os.path.isdir(path_job)) |
156
|
|
|
assert_equal(os.path.basename(path_job), jobname) |
157
|
|
|
|
158
|
|
|
assert_true(jobname in path_job_logs) |
159
|
|
|
assert_true(os.path.isdir(path_job_logs)) |
160
|
|
|
assert_true(os.path.isdir(pjoin(path_job_logs, 'worker'))) |
161
|
|
|
assert_true(os.path.isdir(pjoin(path_job_logs, 'job'))) |
162
|
|
|
assert_true(os.path.isdir(path_job_logs)) |
163
|
|
|
assert_equal(os.path.basename(path_job_logs), "logs") |
164
|
|
|
|
165
|
|
|
assert_true(jobname in path_job_commands) |
166
|
|
|
assert_true(os.path.isdir(path_job_commands)) |
167
|
|
|
assert_equal(os.path.basename(path_job_commands), "commands") |
168
|
|
|
|
169
|
|
|
# In theory the following should not create new folders. |
170
|
|
|
# Insteead it will return the paths to existing folders. |
171
|
|
|
jobname += "2" |
172
|
|
|
os.rename(path_job, path_job + "2") |
173
|
|
|
job_folders_paths = smartdispatch.get_job_folders(temp_dir, jobname) |
174
|
|
|
path_job, path_job_logs, path_job_commands = job_folders_paths |
175
|
|
|
|
176
|
|
|
assert_true(jobname in path_job) |
177
|
|
|
assert_true(os.path.isdir(path_job)) |
178
|
|
|
assert_equal(os.path.basename(path_job), jobname) |
179
|
|
|
|
180
|
|
|
assert_true(jobname in path_job_logs) |
181
|
|
|
assert_true(os.path.isdir(path_job_logs)) |
182
|
|
|
assert_true(os.path.isdir(pjoin(path_job_logs, 'worker'))) |
183
|
|
|
assert_true(os.path.isdir(pjoin(path_job_logs, 'job'))) |
184
|
|
|
assert_true(os.path.isdir(path_job_logs)) |
185
|
|
|
assert_equal(os.path.basename(path_job_logs), "logs") |
186
|
|
|
|
187
|
|
|
assert_true(jobname in path_job_commands) |
188
|
|
|
assert_true(os.path.isdir(path_job_commands)) |
189
|
|
|
assert_equal(os.path.basename(path_job_commands), "commands") |
190
|
|
|
|
191
|
|
|
shutil.rmtree(temp_dir) |
192
|
|
|
|