Commit 46a05a84 authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor and removed the duplicated code in handler test cases.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43560 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 300422d5
#!/usr/bin/env python #!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from cloudooo.handler.tests import runHandlerUnitTest
import sys
import unittest
from argparse import ArgumentParser
from os import path, curdir, environ, chdir
ENVIRONMENT_PATH = path.abspath(path.dirname(__file__))
def exit(msg):
sys.stderr.write(msg)
sys.exit(0)
# XXX - Duplicated function. This function must be generic to be used by all handlers
def run(): def run():
parser = ArgumentParser(description="Unit Test Runner for Cloudooo") runHandlerUnitTest.run("ffmpeg")
parser.add_argument('server_cloudooo_conf')
parser.add_argument('test_name')
parser.add_argument('--paster_path', dest='paster_path',
default='paster',
help="Path to Paster script")
namespace = parser.parse_args()
server_cloudooo_conf = namespace.server_cloudooo_conf
test_name = namespace.test_name
if server_cloudooo_conf.startswith(curdir):
server_cloudooo_conf = path.join(path.abspath(curdir),
server_cloudooo_conf)
environ['server_cloudooo_conf'] = server_cloudooo_conf
python_extension = '.py'
if test_name[-3:] == python_extension:
test_name = test_name[:-3]
if not path.exists(path.join(ENVIRONMENT_PATH,
'%s%s' % (test_name, python_extension))):
exit("%s not exists\n" % test_name)
sys.path.append(ENVIRONMENT_PATH)
module = __import__(test_name)
if not hasattr(module, "test_suite"):
exit("No test suite to run, exiting immediately")
TestRunner = unittest.TextTestRunner
suite = unittest.TestSuite()
suite.addTest(module.test_suite())
chdir(ENVIRONMENT_PATH)
TestRunner(verbosity=2).run(suite)
if __name__ == "__main__":
run()
...@@ -195,8 +195,6 @@ class OOHandler: ...@@ -195,8 +195,6 @@ class OOHandler:
if metadata.get("Data"): if metadata.get("Data"):
self.document.reload(metadata['Data']) self.document.reload(metadata['Data'])
metadata['Data'] = self.document.getContent() metadata['Data'] = self.document.getContent()
else:
metadata['Data'] = ''
self.document.trash() self.document.trash()
return metadata return metadata
......
##############################################################################
#
# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import unittest
import sys
from os import path
from os import environ, putenv
from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.mimemapper import mimemapper
from cloudooo.handler.tests.handlerTestCase import config, check_folder
testcase_path = path.dirname(__file__)
def make_suite(test_case):
"""Function is used to run all tests together"""
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(test_case))
return suite
def startFakeEnvironment(start_openoffice=True, conf_path=None):
"""Create a fake environment"""
config.read(conf_path)
uno_path = config.get("app:main", "uno_path")
working_path = config.get("app:main", "working_path")
hostname = config.get("server:main", "host")
openoffice_port = int(config.get("app:main", "openoffice_port"))
office_binary_path = config.get("app:main", "office_binary_path")
tmp_dir = path.join(working_path, 'tmp')
check_folder(working_path, tmp_dir)
if not environ.get('uno_path'):
environ['uno_path'] = uno_path
office_binary_path = config.get("app:main", "office_binary_path")
if not environ.get('office_binary_path'):
environ['office_binary_path'] = office_binary_path
if uno_path not in sys.path:
sys.path.append(uno_path)
fundamentalrc_file = '%s/fundamentalrc' % office_binary_path
if path.exists(fundamentalrc_file) and \
'URE_BOOTSTRAP' not in environ:
putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
if start_openoffice:
default_language = config.get('app:main',
'openoffice_user_interface_language', False,
{'openoffice_user_interface_language': 'en'})
openoffice.loadSettings(hostname,
openoffice_port,
working_path,
office_binary_path,
uno_path,
default_language)
openoffice.start()
openoffice.acquire()
hostname, port = openoffice.getAddress()
kw = dict(uno_path=config.get("app:main", "uno_path"),
office_binary_path=config.get("app:main", "office_binary_path"))
if not mimemapper.isLoaded():
mimemapper.loadFilterList(hostname, port, **kw)
openoffice.release()
return openoffice
def stopFakeEnvironment(stop_openoffice=True):
"""Stop Openoffice """
if stop_openoffice:
openoffice.stop()
return True
#!/usr/bin/env python #!/usr/bin/env python
##############################################################################
import sys #
import unittest # Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
from argparse import ArgumentParser # Gabriel M. Monnerat <gabriel@tiolive.com>
from time import sleep #
from subprocess import Popen # WARNING: This program as such is intended to be used by professional
from ConfigParser import ConfigParser # programmers who take the whole responsibility of assessing all potential
from os import chdir, path, environ, curdir, remove # consequences resulting from its eventual inadequacies and bugs
from psutil import Process # End users who are looking for a ready-to-use solution with commercial
from signal import SIGQUIT # guarantees and support are strongly adviced to contract a Free Software
# Service Company
ENVIRONMENT_PATH = path.abspath(path.dirname(__file__)) #
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
def wait_use_port(pid, timeout_limit=30): # as published by the Free Software Foundation; either version 2
process = Process(pid) # of the License, or (at your option) any later version.
for n in range(timeout_limit): #
if len(process.get_connections()) > 0: # This program is distributed in the hope that it will be useful,
return True # but WITHOUT ANY WARRANTY; without even the implied warranty of
sleep(1) # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
return False # GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
def exit(msg): # along with this program; if not, write to the Free Software
sys.stderr.write(msg) # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
sys.exit(0) #
##############################################################################
from cloudooo.handler.tests import runHandlerUnitTest
def run(): def run():
parser = ArgumentParser(description="Unit Test Runner for Cloudooo") runHandlerUnitTest.run("ooo")
parser.add_argument('server_cloudooo_conf')
parser.add_argument('test_name')
parser.add_argument('--timeout_limit', dest='timeout_limit',
type=long, default=30,
help="Timeout to waiting for the cloudooo stop")
parser.add_argument('--paster_path', dest='paster_path',
default='paster',
help="Path to Paster script")
namespace = parser.parse_args()
server_cloudooo_conf = namespace.server_cloudooo_conf
test_name = namespace.test_name
if server_cloudooo_conf.startswith(curdir):
server_cloudooo_conf = path.join(path.abspath(curdir),
server_cloudooo_conf)
environ['server_cloudooo_conf'] = server_cloudooo_conf
paster_path = namespace.paster_path
python_extension = '.py'
if test_name[-3:] == python_extension:
test_name = test_name[:-3]
if not path.exists(path.join(ENVIRONMENT_PATH,
'%s%s' % (test_name, python_extension))):
exit("%s not exists\n" % test_name)
from cloudoooTestCase import startFakeEnvironment, stopFakeEnvironment
sys.path.append(ENVIRONMENT_PATH)
config = ConfigParser()
config.read(server_cloudooo_conf)
module = __import__(test_name)
if not hasattr(module, "test_suite"):
exit("No test suite to run, exiting immediately")
DAEMON = getattr(module, 'DAEMON', False)
OPENOFFICE = getattr(module, 'OPENOFFICE', False)
TestRunner = unittest.TextTestRunner
suite = unittest.TestSuite()
suite.addTest(module.test_suite())
if DAEMON:
log_file = '%s/cloudooo_test.log' % config.get('app:main',
'working_path')
if path.exists(log_file):
remove(log_file)
command = [paster_path, 'serve', '--log-file', log_file,
server_cloudooo_conf]
process = Popen(command)
wait_use_port(process.pid)
chdir(ENVIRONMENT_PATH)
try:
TestRunner(verbosity=2).run(suite)
finally:
process.send_signal(SIGQUIT)
process.wait()
elif OPENOFFICE:
chdir(ENVIRONMENT_PATH)
startFakeEnvironment(conf_path=server_cloudooo_conf)
try:
TestRunner(verbosity=2).run(suite)
finally:
stopFakeEnvironment()
else:
chdir(ENVIRONMENT_PATH)
TestRunner(verbosity=2).run(suite)
if __name__ == "__main__":
run()
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
from xmlrpclib import ServerProxy, Fault from xmlrpclib import ServerProxy, Fault
from base64 import encodestring, decodestring from base64 import encodestring, decodestring
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
import magic import magic
file_detector = magic.Magic() file_detector = magic.Magic()
......
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
from xmlrpclib import ServerProxy, Fault from xmlrpclib import ServerProxy, Fault
from base64 import encodestring, decodestring from base64 import encodestring, decodestring
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
import magic import magic
file_detector = magic.Magic() file_detector = magic.Magic()
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
import unittest import unittest
from cloudooo.handler.ooo.application.application import Application from cloudooo.handler.ooo.application.application import Application
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
class TestApplication(unittest.TestCase): class TestApplication(unittest.TestCase):
......
...@@ -33,7 +33,7 @@ from base64 import decodestring ...@@ -33,7 +33,7 @@ from base64 import decodestring
from os import path from os import path
from zipfile import ZipFile from zipfile import ZipFile
from cloudooo.handler.ooo.document import FileSystemDocument from cloudooo.handler.ooo.document import FileSystemDocument
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
class TestFileSystemDocument(unittest.TestCase): class TestFileSystemDocument(unittest.TestCase):
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
import unittest import unittest
from cloudooo.handler.ooo.filter import Filter from cloudooo.handler.ooo.filter import Filter
from cloudoooTestCase import make_suite
class TestFilter(unittest.TestCase): class TestFilter(unittest.TestCase):
......
...@@ -29,8 +29,7 @@ ...@@ -29,8 +29,7 @@
from xmlrpclib import ServerProxy from xmlrpclib import ServerProxy
from base64 import encodestring, decodestring from base64 import encodestring, decodestring
from multiprocessing import Process, Array from multiprocessing import Process, Array
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
import magic import magic
DAEMON = True DAEMON = True
......
...@@ -46,8 +46,7 @@ from cloudooo.interfaces.monitor import IMonitor ...@@ -46,8 +46,7 @@ from cloudooo.interfaces.monitor import IMonitor
from cloudooo.interfaces.granulate import ITableGranulator, \ from cloudooo.interfaces.granulate import ITableGranulator, \
IImageGranulator, \ IImageGranulator, \
ITextGranulator ITextGranulator
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
class TestInterface(unittest.TestCase): class TestInterface(unittest.TestCase):
"""Test All Interfaces""" """Test All Interfaces"""
......
...@@ -26,10 +26,9 @@ ...@@ -26,10 +26,9 @@
# #
############################################################################## ##############################################################################
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.mimemapper import MimeMapper from cloudooo.handler.ooo.mimemapper import MimeMapper
from cloudoooTestCase import make_suite
text_expected_tuple = ( text_expected_tuple = (
('doc', 'Microsoft Word 6.0'), ('doc', 'Microsoft Word 6.0'),
......
...@@ -27,8 +27,7 @@ ...@@ -27,8 +27,7 @@
############################################################################## ##############################################################################
import cloudooo.handler.ooo.monitor as monitor import cloudooo.handler.ooo.monitor as monitor
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.monitor.request import MonitorRequest from cloudooo.handler.ooo.monitor.request import MonitorRequest
from cloudooo.handler.ooo.monitor.memory import MonitorMemory from cloudooo.handler.ooo.monitor.memory import MonitorMemory
......
...@@ -32,7 +32,7 @@ from cloudooo.handler.ooo.application.openoffice import openoffice ...@@ -32,7 +32,7 @@ from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.monitor.memory import MonitorMemory from cloudooo.handler.ooo.monitor.memory import MonitorMemory
from psutil import Process from psutil import Process
from types import IntType from types import IntType
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
OPENOFFICE = True OPENOFFICE = True
......
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
from time import sleep from time import sleep
from cloudooo.handler.ooo.monitor.request import MonitorRequest from cloudooo.handler.ooo.monitor.request import MonitorRequest
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
OPENOFFICE = True OPENOFFICE = True
......
...@@ -30,7 +30,7 @@ import unittest ...@@ -30,7 +30,7 @@ import unittest
from time import sleep from time import sleep
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.monitor.timeout import MonitorTimeout from cloudooo.handler.ooo.monitor.timeout import MonitorTimeout
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
OPENOFFICE = True OPENOFFICE = True
......
...@@ -30,8 +30,7 @@ ...@@ -30,8 +30,7 @@
from zipfile import ZipFile from zipfile import ZipFile
from StringIO import StringIO from StringIO import StringIO
from lxml import etree from lxml import etree
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.granulator import OOGranulator from cloudooo.handler.ooo.granulator import OOGranulator
......
...@@ -29,10 +29,9 @@ ...@@ -29,10 +29,9 @@
import magic import magic
from os import path from os import path
from base64 import encodestring, decodestring from base64 import encodestring, decodestring
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.ooo.handler import OOHandler from cloudooo.handler.ooo.handler import OOHandler
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudoooTestCase import make_suite
import os import os
from lxml import etree from lxml import etree
from zipfile import ZipFile from zipfile import ZipFile
......
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
from zipfile import ZipFile from zipfile import ZipFile
from lxml import etree from lxml import etree
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.document import OdfDocument from cloudooo.handler.ooo.document import OdfDocument
......
...@@ -26,9 +26,8 @@ ...@@ -26,9 +26,8 @@
# #
############################################################################## ##############################################################################
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.ooo.application.openoffice import OpenOffice from cloudooo.handler.ooo.application.openoffice import OpenOffice
from cloudoooTestCase import make_suite
from cloudooo.handler.ooo.utils.utils import waitStopDaemon from cloudooo.handler.ooo.utils.utils import waitStopDaemon
OPENOFFICE = True OPENOFFICE = True
......
...@@ -30,8 +30,7 @@ from os.path import join, exists ...@@ -30,8 +30,7 @@ from os.path import join, exists
from os import remove from os import remove
from xmlrpclib import ServerProxy, Fault from xmlrpclib import ServerProxy, Fault
from base64 import encodestring, decodestring from base64 import encodestring, decodestring
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from zipfile import ZipFile, is_zipfile from zipfile import ZipFile, is_zipfile
from types import DictType from types import DictType
import magic import magic
...@@ -308,8 +307,8 @@ class TestServer(HandlerTestCase): ...@@ -308,8 +307,8 @@ class TestServer(HandlerTestCase):
def testConvertPyToPDF(self): def testConvertPyToPDF(self):
"""Test export python to pdf""" """Test export python to pdf"""
self._testConvertFile("cloudoooTestCase.py", self._testConvertFile(__file__,
join(self.tmp_url, "cloudoooTestCase.py"), join(self.tmp_url, "output.py"),
'py', 'py',
'pdf', 'pdf',
'application/pdf') 'application/pdf')
......
...@@ -31,8 +31,7 @@ import magic ...@@ -31,8 +31,7 @@ import magic
import pkg_resources import pkg_resources
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from os.path import exists, join from os.path import exists, join
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.document import FileSystemDocument from cloudooo.handler.ooo.document import FileSystemDocument
......
...@@ -31,8 +31,7 @@ import pkg_resources ...@@ -31,8 +31,7 @@ import pkg_resources
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from os import environ, path from os import environ, path
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
OPENOFFICE = True OPENOFFICE = True
......
...@@ -30,7 +30,7 @@ import unittest ...@@ -30,7 +30,7 @@ import unittest
import logging import logging
from cloudooo.utils.utils import logger, configureLogger, \ from cloudooo.utils.utils import logger, configureLogger, \
convertStringToBool convertStringToBool
from cloudoooTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
class TestUtils(unittest.TestCase): class TestUtils(unittest.TestCase):
......
#!/usr/bin/env python #!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from cloudooo.handler.tests import runHandlerUnitTest
import sys
import unittest
from argparse import ArgumentParser
from os import path, curdir, environ, chdir
ENVIRONMENT_PATH = path.abspath(path.dirname(__file__))
def exit(msg):
sys.stderr.write(msg)
sys.exit(0)
# XXX - Duplicated function. This function must be generic to be used by all handlers
def run(): def run():
parser = ArgumentParser(description="Unit Test Runner for Cloudooo") runHandlerUnitTest.run("pdf")
parser.add_argument('server_cloudooo_conf')
parser.add_argument('test_name')
parser.add_argument('--paster_path', dest='paster_path',
default='paster',
help="Path to Paster script")
namespace = parser.parse_args()
server_cloudooo_conf = namespace.server_cloudooo_conf
test_name = namespace.test_name
if server_cloudooo_conf.startswith(curdir):
server_cloudooo_conf = path.join(path.abspath(curdir),
server_cloudooo_conf)
environ['server_cloudooo_conf'] = server_cloudooo_conf
python_extension = '.py'
if test_name[-3:] == python_extension:
test_name = test_name[:-3]
if not path.exists(path.join(ENVIRONMENT_PATH,
'%s%s' % (test_name, python_extension))):
exit("%s not exists\n" % test_name)
sys.path.append(ENVIRONMENT_PATH)
module = __import__(test_name)
if not hasattr(module, "test_suite"):
exit("No test suite to run, exiting immediately")
TestRunner = unittest.TextTestRunner
suite = unittest.TestSuite()
suite.addTest(module.test_suite())
chdir(ENVIRONMENT_PATH)
TestRunner(verbosity=2).run(suite)
if __name__ == "__main__":
run()
...@@ -28,13 +28,21 @@ ...@@ -28,13 +28,21 @@
import unittest import unittest
from os import environ, path, mkdir
from ConfigParser import ConfigParser
import sys import sys
from os import environ, path, mkdir, putenv
from ConfigParser import ConfigParser
from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.mimemapper import mimemapper
config = ConfigParser() config = ConfigParser()
def make_suite(test_case):
"""Function is used to run all tests together"""
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(test_case))
return suite
def check_folder(working_path, tmp_dir_path): def check_folder(working_path, tmp_dir_path):
if not path.exists(working_path): if not path.exists(working_path):
mkdir(working_path) mkdir(working_path)
...@@ -42,6 +50,58 @@ def check_folder(working_path, tmp_dir_path): ...@@ -42,6 +50,58 @@ def check_folder(working_path, tmp_dir_path):
mkdir(tmp_dir_path) mkdir(tmp_dir_path)
def startFakeEnvironment(start_openoffice=True, conf_path=None):
"""Create a fake environment"""
config.read(conf_path)
uno_path = config.get("app:main", "uno_path")
working_path = config.get("app:main", "working_path")
hostname = config.get("server:main", "host")
openoffice_port = int(config.get("app:main", "openoffice_port"))
office_binary_path = config.get("app:main", "office_binary_path")
tmp_dir = path.join(working_path, 'tmp')
check_folder(working_path, tmp_dir)
if not environ.get('uno_path'):
environ['uno_path'] = uno_path
office_binary_path = config.get("app:main", "office_binary_path")
if not environ.get('office_binary_path'):
environ['office_binary_path'] = office_binary_path
if uno_path not in sys.path:
sys.path.append(uno_path)
fundamentalrc_file = '%s/fundamentalrc' % office_binary_path
if path.exists(fundamentalrc_file) and \
'URE_BOOTSTRAP' not in environ:
putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
if start_openoffice:
default_language = config.get('app:main',
'openoffice_user_interface_language', False,
{'openoffice_user_interface_language': 'en'})
openoffice.loadSettings(hostname,
openoffice_port,
working_path,
office_binary_path,
uno_path,
default_language)
openoffice.start()
openoffice.acquire()
hostname, port = openoffice.getAddress()
kw = dict(uno_path=config.get("app:main", "uno_path"),
office_binary_path=config.get("app:main", "office_binary_path"))
if not mimemapper.isLoaded():
mimemapper.loadFilterList(hostname, port, **kw)
openoffice.release()
return openoffice
def stopFakeEnvironment(stop_openoffice=True):
"""Stop Openoffice """
if stop_openoffice:
openoffice.stop()
return True
class HandlerTestCase(unittest.TestCase): class HandlerTestCase(unittest.TestCase):
"""Test Case to load cloudooo conf.""" """Test Case to load cloudooo conf."""
......
#!/usr/bin/env python
import sys
from pkg_resources import resource_filename
import unittest
from time import sleep
from subprocess import Popen
from ConfigParser import ConfigParser
from argparse import ArgumentParser
from os import chdir, path, environ, curdir, remove
from psutil import Process
from signal import SIGQUIT
def wait_use_port(pid, timeout_limit=30):
process = Process(pid)
for n in range(timeout_limit):
if len(process.get_connections()) > 0:
return True
sleep(1)
return False
def exit(msg):
sys.stderr.write(msg)
sys.exit(0)
def run(handler_name):
description = "Unit Test Runner for %s Handler" % handler_name.capitalize()
parser = ArgumentParser(description=description)
parser.add_argument('server_cloudooo_conf')
parser.add_argument('test_name')
parser.add_argument('--timeout_limit', dest='timeout_limit',
type=long, default=30,
help="Timeout to waiting for the cloudooo stop")
parser.add_argument('--paster_path', dest='paster_path',
default='paster',
help="Path to Paster script")
namespace = parser.parse_args()
environment_path = resource_filename("cloudooo",
"handler/%s/tests" % handler_name)
server_cloudooo_conf = namespace.server_cloudooo_conf
test_name = namespace.test_name
if server_cloudooo_conf.startswith(curdir):
server_cloudooo_conf = path.join(path.abspath(curdir),
server_cloudooo_conf)
environ['server_cloudooo_conf'] = server_cloudooo_conf
paster_path = namespace.paster_path
python_extension = '.py'
if test_name[-3:] == python_extension:
test_name = test_name[:-3]
if not path.exists(path.join(environment_path,
'%s%s' % (test_name, python_extension))):
exit("%s not exists\n" % test_name)
from cloudooo.handler.tests.handlerTestCase import startFakeEnvironment, \
stopFakeEnvironment
sys.path.append(environment_path)
config = ConfigParser()
config.read(server_cloudooo_conf)
module = __import__(test_name)
if not hasattr(module, "test_suite"):
exit("No test suite to run, exiting immediately")
DAEMON = getattr(module, 'DAEMON', False)
OPENOFFICE = getattr(module, 'OPENOFFICE', False)
TestRunner = unittest.TextTestRunner
suite = unittest.TestSuite()
suite.addTest(module.test_suite())
if DAEMON:
log_file = '%s/cloudooo_test.log' % config.get('app:main',
'working_path')
if path.exists(log_file):
remove(log_file)
command = [paster_path, 'serve', '--log-file', log_file,
server_cloudooo_conf]
process = Popen(command)
wait_use_port(process.pid)
chdir(environment_path)
try:
TestRunner(verbosity=2).run(suite)
finally:
process.send_signal(SIGQUIT)
process.wait()
elif OPENOFFICE:
chdir(environment_path)
startFakeEnvironment(conf_path=server_cloudooo_conf)
try:
TestRunner(verbosity=2).run(suite)
finally:
stopFakeEnvironment()
else:
chdir(environment_path)
TestRunner(verbosity=2).run(suite)
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