| 141 |
Kevin |
1 |
#!/usr/bin/python
|
|
|
2 |
#
|
|
|
3 |
# bg_test: tests the bg command
|
|
|
4 |
#
|
|
|
5 |
# Test the stop command for stopping a process by its pid.
|
|
|
6 |
# Requires the following commands to be implemented
|
|
|
7 |
# or otherwise usable:
|
|
|
8 |
#
|
|
|
9 |
# bg, sleep, stop
|
|
|
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 |
|
|
|
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 program sleep
|
|
|
41 |
c.sendline("sleep 30 &")
|
|
|
42 |
|
|
|
43 |
#Used to get the jobid and pid of the sleep process
|
|
|
44 |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
|
|
|
45 |
|
|
|
46 |
# ensure that shell prints expected prompt
|
|
|
47 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
# send the stop command to the process
|
|
|
52 |
c.sendline(def_module.builtin_commands['stop'] % jobid)
|
|
|
53 |
|
|
|
54 |
#Ensure that sleep has enough time to stop before we read its
|
|
|
55 |
#/proc/ /stat file.
|
|
|
56 |
proc_check.count_children_timeout(c, 1, 1)
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
#Ensure that sleep is now stopped in the background, and is not
|
|
|
61 |
#the foreground process.
|
|
|
62 |
assert not proc_check.check_pid_fgpgrp(pid), \
|
|
|
63 |
'Error: process is in the foreground'
|
|
|
64 |
assert proc_check.check_pid_status(pid, 'T'), 'Error: process not stopped'
|
|
|
65 |
|
|
|
66 |
#check the prompt prints
|
|
|
67 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
#resume the sleep program
|
|
|
72 |
c.sendline(def_module.builtin_commands['bg'] % jobid)
|
|
|
73 |
|
|
|
74 |
#check the prompt prints
|
|
|
75 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
76 |
|
|
|
77 |
#Ensure that sleep has enough time to start before we read its
|
|
|
78 |
#/proc/ /stat file.
|
|
|
79 |
proc_check.count_children_timeout(c, 1, 1)
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
#Ensure that sleep is running now in the background, and is not
|
|
|
84 |
#the foreground process.
|
|
|
85 |
assert not proc_check.check_pid_fgpgrp(pid), \
|
|
|
86 |
'Error: process is in the foreground'
|
|
|
87 |
assert proc_check.check_pid_status(pid, 'S'), 'Error: process not running'
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
#exit
|
|
|
92 |
c.sendline("exit");
|
|
|
93 |
assert c.expect("exit\r\n") == 0, "Shell output extraneous characters"
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
shellio.success()
|