| 141 |
Kevin |
1 |
#!/usr/bin/python
|
|
|
2 |
#
|
|
|
3 |
# Ctrl-Z Test: Start a shell, run a program,
|
|
|
4 |
# send SIGTSTP, wait for status msg, move in
|
|
|
5 |
# foreground, then send SIGINT, then exit
|
|
|
6 |
#
|
|
|
7 |
# Requires use of the following commands:
|
|
|
8 |
#
|
|
|
9 |
# ctrl-z control, fg, sleep
|
|
|
10 |
#
|
|
|
11 |
|
|
|
12 |
import sys, imp, atexit
|
|
|
13 |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/");
|
|
|
14 |
import pexpect, proc_check, shellio, signal, time, threading
|
|
|
15 |
|
|
|
16 |
#Ensure the shell process is terminated
|
|
|
17 |
def force_shell_termination(shell_process):
|
|
|
18 |
c.close(force=True)
|
|
|
19 |
|
|
|
20 |
# pulling in the regular expression and other definitions
|
|
|
21 |
definitions_scriptname = sys.argv[1]
|
|
|
22 |
def_module = imp.load_source('', definitions_scriptname)
|
|
|
23 |
logfile = None
|
|
|
24 |
if hasattr(def_module, 'logfile'):
|
|
|
25 |
logfile = def_module.logfile
|
|
|
26 |
|
|
|
27 |
# spawn an instance of the shell
|
|
|
28 |
c = pexpect.spawn(def_module.shell, drainpty=True, logfile=logfile)
|
|
|
29 |
atexit.register(force_shell_termination, shell_process=c)
|
|
|
30 |
|
|
|
31 |
# set timeout for all following 'expect*' calls to 2 seconds
|
|
|
32 |
c.timeout = 2
|
|
|
33 |
|
|
|
34 |
# ensure that shell prints expected prompt
|
|
|
35 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
# run a command
|
|
|
40 |
c.sendline("sleep 60")
|
|
|
41 |
|
|
|
42 |
# The following call is necessary to ensure that the SIGTSTP
|
|
|
43 |
# we are sending below via 'sendcontrol' reaches the 'sleep' child.
|
|
|
44 |
proc_check.wait_until_child_is_in_foreground(c)
|
|
|
45 |
|
|
|
46 |
#checks the number of active child processes
|
|
|
47 |
#using a timeout based process count
|
|
|
48 |
proc_check.count_children_timeout(c, 1, 1)
|
|
|
49 |
|
|
|
50 |
#checks the number of active child processes
|
|
|
51 |
#at this moment in time
|
|
|
52 |
proc_check.count_active_children(c, 1)
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
# send SIGTSTP to 'sleep'
|
|
|
57 |
c.sendcontrol('z')
|
|
|
58 |
|
|
|
59 |
# shell should pick up that 'sleep' was stopped and respond with job status
|
|
|
60 |
# it should output a line such as [6]+ Stopped (sleep 60)
|
|
|
61 |
# (note that the provided regexp assumes the job name appears in parentheses,
|
|
|
62 |
# adjust your eshoutput.py if needed)
|
|
|
63 |
(jobid, statusmsg, cmdline) = \
|
|
|
64 |
shellio.parse_regular_expression(c, def_module.job_status_regex)
|
|
|
65 |
assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job"
|
|
|
66 |
|
|
|
67 |
# move job into foreground
|
|
|
68 |
c.sendline(def_module.builtin_commands['fg'] % jobid)
|
|
|
69 |
|
|
|
70 |
# when moving a job in the foreground, bash outputs its command line
|
|
|
71 |
assert c.expect_exact(cmdline) == 0, "Shell did not report the job moved into the foreground"
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
# send SIGINT
|
|
|
76 |
c.sendintr()
|
|
|
77 |
|
|
|
78 |
#check that the prompt prints
|
|
|
79 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
#exit
|
|
|
84 |
c.sendline("exit")
|
|
|
85 |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
shellio.success()
|