11"""Test BatchSpawner and subclasses"""
22
3+ import itertools
34import re
45from unittest import mock
56from .. import BatchSpawnerRegexStates
@@ -29,11 +30,17 @@ class BatchDummy(BatchSpawnerRegexStates):
2930 state_running_re = Unicode ('RUN' )
3031 state_exechost_re = Unicode ('RUN (.*)$' )
3132
32- cmd_expectlist = None
33+ cmd_expectlist = None #List of (re)
3334 out_expectlist = None
35+ env_testlist = None # List of functions to call on env dict, function should assert within.
3436 def run_command (self , * args , ** kwargs ):
3537 """Overwriten run command to test templating and outputs"""
3638 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' ])
3744 # Test that the command matches the expectations
3845 if self .cmd_expectlist :
3946 run_re = self .cmd_expectlist .pop (0 )
@@ -221,13 +228,14 @@ def run_command(self, cmd, *args, **kwargs):
221228 assert status == 1
222229
223230def 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 ):
225233 """Run a spawner script and test that the output and behavior is as expected.
226234
227235 db: same as in this module
228236 io_loop: same as in this module
229237 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 )
231239 batch_script_re_list: if given, assert batch script matches all of these
232240 """
233241 # Create the expected scripts
@@ -238,6 +246,9 @@ def run_spawner_script(db, io_loop, spawner, script,
238246 class BatchDummyTestScript (spawner ):
239247 @gen .coroutine
240248 def run_command (self , cmd , input = None , env = None ):
249+ # Test the environment
250+ if env_test :
251+ env_test (env )
241252 # Test the input
242253 run_re = cmd_expectlist .pop (0 )
243254 if run_re :
@@ -402,15 +413,17 @@ def run_typical_slurm_spawner(db, io_loop,
402413 spawner = SlurmSpawner ,
403414 script = normal_slurm_script ,
404415 batch_script_re_list = None ,
405- spawner_kwargs = {}):
416+ spawner_kwargs = {},
417+ env_test = None ):
406418 """Run a full slurm job with default (overrideable) parameters.
407419
408420 This is useful, for example, for changing options and testing effect
409421 of batch scripts.
410422 """
411423 return run_spawner_script (db , io_loop , spawner , script ,
412424 batch_script_re_list = batch_script_re_list ,
413- spawner_kwargs = spawner_kwargs )
425+ spawner_kwargs = spawner_kwargs ,
426+ env_test = env_test )
414427
415428
416429#def test_gridengine(db, io_loop):
@@ -489,16 +502,21 @@ def test_lfs(db, io_loop):
489502
490503
491504def test_keepvars (db , io_loop ):
505+ """Test of environment handling
506+ """
492507 # req_keepvars
493508 spawner_kwargs = {
494509 'req_keepvars_default' : 'ABCDE' ,
495510 }
496511 batch_script_re_list = [
497512 re .compile (r'--export=ABCDE' , re .X | re .M ),
498513 ]
514+ def env_test (env ):
515+ assert 'ABCDE' in env
499516 run_typical_slurm_spawner (db , io_loop ,
500517 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 )
502520
503521 # req_keepvars
504522 spawner_kwargs = {
@@ -516,11 +534,18 @@ def test_keepvars(db, io_loop):
516534 'admin_environment' : 'ABCDE' ,
517535 }
518536 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
520538 ]
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'
521544 run_typical_slurm_spawner (db , io_loop ,
522545 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' ]
524549
525550 # req_keepvars AND req_keepvars together
526551 spawner_kwargs = {
0 commit comments