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
# fg_test: tests the fg command
4
# 
5
# Test the fg command for bringing a command back to the foreground.
6
# Requires the following commands to be implemented
7
# or otherwise usable:
8
#
9
#	fg, sleep, ctrl-c control, ctrl-z control
10
#
11
 
12
import sys, imp, atexit
13
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/");
14
import pexpect, shellio, signal, time, os, re, proc_check
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 (1)"
36
 
37
 
38
 
39
# run a command
40
c.sendline("sleep 30 &")
41
 
42
#snag the jobid and pid of the sleep command
43
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
44
 
45
#check the prompt prints
46
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (2)"
47
 
48
 
49
 
50
#resume the sleep command
51
c.sendline(def_module.builtin_commands['fg'] % jobid)
52
 
53
#wait until it takes over the foreground process group
54
# This is NOT a bullet-proof fix, you may fail on occasion!
55
time.sleep(1)
56
 
57
#send the command back to the background
58
c.sendcontrol('z')
59
 
60
#check the prompt prints
61
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (3)"
62
 
63
 
64
 
65
#run a command to the background
66
c.sendline("sleep 300 &")
67
 
68
#snag the jobid and pid of the second sleep command
69
(jobid2, pid2) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
70
 
71
#check the prompt prints
72
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (4)"
73
 
74
#resume the command by its jobid
75
c.sendline(def_module.builtin_commands['fg'] % jobid)
76
 
77
#wait until it takes over the foreground process group
78
# This is NOT a bullet-proof fix, you may fail on occasion!
79
time.sleep(1)
80
 
81
#Ensure that the sleep is in the foreground process group via /proc/
82
#assert proc_check.check_pid_fgpgrp(pid),  "Error, the pid's process group is \
83
#                                           not the foreground process group"
84
 
85
#send the command back to the background
86
c.sendcontrol('z')
87
 
88
#check the prompt prints
89
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (5)"
90
 
91
 
92
 
93
#resume the command by its jobid
94
c.sendline(def_module.builtin_commands['fg'] % jobid2)
95
 
96
#wait until it takes over the foreground process group
97
# This is NOT a bullet-proof fix, you may fail on occasion!
98
time.sleep(1)
99
 
100
#Ensure that the sleep is in the foreground process group via /proc/
101
#assert proc_check.check_pid_fgpgrp(pid2),  "Error, the pid's process group is \
102
#                                           not the foreground process group"
103
 
104
#end the process
105
c.sendintr()
106
 
107
#check that the prompt prints
108
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (6)"
109
 
110
 
111
 
112
#resume the first sleep command
113
c.sendline(def_module.builtin_commands['fg'] % jobid)
114
 
115
#wait until the process takes over the foreground process group
116
# This is NOT a bullet-proof fix, you may fail on occasion!
117
time.sleep(1)
118
 
119
 
120
#Ensure that the sleep is in the foreground process group via /proc/
121
#assert proc_check.check_pid_fgpgrp(pid),  "Error, the pid's process group is \
122
#                                           not the foreground process group"
123
 
124
 
125
 
126
#exit
127
c.sendline("exit");
128
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
129
 
130
 
131
shellio.success()