Commit 6f19208c authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[1454] Make sure the _listen_socket is set

parent fb20b8bb
......@@ -31,13 +31,16 @@ from isc.log_messages.ddns_messages import *
from optparse import OptionParser, OptionValueError
import os
import os.path
import signal
import socket
isc.log.init("b10-ddns")
logger = isc.log.Logger("ddns")
TRACE_BASIC = logger.DBGLVL_TRACE_BASIC
DATA_PATH = bind10_config.DATA_PATH
SOCKET_FILE = DATA_PATH + '/ddns_socket'
if "B10_FROM_SOURCE" in os.environ:
DATA_PATH = os.environ['B10_FROM_SOURCE'] + "/src/bin/ddns"
SPECFILE_LOCATION = DATA_PATH + "/ddns.spec"
......@@ -69,6 +72,13 @@ class DDNSSession:
'''Initialize a DDNS Session'''
pass
def clear_socket():
'''
Removes the socket file, if it exists.
'''
if os.path.exists(SOCKET_FILE):
os.remove(SOCKET_FILE)
class DDNSServer:
def __init__(self, cc_session=None):
'''
......@@ -91,6 +101,10 @@ class DDNSServer:
self._shutdown = False
# List of the session receivers where we get the requests
self._socksession_receivers = {}
clear_socket()
self._listen_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._listen_socket.bind(SOCKET_FILE)
self._listen_socket.listen(16)
def config_handler(self, new_config):
'''Update config data.'''
......@@ -270,6 +284,7 @@ def main(ddns_server=None):
logger.error(DDNS_CC_SESSION_TIMEOUT_ERROR)
except Exception as e:
logger.error(DDNS_UNCAUGHT_EXCEPTION, type(e).__name__, str(e))
clear_socket()
if '__main__' == __name__:
main()
......@@ -22,6 +22,8 @@ import isc.config
import select
import errno
import isc.util.io.socketsession
import socket
import os.path
class FakeSocket:
"""
......@@ -112,6 +114,28 @@ class TestDDNSServer(unittest.TestCase):
ddns.isc.util.io.socketsession.SocketSessionReceiver = \
isc.util.io.socketsession.SocketSessionReceiver
def test_listen(self):
'''
Test the old socket file is removed (if any) and a new socket
is created when the ddns server is created.
'''
# Make sure the socket does not exist now
ddns.clear_socket()
# Hook the call for clearing the socket
orig_clear = ddns.clear_socket
ddns.clear_socket = self.__hook
# Create the server
ddnss = ddns.DDNSServer(MyCCSession())
ddns.clear_socket = orig_clear
# The socket is created
self.assertTrue(os.path.exists(ddns.SOCKET_FILE))
self.assertTrue(isinstance(ddnss._listen_socket, socket.socket))
# And deletion of the socket was requested
self.assertIsNone(self.__hook_called)
# Now make sure the clear_socket really works
ddns.clear_socket()
self.assertFalse(os.path.exists(ddns.SOCKET_FILE))
def test_config_handler(self):
# Config handler does not do anything yet, but should at least
# return 'ok' for now.
......@@ -299,11 +323,21 @@ class TestDDNSServer(unittest.TestCase):
class TestMain(unittest.TestCase):
def setUp(self):
self._server = MyDDNSServer()
self.__orig_clear = ddns.clear_socket
ddns.clear_socket = self.__clear_socket
self.__clear_called = False
def tearDown(self):
ddns.clear_socket = self.__orig_clear
def test_main(self):
self.assertFalse(self._server.run_called)
ddns.main(self._server)
self.assertTrue(self._server.run_called)
self.assertTrue(self.__clear_called)
def __clear_socket(self):
self.__clear_called = True
def check_exception(self, ex):
'''Common test sequence to see if the given exception is caused.
......
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