/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/bg_test.py |
---|
0,0 → 1,96 |
#!/usr/bin/python |
# |
# bg_test: tests the bg command |
# |
# Test the stop command for stopping a process by its pid. |
# Requires the following commands to be implemented |
# or otherwise usable: |
# |
# bg, sleep, stop |
# |
import sys, imp, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, shellio, signal, time, os, re, proc_check |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
#pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
#spawn an instance of the shell |
c = 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 seconds |
c.timeout = 2 |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# run program sleep |
c.sendline("sleep 30 &") |
#Used to get the jobid and pid of the sleep process |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex) |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# send the stop command to the process |
c.sendline(def_module.builtin_commands['stop'] % jobid) |
#Ensure that sleep has enough time to stop before we read its |
#/proc/ /stat file. |
proc_check.count_children_timeout(c, 1, 1) |
#Ensure that sleep is now stopped in the background, and is not |
#the foreground process. |
assert not proc_check.check_pid_fgpgrp(pid), \ |
'Error: process is in the foreground' |
assert proc_check.check_pid_status(pid, 'T'), 'Error: process not stopped' |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#resume the sleep program |
c.sendline(def_module.builtin_commands['bg'] % jobid) |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#Ensure that sleep has enough time to start before we read its |
#/proc/ /stat file. |
proc_check.count_children_timeout(c, 1, 1) |
#Ensure that sleep is running now in the background, and is not |
#the foreground process. |
assert not proc_check.check_pid_fgpgrp(pid), \ |
'Error: process is in the foreground' |
assert proc_check.check_pid_status(pid, 'S'), 'Error: process not running' |
#exit |
c.sendline("exit"); |
assert c.expect("exit\r\n") == 0, "Shell output extraneous characters" |
shellio.success() |
/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/ctrl-c_test.py |
---|
0,0 → 1,63 |
#!/usr/bin/python |
# |
# Ctrl-C Test: Start a shell, send SIGINT, run a program, |
# send SIGINT, then exit |
# |
# Requires the following commands to be implemented |
# or otherwise usable: |
# |
# sleep, ctrl-c control |
# |
import sys, imp, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, proc_check, shellio, signal, time, threading |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
#pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
# spawn an instance of the shell |
c = 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 seconds |
c.timeout = 2 |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# run a command |
c.sendline("sleep 60") |
# The following call is necessary to ensure that the SIGINT |
# we are sending below reaches the 'sleep' child. |
proc_check.wait_until_child_is_in_foreground(c) |
#checks that our process is running |
proc_check.count_active_children(c, 1) |
# send SIGINT |
c.sendintr() |
#prompt check |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#checks that the process was ended |
proc_check.count_active_children(c, 0) |
c.sendline("exit") |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" |
shellio.success() |
/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/ctrl-z_test.py |
---|
0,0 → 1,88 |
#!/usr/bin/python |
# |
# Ctrl-Z Test: Start a shell, run a program, |
# send SIGTSTP, wait for status msg, move in |
# foreground, then send SIGINT, then exit |
# |
# Requires use of the following commands: |
# |
# ctrl-z control, fg, sleep |
# |
import sys, imp, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, proc_check, shellio, signal, time, threading |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
# pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
# spawn an instance of the shell |
c = 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 seconds |
c.timeout = 2 |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# run a command |
c.sendline("sleep 60") |
# The following call is necessary to ensure that the SIGTSTP |
# we are sending below via 'sendcontrol' reaches the 'sleep' child. |
proc_check.wait_until_child_is_in_foreground(c) |
#checks the number of active child processes |
#using a timeout based process count |
proc_check.count_children_timeout(c, 1, 1) |
#checks the number of active child processes |
#at this moment in time |
proc_check.count_active_children(c, 1) |
# send SIGTSTP to 'sleep' |
c.sendcontrol('z') |
# shell should pick up that 'sleep' was stopped and respond with job status |
# it should output a line such as [6]+ Stopped (sleep 60) |
# (note that the provided regexp assumes the job name appears in parentheses, |
# adjust your eshoutput.py if needed) |
(jobid, statusmsg, cmdline) = \ |
shellio.parse_regular_expression(c, def_module.job_status_regex) |
assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job" |
# move job into foreground |
c.sendline(def_module.builtin_commands['fg'] % jobid) |
# when moving a job in the foreground, bash outputs its command line |
assert c.expect_exact(cmdline) == 0, "Shell did not report the job moved into the foreground" |
# send SIGINT |
c.sendintr() |
#check that the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#exit |
c.sendline("exit") |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" |
shellio.success() |
/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/fg_test.py |
---|
0,0 → 1,152 |
#!/usr/bin/python |
# |
# fg_test: tests the fg command |
# |
# Test the fg command for bringing a command back to the foreground. |
# Requires the following commands to be implemented |
# or otherwise usable: |
# |
# fg, sleep, ctrl-c control, ctrl-z control |
# |
import sys, imp, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, shellio, signal, time, os, re, proc_check |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
#pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
# spawn an instance of the shell |
c = 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 seconds |
c.timeout = 2 |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (1)" |
# run a command |
c.sendline("sleep 60") |
# The following call is necessary to ensure that the SIGTSTP |
# we are sending below via 'sendcontrol' reaches the 'sleep' child. |
proc_check.wait_until_child_is_in_foreground(c) |
# send SIGTSTP to 'sleep' |
c.sendcontrol('z') |
# shell should pick up that 'sleep' was stopped and respond with job status |
# it should output a line such [6]+ Stopped sleep 60 |
(jobid, statusmsg, cmdline) = \ |
shellio.parse_regular_expression(c, def_module.job_status_regex) |
assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job" |
# move job into foreground |
c.sendline(def_module.builtin_commands['fg'] % jobid) |
# when moving a job in the foreground, bash outputs its command line |
assert c.expect_exact(cmdline) == 0, "Shell did not report the job moved into the foreground" |
# end the program |
c.sendintr() |
# run a command |
c.sendline("sleep 30 &") |
#snag the jobid and pid of the sleep command |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex) |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (2)" |
#resume the sleep command |
c.sendline(def_module.builtin_commands['fg'] % jobid) |
#wait until it takes over the foreground process group |
proc_check.wait_until_child_is_in_foreground(c) |
#send the command back to the background |
c.sendcontrol('z') |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (3)" |
#run a command to the background |
c.sendline("sleep 300 &") |
#snag the jobid and pid of the second sleep command |
(jobid2, pid2) = shellio.parse_regular_expression(c, def_module.bgjob_regex) |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (4)" |
#resume the command by its jobid |
c.sendline(def_module.builtin_commands['fg'] % jobid) |
#wait until it takes over the foreground process group |
proc_check.wait_until_child_is_in_foreground(c) |
#Ensure that the sleep is in the foreground process group via /proc/ |
assert proc_check.check_pid_fgpgrp(pid), "Error, the pid's process group is \ |
not the foreground process group" |
#send the command back to the background |
c.sendcontrol('z') |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (5)" |
#resume the command by its jobid |
c.sendline(def_module.builtin_commands['fg'] % jobid2) |
#wait until it takes over the foreground process group |
proc_check.wait_until_child_is_in_foreground(c) |
#Ensure that the sleep is in the foreground process group via /proc/ |
assert proc_check.check_pid_fgpgrp(pid2), "Error, the pid's process group is \ |
not the foreground process group" |
#end the process |
c.sendintr() |
#check that the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (6)" |
#resume the first sleep command |
c.sendline(def_module.builtin_commands['fg'] % jobid) |
#wait until the process takes over the foreground process group |
proc_check.wait_until_child_is_in_foreground(c) |
#Ensure that the sleep is in the foreground process group via /proc/ |
assert proc_check.check_pid_fgpgrp(pid), "Error, the pid's process group is \ |
not the foreground process group" |
#exit |
c.sendline("exit"); |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" |
shellio.success() |
/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/jobs_test.py |
---|
0,0 → 1,164 |
#!/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, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, shellio, signal, time, os, re, proc_check |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
#pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
# spawn an instance of bash. PS1 is the env variable from which bash |
# draws its prompt |
c = 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 seconds |
c.timeout = 2 |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#check that the jobs list outputs nothing |
c.sendline(def_module.builtin_commands['jobs']) |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# run a command |
c.sendline("sleep 30") |
#Wait until the sleep command takes over the foreground |
proc_check.wait_until_child_is_in_foreground(c) |
#send the job to the background |
c.sendcontrol('z') |
# ensure that the shell prints the expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#Request the jobs list |
c.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 prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#Add another job |
c.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 list |
c.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-c |
c.sendline(def_module.builtin_commands['fg'] % jobid2) |
#Wait until the sleep command takes over the foreground |
proc_check.wait_until_child_is_in_foreground(c) |
#ctrl-c to close the process |
c.sendintr() |
#clear any printout of the old job that was just killed by ctrl-c |
c.sendline(def_module.builtin_commands['jobs']) |
#check the prompt and move past this text |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#check the prompt and move past this text |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#check the jobs list |
c.sendline(def_module.builtin_commands['jobs']) |
#check that the first job is still on the jobs list |
assert (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 printed |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# exit |
c.sendline("exit"); |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" |
shellio.success() |
/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/kill_test.py |
---|
0,0 → 1,81 |
#!/usr/bin/python |
# |
# kill_test: tests the kill command with the default |
# semantics of: |
# |
# kill <jid> |
# |
# This test may require updating such that we test other signals |
# |
# Requires the following commands to be implemented |
# or otherwise usable: |
# |
# kill, sleep |
# |
import sys, imp, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, shellio, signal, time, os, re, proc_check |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
# pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
# spawn an instance of the shell |
c = 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 seconds |
c.timeout = 2 |
# ensure that the shell prints the expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# run a command |
c.sendline("sleep 30 &") |
# parse the jobid and pid output |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex) |
# ensure that the shell prints the expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# The job needs to be running when we call kill |
proc_check.count_children_timeout(c, 1, 1) |
# Run the kill command and kill the sleep process in the background |
c.sendline(def_module.builtin_commands['kill'] % jobid) |
# ensure that the shell prints the expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# ensure there is enough time for the process to be killed |
time.sleep(.5) |
# check the proc file that the process has actually been stopped |
# the proc file should not exist |
assert not os.path.exists("/proc/" + pid + "/stat"), 'the process was not \ |
killed' |
# end the shell program by sending it an end-of-file character |
c.sendline("exit"); |
# ensure that no extra characters are output after exiting |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" |
# the test was successful |
shellio.success() |
/Classwork/CS3214 - Computer Systems/Project 2 - Extensible Shell/eshtests/basic/stop_test.py |
---|
0,0 → 1,77 |
#!/usr/bin/python |
# |
# stop_test: tests the stop command |
# |
# Test the stop command for stopping a process by its pid. |
# Requires the following commands to be implemented |
# or otherwise usable: |
# |
# stop, sleep |
# |
import sys, imp, atexit |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); |
import pexpect, shellio, signal, time, os, re, proc_check |
#Ensure the shell process is terminated |
def force_shell_termination(shell_process): |
c.close(force=True) |
#pulling in the regular expression and other definitions |
definitions_scriptname = sys.argv[1] |
def_module = imp.load_source('', definitions_scriptname) |
logfile = None |
if hasattr(def_module, 'logfile'): |
logfile = def_module.logfile |
# spawn an instance of the shell |
c = 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 seconds |
c.timeout = 2 |
# ensure that shell prints expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
# run a command |
c.sendline("sleep 30 &") |
# pull the jobid and pid from the background process printout |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex) |
# ensure that the shell prints the expected prompt |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
#The job needs to be running when we call stop |
proc_check.count_children_timeout(c, 1, 1) |
# send the stop command to the process |
c.sendline(def_module.builtin_commands['stop'] % jobid) |
#Ensure that sleep has enough time to stop before we read its |
#/proc/ pid /stat file. |
time.sleep(.5) |
#Ensure that sleep is now stopped in the background, and is not |
#the foreground process. |
assert not proc_check.check_pid_fgpgrp(pid), \ |
'Error: process is in the foreground' |
assert proc_check.check_pid_status(pid, 'T'), 'Error: process not stopped' |
#check the prompt prints |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" |
c.sendline("exit"); |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" |
shellio.success() |