2012-11-09 21:03:57 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import subprocess
|
2012-11-13 23:54:08 +01:00
|
|
|
import requests
|
2013-05-21 23:09:08 +02:00
|
|
|
import optparse
|
2012-11-09 21:03:57 +01:00
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os
|
2013-03-05 22:50:06 +01:00
|
|
|
import glob
|
2012-11-09 21:03:57 +01:00
|
|
|
|
2013-05-21 23:09:08 +02:00
|
|
|
#
|
|
|
|
# In order to use remote casperjs debugging, pass the --remote-debug flag
|
|
|
|
# This will start a remote debugging session listening on port 7777
|
|
|
|
#
|
2013-07-24 23:29:47 +02:00
|
|
|
# See https://wiki.zulip.net/wiki/Testing_the_app for more information
|
2013-05-21 23:09:08 +02:00
|
|
|
# on how to use remote debugging
|
|
|
|
#
|
|
|
|
|
2013-01-10 19:05:53 +01:00
|
|
|
os.environ["TORNADO_SERVER"] = "http://localhost:9983"
|
|
|
|
|
2013-05-21 23:09:08 +02:00
|
|
|
parser = optparse.OptionParser(r"""
|
|
|
|
|
|
|
|
%prog --remote-debug file1 file2 file3""")
|
|
|
|
|
|
|
|
parser.add_option('--remote-debug',
|
|
|
|
help='Whether or not to enable remote debugging on port 7777',
|
|
|
|
action="store_true",
|
|
|
|
default=False)
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2013-03-05 17:24:37 +01:00
|
|
|
test_files = []
|
2013-05-21 23:09:08 +02:00
|
|
|
for file in args:
|
2013-03-05 17:24:37 +01:00
|
|
|
if not os.path.exists(file):
|
2015-09-20 00:51:17 +02:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '../zerver/tests/frontend/tests', file)
|
2013-10-28 15:54:32 +01:00
|
|
|
test_files.append(os.path.abspath(file))
|
2013-03-05 17:24:37 +01:00
|
|
|
|
2013-10-28 15:54:32 +01:00
|
|
|
os.chdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../..'))
|
2012-11-09 21:03:57 +01:00
|
|
|
|
2013-08-07 21:41:34 +02:00
|
|
|
subprocess.check_call('tools/generate-fixtures')
|
2012-11-09 21:03:57 +01:00
|
|
|
|
2015-08-20 02:57:30 +02:00
|
|
|
subprocess.check_call('tools/generate-test-credentials')
|
|
|
|
|
2013-03-05 22:50:06 +01:00
|
|
|
subprocess.check_call(['rm', '-f'] + glob.glob('/tmp/casper-failure*.png'))
|
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
log = open('zerver/tests/frontend/server.log', 'w')
|
2012-11-09 21:03:57 +01:00
|
|
|
|
|
|
|
# Run this not through the shell, so that we have the actual PID.
|
|
|
|
server = subprocess.Popen(('tools/run-dev.py', '--test'),
|
|
|
|
stdout=log, stderr=log)
|
|
|
|
|
2012-11-14 20:04:31 +01:00
|
|
|
def assert_server_running():
|
|
|
|
# Get the exit code of the server, or None if it is still running.
|
|
|
|
if server.poll() is not None:
|
2013-07-29 23:03:31 +02:00
|
|
|
raise RuntimeError, 'Server died unexpectedly! Check zerver/tests/frontend/server.log'
|
2012-11-14 20:04:31 +01:00
|
|
|
|
2012-11-13 23:54:08 +01:00
|
|
|
def server_is_up():
|
2012-11-14 20:04:31 +01:00
|
|
|
assert_server_running()
|
2012-11-13 23:54:08 +01:00
|
|
|
try:
|
|
|
|
# We could get a 501 error if the reverse proxy is up but the Django app isn't.
|
|
|
|
return requests.get('http://localhost:9981/accounts/home').status_code == 200
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2012-11-14 00:01:43 +01:00
|
|
|
ret = 1
|
2012-11-09 21:03:57 +01:00
|
|
|
|
2012-11-14 00:01:43 +01:00
|
|
|
try:
|
|
|
|
# Wait for the server to start up.
|
2012-11-16 16:56:15 +01:00
|
|
|
sys.stdout.write('Waiting for test server')
|
2012-11-14 00:01:43 +01:00
|
|
|
while not server_is_up():
|
2012-11-16 16:56:15 +01:00
|
|
|
sys.stdout.write('.')
|
|
|
|
sys.stdout.flush()
|
2012-11-14 00:01:43 +01:00
|
|
|
time.sleep(0.1)
|
2012-11-16 16:56:15 +01:00
|
|
|
sys.stdout.write('\n')
|
2012-11-09 21:03:57 +01:00
|
|
|
|
2013-05-21 23:09:08 +02:00
|
|
|
remote_debug = ""
|
|
|
|
if options.remote_debug:
|
|
|
|
remote_debug = "--remote-debugger-port=7777 --remote-debugger-autorun=yes"
|
2013-07-29 23:03:31 +02:00
|
|
|
cmd = "zerver/tests/frontend/casperjs/bin/casperjs %s test " % (remote_debug,)
|
2013-05-21 23:09:08 +02:00
|
|
|
|
2013-03-05 17:24:37 +01:00
|
|
|
if test_files:
|
|
|
|
cmd += ' '.join(test_files)
|
|
|
|
else:
|
2013-07-29 23:03:31 +02:00
|
|
|
cmd += 'zerver/tests/frontend/tests'
|
2013-03-05 17:24:37 +01:00
|
|
|
print "Running %s" % (cmd,)
|
|
|
|
ret = subprocess.call(cmd, shell=True)
|
2012-11-14 00:01:43 +01:00
|
|
|
finally:
|
2012-11-14 20:04:31 +01:00
|
|
|
assert_server_running()
|
2012-11-14 00:01:43 +01:00
|
|
|
server.terminate()
|
2012-11-09 21:03:57 +01:00
|
|
|
|
2015-08-21 22:48:57 +02:00
|
|
|
if ret != 0:
|
|
|
|
print >>sys.stderr, """
|
|
|
|
Oops, the frontend tests failed. Tips for debugging:
|
|
|
|
* Check the frontend test server logs at zerver/tests/frontend/server.log
|
|
|
|
* Check the screenshots of failed tests at /tmp/casper-failure*.png
|
|
|
|
* Try remote debugging the test web browser as described in docs/testing.rst
|
|
|
|
"""
|
|
|
|
|
2012-11-09 21:03:57 +01:00
|
|
|
sys.exit(ret)
|