-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_shellcmd.py
executable file
·451 lines (376 loc) · 14.2 KB
/
test_shellcmd.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#! /usr/bin/env python
#
"""
Unit tests for the `gc3libs.backends.shellcmd` module.
"""
# Copyright (C) 2011-2015 S3IT, Zentrale Informatik, University of Zurich. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
__docformat__ = 'reStructuredText'
import os
import shutil
import tempfile
import time
import pytest
import gc3libs
import gc3libs.config
import gc3libs.core
from gc3libs.quantity import Memory
class TestBackendShellcmd(object):
CONF = """
[resource/localhost_test]
type=shellcmd
transport=local
# time_cmd=/usr/bin/time
# Use unusual values so that we can easily spot if the `override` option works
max_cores=123
max_cores_per_job=123
max_memory_per_core=999
max_walltime=2
architecture=x64_64
auth=noauth
enabled=True
override=False
resourcedir=%s
[auth/noauth]
type=none
"""
@pytest.fixture(autouse=True)
def setUp(self):
(fd, cfgfile) = tempfile.mkstemp()
f = os.fdopen(fd, 'w+')
CONFIG = TestBackendShellcmd.CONF % (cfgfile + '.d')
f.write(CONFIG)
f.close()
self.files_to_remove = [cfgfile, cfgfile + '.d']
self.apps_to_kill = []
self.cfg = gc3libs.config.Configuration()
self.cfg.merge_file(cfgfile)
self.core = gc3libs.core.Core(self.cfg)
self.backend = self.core.get_backend('localhost_test')
# Update resource status
self.backend.get_resource_status()
yield
for fname in self.files_to_remove:
if os.path.isdir(fname):
shutil.rmtree(fname)
elif os.path.exists(fname):
os.remove(fname)
for app in self.apps_to_kill:
try:
self.core.kill(app)
except:
pass
try:
self.core.free(app)
except:
pass
def cleanup_file(self, fname):
self.files_to_remove.append(fname)
def run_until_terminating(self, app, max_wait=10, polling_interval=0.1):
"""
Wait until the given `app` task is done,
but timeout and raise an error if it takes too much time.
"""
waited = 0
while (app.execution.state != gc3libs.Run.State.TERMINATING
and waited < max_wait):
time.sleep(polling_interval)
waited += polling_interval
self.core.update_job_state(app)
def test_submission_ok(self):
"""Test a successful submission cycle and the backends' resource book-keeping"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
app = gc3libs.Application(
arguments=['/usr/bin/env'],
inputs=[],
outputs=[],
output_dir=tmpdir,
stdout="stdout.txt",
stderr="stderr.txt",
requested_cores=1, )
self.core.submit(app)
self.apps_to_kill.append(app)
self.cleanup_file(tmpdir)
self.cleanup_file(app.execution.lrms_execdir)
# there's no SUBMITTED state here: jobs go immediately into
# RUNNING state
assert app.execution.state == gc3libs.Run.State.SUBMITTED
assert self.backend.free_slots == 122
assert self.backend.user_queued == 0
assert self.backend.user_run == 1
self.run_until_terminating(app)
try:
assert app.execution.state == gc3libs.Run.State.TERMINATING
assert self.backend.free_slots == 123
assert self.backend.user_queued == 0
assert self.backend.user_run == 0
except:
self.core.fetch_output(app)
self.core.free(app)
raise
self.core.fetch_output(app)
try:
assert app.execution.state == gc3libs.Run.State.TERMINATED
assert self.backend.free_slots == 123
assert self.backend.user_queued == 0
assert self.backend.user_run == 0
except:
self.core.free(app)
raise
def test_check_app_after_reloading_session(self):
"""Check that the job status is still available the end of the starter script"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
app = gc3libs.Application(
arguments=['/usr/bin/env'],
inputs=[],
outputs=[],
output_dir=tmpdir,
stdout="stdout.txt",
stderr="stderr.txt",
requested_cores=1, )
self.core.submit(app)
self.apps_to_kill.append(app)
self.cleanup_file(app.execution.lrms_execdir)
# The wrapper process should die and write the final status
# and the output to a file, so that `Core` will be able to
# retrieve it.
self.run_until_terminating(app)
assert app.execution.state == gc3libs.Run.State.TERMINATING
assert app.execution.returncode == 0
def test_app_argument_with_spaces(self):
"""Check that arguments with spaces are not split"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
app = gc3libs.Application(
arguments=['/bin/ls', '-d', '/ /'],
inputs=[],
outputs=[],
output_dir=tmpdir,
stdout="stdout.txt",
stderr="stderr.txt",
requested_cores=1, )
self.core.submit(app)
self.apps_to_kill.append(app)
self.cleanup_file(app.execution.lrms_execdir)
self.run_until_terminating(app)
assert app.execution.state == gc3libs.Run.State.TERMINATING
assert app.execution.returncode != 0
def test_resource_usage(self):
"""Check book-keeping of core and memory resources"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
app = gc3libs.Application(
arguments=['/bin/echo', 'Hello', 'World'],
inputs=[],
outputs=[],
output_dir=tmpdir,
requested_cores=2,
requested_memory=10 * Memory.MB, )
cores_before = self.backend.free_slots
mem_before = self.backend.available_memory
self.core.submit(app)
self.apps_to_kill.append(app)
cores_after = self.backend.free_slots
mem_after = self.backend.available_memory
assert cores_before == cores_after + 2
assert mem_before == mem_after + app.requested_memory
self.run_until_terminating(app)
assert self.backend.free_slots == cores_before
assert self.backend.available_memory == mem_before
def test_env_vars_definition(self):
"""Check that `Application.environment` settings are correctly propagated"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
app = gc3libs.Application(
arguments=['/bin/echo', '$MSG'],
inputs=[],
outputs=[],
output_dir=tmpdir,
stdout='stdout.txt',
environment={'MSG':'OK'})
self.core.submit(app)
self.apps_to_kill.append(app)
self.run_until_terminating(app)
self.core.fetch_output(app)
stdout_file = os.path.join(app.output_dir, app.stdout)
assert os.path.exists(stdout_file)
assert os.path.isfile(stdout_file)
stdout_contents = open(stdout_file, 'r').read()
assert stdout_contents == 'OK\n'
def test_stdout_in_directory(self):
"""Check that `Application.stdout` can include a full path"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
app = gc3libs.Application(
arguments=['/bin/echo', 'OK'],
inputs=[],
outputs=[],
output_dir=tmpdir,
stdout='logs/stdout.txt')
self.core.submit(app)
self.apps_to_kill.append(app)
self.run_until_terminating(app)
self.core.fetch_output(app)
stdout_dir = os.path.join(app.output_dir, 'logs')
stdout_file = os.path.join(stdout_dir, 'stdout.txt')
assert os.path.exists(stdout_dir)
assert os.path.isdir(stdout_dir)
assert os.path.exists(stdout_file)
assert os.path.isfile(stdout_file)
stdout_contents = open(stdout_file, 'r').read()
assert stdout_contents == 'OK\n'
def test_not_enough_cores_usage(self):
"""Check that a `NoResources` exception is raised if more cores are requested than available"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
bigapp = gc3libs.Application(
arguments=['/bin/echo', 'Hello', 'World'],
inputs=[],
outputs=[],
output_dir=tmpdir,
requested_cores=self.backend.free_slots + 1,
requested_memory=10 * Memory.MiB, )
with pytest.raises(gc3libs.exceptions.NoResources):
self.core.submit(bigapp)
def test_not_enough_memory_usage(self):
"""Check that a `NoResources` exception is raised if more memory is requested than available"""
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
self.cleanup_file(tmpdir)
bigapp = gc3libs.Application(
arguments=['/bin/echo', 'Hello', 'World'],
inputs=[],
outputs=[],
output_dir=tmpdir,
requested_cores=1,
requested_memory=self.backend.total_memory + Memory.B, )
with pytest.raises(gc3libs.exceptions.NoResources):
self.core.submit(bigapp)
class TestBackendShellcmdCFG(object):
CONF = """
[resource/localhost_test]
type=shellcmd
transport=local
time_cmd=/usr/bin/time
max_cores=1000
max_cores_per_job=4
max_memory_per_core=2
max_walltime=2
architecture=x64_64
auth=noauth
enabled=True
override=%s
resourcedir=%s
[auth/noauth]
type=none
"""
@pytest.fixture(autouse=True)
def setUp(self):
self.files_to_remove = []
yield
for fname in self.files_to_remove:
if os.path.isdir(fname):
shutil.rmtree(fname)
elif os.path.exists(fname):
os.remove(fname)
def cleanup_file(self, fname):
self.files_to_remove.append(fname)
def test_override_cfg_flag(self):
(fd, cfgfile) = tempfile.mkstemp()
f = os.fdopen(fd, 'w+')
f.write(TestBackendShellcmdCFG.CONF % ("True", cfgfile + '.d'))
f.close()
self.files_to_remove = [cfgfile, cfgfile + '.d']
self.cfg = gc3libs.config.Configuration()
self.cfg.merge_file(cfgfile)
self.core = gc3libs.core.Core(self.cfg)
self.backend = self.core.get_backend('localhost_test')
# Update resource status
self.backend.get_resource_status()
assert self.backend.max_cores < 1000
def test_do_not_override_cfg_flag(self):
(fd, cfgfile) = tempfile.mkstemp()
f = os.fdopen(fd, 'w+')
f.write(TestBackendShellcmdCFG.CONF % ("False", cfgfile + '.d'))
f.close()
self.files_to_remove = [cfgfile, cfgfile + '.d']
self.cfg = gc3libs.config.Configuration()
self.cfg.merge_file(cfgfile)
self.core = gc3libs.core.Core(self.cfg)
self.backend = self.core.get_backend('localhost_test')
# Update resource status
assert self.backend.max_cores == 1000
def test_resource_sharing_w_multiple_backends(self):
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
(fd, cfgfile) = tempfile.mkstemp()
f = os.fdopen(fd, 'w+')
f.write(TestBackendShellcmdCFG.CONF % ("False", cfgfile + '.d'))
f.close()
self.files_to_remove = [cfgfile, cfgfile + '.d', tmpdir]
cfg1 = gc3libs.config.Configuration()
cfg1.merge_file(cfgfile)
cfg2 = gc3libs.config.Configuration()
cfg2.merge_file(cfgfile)
core1 = gc3libs.core.Core(cfg1)
core2 = gc3libs.core.Core(cfg2)
backend1 = core1.get_backend('localhost_test')
backend2 = core2.get_backend('localhost_test')
app = gc3libs.Application(
arguments=['/bin/echo', 'Hello', 'World'],
inputs=[],
outputs=[],
output_dir=tmpdir,
requested_cores=1,
requested_memory=10 * Memory.MiB, )
try:
core1.submit(app)
assert (backend1.free_slots ==
backend1.max_cores - app.requested_cores)
assert backend2.free_slots == backend2.max_cores
backend2.get_resource_status()
assert (backend2.free_slots ==
backend2.max_cores - app.requested_cores)
finally:
core1.kill(app)
core1.free(app)
def test_pid_list_is_string(self):
tmpdir = tempfile.mkdtemp(prefix=__name__, suffix='.d')
(fd, cfgfile) = tempfile.mkstemp()
f = os.fdopen(fd, 'w+')
f.write(TestBackendShellcmdCFG.CONF % ("False", cfgfile + '.d'))
f.close()
self.files_to_remove = [cfgfile, cfgfile + '.d']
self.cfg = gc3libs.config.Configuration()
self.cfg.merge_file(cfgfile)
self.core = gc3libs.core.Core(self.cfg)
self.backend = self.core.get_backend('localhost_test')
# Update resource status
app = gc3libs.Application(
arguments=['/bin/echo', 'Hello', 'World'],
inputs=[],
outputs=[],
output_dir=tmpdir,
requested_cores=1,
requested_memory=10 * Memory.MiB, )
try:
self.core.submit(app)
for pid in self.backend._job_infos.keys():
assert isinstance(pid,str)
finally:
self.core.kill(app)
self.core.free(app)
if __name__ == "__main__":
pytest.main(["-v", __file__])