Blame | Last modification | View Log | Download | RSS feed
#!/usr/bin/python## jobs_test: tests the jobs command## Test the jobs command for status messages and proper output# of the command and jobid. Requires the following commands to be implemented# or otherwise usable:## jobs, ctrl-z control, ctrl-c control, sleep, fg, clear#import sys, imp, atexitsys.path.append("/home/courses/cs3214/software/pexpect-dpty/");import pexpect, shellio, signal, time, os, re, proc_check#Ensure the shell process is terminateddef force_shell_termination(shell_process):c.close(force=True)#pulling in the regular expression and other definitionsdefinitions_scriptname = sys.argv[1]def_module = imp.load_source('', definitions_scriptname)logfile = Noneif hasattr(def_module, 'logfile'):logfile = def_module.logfile# spawn an instance of bash. PS1 is the env variable from which bash# draws its promptc = pexpect.spawn(def_module.shell, drainpty=True, logfile=logfile)atexit.register(force_shell_termination, shell_process=c)# set timeout for all following 'expect*' calls to 2 secondsc.timeout = 2# ensure that shell prints expected promptassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#check that the jobs list outputs nothingc.sendline(def_module.builtin_commands['jobs'])# ensure that shell prints expected promptassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"# run a commandc.sendline("sleep 30")#Wait until the sleep command takes over the foreground# This is NOT a bullet-proof fix, you may fail on occasion!time.sleep(1)#send the job to the backgroundc.sendcontrol('z')# ensure that the shell prints the expected promptassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#Request the jobs listc.sendline(def_module.builtin_commands['jobs'])#Check the jobs list(jobid, status_message, command_line) = shellio.parse_regular_expression(c, def_module.job_status_regex)assert status_message == def_module.jobs_status_msg['stopped'] and \'sleep 30' in command_line, "Job status not properly displayed"# ensure that the shell prints the expected promptassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#Add another jobc.sendline("sleep 300 &")# pick up the background job output(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#Both jobs need to be active and running before the jobs command is#sent. if this isn't so, the test is failed.proc_check.count_active_children(c, 2)#Recheck the jobs listc.sendline(def_module.builtin_commands['jobs'])#Check the jobs list(jobid, status_message, command_line) = \shellio.parse_regular_expression(c, def_module.job_status_regex)(jobid2, status_message2, command_line2) = \shellio.parse_regular_expression(c, def_module.job_status_regex)# Check that the jobs list contains both jobs in some order#check the first possible order of job statuses, and then#the second possible order.assert (status_message == def_module.jobs_status_msg['stopped'] and \'sleep 30' in command_line and \\status_message2 == def_module.jobs_status_msg['running'] and \'sleep 300' in command_line2) \\or \\(status_message2 == def_module.jobs_status_msg['stopped'] and \'sleep 30' in command_line2 and \\status_message == def_module.jobs_status_msg['running'] and \'sleep 300' in command_line), "Job status not properly displayed"# Check that there are no duplicate job id's.assert jobid != jobid2, "Duplicate job id's."assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#bring the second sleep command back to foreground#so that we can end it with ctrl-cc.sendline(def_module.builtin_commands['fg'] % jobid2)#Wait until the sleep command takes over the foreground# This is NOT a bullet-proof fix, you may fail on occasion!time.sleep(1)#ctrl-c to close the processc.sendintr()#clear any printout of the old job that was just killed by ctrl-cc.sendline(def_module.builtin_commands['jobs'])#check the prompt and move past this textassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#check the prompt and move past this textassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"#check the jobs listc.sendline(def_module.builtin_commands['jobs'])#check that the first job is still on the jobs listassert (jobid, status_message, command_line) == \shellio.parse_regular_expression(c, def_module.job_status_regex), \"The original job was not displayed properly after ending a previous job."# ensure the prompt is printedassert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"# exitc.sendline("exit");assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"shellio.success()