Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
17
#Ensure the shell process is terminated
18
def force_shell_termination(shell_process):
19
	c.close(force=True)
20
 
21
# pulling in the regular expression and other definitions
22
definitions_scriptname = sys.argv[1]
23
def_module = imp.load_source('', definitions_scriptname)
24
logfile = None
25
if hasattr(def_module, 'logfile'):
26
    logfile = def_module.logfile
27
 
28
# spawn an instance of the shell
29
c = pexpect.spawn(def_module.shell, drainpty=True, logfile=logfile)
30
atexit.register(force_shell_termination, shell_process=c)
31
 
32
# set timeout for all following 'expect*' calls to 2 seconds
33
c.timeout = 2
34
 
35
# ensure that shell prints expected prompt
36
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
37
 
38
 
39
 
40
# run a command
41
c.sendline("sleep 60")
42
 
43
# The following call is necessary to ensure that the SIGTSTP
44
# we are sending below via 'sendcontrol' reaches the 'sleep' child.
45
# This is NOT a bullet-proof fix, you may fail on occasion!
46
time.sleep(1)
47
 
48
 
49
#checks the number of active child processes
50
#using a timeout based process count
51
proc_check.count_children_timeout(c, 1, 1)
52
 
53
#checks the number of active child processes
54
#at this moment in time
55
proc_check.count_active_children(c, 1)
56
 
57
 
58
 
59
# send SIGTSTP to 'sleep'
60
c.sendcontrol('z')
61
 
62
# shell should pick up that 'sleep' was stopped and respond with job status
63
# it should output a line such [6]+  Stopped                 sleep 60
64
(jobid, statusmsg, cmdline) = \
65
        shellio.parse_regular_expression(c, def_module.job_status_regex)
66
assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job"
67
 
68
# move job into foreground
69
c.sendline(def_module.builtin_commands['fg'] % jobid)
70
 
71
# when moving a job in the foreground, bash outputs its command line
72
assert c.expect_exact(cmdline) == 0, "Shell did not move job in foreground"
73
 
74
 
75
 
76
# send SIGINT
77
c.sendintr()
78
 
79
#check that the prompt prints
80
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
81
 
82
 
83
 
84
#exit
85
c.sendline("exit")
86
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
87
 
88
shellio.success()