| 141 |
Kevin |
1 |
#!/usr/bin/python
|
|
|
2 |
#
|
|
|
3 |
# jobs_test: tests the jobs command
|
|
|
4 |
#
|
|
|
5 |
# Test the jobs command for status messages and proper output
|
|
|
6 |
# of the command and jobid. Requires the following commands to be implemented
|
|
|
7 |
# or otherwise usable:
|
|
|
8 |
#
|
|
|
9 |
# jobs, ctrl-z control, ctrl-c control, sleep, fg, clear
|
|
|
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 bash. PS1 is the env variable from which bash
|
|
|
28 |
# draws its prompt
|
|
|
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 |
#check that the jobs list outputs nothing
|
|
|
41 |
c.sendline(def_module.builtin_commands['jobs'])
|
|
|
42 |
|
|
|
43 |
# ensure that shell prints expected prompt
|
|
|
44 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
# run a command
|
|
|
49 |
c.sendline("sleep 30")
|
|
|
50 |
|
|
|
51 |
#Wait until the sleep command takes over the foreground
|
|
|
52 |
proc_check.wait_until_child_is_in_foreground(c)
|
|
|
53 |
|
|
|
54 |
#send the job to the background
|
|
|
55 |
c.sendcontrol('z')
|
|
|
56 |
|
|
|
57 |
# ensure that the shell prints the expected prompt
|
|
|
58 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
#Request the jobs list
|
|
|
63 |
c.sendline(def_module.builtin_commands['jobs'])
|
|
|
64 |
|
|
|
65 |
#Check the jobs list
|
|
|
66 |
(jobid, status_message, command_line) = shellio.parse_regular_expression(c, def_module.job_status_regex)
|
|
|
67 |
assert status_message == def_module.jobs_status_msg['stopped'] and \
|
|
|
68 |
'sleep 30' in command_line, "Job status not properly displayed"
|
|
|
69 |
|
|
|
70 |
# ensure that the shell prints the expected prompt
|
|
|
71 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
#Add another job
|
|
|
76 |
c.sendline("sleep 300 &")
|
|
|
77 |
|
|
|
78 |
# pick up the background job output
|
|
|
79 |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
|
|
|
80 |
|
|
|
81 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
82 |
|
|
|
83 |
#Both jobs need to be active and running before the jobs command is
|
|
|
84 |
#sent. if this isn't so, the test is failed.
|
|
|
85 |
proc_check.count_active_children(c, 2)
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
#Recheck the jobs list
|
|
|
90 |
c.sendline(def_module.builtin_commands['jobs'])
|
|
|
91 |
|
|
|
92 |
#Check the jobs list
|
|
|
93 |
(jobid, status_message, command_line) = \
|
|
|
94 |
shellio.parse_regular_expression(c, def_module.job_status_regex)
|
|
|
95 |
(jobid2, status_message2, command_line2) = \
|
|
|
96 |
shellio.parse_regular_expression(c, def_module.job_status_regex)
|
|
|
97 |
|
|
|
98 |
# Check that the jobs list contains both jobs in some order
|
|
|
99 |
|
|
|
100 |
#check the first possible order of job statuses, and then
|
|
|
101 |
#the second possible order.
|
|
|
102 |
assert (status_message == def_module.jobs_status_msg['stopped'] and \
|
|
|
103 |
'sleep 30' in command_line and \
|
|
|
104 |
\
|
|
|
105 |
status_message2 == def_module.jobs_status_msg['running'] and \
|
|
|
106 |
'sleep 300' in command_line2) \
|
|
|
107 |
\
|
|
|
108 |
or \
|
|
|
109 |
\
|
|
|
110 |
(status_message2 == def_module.jobs_status_msg['stopped'] and \
|
|
|
111 |
'sleep 30' in command_line2 and \
|
|
|
112 |
\
|
|
|
113 |
status_message == def_module.jobs_status_msg['running'] and \
|
|
|
114 |
'sleep 300' in command_line), "Job status not properly displayed"
|
|
|
115 |
|
|
|
116 |
# Check that there are no duplicate job id's.
|
|
|
117 |
assert jobid != jobid2, "Duplicate job id's."
|
|
|
118 |
|
|
|
119 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
#bring the second sleep command back to foreground
|
|
|
124 |
#so that we can end it with ctrl-c
|
|
|
125 |
c.sendline(def_module.builtin_commands['fg'] % jobid2)
|
|
|
126 |
|
|
|
127 |
#Wait until the sleep command takes over the foreground
|
|
|
128 |
proc_check.wait_until_child_is_in_foreground(c)
|
|
|
129 |
|
|
|
130 |
#ctrl-c to close the process
|
|
|
131 |
c.sendintr()
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
#clear any printout of the old job that was just killed by ctrl-c
|
|
|
136 |
c.sendline(def_module.builtin_commands['jobs'])
|
|
|
137 |
|
|
|
138 |
#check the prompt and move past this text
|
|
|
139 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
#check the prompt and move past this text
|
|
|
143 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
#check the jobs list
|
|
|
147 |
c.sendline(def_module.builtin_commands['jobs'])
|
|
|
148 |
|
|
|
149 |
#check that the first job is still on the jobs list
|
|
|
150 |
assert (jobid, status_message, command_line) == \
|
|
|
151 |
shellio.parse_regular_expression(c, def_module.job_status_regex), \
|
|
|
152 |
"The original job was not displayed properly after ending a previous job."
|
|
|
153 |
|
|
|
154 |
# ensure the prompt is printed
|
|
|
155 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
# exit
|
|
|
160 |
c.sendline("exit");
|
|
|
161 |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
shellio.success()
|