587
|
1 |
#============================================================================
|
|
2 |
#Name : test_timeout_launcher.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
""" unit tests the timeout launcher """
|
|
20 |
|
628
|
21 |
# pylint: disable=E1101
|
587
|
22 |
|
|
23 |
import logging
|
|
24 |
import sys
|
|
25 |
import mocker
|
|
26 |
import subprocess
|
|
27 |
|
|
28 |
|
|
29 |
_logger = logging.getLogger('test.configuration')
|
|
30 |
logging.basicConfig(level=logging.INFO)
|
|
31 |
|
|
32 |
# Platform
|
|
33 |
WINDOWS = False
|
|
34 |
if sys.platform == "win32":
|
|
35 |
import win32process
|
|
36 |
import win32con
|
|
37 |
import win32api
|
|
38 |
WINDOWS = True
|
|
39 |
|
|
40 |
|
628
|
41 |
# pylint: disable=C0103
|
588
|
42 |
|
587
|
43 |
class os(object): #has to be named this as it is mocking os method.
|
|
44 |
""" dummy the os function call"""
|
588
|
45 |
|
587
|
46 |
def kill(self, pid, value):
|
|
47 |
"""dummy OS class"""
|
|
48 |
pid = value #just for pylint
|
|
49 |
value = pid #just for pylint
|
|
50 |
return 1
|
588
|
51 |
# pylint: enable-msg=C0103
|
587
|
52 |
|
|
53 |
class TimeoutLauncherTest(mocker.MockerTestCase):
|
|
54 |
"""class containing methods to test the timeout launcher"""
|
|
55 |
|
|
56 |
def test_cmdlineIsEmpty(self):
|
|
57 |
"""test_cmdlineIsEmpty: nothing in the command line"""
|
|
58 |
import timeout_launcher
|
|
59 |
obj = self.mocker.replace(timeout_launcher.sys)
|
|
60 |
obj.exit(-1)
|
|
61 |
self.mocker.result(1)
|
|
62 |
self.mocker.replay()
|
|
63 |
|
|
64 |
sys.argv = ['timeout_launcher.py', '--timeout=1', 'version']
|
|
65 |
timeout_launcher.main()
|
|
66 |
|
628
|
67 |
# pylint: disable=W0104
|
587
|
68 |
|
|
69 |
def test_valid_with_timeout(self):
|
|
70 |
"""test_valid_with_timeout: initial test with valid values and timeout."""
|
|
71 |
import timeout_launcher
|
|
72 |
cmdline = ['dir']
|
|
73 |
shell = True
|
|
74 |
if WINDOWS:
|
|
75 |
shell = False
|
|
76 |
process = self.mocker.mock()
|
|
77 |
process.poll()
|
|
78 |
self.mocker.result(None)
|
|
79 |
process.poll()
|
|
80 |
self.mocker.result(1) #pylint says 'Statement seems to have no effect' (it's mocking code so it is valid) hence disable W0104
|
|
81 |
|
|
82 |
obj = self.mocker.replace("subprocess.Popen")
|
|
83 |
obj(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
|
|
84 |
self.mocker.result(process)
|
|
85 |
|
|
86 |
obj2 = self.mocker.replace(timeout_launcher.sys)
|
|
87 |
obj2.exit(mocker.ANY)
|
|
88 |
self.mocker.result(1)
|
|
89 |
|
|
90 |
process.communicate()[0]
|
|
91 |
self.mocker.result(None)
|
|
92 |
process.returncode
|
|
93 |
self.mocker.result(1)
|
|
94 |
|
|
95 |
self.mocker.replay()
|
|
96 |
|
|
97 |
sys.argv = ['--timeout=1', '--', ' '.join(cmdline)]
|
|
98 |
timeout_launcher.main()
|
|
99 |
|
|
100 |
def test_valid_no_timeout(self):
|
|
101 |
"""test_valid_no_timeout: initial test with valid values and no timeout."""
|
|
102 |
import timeout_launcher
|
|
103 |
cmdline = ['dir']
|
|
104 |
process = self.mocker.mock()
|
|
105 |
|
|
106 |
obj = self.mocker.replace("subprocess.Popen")
|
628
|
107 |
shell = True
|
|
108 |
if WINDOWS:
|
|
109 |
shell = False
|
|
110 |
obj(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
|
587
|
111 |
self.mocker.result(process)
|
|
112 |
|
|
113 |
process.communicate()[0]
|
|
114 |
self.mocker.result(None)
|
|
115 |
process.returncode
|
|
116 |
self.mocker.result(1)
|
|
117 |
|
|
118 |
obj2 = self.mocker.replace(timeout_launcher.sys)
|
|
119 |
obj2.exit(mocker.ANY)
|
|
120 |
self.mocker.result(1)
|
|
121 |
self.mocker.replay()
|
|
122 |
|
|
123 |
sys.argv = ['--', ' '.join(cmdline)]
|
|
124 |
timeout_launcher.main()
|
|
125 |
|
|
126 |
def test_timedout(self):
|
|
127 |
"""test_timedout: initial test with valid values but times out."""
|
|
128 |
import timeout_launcher
|
|
129 |
cmdline = ['dir']
|
|
130 |
shell = True
|
|
131 |
if WINDOWS:
|
|
132 |
shell = False
|
|
133 |
process = self.mocker.mock()
|
|
134 |
|
|
135 |
timeValue = self.mocker.replace("time.time", count=False)
|
|
136 |
timeValue()
|
|
137 |
self.mocker.result(1)
|
|
138 |
#if debug not set then it won't call the logger functions and so these are not needed
|
|
139 |
if logging.DEBUG:
|
|
140 |
timeValue()
|
|
141 |
self.mocker.result(1)
|
|
142 |
timeValue()
|
|
143 |
self.mocker.result(1)
|
|
144 |
timeValue()
|
|
145 |
self.mocker.result(1)
|
|
146 |
timeValue()
|
|
147 |
self.mocker.result(1)
|
|
148 |
|
|
149 |
obj = self.mocker.replace("subprocess.Popen")
|
|
150 |
obj(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
|
|
151 |
self.mocker.result(process)
|
|
152 |
|
|
153 |
self.mocker.order()
|
|
154 |
process.poll()
|
|
155 |
self.mocker.result(None)
|
|
156 |
timeValue()
|
|
157 |
self.mocker.result(5)
|
|
158 |
self.mocker.unorder()
|
|
159 |
|
|
160 |
process.pid
|
|
161 |
self.mocker.result(0x0129B460)
|
|
162 |
|
|
163 |
if WINDOWS:
|
|
164 |
handle = self.mocker.mock()
|
|
165 |
handle_1 = self.mocker.replace("win32api.OpenProcess")
|
|
166 |
handle_1(True, win32con.PROCESS_TERMINATE, mocker.ANY)
|
|
167 |
self.mocker.result(handle)
|
|
168 |
handle_1 = self.mocker.replace("win32process.TerminateProcess")
|
|
169 |
handle_1(handle, -1)
|
|
170 |
|
|
171 |
|
|
172 |
self.mocker.replay()
|
|
173 |
|
|
174 |
sys.argv = ['--timeout=3', '--', ' '.join(cmdline)]
|
|
175 |
failed = False
|
|
176 |
try:
|
|
177 |
timeout_launcher.main()
|
628
|
178 |
except Exception:
|
587
|
179 |
failed = True
|
|
180 |
assert failed
|