Commit fb20b8bb authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[1454] Various minor tweaks based on review

Like renaming variables, rewording comments, improving log messages.
parent 0458ade8
......@@ -35,7 +35,7 @@ import signal
isc.log.init("b10-ddns")
logger = isc.log.Logger("ddns")
DBG_ACTIVITY = logger.DBGLVL_TRACE_BASIC
TRACE_BASIC = logger.DBGLVL_TRACE_BASIC
DATA_PATH = bind10_config.DATA_PATH
if "B10_FROM_SOURCE" in os.environ:
......@@ -89,8 +89,8 @@ class DDNSServer:
self._config_data = self._cc.get_full_config()
self._cc.start()
self._shutdown = False
# List of the sessions where we get the packets
self._socket_sessions = {}
# List of the session receivers where we get the requests
self._socksession_receivers = {}
def config_handler(self, new_config):
'''Update config data.'''
......@@ -137,38 +137,38 @@ class DDNSServer:
"""
socket = self._listen_socket.accept()
fileno = socket.fileno()
logger.debug(DBG_ACTIVITY, DDNS_NEW_CONN, fileno)
session = isc.util.io.socketsession.SocketSessionReceiver(socket)
self._socket_sessions[fileno] = (socket, session)
logger.debug(TRACE_BASIC, DDNS_NEW_CONN, fileno, socket.getpeername())
receiver = isc.util.io.socketsession.SocketSessionReceiver(socket)
self._socksession_receivers[fileno] = (socket, receiver)
def handle_request(self, request):
"""
This is called whenever a new DDNS request comes. Here the magic
happens, the rest is either subroutines of the magic or the accounting
around it that calls it from time to time.
This is the place where the actual DDNS processing is done. Other
methods are either subroutines of this method or methods doing the
uninteresting "accounting" stuff, like accepting socket,
initialization, etc.
It is called with the request being session as received from
SocketSessionReceiver, eg. tuple
SocketSessionReceiver, i.e. tuple
(socket, local_address, remote_address, data).
"""
# TODO: Implement the magic
pass
def handle_incoming(self, fileno):
def handle_session(self, fileno):
"""
Handle incoming event on the socket with given fileno.
"""
logger.debug(DBG_ACTIVITY, DDNS_INCOMING, fileno)
(socket, session) = self._socket_sessions[fileno]
logger.debug(TRACE_BASIC, DDNS_SESSION, fileno)
(socket, receiver) = self._socksession_receivers[fileno]
try:
request = session.pop()
self.handle_request(request)
self.handle_request(receiver.pop())
except isc.util.io.socketsession.SocketSessionError as se:
# No matter why this failed, the connection is in unknown, possibly
# broken state. So, we close the socket and remove the session.
# broken state. So, we close the socket and remove the receiver.
logger.warn(DDNS_DROP_CONN, fileno, se)
del self._socket_sessions[fileno]
socket.close()
del self._socksession_receivers[fileno]
def run(self):
'''
......@@ -203,7 +203,7 @@ class DDNSServer:
elif fileno == listen_fileno:
self.accept()
else:
self.handle_incoming(fileno)
self.handle_session(fileno)
self.shutdown_cleanup()
logger.info(DDNS_STOPPED)
......
......@@ -39,11 +39,6 @@ authoritative server shuts down, the connection would get closed. It also
can mean the system is busy and can't keep up or that the other side got
confused and sent bad data.
% DDNS_INCOMING incoming activity on file descriptor %1
A debug message, informing there's some activity on the given file descriptor.
It will be either a request or the file descriptor will be closed. See
following log messages to see what of it.
% DDNS_MODULECC_SESSION_ERROR error encountered by configuration/command module: %1
There was a problem in the lower level module handling configuration and
control commands. This could happen for various reasons, but the most likely
......@@ -51,9 +46,11 @@ cause is that the configuration database contains a syntax error and ddns
failed to start at initialization. A detailed error message from the module
will also be displayed.
% DDNS_NEW_CONN new connection on file descriptor %1
% DDNS_NEW_CONN new connection on file descriptor %1 from %2
Debug message. We received a connection and we are going to start handling
requests from it.
requests from it. The file descriptor number and the address where the request
comes from is logged. The connection is over a unix domain socket and is likely
coming from a b10-auth process.
% DDNS_RECEIVED_SHUTDOWN_COMMAND shutdown command received
The ddns process received a shutdown command from the command channel
......@@ -63,6 +60,11 @@ and will now shut down.
The ddns process has successfully started and is now ready to receive commands
and updates.
% DDNS_SESSION session arrived on file descriptor %1
A debug message, informing there's some activity on the given file descriptor.
It will be either a request or the file descriptor will be closed. See
following log messages to see what of it.
% DDNS_SHUTDOWN ddns server shutting down
The ddns process is shutting down. It will no longer listen for new commands
or updates. Any command or update that is being addressed at this moment will
......
......@@ -25,16 +25,18 @@ import isc.util.io.socketsession
class FakeSocket:
"""
A fake socket. It only provides a file number.
A fake socket. It only provides a file number, peer name and accept method.
"""
def __init__(self, fileno):
self.__fileno = fileno
def fileno(self):
return self.__fileno
def getpeername(self):
return "fake_unix_socket"
def accept(self):
return FakeSocket(self.__fileno + 1)
class FakeSession:
class FakeSessionReceiver:
"""
A fake socket session receiver, for our tests.
"""
......@@ -96,7 +98,7 @@ class TestDDNSServer(unittest.TestCase):
cc_session = MyCCSession()
self.assertFalse(cc_session._started)
self.ddns_server = ddns.DDNSServer(cc_session)
self.cc_session = cc_session
self.__cc_session = cc_session
self.assertTrue(cc_session._started)
self.__select_expected = None
self.__select_answer = None
......@@ -184,7 +186,7 @@ class TestDDNSServer(unittest.TestCase):
Test the check_command is called when there's something on the
socket.
"""
self.cc_session.check_command = self.__hook
self.__cc_session.check_command = self.__hook
self.__select_expected = ([1, 2], [], [], None)
self.__select_answer = ([1], [], [])
self.ddns_server.run()
......@@ -200,25 +202,30 @@ class TestDDNSServer(unittest.TestCase):
Test that we can accept a new connection.
"""
# There's nothing before the accept
ddns.isc.util.io.socketsession.SocketSessionReceiver = FakeSession
self.assertEqual({}, self.ddns_server._socket_sessions)
ddns.isc.util.io.socketsession.SocketSessionReceiver = \
FakeSessionReceiver
self.assertEqual({}, self.ddns_server._socksession_receivers)
self.ddns_server.accept()
# Now the new socket session receiver is stored in the dict
self.assertEqual([3], list(self.ddns_server._socket_sessions.keys()))
(socket, session) = self.ddns_server._socket_sessions[3]
# The 3 comes from _listen_socket.accept() - _listen_socket has
# fileno 2 and accept returns socket with fileno increased by one.
self.assertEqual([3],
list(self.ddns_server._socksession_receivers.keys()))
(socket, receiver) = self.ddns_server._socksession_receivers[3]
self.assertTrue(isinstance(socket, FakeSocket))
self.assertEqual(3, socket.fileno())
self.assertTrue(isinstance(session, FakeSession))
self.assertEqual(socket, session.socket())
self.assertTrue(isinstance(receiver, FakeSessionReceiver))
self.assertEqual(socket, receiver.socket())
def test_incoming_called(self):
"""
Test the run calls handle_incoming when there's something on the
Test the run calls handle_session when there's something on the
socket.
"""
socket = FakeSocket(3)
self.ddns_server._socket_sessions = {3: (socket, FakeSession(socket))}
self.ddns_server.handle_incoming = self.__hook
self.ddns_server._socksession_receivers = \
{3: (socket, FakeSessionReceiver(socket))}
self.ddns_server.handle_session = self.__hook
self.__select_expected = ([1, 2, 3], [], [], None)
self.__select_answer = ([3], [], [])
self.ddns_server.run()
......@@ -226,47 +233,47 @@ class TestDDNSServer(unittest.TestCase):
self.assertIsNone(self.__select_answer)
self.assertEqual(3, self.__hook_called)
def test_handle_incoming_ok(self):
def test_handle_session_ok(self):
"""
Test the handle_incoming pops the session and calls handle_request
Test the handle_session pops the receiver and calls handle_request
when everything is OK.
"""
socket = FakeSocket(3)
session = FakeSession(socket)
receiver = FakeSessionReceiver(socket)
# It doesn't really matter what data we use here, it is only passed
# through the code
param = (FakeSocket(4), ('127.0.0.1', 1234), ('127.0.0.1', 1235),
'Some data')
def pop():
return param
# Prepare data into the session
session.pop = pop
self.ddns_server._socket_sessions = {3: (socket, session)}
# Prepare data into the receiver
receiver.pop = pop
self.ddns_server._socksession_receivers = {3: (socket, receiver)}
self.ddns_server.handle_request = self.__hook
# Call it
self.ddns_server.handle_incoming(3)
self.ddns_server.handle_session(3)
# The popped data are passed into the handle_request
self.assertEqual(param, self.__hook_called)
# The sessions are kept the same
self.assertEqual({3: (socket, session)},
self.ddns_server._socket_sessions)
# The receivers are kept the same
self.assertEqual({3: (socket, receiver)},
self.ddns_server._socksession_receivers)
def test_handle_incoming_fail(self):
def test_handle_session_fail(self):
"""
Test the handle_incoming removes (and closes) the socket and session
when the session complains.
Test the handle_session removes (and closes) the socket and receiver
when the receiver complains.
"""
socket = FakeSocket(3)
session = FakeSession(socket)
receiver = FakeSessionReceiver(socket)
def pop():
raise isc.util.io.socketsession.SocketSessionError('Test error')
session.pop = pop
receiver.pop = pop
socket.close = self.__hook
self.__hook_called = False
self.ddns_server._socket_sessions = {3: (socket, session)}
self.ddns_server.handle_incoming(3)
# The "dead" session is removed
self.assertEqual({}, self.ddns_server._socket_sessions)
self.ddns_server._socksession_receivers = {3: (socket, receiver)}
self.ddns_server.handle_session(3)
# The "dead" receiver is removed
self.assertEqual({}, self.ddns_server._socksession_receivers)
# Close is called with no parameter, so the default None
self.assertIsNone(self.__hook_called)
......
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