2014-12-26 10:41:15 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2015-07-07 03:05:59 +03:00
|
|
|
import platform
|
|
|
|
|
|
|
|
(system, node, release, version, machine, processor) = platform.uname()
|
|
|
|
|
|
|
|
httpsServer = ['node', 'httpsServer.js']
|
2015-07-07 03:07:00 +03:00
|
|
|
sslEchoServer = ['node', 'sslEchoServer.js']
|
2015-07-07 03:05:59 +03:00
|
|
|
|
|
|
|
if system == "Darwin":
|
|
|
|
httpsServer = './httpsServer.py'
|
2015-07-07 03:07:00 +03:00
|
|
|
sslEchoServer = './sslEchoServer.py'
|
2014-12-26 10:41:15 +03:00
|
|
|
|
|
|
|
# The test automation scripts to run via casperjs/slimerjs.
|
|
|
|
automation_scripts = [
|
2015-03-27 02:02:40 +03:00
|
|
|
['tests', 'automation.js'],
|
|
|
|
['tests', 'fs', 'automation.js'],
|
2014-12-26 10:41:15 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
# The exit code to return. We set this to 1 if an automation script outputs
|
|
|
|
# "FAIL" (case-sensitive) at any point in the test run. Ideally, we'd determine
|
|
|
|
# the success/failure based on the exit code of the test run, but casperjs
|
|
|
|
# with slimerjs always returns 0, so instead we must look for the string "FAIL",
|
|
|
|
# which only occurs on test failure.
|
|
|
|
#
|
|
|
|
# See https://github.com/laurentj/slimerjs/issues/50 for more information.
|
|
|
|
#
|
|
|
|
exit_code = 0
|
|
|
|
|
|
|
|
# Open the server processes that handle HTTP/S requests and socket connections.
|
|
|
|
# We pipe their standard output/error back to the parent process to print it
|
|
|
|
# to the parent's standard output interspersed with the output produced by
|
|
|
|
# the automation scripts (which we have to pipe, instead of letting them print
|
|
|
|
# their output directly, so we can look for "FAIL", as described above).
|
|
|
|
#
|
|
|
|
# We don't actually pipe anything into standard input, but we have to specify
|
|
|
|
# that a new pipe should be created for it, otherwise for some reason it causes
|
|
|
|
# print/sys.stdout.write in the parent process to throw the exception "IOError:
|
|
|
|
# [Errno 35] Resource temporarily unavailable" on large amounts of output.
|
|
|
|
#
|
|
|
|
server_processes = [
|
|
|
|
subprocess.Popen('tests/httpServer.py', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
|
|
bufsize=1),
|
|
|
|
subprocess.Popen('tests/echoServer.py', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
|
|
bufsize=1),
|
|
|
|
|
|
|
|
# The SSL-based servers need to have their current working directory set
|
|
|
|
# to the tests/ subdirectory, since they load cert/key files relative to it.
|
2015-07-07 03:05:59 +03:00
|
|
|
subprocess.Popen(httpsServer, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
2014-12-26 10:41:15 +03:00
|
|
|
bufsize=1, cwd='tests'),
|
2015-07-07 03:07:00 +03:00
|
|
|
subprocess.Popen(sslEchoServer, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
2014-12-26 10:41:15 +03:00
|
|
|
bufsize=1, cwd='tests'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# The output streams for the servers.
|
|
|
|
server_output_streams = [p.stdout for p in server_processes]
|
|
|
|
|
|
|
|
def wait_server(port):
|
|
|
|
end = time.time() + 30 # Timeout of 30 seconds
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.connect(('', port))
|
|
|
|
s.close()
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
if end < time.time():
|
|
|
|
raise Exception("Can't connect to " + str(port))
|
|
|
|
else:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def run_test(script_path):
|
2014-12-31 05:12:21 +03:00
|
|
|
global exit_code
|
|
|
|
|
2015-04-08 01:41:49 +03:00
|
|
|
args = ['casperjs', '--engine=slimerjs']
|
2015-06-06 01:41:17 +03:00
|
|
|
if 'VERBOSE' in os.environ and os.environ['VERBOSE'] != '0':
|
2015-04-08 01:41:49 +03:00
|
|
|
args.append('--log-level=debug')
|
|
|
|
args.extend(['test', script_path])
|
|
|
|
|
|
|
|
script_process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
|
|
bufsize=1)
|
2014-12-26 10:41:15 +03:00
|
|
|
output_streams = list(server_output_streams)
|
|
|
|
output_streams.append(script_process.stdout)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
readable_streams, _, _ = select.select(output_streams, [], [])
|
|
|
|
|
|
|
|
for stream in readable_streams:
|
|
|
|
line = stream.readline()
|
|
|
|
|
|
|
|
if stream is script_process.stdout and "FAIL" in line:
|
|
|
|
exit_code = 1
|
|
|
|
|
|
|
|
sys.stdout.write(line)
|
|
|
|
|
|
|
|
if script_process.poll() is not None:
|
|
|
|
# Print any famous last words the process wrote to its output stream
|
|
|
|
# between the last time we polled it and its termination.
|
|
|
|
sys.stdout.write(script_process.stdout.read())
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
# Wait for the servers to become ready for connections.
|
|
|
|
wait_server(8000)
|
|
|
|
wait_server(50003)
|
|
|
|
wait_server(4443)
|
|
|
|
wait_server(54443)
|
|
|
|
|
|
|
|
# Run each test automation script in turn.
|
2015-03-27 02:02:40 +03:00
|
|
|
for scriptParts in automation_scripts:
|
|
|
|
run_test(os.path.join(os.getcwd(), *scriptParts))
|
2014-12-26 10:41:15 +03:00
|
|
|
|
|
|
|
# Terminate all the server processes.
|
|
|
|
for process in server_processes:
|
|
|
|
process.terminate()
|
|
|
|
|
|
|
|
# Print any famous last words the processes wrote to their output streams
|
|
|
|
# between the last time we polled them and their termination.
|
|
|
|
for stream in server_output_streams:
|
|
|
|
sys.stdout.write(stream.read())
|
|
|
|
|
2015-02-18 00:45:18 +03:00
|
|
|
p = subprocess.Popen(['js', 'jsshell.js', 'Basic'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2015-02-27 14:19:14 +03:00
|
|
|
shell_success = False
|
2015-02-18 00:45:18 +03:00
|
|
|
for line in iter(p.stdout.readline, b''):
|
|
|
|
if "The end" in line:
|
2015-02-27 14:19:14 +03:00
|
|
|
shell_success = True
|
2015-02-18 00:45:18 +03:00
|
|
|
sys.stdout.write(line)
|
|
|
|
|
2015-02-27 14:19:14 +03:00
|
|
|
if exit_code == 0 and not shell_success:
|
|
|
|
exit_code = 1
|
|
|
|
|
2014-12-26 10:41:15 +03:00
|
|
|
sys.exit(exit_code)
|