Skip to content

Commit 948846f

Browse files
committed
Add tests for exec_prefix and batch script
1 parent 011b200 commit 948846f

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

batchspawner/tests/test_spawners.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
import pytest
99
from jupyterhub import orm, version_info
10+
from tornado import gen
1011

1112
try:
1213
from jupyterhub.objects import Hub
@@ -50,7 +51,7 @@ def run_command(self, *args, **kwargs):
5051
"Failed output: re={0} cmd={1} out={2}".format(out_re, cmd, out)
5152
return out
5253

53-
def new_spawner(db, **kwargs):
54+
def new_spawner(db, spawner_class=BatchDummy, **kwargs):
5455
kwargs.setdefault('cmd', ['singleuser_command'])
5556
user = db.query(orm.User).first()
5657
if version_info < (0,8):
@@ -65,10 +66,10 @@ def new_spawner(db, **kwargs):
6566
kwargs.setdefault('KILL_TIMEOUT', 1)
6667
kwargs.setdefault('poll_interval', 1)
6768
if version_info < (0,8):
68-
return BatchDummy(db=db, **kwargs)
69+
return spawner_class(db=db, **kwargs)
6970
else:
7071
print("JupyterHub >=0.8 detected, using new spawner creation")
71-
return user._new_spawner('', spawner_class=BatchDummy, **kwargs)
72+
return user._new_spawner('', spawner_class=spawner_class, **kwargs)
7273

7374
def test_stress_submit(db, io_loop):
7475
for i in range(200):
@@ -138,6 +139,7 @@ def test_pending_fails(db, io_loop):
138139
assert spawner.job_status == ''
139140

140141
def test_templates(db, io_loop):
142+
"""Test templates in the run_command commands"""
141143
spawner = new_spawner(db=db)
142144

143145
# Test when not running
@@ -171,3 +173,45 @@ def test_templates(db, io_loop):
171173
status = io_loop.run_sync(spawner.poll, timeout=5)
172174
assert status == 1
173175
assert spawner.get_state() == {}
176+
177+
def test_batch_script(db, io_loop):
178+
"""Test that the batch script substitutes {cmd}"""
179+
class BatchDummyTestScript(BatchDummy):
180+
@gen.coroutine
181+
def _get_batch_script(self, **subvars):
182+
script = yield super()._get_batch_script(**subvars)
183+
assert 'singleuser_command' in script
184+
return script
185+
spawner = new_spawner(db=db, SpawnerClass=BatchDummyTestScript)
186+
#status = io_loop.run_sync(spawner.poll, timeout=5)
187+
io_loop.run_sync(spawner.start, timeout=5)
188+
#status = io_loop.run_sync(spawner.poll, timeout=5)
189+
#io_loop.run_sync(spawner.stop, timeout=5)
190+
191+
def test_exec_prefix(db, io_loop):
192+
"""Test that all run_commands have exec_prefix"""
193+
class BatchDummyTestScript(BatchDummy):
194+
exec_prefix = 'PREFIX'
195+
@gen.coroutine
196+
def run_command(self, cmd, *args, **kwargs):
197+
assert cmd.startswith('PREFIX ')
198+
cmd = cmd[7:]
199+
print(cmd)
200+
out = yield super().run_command(cmd, *args, **kwargs)
201+
return out
202+
spawner = new_spawner(db=db, SpawnerClass=BatchDummyTestScript)
203+
# Not running
204+
status = io_loop.run_sync(spawner.poll, timeout=5)
205+
assert status == 1
206+
# Start
207+
io_loop.run_sync(spawner.start, timeout=5)
208+
assert spawner.job_id == testjob
209+
# Poll
210+
status = io_loop.run_sync(spawner.poll, timeout=5)
211+
assert status is None
212+
# Stop
213+
spawner.batch_query_cmd = 'echo NOPE'
214+
io_loop.run_sync(spawner.stop, timeout=5)
215+
status = io_loop.run_sync(spawner.poll, timeout=5)
216+
assert status == 1
217+

0 commit comments

Comments
 (0)