Commit 242486d5 authored by Gabriel Monnerat's avatar Gabriel Monnerat

- Add function to stop OpenOffice.org that is using the same port to new...

- Add function to stop OpenOffice.org that is using the same port to new OpenOffice.org and when this process isn't controlled by cloudooo
- Add test to check if this function will not stop the wrong OpenOffice.org process


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@41878 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3fd9d2cf
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
############################################################################## ##############################################################################
import pkg_resources import pkg_resources
import psutil
from os import environ from os import environ
from os.path import exists, join from os.path import exists, join
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
...@@ -36,7 +37,8 @@ from application import Application ...@@ -36,7 +37,8 @@ from application import Application
from xvfb import xvfb from xvfb import xvfb
from cloudooo.interfaces.lockable import ILockable from cloudooo.interfaces.lockable import ILockable
from cloudooo.utils import logger, waitStartDaemon, removeDirectory, \ from cloudooo.utils import logger, waitStartDaemon, removeDirectory, \
waitStopDaemon, convertStringToBool waitStopDaemon, convertStringToBool, \
socketStatus
class OpenOffice(Application): class OpenOffice(Application):
...@@ -103,6 +105,19 @@ class OpenOffice(Application): ...@@ -103,6 +105,19 @@ class OpenOffice(Application):
waitStartDaemon(self, self.timeout) waitStartDaemon(self, self.timeout)
return self._testOpenOffice(self.hostname, self.port) return self._testOpenOffice(self.hostname, self.port)
def _releaseOpenOfficePort(self):
for process in psutil.process_iter():
try:
if process.exe == join(self.office_binary_path, self._bin_soffice):
connection_list = process.get_connections()
if len(connection_list) > 0 and \
connection_list[0].local_address[1] == self.port:
process.terminate()
except psutil.error.AccessDenied, e:
logger.error(e)
except NotImplementedError, e:
logger.error("lsof isn't installed on this machine: " + str(e))
def start(self): def start(self):
"""Start Instance.""" """Start Instance."""
if not xvfb.status(): if not xvfb.status():
...@@ -128,6 +143,8 @@ class OpenOffice(Application): ...@@ -128,6 +143,8 @@ class OpenOffice(Application):
env["TMP"] = self.path_user_installation env["TMP"] = self.path_user_installation
env["TMPDIR"] = self.path_user_installation env["TMPDIR"] = self.path_user_installation
env["DISPLAY"] = ":%s" % self.display_id env["DISPLAY"] = ":%s" % self.display_id
if socketStatus(self.hostname, self.port):
self._releaseOpenOfficePort()
process_started = self._start_process(self.command, env) process_started = self._start_process(self.command, env)
if not process_started: if not process_started:
self.stop() self.stop()
......
...@@ -31,6 +31,7 @@ from cloudoooTestCase import cloudoooTestCase ...@@ -31,6 +31,7 @@ from cloudoooTestCase import cloudoooTestCase
from cloudooo.application.openoffice import OpenOffice from cloudooo.application.openoffice import OpenOffice
from cloudoooTestCase import make_suite from cloudoooTestCase import make_suite
from cloudooo.utils import waitStopDaemon from cloudooo.utils import waitStopDaemon
from psutil import Process, AccessDenied
class TestOpenOffice(cloudoooTestCase): class TestOpenOffice(cloudoooTestCase):
...@@ -86,6 +87,79 @@ class TestOpenOffice(cloudoooTestCase): ...@@ -86,6 +87,79 @@ class TestOpenOffice(cloudoooTestCase):
self.openoffice.release() self.openoffice.release()
self.assertEquals(self.openoffice.isLocked(), False) self.assertEquals(self.openoffice.isLocked(), False)
def testStartTwoOpenOfficeWithTheSameAddress(self):
"""Check if starting two openoffice using the same address, the second
openoffice will terminate the first"""
second_openoffice = OpenOffice()
second_openoffice.loadSettings("localhost", 4090,
self.working_path,
self.virtual_display_id,
self.office_binary_path,
self.uno_path)
try:
second_openoffice.start()
try:
openoffice_process = Process(self.openoffice.pid())
openoffice_process.get_connections()
self.fail("Access get_connections() function should fails")
except AccessDenied:
self.assertTrue("Excepted failure")
finally:
second_openoffice.stop()
self.openoffice.start()
second_openoffice = OpenOffice()
second_openoffice.loadSettings("localhost", 4091,
self.working_path + "_",
self.virtual_display_id,
self.office_binary_path,
self.uno_path)
try:
second_openoffice.start()
try:
openoffice_process = Process(self.openoffice.pid())
connection = openoffice_process.get_connections()[0]
self.assertEquals(connection.local_address[1], 4090)
openoffice_process = Process(second_openoffice.pid())
connection = openoffice_process.get_connections()[0]
self.assertEquals(connection.local_address[1], 4091)
except AccessDenied:
self.fail("Access get_connections() function should be allowed")
finally:
second_openoffice.stop()
if not self.openoffice.status():
self.openoffice.start()
second_openoffice = OpenOffice()
second_openoffice.loadSettings("localhost", 40900,
self.working_path + "_",
self.virtual_display_id,
self.office_binary_path,
self.uno_path)
second_openoffice.start()
third_openoffice = OpenOffice()
third_openoffice.loadSettings("localhost", 40900,
self.working_path + "_",
self.virtual_display_id,
self.office_binary_path,
self.uno_path)
try:
third_openoffice.start()
try:
openoffice_process = Process(self.openoffice.pid())
connection = openoffice_process.get_connections()[0]
self.assertEquals(connection.local_address[1], 4090)
openoffice_process = Process(second_openoffice.pid())
openoffice_process.get_connections()
self.fail("Access get_connections() function should fails")
except AccessDenied:
self.assertTrue("Excepted failure")
finally:
second_openoffice.stop()
third_openoffice.stop()
def test_suite(): def test_suite():
return make_suite(TestOpenOffice) return make_suite(TestOpenOffice)
......
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