| 141 |
Kevin |
1 |
#!/usr/bin/python
|
|
|
2 |
#
|
|
|
3 |
# Ctrl-C Test: Start a shell, send SIGINT, run a program,
|
|
|
4 |
# send SIGINT, then exit
|
|
|
5 |
#
|
|
|
6 |
# Requires the following commands to be implemented
|
|
|
7 |
# or otherwise usable:
|
|
|
8 |
#
|
|
|
9 |
# sleep, ctrl-c control
|
|
|
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 |
# run a command
|
|
|
38 |
c.sendline("sleep 60")
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
# The following call is necessary to ensure that the SIGINT
|
|
|
42 |
# we are sending below reaches the 'sleep' child.
|
|
|
43 |
# This is NOT a bullet-proof fix, you may fail on occasion!
|
|
|
44 |
time.sleep(1)
|
|
|
45 |
|
|
|
46 |
#checks that our process is running
|
|
|
47 |
proc_check.count_active_children(c, 1)
|
|
|
48 |
|
|
|
49 |
# send SIGINT
|
|
|
50 |
c.sendintr()
|
|
|
51 |
|
|
|
52 |
#prompt check
|
|
|
53 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
54 |
|
|
|
55 |
#checks that the process was ended
|
|
|
56 |
proc_check.count_active_children(c, 0)
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
c.sendline("exit")
|
|
|
61 |
|
|
|
62 |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
shellio.success()
|