-
Notifications
You must be signed in to change notification settings - Fork 0
/
testrunner.py
445 lines (381 loc) · 15.6 KB
/
testrunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# Copyright 2010 Orbitz WorldWide
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Modified by NSN
# Copyright 2010-2012 Nokia Siemens Networks Oyj
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from Queue import Empty, Queue
import SocketServer
import atexit
import codecs
import os
import pickle
import shutil
import socket
import subprocess
import tempfile
import threading
import signal
import sys
from robot.output.loggerhelper import LEVELS
from robot.utils.encoding import SYSTEM_ENCODING
from robotide.context.platform import IS_WINDOWS
from robotide.contrib.testrunner import TestRunnerAgent
from robotide.controller.testexecutionresults import TestExecutionResults
import robotide.utils as utils
import jprops
ATEXIT_LOCK = threading.RLock()
class TestRunner(object):
def __init__(self, chief):
self._output_dir = None
self._process = None
self._server = None
self._server_thread = None
self._pause_on_failure = False
self._results = TestExecutionResults()
self.port = None
self._chief = chief
self.profiles = {}
def enable(self, result_handler):
self._start_listener_server(result_handler)
self._create_temporary_directory()
def _create_temporary_directory(self):
self._output_dir = tempfile.mkdtemp(".d", "RIDE")
atexit.register(self._remove_temporary_directory)
# this plugin creates a temporary directory which _should_
# get reaped at exit. Sometimes things happen which might
# cause it to not get deleted. Maybe this would be a good
# place to check for temporary directories that match the
# signature and delete them if they are more than a few
# days old...
def _remove_temporary_directory(self):
with ATEXIT_LOCK:
if os.path.exists(self._output_dir):
shutil.rmtree(self._output_dir)
def add_profile(self, name, item):
self.profiles[name] = item
def get_profile(self, name):
return self.profiles[name]
def get_profile_names(self):
return sorted(self.profiles.keys())
def _start_listener_server(self, result_handler):
def handle(*args):
self._result_handler(*args)
result_handler(*args)
self._server = RideListenerServer(RideListenerHandler, handle)
self._server_thread = threading.Thread(target=self._server.serve_forever)
self._server_thread.setDaemon(True)
self._server_thread.start()
self.port = self._server.server_address[1]
def _result_handler(self, event, *args):
if event == 'pid':
self._pid_to_kill = int(args[0])
if event == 'port' and self._process:
self._process.set_port(args[0])
self._send_pause_on_failure_information()
if event == 'start_test':
longname = args[1]['longname']
testname = args[0]
self._results.set_running(self._get_test_controller(longname, testname))
if event == 'end_test':
longname = args[1]['longname']
testname = args[0]
if args[1]['status'] == 'PASS':
self._results.set_passed(self._get_test_controller(longname, testname))
else:
self._results.set_failed(self._get_test_controller(longname, testname))
def _get_test_controller(self, longname, testname = None):
ret = self._chief.find_controller_by_longname(longname, testname)
return ret
def clear_server(self):
self._server = None
def shutdown_server(self):
if self._server:
self._server.shutdown()
def test_execution_started(self):
self._results.test_execution_started()
def kill_process(self):
if self._process:
self._process.kill(force=True)
def set_pause_on_failure(self, pause):
self._pause_on_failure = pause
self._send_pause_on_failure_information()
def _send_pause_on_failure_information(self):
if self._process:
self._process.pause_on_failure(self._pause_on_failure)
def send_stop_signal(self):
if self._process:
self._process.kill(killer_pid=self._pid_to_kill)
def send_pause_signal(self):
if self._process:
self._process.pause()
def send_continue_signal(self):
if self._process:
self._process.resume()
def send_step_next_signal(self):
if self._process:
self._process.step_next()
def send_step_over_signal(self):
if self._process:
self._process.step_over()
def run_command(self, command, cwd):
self._pid_to_kill = None
self._process = Process(cwd)
self._process.run_command(command)
def get_command(self, profile, pythonpath, monitor_width, test_names):
'''Return the command (as a list) used to run the test'''
command = profile.get_command_prefix()[:]
argfile = os.path.join(self._output_dir, "argfile.txt")
command.extend(["--argumentfile", argfile])
command.extend(["--listener", self._get_listener_to_cmd()])
command.append(self._get_suite_source_for_command())
self._write_argfile(argfile, self._create_standard_args(command, profile, pythonpath, monitor_width, test_names))
return command
def get_message_log_level(self, command):
min_log_level_number = LEVELS['INFO']
if '-L' in command:
switch = '-L'
elif '--loglevel' in command:
switch = '--loglevel'
else:
return min_log_level_number
i = command.index(switch)
if len(command) == i:
return
level = command[i+1].upper().split(':')[0]
return LEVELS.get(level, min_log_level_number)
def _get_listener_to_cmd(self):
path = os.path.abspath(TestRunnerAgent.__file__)
if path[-1] in ['c', 'o']:
path = path[:-1]
return '%s:%s' % (path, self.port)
def _get_suite_source_for_command(self):
cur = os.path.abspath(os.path.curdir)
source = os.path.abspath(self._chief.suite.source)
if not utils.is_same_drive(cur, source):
return source
return os.path.abspath(self._chief.suite.source)
def _create_standard_args(self, command, profile, pythonpath, monitor_width, test_names):
standard_args = []
standard_args.extend(profile.get_custom_args())
self._add_tmp_outputdir_if_not_given_by_user(command, standard_args)
self._add_pythonpath_if_in_settings_and_not_given_by_user(command,
standard_args,
pythonpath)
standard_args.extend(["--monitorcolors", "off"])
standard_args.extend(["--monitorwidth", monitor_width])
for tc in test_names:
standard_args += ['--test', tc]
return standard_args
def _add_tmp_outputdir_if_not_given_by_user(self, command, standard_args):
if "--outputdir" not in command and "-d" not in command:
standard_args.extend(["--outputdir", self._output_dir])
def _add_pythonpath_if_in_settings_and_not_given_by_user(self, command, standard_args, pythonpath):
if '--pythonpath' in command:
return
if '-P' in command:
return
if not pythonpath:
return
standard_args.extend(['--pythonpath', ':'.join(pythonpath)])
def _write_argfile(self, argfile, args):
f = codecs.open(argfile, "w", "utf-8")
f.write("\n".join(args))
f.close()
def get_output_and_errors(self):
return self._process.get_output(), self._process.get_errors()
def is_running(self):
return self._process and self._process.is_alive()
def command_ended(self):
self._process = None
class Process(object):
def __init__(self, cwd):
self._process = None
self._error_stream = None
self._output_stream = None
self._cwd = cwd
self._port = None
self._sock = None
def run_command(self, command):
# We need to supply an stdin for subprocess, because otherways in pythonw
# subprocess will try using sys.stdin which will cause an error in windows
subprocess_args = dict(bufsize=0,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
cwd=self._cwd.encode(SYSTEM_ENCODING))
if IS_WINDOWS:
startupinfo = subprocess.STARTUPINFO()
try:
import _subprocess
startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
except ImportError:
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess_args['startupinfo'] = startupinfo
else:
subprocess_args['preexec_fn'] = os.setsid
subprocess_args['shell'] = True
self._process = subprocess.Popen(command.encode(SYSTEM_ENCODING), **subprocess_args)
self._process.stdin.close()
self._output_stream = StreamReaderThread(self._process.stdout)
self._error_stream = StreamReaderThread(self._process.stderr)
self._output_stream.run()
self._error_stream.run()
self._kill_called = False
def set_port(self, port):
self._port = port
def get_output(self):
return self._output_stream.pop()
def get_errors(self):
return self._error_stream.pop()
def is_alive(self):
return self._process.poll() is None
def wait(self):
self._process.wait()
def kill(self, force=False, killer_pid=None):
if not self._process:
return
if force:
self._process.kill()
self.resume() # Send so that RF is not blocked
if IS_WINDOWS and not self._kill_called and self._port is not None:
self._signal_kill_with_listener_server()
self._kill_called = True
else:
self._kill(killer_pid or self._process.pid)
def _signal_kill_with_listener_server(self):
self._send_socket('kill')
'''New'''
def _send_socket(self, data):
envLocation = os.environ.get('AUTOMATED_HOME')
propLocation = os.path.join(envLocation, 'listenerLog0.properties')
propLocation = os.path.abspath(propLocation)
lastTestPassed = "true"
onHold = "false"
breakPoint = "false"
lastKeyword = ""
with open(propLocation) as fp:
for key, value in jprops.iter_properties(fp):
if key.startswith('lastTestPassed'):
lastTestPassed = value
if key.startswith('onHold'):
onHold = value
if key.startswith('breakPoint'):
breakPoint = value
if key.startswith('lastKeyword'):
lastKeyword = value
'''lastTestPassed = "true"
onHold = "false"
breakPoint = "false"
with open(propLocation) as fp:
for key, value in jprops.iter_properties(fp):
if key.startswith('lastTestPassed'):
lastTestPassed = value
if key.startswith('onHold'):
onHold = value
if key.startswith('breakPoint'):
breakPoint = value
with open(propLocation, 'w') as fp:
jprops.write_property(fp, 'lastTestPassed', lastTestPassed)
jprops.write_property(fp, 'onHold', onHold)
jprops.write_property(fp, 'breakPoint', 'true')'''
with open(propLocation, 'w') as fp:
jprops.write_property(fp, 'data', data)
jprops.write_property(fp, 'lastTestPassed', lastTestPassed)
jprops.write_property(fp, 'onHold', onHold)
jprops.write_property(fp, 'lastKeyword', lastKeyword)
jprops.write_property(fp, 'breakPoint', 'true')
'''def _send_socket(self, data):
if self._port is None:
return # Silent failure..
sock = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', self._port))
sock.send(data)
finally:
sock.close()'''
def pause(self):
self._send_socket('pause')
def pause_on_failure(self, pause):
if pause:
self._send_socket('pause_on_failure')
else:
self._send_socket('do_not_pause_on_failure')
def resume(self):
self._send_socket('resume')
def step_next(self):
self._send_socket('step_next')
def step_over(self):
self._send_socket('step_over')
def _kill(self, pid):
if pid:
try:
if os.name == 'nt' and sys.version_info < (2,7):
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(self._process._handle), -1)
else:
os.kill(pid, signal.SIGINT)
except OSError:
pass
class StreamReaderThread(object):
def __init__(self, stream):
self._queue = Queue()
self._thread = None
self._stream = stream
def run(self):
self._thread = threading.Thread(target=self._enqueue_output, args=(self._stream,))
self._thread.daemon = True
self._thread.start()
def _enqueue_output(self, out):
for line in iter(out.readline, b''):
self._queue.put(line)
def pop(self):
result = ""
for _ in xrange(self._queue.qsize()):
try:
result += self._queue.get_nowait()
except Empty:
pass
return result.decode('UTF-8')
# The following two classes implement a small line-buffered socket
# server. It is designed to run in a separate thread, read data
# from the given port and update the UI -- hopefully all in a
# thread-safe manner.
class RideListenerServer(SocketServer.TCPServer):
"""Implements a simple line-buffered socket server"""
allow_reuse_address = True
def __init__(self, RequestHandlerClass, callback):
SocketServer.TCPServer.__init__(self, ("",0), RequestHandlerClass)
self.callback = callback
class RideListenerHandler(SocketServer.StreamRequestHandler):
def handle(self):
unpickler = pickle.Unpickler(self.request.makefile('r'))
while True:
try:
(name, args) = unpickler.load()
self.server.callback(name, *args)
except (EOFError, IOError):
# I should log this...
break