Commit 56448ead authored by Julien Muchembled's avatar Julien Muchembled

ERP5TypeFunctionalTestCase: fix a few coding crimes and do some cleanup

parent a0e5aa6b
...@@ -31,7 +31,7 @@ import os ...@@ -31,7 +31,7 @@ import os
import time import time
import signal import signal
import re import re
from subprocess import Popen, PIPE import subprocess
import shutil import shutil
import transaction import transaction
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase, \ from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase, \
...@@ -71,29 +71,19 @@ class Xvfb: ...@@ -71,29 +71,19 @@ class Xvfb:
self.display_list = [":%s" % i for i in range(123, 144)] self.display_list = [":%s" % i for i in range(123, 144)]
self.display = None self.display = None
self.fbdir = fbdir self.fbdir = fbdir
self.pid = None
def _runCommand(self, display): def _runCommand(self, display):
xvfb_bin = os.environ.get("xvfb_bin", "Xvfb") xvfb_bin = os.environ.get("xvfb_bin", "Xvfb")
command = [xvfb_bin, '-fbdir' , self.fbdir, display] with open(os.devnull, 'w') as null:
self.process = subprocess.Popen(
self.process = Popen(" ".join(command), (xvfb_bin, '-fbdir' , self.fbdir, display),
stdout=PIPE, stdout=null, stderr=null, close_fds=True)
stderr=PIPE, # try to check if X screen is available
shell=True, time.sleep(5)
close_fds=True) if subprocess.call(('xdpyinfo', '-display', display),
stdout=null, stderr=subprocess.STDOUT):
# try to check if X screen is available # Xvfb did not start properly so stop here
time.sleep(5) raise EnvironmentError("Can not start Xvfb, stop test execution")
check_process = Popen('xdpyinfo -display %s >/dev/null 2>&1 && echo "Used" || echo "Free"' %display,
stdout=PIPE,
stderr=PIPE,
shell=True,
close_fds=True)
result = check_process.communicate()[0]
if result == 'Free':
# Xvfb did not start properly so stop here
raise NotImplementedError, "Can not start Xvfb, stop test execution"
def run(self): def run(self):
for display_try in self.display_list: for display_try in self.display_list:
...@@ -103,38 +93,22 @@ class Xvfb: ...@@ -103,38 +93,22 @@ class Xvfb:
self.display = display_try self.display = display_try
break break
display = os.environ.get('DISPLAY')
if display:
auth = Popen(['xauth', 'list', display], stdout=PIPE).communicate()[0]
if auth:
(displayname, protocolname, hexkey) = auth.split()
Popen(['xauth', 'add', 'localhost/unix:%s' % display, protocolname, hexkey])
print 'Xvfb : %d' % self.process.pid print 'Xvfb : %d' % self.process.pid
print 'Take screenshots using xwud -in %s/Xvfb_screen0' % self.fbdir print 'Take screenshots using xwud -in %s/Xvfb_screen0' % self.fbdir
def quit(self): def quit(self):
if hasattr(self, 'process'): if hasattr(self, 'process'):
process_pid = self.process.pid self.process.terminate()
try:
self.process.terminate()
finally:
if process_pid:
print "Stopping Xvfb on pid: %s" % self.pid
os.kill(process_pid, signal.SIGTERM)
class Browser: class Browser:
use_xvfb = 1
def __init__(self, profile_dir, host, port): def __init__(self, profile_dir, host, port):
self.profile_dir = profile_dir self.profile_dir = profile_dir
self.host = host self.host = host
self.port = port self.port = port
self.pid = None
def quit(self): def quit(self):
if self.pid: self.process.kill()
os.kill(self.pid, signal.SIGTERM)
def _run(self, url, display): def _run(self, url, display):
""" This method should be implemented on a subclass """ """ This method should be implemented on a subclass """
...@@ -148,11 +122,12 @@ class Browser: ...@@ -148,11 +122,12 @@ class Browser:
self._setEnviron() self._setEnviron()
self._setDisplay(display) self._setDisplay(display)
self._run(url) self._run(url)
print "Browser %s running on pid: %s" % (self.__class__.__name__, self.pid) print "Browser %s running on pid: %s" % (self.__class__.__name__,
self.process.pid)
def clean(self): def clean(self):
""" Clean up removing profile dir and recreating it""" """ Clean up removing profile dir and recreating it"""
os.system("rm -rf %s" % self.profile_dir) shutil.rmtree(self.profile_dir, ignore_errors=True)
os.mkdir(self.profile_dir) os.mkdir(self.profile_dir)
def _createFile(self, filename, content): def _createFile(self, filename, content):
...@@ -162,17 +137,12 @@ class Browser: ...@@ -162,17 +137,12 @@ class Browser:
return file_path return file_path
def _setDisplay(self, display): def _setDisplay(self, display):
if display is None: if display:
try:
shutil.copy2(os.path.expanduser('~/.Xauthority'), '%s/.Xauthority' % self.profile_dir)
except IOError:
pass
else:
os.environ["DISPLAY"] = display os.environ["DISPLAY"] = display
def _runCommand(self, command_tuple): def _runCommand(self, *args):
print " ".join(list(command_tuple)) print " ".join(args)
self.pid = os.spawnlp(os.P_NOWAIT, *command_tuple) self.process = subprocess.Popen(args, close_fds=True)
class Firefox(Browser): class Firefox(Browser):
""" Use firefox to open run all the tests""" """ Use firefox to open run all the tests"""
...@@ -188,8 +158,8 @@ class Firefox(Browser): ...@@ -188,8 +158,8 @@ class Firefox(Browser):
# Prepare to run # Prepare to run
self._createFile('prefs.js', self.getPrefJs()) self._createFile('prefs.js', self.getPrefJs())
firefox_bin = os.environ.get("firefox_bin", "firefox") firefox_bin = os.environ.get("firefox_bin", "firefox")
self._runCommand((firefox_bin, firefox_bin, "-no-remote", self._runCommand(firefox_bin, "-no-remote",
"-profile", self.profile_dir, url)) "-profile", self.profile_dir, url)
os.environ['MOZ_NO_REMOTE'] = '0' os.environ['MOZ_NO_REMOTE'] = '0'
...@@ -227,8 +197,11 @@ user_pref("dom.max_script_run_time", 120); ...@@ -227,8 +197,11 @@ user_pref("dom.max_script_run_time", 120);
user_pref("capability.principal.codebase.p1.granted", "UniversalFileRead"); user_pref("capability.principal.codebase.p1.granted", "UniversalFileRead");
user_pref("signed.applets.codebase_principal_support", true); user_pref("signed.applets.codebase_principal_support", true);
user_pref("capability.principal.codebase.p1.id", "http://%s:%s"); user_pref("capability.principal.codebase.p1.id", "http://%s:%s");
user_pref("capability.principal.codebase.p1.subjectName", "");""" % \ user_pref("capability.principal.codebase.p1.subjectName", "");
(self.host, self.port)
// For debugging, do not waste space on screen
user_pref("browser.tabs.autoHide", true);
""" % (self.host, self.port)
class PhantomJS(Browser): class PhantomJS(Browser):
def _createRunJS(self): def _createRunJS(self):
...@@ -249,7 +222,7 @@ page.open(address, function (status) { ...@@ -249,7 +222,7 @@ page.open(address, function (status) {
return self._createFile('run.js', run_js) return self._createFile('run.js', run_js)
def _run(self, url): def _run(self, url):
self._runCommand(("phantomjs", "phantomjs", self._createRunJS(), url)) self._runCommand("phantomjs", self._createRunJS(), url)
class FunctionalTestRunner: class FunctionalTestRunner:
...@@ -273,7 +246,7 @@ class FunctionalTestRunner: ...@@ -273,7 +246,7 @@ class FunctionalTestRunner:
self.browser = Firefox(profile_dir, host, int(port)) self.browser = Firefox(profile_dir, host, int(port))
def getStatus(self): def getStatus(self):
transaction.commit() transaction.begin()
return self.portal.portal_tests.TestTool_getResults(self.run_only) return self.portal.portal_tests.TestTool_getResults(self.run_only)
def _getTestURL(self): def _getTestURL(self):
...@@ -291,13 +264,15 @@ class FunctionalTestRunner: ...@@ -291,13 +264,15 @@ class FunctionalTestRunner:
xvfb = Xvfb(self.instance_home) xvfb = Xvfb(self.instance_home)
try: try:
start = time.time() start = time.time()
if not debug and self.browser.use_xvfb: if not debug:
print("\nSet 'erp5_debug_mode' environment variable to 1"
" to use your existing display instead of Xvfb.")
xvfb.run() xvfb.run()
self.browser.run(self._getTestURL() , xvfb.display) self.browser.run(self._getTestURL() , xvfb.display)
while self.getStatus() is None: while self.getStatus() is None:
time.sleep(10) time.sleep(10)
if (time.time() - start) > float(self.timeout): if (time.time() - start) > float(self.timeout):
raise TimeoutError("Test took more them %s seconds" % self.timeout) raise TimeoutError("Test took more than %s seconds" % self.timeout)
finally: finally:
self.browser.quit() self.browser.quit()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment