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
# stop_test: tests the stop 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
#	stop, sleep
10
#
11
 
12
 
13
import sys, imp, atexit
14
sys.path.append("/home/courses/cs3214/software/pexpect-dpty/");
15
import pexpect, shellio, signal, time, os, re, proc_check
16
 
17
#Ensure the shell process is terminated
18
def force_shell_termination(shell_process):
19
	c.close(force=True)
20
 
21
#pulling in the regular expression and other definitions
22
definitions_scriptname = sys.argv[1]
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 a command
41
c.sendline("sleep 30 &")
42
 
43
# pull the jobid and pid from the background process printout
44
(jobid, pid) = shellio.parse_regular_expression(c, def_module.bgjob_regex)
45
 
46
# ensure that the shell prints the expected prompt
47
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
48
 
49
 
50
 
51
#The job needs to be running when we call stop
52
proc_check.count_children_timeout(c, 1, 1)
53
 
54
# send the stop command to the process
55
c.sendline(def_module.builtin_commands['stop'] % jobid)
56
 
57
#Ensure that sleep has enough time to stop before we read its
58
#/proc/ pid /stat file.
59
time.sleep(.5)
60
 
61
 
62
 
63
#Ensure that sleep is now stopped in the background, and is not
64
#the foreground process.
65
assert not proc_check.check_pid_fgpgrp(pid), \
66
                            'Error: process is in the foreground'
67
assert proc_check.check_pid_status(pid, 'T'), 'Error: process not stopped'
68
 
69
#check the prompt prints
70
assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt"
71
 
72
 
73
 
74
c.sendline("exit");
75
assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters"
76
 
77
shellio.success()