Skip to content

Commit 705bcf8

Browse files
committed
Fix all the environment variable handling, make tests work
- This fixes up all the previous commits on this branch
1 parent 12100a2 commit 705bcf8

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

batchspawner/batchspawner.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,18 @@ def _req_homedir_default(self):
148148
"to a default by JupyterHub, and if you change this list "
149149
"the previous ones are _not_ included and your submissions will "
150150
"break unless you re-add necessary variables. Consider "
151-
"req_keepvars for most use cases."
152-
)
153-
@default('req_keepvars_all')
154-
def _req_keepvars_all_default(self):
155-
return ','.join(self.get_env().keys())
151+
"req_keepvars for most use cases, don't edit this under normal "
152+
"use.").tag(config=True)
153+
154+
@default('req_keepvarsdefault')
155+
def _req_keepvars_default_default(self):
156+
return ','.join(super(BatchSpawnerBase, self).get_env().keys())
156157

157158
req_keepvars = Unicode(
158159
help="Extra environment variables which should be configured, "
159160
"added to the defaults in keepvars, "
160161
"comma separated list.").tag(config=True)
162+
161163
admin_environment = Unicode(
162164
help="Comma-separated list of environment variables to be passed to "
163165
"the batch submit/cancel commands, but _not_ to the batch script "
@@ -213,6 +215,21 @@ def cmd_formatted_for_batch(self):
213215
"""The command which is substituted inside of the batch script"""
214216
return ' '.join([self.batchspawner_singleuser_cmd] + self.cmd + self.get_args())
215217

218+
def get_env(self):
219+
"""Get the env dict from JH, adding req_keepvars options
220+
221+
get_env() returns the variables given by JH, but not anything
222+
specified by req_keepvars since that's our creation. Add those
223+
(from the JH environment) to the environment passed to the batch
224+
start/stop/cancel/etc commands.
225+
"""
226+
env = super(BatchSpawnerBase, self).get_env()
227+
for k in self.req_keepvars.split(',') + self.req_keepvars_default.split(','):
228+
if k in os.environ:
229+
print('setting %s in get_env'%k)
230+
env[k] = os.environ[k]
231+
return env
232+
216233
def get_admin_env(self):
217234
"""Get the environment passed to the batch submit/cancel/etc commands.
218235

batchspawner/tests/test_spawners.py

+58-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Test BatchSpawner and subclasses"""
22

3+
import contextlib
34
import itertools
5+
import os
46
import re
57
from unittest import mock
68
from .. import BatchSpawnerRegexStates
@@ -20,6 +22,20 @@
2022
testjob = "12345"
2123
testport = 54321
2224

25+
@contextlib.contextmanager
26+
def setenv_context(**kwargs):
27+
orig = { }
28+
for k, v in kwargs.items():
29+
orig[k] = os.environ.get(k, None)
30+
os.environ[k] = v
31+
yield
32+
for k in kwargs:
33+
if orig[k] is not None:
34+
os.environ[k] = orig[k]
35+
else:
36+
del os.environ[k]
37+
38+
2339
class BatchDummy(BatchSpawnerRegexStates):
2440
exec_prefix = ''
2541
batch_submit_cmd = Unicode('cat > /dev/null; echo '+testjob)
@@ -504,32 +520,51 @@ def test_lfs(db, io_loop):
504520
def test_keepvars(db, io_loop):
505521
"""Test of environment handling
506522
"""
507-
# req_keepvars
523+
environment = {'ABCDE': 'TEST1', 'VWXYZ': 'TEST2', 'XYZ': 'TEST3',}
524+
525+
526+
# req_keepvars_default - anything NOT here should not be propogated.
508527
spawner_kwargs = {
509528
'req_keepvars_default': 'ABCDE',
510529
}
511530
batch_script_re_list = [
512531
re.compile(r'--export=ABCDE', re.X|re.M),
532+
re.compile(r'^((?!JUPYTERHUB_API_TOKEN).)*$', re.X|re.S), # *not* in the script
513533
]
514534
def env_test(env):
515-
assert 'ABCDE' in env
516-
run_typical_slurm_spawner(db, io_loop,
517-
spawner_kwargs=spawner_kwargs,
518-
batch_script_re_list=batch_script_re_list,
519-
env_test=env_test)
520-
521-
# req_keepvars
535+
# We can't test these - becasue removing these from the environment is
536+
# a job of the batch system itself, which we do *not* run here.
537+
#assert 'ABCDE' in env
538+
#assert 'JUPYTERHUB_API_TOKEN' not in env
539+
pass
540+
with setenv_context(**environment):
541+
print(sorted(os.environ.keys()))
542+
run_typical_slurm_spawner(db, io_loop,
543+
spawner_kwargs=spawner_kwargs,
544+
batch_script_re_list=batch_script_re_list,
545+
env_test=env_test)
546+
547+
# req_keepvars - this should be added to the environment
522548
spawner_kwargs = {
523549
'req_keepvars': 'ABCDE',
524550
}
525551
batch_script_re_list = [
526552
re.compile(r'--export=.*ABCDE', re.X|re.M),
553+
re.compile(r'^((?!VWXYZ).)*$', re.X|re.M), # *not* in line
554+
re.compile(r'--export=.*JUPYTERHUB_API_TOKEN', re.X|re.S),
527555
]
528-
run_typical_slurm_spawner(db, io_loop,
529-
spawner_kwargs=spawner_kwargs,
530-
batch_script_re_list=batch_script_re_list)
531-
532-
# req_keepvars
556+
def env_test(env):
557+
assert 'ABCDE' in env
558+
assert 'VWXYZ' not in env
559+
with setenv_context(**environment):
560+
run_typical_slurm_spawner(db, io_loop,
561+
spawner_kwargs=spawner_kwargs,
562+
batch_script_re_list=batch_script_re_list,
563+
env_test=env_test)
564+
565+
# admin_environment - this should be in the environment passed to
566+
# run commands but not the --export command which is included in
567+
# the batch scripts
533568
spawner_kwargs = {
534569
'admin_environment': 'ABCDE',
535570
}
@@ -539,13 +574,12 @@ def env_test(env):
539574
def env_test(env):
540575
assert 'ABCDE' in env
541576
assert 'VWXYZ' not in env
542-
os.environ['ABCDE'] = 'TEST1'
543-
os.environ['VWXYZ'] = 'TEST2'
544-
run_typical_slurm_spawner(db, io_loop,
545-
spawner_kwargs=spawner_kwargs,
546-
batch_script_re_list=batch_script_re_list,
547-
env_test=env_test)
548-
del os.environ['ABCDE'], os.environ['VWXYZ']
577+
assert 'JUPYTERHUB_API_TOKEN' in env
578+
with setenv_context(**environment):
579+
run_typical_slurm_spawner(db, io_loop,
580+
spawner_kwargs=spawner_kwargs,
581+
batch_script_re_list=batch_script_re_list,
582+
env_test=env_test)
549583

550584
# req_keepvars AND req_keepvars together
551585
spawner_kwargs = {
@@ -555,6 +589,7 @@ def env_test(env):
555589
batch_script_re_list = [
556590
re.compile(r'--export=ABCDE,XYZ', re.X|re.M),
557591
]
558-
run_typical_slurm_spawner(db, io_loop,
559-
spawner_kwargs=spawner_kwargs,
560-
batch_script_re_list=batch_script_re_list)
592+
with setenv_context(**environment):
593+
run_typical_slurm_spawner(db, io_loop,
594+
spawner_kwargs=spawner_kwargs,
595+
batch_script_re_list=batch_script_re_list)

0 commit comments

Comments
 (0)