| 141 |
Kevin |
1 |
#!/usr/bin/python
|
|
|
2 |
#
|
|
|
3 |
# kill_test: tests the kill command with the default
|
|
|
4 |
# semantics of:
|
|
|
5 |
#
|
|
|
6 |
# kill <pid>
|
|
|
7 |
#
|
|
|
8 |
# This test may require updating such that we test other signals
|
|
|
9 |
#
|
|
|
10 |
# Requires the following commands to be implemented
|
|
|
11 |
# or otherwise usable:
|
|
|
12 |
#
|
|
|
13 |
# kill, sleep
|
|
|
14 |
#
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
import sys, imp, atexit
|
|
|
18 |
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/");
|
|
|
19 |
import pexpect, shellio, signal, time, os, re, proc_check
|
|
|
20 |
|
|
|
21 |
#Ensure the shell process is terminated
|
|
|
22 |
def force_shell_termination(shell_process):
|
|
|
23 |
c.close(force=True)
|
|
|
24 |
|
|
|
25 |
# pulling in the regular expression and other definitions
|
|
|
26 |
definitions_scriptname = sys.argv[1]
|
|
|
27 |
def_module = imp.load_source('', definitions_scriptname)
|
|
|
28 |
logfile = None
|
|
|
29 |
if hasattr(def_module, 'logfile'):
|
|
|
30 |
logfile = def_module.logfile
|
|
|
31 |
|
|
|
32 |
# spawn an instance of the shell
|
|
|
33 |
c = pexpect.spawn(def_module.shell, drainpty=True, logfile=logfile)
|
|
|
34 |
atexit.register(force_shell_termination, shell_process=c)
|
|
|
35 |
|
|
|
36 |
# set timeout for all following 'expect*' calls to 2 seconds
|
|
|
37 |
c.timeout = 2
|
|
|
38 |
|
|
|
39 |
# ensure that the shell prints the expected prompt
|
|
|
40 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
# run a command
|
|
|
45 |
c.sendline("sleep 30 &")
|
|
|
46 |
|
|
|
47 |
# parse the jobid and pid output
|
|
|
48 |
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
|
|
|
49 |
|
|
|
50 |
# ensure that the shell prints the expected prompt
|
|
|
51 |
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
|
|
|
52 |
|
|
|
53 |
# The job needs to be running when we call kill
|
|
|
54 |
proc_check.count_children_timeout(c, 1, 1)
|
|
|
55 |
|
|
|
56 |
# Run the kill command and kill the sleep process in the background
|
|
|
57 |
c.sendline(def_module.builtin_commands['kill'] % jobid)
|
|
|
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 |
# ensure there is enough time for the process to be killed
|
|
|
63 |
time.sleep(.5)
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
# check the proc file that the process has actually been stopped
|
|
|
68 |
# the proc file should not exist
|
|
|
69 |
assert not os.path.exists("/proc/" + pid + "/stat"), 'the process was not \
|
|
|
70 |
killed'
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
# end the shell program by sending it an end-of-file character
|
|
|
74 |
c.sendline("exit");
|
|
|
75 |
|
|
|
76 |
# ensure that no extra characters are output after exiting
|
|
|
77 |
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
|
|
|
78 |
|
|
|
79 |
# the test was successful
|
|
|
80 |
shellio.success()
|