11"""Test BatchSpawner and subclasses"""
22
3+ import re
34from unittest import mock
45from .. import BatchSpawnerRegexStates
56from 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+
2953def 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