Skip to content

Commit 12100a2

Browse files
committed
admin_environment: test and make active
1 parent 8660d3a commit 12100a2

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

batchspawner/batchspawner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ async def submit_batch_script(self):
284284
script = await self._get_batch_script(**subvars)
285285
self.log.info('Spawner submitting job using ' + cmd)
286286
self.log.info('Spawner submitted script:\n' + script)
287-
out = await self.run_command(cmd, input=script, env=self.get_env())
287+
out = await self.run_command(cmd, input=script, env=self.get_admin_env())
288288
try:
289289
self.log.info('Job submitted. cmd: ' + cmd + ' output: ' + out)
290290
self.job_id = self.parse_job_id(out)
@@ -310,7 +310,7 @@ async def read_job_state(self):
310310
format_template(self.batch_query_cmd, **subvars)))
311311
self.log.debug('Spawner querying job: ' + cmd)
312312
try:
313-
out = await self.run_command(cmd)
313+
out = await self.run_command(cmd, env=self.get_admin_env())
314314
self.job_status = out
315315
except Exception as e:
316316
self.log.error('Error querying job ' + self.job_id)
@@ -328,7 +328,7 @@ async def cancel_batch_job(self):
328328
cmd = ' '.join((format_template(self.exec_prefix, **subvars),
329329
format_template(self.batch_cancel_cmd, **subvars)))
330330
self.log.info('Cancelling job ' + self.job_id + ': ' + cmd)
331-
await self.run_command(cmd)
331+
await self.run_command(cmd, env=self.get_admin_env())
332332

333333
def load_state(self, state):
334334
"""load job_id from state"""

batchspawner/tests/test_spawners.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test BatchSpawner and subclasses"""
22

3+
import itertools
34
import re
45
from unittest import mock
56
from .. 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

223230
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):
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

491504
def 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

Comments
 (0)