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
# 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
# This is NOT a bullet-proof fix, you may fail on occasion!
53
time.sleep(1)
54
 
55
 
56
#send the job to the background
57
c.sendcontrol('z')
58
 
59
# ensure that the shell prints the expected prompt
60
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
61
 
62
 
63
 
64
#Request the jobs list
65
c.sendline(def_module.builtin_commands['jobs'])
66
 
67
#Check the jobs list
68
(jobid, status_message, command_line) = shellio.parse_regular_expression(c, def_module.job_status_regex)
69
assert status_message == def_module.jobs_status_msg['stopped'] and \
70
		'sleep 30' in command_line, "Job status not properly displayed"
71
 
72
# ensure that the shell prints the expected prompt
73
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
74
 
75
 
76
 
77
#Add another job
78
c.sendline("sleep 300 &")
79
 
80
# pick up the background job output
81
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
82
 
83
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
84
 
85
#Both jobs need to be active and running before the jobs command is
86
#sent.  if this isn't so, the test is failed.
87
proc_check.count_active_children(c, 2)
88
 
89
 
90
 
91
#Recheck the jobs list
92
c.sendline(def_module.builtin_commands['jobs'])
93
 
94
#Check the jobs list
95
(jobid, status_message, command_line) = \
96
            shellio.parse_regular_expression(c, def_module.job_status_regex) 
97
(jobid2, status_message2, command_line2) = \
98
            shellio.parse_regular_expression(c, def_module.job_status_regex)
99
 
100
# Check that the jobs list contains both jobs in some order
101
 
102
#check the first possible order of job statuses, and then
103
#the second possible order.
104
assert  (status_message == def_module.jobs_status_msg['stopped'] and \
105
		'sleep 30' in command_line and \
106
		\
107
		status_message2 == def_module.jobs_status_msg['running'] and \
108
		'sleep 300' in command_line2) \
109
		\
110
		or \
111
		\
112
		(status_message2 == def_module.jobs_status_msg['stopped'] and \
113
		'sleep 30' in command_line2 and \
114
		\
115
		status_message == def_module.jobs_status_msg['running'] and \
116
		'sleep 300' in command_line), "Job status not properly displayed"
117
 
118
# Check that there are no duplicate job id's.
119
assert jobid != jobid2, "Duplicate job id's."
120
 
121
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
122
 
123
 
124
 
125
#bring the second sleep command back to foreground 
126
#so that we can end it with ctrl-c
127
c.sendline(def_module.builtin_commands['fg'] % jobid2)
128
 
129
 
130
#Wait until the sleep command takes over the foreground
131
# This is NOT a bullet-proof fix, you may fail on occasion!
132
time.sleep(1)
133
 
134
#ctrl-c to close the process
135
c.sendintr()
136
 
137
 
138
 
139
#clear any printout of the old job that was just killed by ctrl-c
140
c.sendline(def_module.builtin_commands['jobs'])
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 prompt and move past this text
147
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
148
 
149
 
150
#check the jobs list
151
c.sendline(def_module.builtin_commands['jobs'])
152
 
153
#check that the first job is still on the jobs list
154
assert (jobid, status_message, command_line) == \
155
    shellio.parse_regular_expression(c, def_module.job_status_regex), \
156
    "The original job was not displayed properly after ending a previous job."
157
 
158
# ensure the prompt is printed
159
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
160
 
161
 
162
 
163
# exit
164
c.sendline("exit");
165
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
166
 
167
shellio.success()