1
1
"""Test BatchSpawner and subclasses"""
2
2
3
+ import itertools
3
4
import re
4
5
from unittest import mock
5
6
from .. import BatchSpawnerRegexStates
@@ -29,11 +30,17 @@ class BatchDummy(BatchSpawnerRegexStates):
29
30
state_running_re = Unicode ('RUN' )
30
31
state_exechost_re = Unicode ('RUN (.*)$' )
31
32
32
- cmd_expectlist = None
33
+ cmd_expectlist = None #List of (re)
33
34
out_expectlist = None
35
+ env_testlist = None # List of functions to call on env dict, function should assert within.
34
36
def run_command (self , * args , ** kwargs ):
35
37
"""Overwriten run command to test templating and outputs"""
36
38
cmd = args [0 ]
39
+ # Test the environment
40
+ if self .env_testlist : # if first item is None, also pop and advance
41
+ env_test = self .env_testlist .pop (0 )
42
+ if env_test :
43
+ env_test (kwargs ['env' ])
37
44
# Test that the command matches the expectations
38
45
if self .cmd_expectlist :
39
46
run_re = self .cmd_expectlist .pop (0 )
@@ -221,13 +228,14 @@ def run_command(self, cmd, *args, **kwargs):
221
228
assert status == 1
222
229
223
230
def run_spawner_script (db , io_loop , spawner , script ,
224
- batch_script_re_list = None , spawner_kwargs = {}):
231
+ batch_script_re_list = None , spawner_kwargs = {},
232
+ env_test = None ):
225
233
"""Run a spawner script and test that the output and behavior is as expected.
226
234
227
235
db: same as in this module
228
236
io_loop: same as in this module
229
237
spawner: the BatchSpawnerBase subclass to test
230
- script: list of (input_re_to_match, output)
238
+ script: list of (input_re_to_match, output, env_testfunc )
231
239
batch_script_re_list: if given, assert batch script matches all of these
232
240
"""
233
241
# Create the expected scripts
@@ -238,6 +246,9 @@ def run_spawner_script(db, io_loop, spawner, script,
238
246
class BatchDummyTestScript (spawner ):
239
247
@gen .coroutine
240
248
def run_command (self , cmd , input = None , env = None ):
249
+ # Test the environment
250
+ if env_test :
251
+ env_test (env )
241
252
# Test the input
242
253
run_re = cmd_expectlist .pop (0 )
243
254
if run_re :
@@ -402,15 +413,17 @@ def run_typical_slurm_spawner(db, io_loop,
402
413
spawner = SlurmSpawner ,
403
414
script = normal_slurm_script ,
404
415
batch_script_re_list = None ,
405
- spawner_kwargs = {}):
416
+ spawner_kwargs = {},
417
+ env_test = None ):
406
418
"""Run a full slurm job with default (overrideable) parameters.
407
419
408
420
This is useful, for example, for changing options and testing effect
409
421
of batch scripts.
410
422
"""
411
423
return run_spawner_script (db , io_loop , spawner , script ,
412
424
batch_script_re_list = batch_script_re_list ,
413
- spawner_kwargs = spawner_kwargs )
425
+ spawner_kwargs = spawner_kwargs ,
426
+ env_test = env_test )
414
427
415
428
416
429
#def test_gridengine(db, io_loop):
@@ -489,16 +502,21 @@ def test_lfs(db, io_loop):
489
502
490
503
491
504
def test_keepvars (db , io_loop ):
505
+ """Test of environment handling
506
+ """
492
507
# req_keepvars
493
508
spawner_kwargs = {
494
509
'req_keepvars_default' : 'ABCDE' ,
495
510
}
496
511
batch_script_re_list = [
497
512
re .compile (r'--export=ABCDE' , re .X | re .M ),
498
513
]
514
+ def env_test (env ):
515
+ assert 'ABCDE' in env
499
516
run_typical_slurm_spawner (db , io_loop ,
500
517
spawner_kwargs = spawner_kwargs ,
501
- batch_script_re_list = batch_script_re_list )
518
+ batch_script_re_list = batch_script_re_list ,
519
+ env_test = env_test )
502
520
503
521
# req_keepvars
504
522
spawner_kwargs = {
@@ -516,11 +534,18 @@ def test_keepvars(db, io_loop):
516
534
'admin_environment' : 'ABCDE' ,
517
535
}
518
536
batch_script_re_list = [
519
- re .compile (r'^((?!ABCDE).)*$' , re .X | re .S ),
537
+ re .compile (r'^((?!ABCDE).)*$' , re .X | re .S ), # ABCDE not in the script
520
538
]
539
+ def env_test (env ):
540
+ assert 'ABCDE' in env
541
+ assert 'VWXYZ' not in env
542
+ os .environ ['ABCDE' ] = 'TEST1'
543
+ os .environ ['VWXYZ' ] = 'TEST2'
521
544
run_typical_slurm_spawner (db , io_loop ,
522
545
spawner_kwargs = spawner_kwargs ,
523
- batch_script_re_list = batch_script_re_list )
546
+ batch_script_re_list = batch_script_re_list ,
547
+ env_test = env_test )
548
+ del os .environ ['ABCDE' ], os .environ ['VWXYZ' ]
524
549
525
550
# req_keepvars AND req_keepvars together
526
551
spawner_kwargs = {
0 commit comments