@@ -215,3 +215,87 @@ def run_command(self, cmd, *args, **kwargs):
215215 status = io_loop .run_sync (spawner .poll , timeout = 5 )
216216 assert status == 1
217217
218+ def run_spawner_script (db , io_loop , spawner , script , batch_re_list = None , ** kwargs ):
219+ """Run a spawner script and test that the output and behavior is as expected.
220+
221+ db: same as in this module
222+ io_loop: same as in this module
223+ spawner: the BatchSpawnerBase subclass to test
224+ script: list of (input_re_to_match, output)
225+ batch_re_list: if given
226+ """
227+ # Create the expected scripts
228+ cmd_expectlist , out_list = zip (* script )
229+ cmd_expectlist = list (cmd_expectlist )
230+ out_list = list (out_list )
231+
232+ class BatchDummyTestScript (spawner ):
233+ @gen .coroutine
234+ def run_command (self , cmd , * args , ** kwargs ):
235+ # Test the input
236+ run_re = cmd_expectlist .pop (0 )
237+ if run_re :
238+ print ('run: "{}" [{}]' .format (cmd , run_re ))
239+ assert run_re .search (cmd ) is not None , \
240+ "Failed test: re={0} cmd={1}" .format (run_re , cmd )
241+ # Test the stdin - will only be the batch script. For
242+ # each regular expression in batch_re_list, assert that
243+ # each re in that list matches the batch script.
244+ if batch_re_list and 'input' in kwargs :
245+ batch_script = kwargs ['input' ]
246+ for match_re in batch_re_list :
247+ assert match_re .search (batch_script ) is not None , \
248+ "Batch script does not match {}" .format (match_re )
249+ # Return expected output.
250+ out = out_list .pop (0 )
251+ print (' --> ' + out )
252+ return out
253+
254+ spawner = new_spawner (db = db , spawner_class = BatchDummyTestScript , ** kwargs )
255+ # Not running at beginning (no command run)
256+ status = io_loop .run_sync (spawner .poll , timeout = 5 )
257+ assert status == 1
258+ # batch_submit_cmd
259+ # batch_query_cmd
260+ io_loop .run_sync (spawner .start , timeout = 5 )
261+ assert spawner .job_id == testjob
262+ check_ip (spawner , testhost )
263+ # batch_query_cmd
264+ status = io_loop .run_sync (spawner .poll , timeout = 5 )
265+ assert status is None
266+ # batch_cancel_cmd
267+ io_loop .run_sync (spawner .stop , timeout = 5 )
268+ # batch_poll_cmd
269+ status = io_loop .run_sync (spawner .poll , timeout = 5 )
270+ assert status == 1
271+
272+
273+
274+ def test_torque (db , io_loop ):
275+ script = [
276+ (re .compile ('sudo.*qsub' ), str (testjob )),
277+ (re .compile ('sudo.*qstat' ), '<job_state>R</job_state><exec_host>{}/1</exec_host>' .format (testhost )),
278+ (re .compile ('sudo.*qstat' ), '<job_state>R</job_state>' + testhost ),
279+ (re .compile ('sudo.*qdel' ), 'STOP' ),
280+ (re .compile ('sudo.*qstat' ), '' ),
281+ ]
282+ batch_re_list = [
283+ re .compile ('singleuser_command' )
284+ ]
285+ from .. import TorqueSpawner
286+ run_spawner_script (db , io_loop , TorqueSpawner , script , batch_re_list )
287+
288+
289+ def test_slurm (db , io_loop ):
290+ script = [
291+ (re .compile ('sudo.*sbatch' ), str (testjob )),
292+ (re .compile ('sudo.*squeue' ), 'RUNNING ' + testhost ),
293+ (re .compile ('sudo.*squeue' ), 'RUNNING ' + testhost ),
294+ (re .compile ('sudo.*scancel' ), 'STOP' ),
295+ (re .compile ('sudo.*squeue' ), '' ),
296+ ]
297+ batch_re_list = [
298+ re .compile ('srun.*singleuser_command' )
299+ ]
300+ from .. import SlurmSpawner
301+ run_spawner_script (db , io_loop , SlurmSpawner , script , batch_re_list )
0 commit comments