Skip to content

Commit 8920725

Browse files
committed
Add templates and run_command tests
1 parent 1189aad commit 8920725

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

batchspawner/batchspawner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def submit_batch_script(self):
215215
if hasattr(self, 'user_options'):
216216
subvars.update(self.user_options)
217217
script = format_template(self.batch_script, **subvars)
218+
self._last_batch_script = script # used for testing only
218219
self.log.info('Spawner submitting job using ' + cmd)
219220
self.log.info('Spawner submitted script:\n' + script)
220221
out = yield self.run_command(cmd, input=script, env=self.get_env())

batchspawner/tests/test_spawners.py

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

3+
import re
34
from unittest import mock
45
from .. import BatchSpawnerRegexStates
56
from traitlets import Unicode
@@ -26,6 +27,29 @@ class BatchDummy(BatchSpawnerRegexStates):
2627
state_running_re = Unicode('RUN')
2728
state_exechost_re = Unicode('RUN (.*)$')
2829

30+
cmd_expectlist = None
31+
out_expectlist = None
32+
def run_command(self, *args, **kwargs):
33+
"""Overwriten run command to test templating and outputs"""
34+
cmd = args[0]
35+
# Test that the command matches the expectations
36+
if self.cmd_expectlist:
37+
run_re = self.cmd_expectlist.pop(0)
38+
if run_re:
39+
print('run:', run_re)
40+
assert run_re.search(cmd) is not None, \
41+
"Failed test: re={0} cmd={1}".format(run_re, cmd)
42+
# Run command normally
43+
out = super().run_command(*args, **kwargs)
44+
# Test that the command matches the expectations
45+
if self.out_expectlist:
46+
out_re = self.out_expectlist.pop(0)
47+
if out_re:
48+
print('out:', out_re)
49+
assert out_re.search(cmd) is not None, \
50+
"Failed output: re={0} cmd={1} out={2}".format(out_re, cmd, out)
51+
return out
52+
2953
def new_spawner(db, **kwargs):
3054
kwargs.setdefault('cmd', ['singleuser_command'])
3155
user = db.query(orm.User).first()
@@ -112,3 +136,38 @@ def test_pending_fails(db, io_loop):
112136
io_loop.run_sync(spawner.start, timeout=30)
113137
assert spawner.job_id == ''
114138
assert spawner.job_status == ''
139+
140+
def test_templates(db, io_loop):
141+
spawner = new_spawner(db=db)
142+
143+
# Test when not running
144+
spawner.cmd_expectlist = [re.compile('.*RUN'),
145+
]
146+
status = io_loop.run_sync(spawner.poll, timeout=5)
147+
assert status == 1
148+
assert spawner.job_id == ''
149+
assert spawner.get_state() == {}
150+
151+
# Test starting
152+
spawner.cmd_expectlist = [re.compile('.*echo'),
153+
re.compile('.*RUN'),
154+
]
155+
io_loop.run_sync(spawner.start, timeout=5)
156+
check_ip(spawner, testhost)
157+
assert spawner.job_id == testjob
158+
159+
# Test poll - running
160+
spawner.cmd_expectlist = [re.compile('.*RUN'),
161+
]
162+
status = io_loop.run_sync(spawner.poll, timeout=5)
163+
assert status is None
164+
165+
# Test stopping
166+
spawner.batch_query_cmd = 'echo NOPE'
167+
spawner.cmd_expectlist = [re.compile('.*STOP'),
168+
re.compile('.*NOPE'),
169+
]
170+
io_loop.run_sync(spawner.stop, timeout=5)
171+
status = io_loop.run_sync(spawner.poll, timeout=5)
172+
assert status == 1
173+
assert spawner.get_state() == {}

0 commit comments

Comments
 (0)