|
@@ -20,14 +20,11 @@ Utilities and mock modules for unittests of statistics modules
|
|
|
import os
|
|
|
import io
|
|
|
import time
|
|
|
-import sys
|
|
|
import threading
|
|
|
-import tempfile
|
|
|
import json
|
|
|
import signal
|
|
|
import socket
|
|
|
|
|
|
-import msgq
|
|
|
import isc.config.cfgmgr
|
|
|
import stats
|
|
|
import stats_httpd
|
|
@@ -52,19 +49,6 @@ class SignalHandler():
|
|
|
"""invokes unittest.TestCase.fail as a signal handler"""
|
|
|
self.fail_handler("A deadlock might be detected")
|
|
|
|
|
|
-def send_command(command_name, module_name, params=None):
|
|
|
- cc_session = isc.cc.Session()
|
|
|
- command = isc.config.ccsession.create_command(command_name, params)
|
|
|
- seq = cc_session.group_sendmsg(command, module_name)
|
|
|
- try:
|
|
|
- (answer, env) = cc_session.group_recvmsg(False, seq)
|
|
|
- if answer:
|
|
|
- return isc.config.ccsession.parse_answer(answer)
|
|
|
- except isc.cc.SessionTimeout:
|
|
|
- pass
|
|
|
- finally:
|
|
|
- cc_session.close()
|
|
|
-
|
|
|
class ThreadingServerManager:
|
|
|
def __init__(self, server, *args, **kwargs):
|
|
|
self.server = server(*args, **kwargs)
|
|
@@ -92,45 +76,7 @@ class ThreadingServerManager:
|
|
|
else:
|
|
|
self.server._thread.join(0) # timeout is 0
|
|
|
|
|
|
-class MockMsgq:
|
|
|
- def __init__(self):
|
|
|
- self._started = threading.Event()
|
|
|
- self.msgq = msgq.MsgQ(verbose=False)
|
|
|
- result = self.msgq.setup()
|
|
|
- if result:
|
|
|
- sys.exit("Error on Msgq startup: %s" % result)
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self._started.set()
|
|
|
- try:
|
|
|
- self.msgq.run()
|
|
|
- finally:
|
|
|
- # Make sure all the sockets, etc, are removed once it stops.
|
|
|
- self.msgq.shutdown()
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- # Ask it to terminate nicely
|
|
|
- self.msgq.stop()
|
|
|
-
|
|
|
-class MockCfgmgr:
|
|
|
- def __init__(self):
|
|
|
- self._started = threading.Event()
|
|
|
- self.cfgmgr = isc.config.cfgmgr.ConfigManager(
|
|
|
- os.environ['CONFIG_TESTDATA_PATH'], "b10-config.db")
|
|
|
- self.cfgmgr.read_config()
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self._started.set()
|
|
|
- try:
|
|
|
- self.cfgmgr.run()
|
|
|
- except Exception:
|
|
|
- pass
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- self.cfgmgr.running = False
|
|
|
-
|
|
|
-class MockInit:
|
|
|
- spec_str = """\
|
|
|
+INIT_SPEC_STR = """\
|
|
|
{
|
|
|
"module_spec": {
|
|
|
"module_name": "Init",
|
|
@@ -222,56 +168,8 @@ class MockInit:
|
|
|
}
|
|
|
}
|
|
|
"""
|
|
|
- _BASETIME = CONST_BASETIME
|
|
|
|
|
|
- def __init__(self):
|
|
|
- self._started = threading.Event()
|
|
|
- self.running = False
|
|
|
- self.spec_file = io.StringIO(self.spec_str)
|
|
|
- # create ModuleCCSession object
|
|
|
- self.mccs = isc.config.ModuleCCSession(
|
|
|
- self.spec_file,
|
|
|
- self.config_handler,
|
|
|
- self.command_handler)
|
|
|
- self.spec_file.close()
|
|
|
- self.cc_session = self.mccs._session
|
|
|
- self.got_command_name = ''
|
|
|
- self.pid_list = [[ 9999, "b10-auth", "Auth" ],
|
|
|
- [ 9998, "b10-auth-2", "Auth" ]]
|
|
|
- self.statistics_data = {
|
|
|
- 'boot_time': time.strftime('%Y-%m-%dT%H:%M:%SZ', self._BASETIME)
|
|
|
- }
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self.mccs.start()
|
|
|
- self.running = True
|
|
|
- self._started.set()
|
|
|
- try:
|
|
|
- while self.running:
|
|
|
- self.mccs.check_command(False)
|
|
|
- except Exception:
|
|
|
- pass
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- self.running = False
|
|
|
-
|
|
|
- def config_handler(self, new_config):
|
|
|
- return isc.config.create_answer(0)
|
|
|
-
|
|
|
- def command_handler(self, command, *args, **kwargs):
|
|
|
- self._started.set()
|
|
|
- self.got_command_name = command
|
|
|
- sdata = self.statistics_data
|
|
|
- if command == 'getstats':
|
|
|
- return isc.config.create_answer(0, sdata)
|
|
|
- elif command == 'show_processes':
|
|
|
- # Return dummy pids
|
|
|
- return isc.config.create_answer(
|
|
|
- 0, self.pid_list)
|
|
|
- return isc.config.create_answer(1, "Unknown Command")
|
|
|
-
|
|
|
-class MockAuth:
|
|
|
- spec_str = """\
|
|
|
+AUTH_SPEC_STR = """\
|
|
|
{
|
|
|
"module_spec": {
|
|
|
"module_name": "Auth",
|
|
@@ -393,68 +291,6 @@ class MockAuth:
|
|
|
}
|
|
|
}
|
|
|
"""
|
|
|
- def __init__(self):
|
|
|
- self._started = threading.Event()
|
|
|
- self.running = False
|
|
|
- self.spec_file = io.StringIO(self.spec_str)
|
|
|
- # create ModuleCCSession object
|
|
|
- self.mccs = isc.config.ModuleCCSession(
|
|
|
- self.spec_file,
|
|
|
- self.config_handler,
|
|
|
- self.command_handler)
|
|
|
- self.spec_file.close()
|
|
|
- self.cc_session = self.mccs._session
|
|
|
- self.got_command_name = ''
|
|
|
- self.queries_tcp = 3
|
|
|
- self.queries_udp = 2
|
|
|
- self.queries_per_zone = [{
|
|
|
- 'zonename': 'test1.example',
|
|
|
- 'queries.tcp': 5,
|
|
|
- 'queries.udp': 4
|
|
|
- }]
|
|
|
- self.nds_queries_per_zone = {
|
|
|
- 'test10.example': {
|
|
|
- 'queries.tcp': 5,
|
|
|
- 'queries.udp': 4
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self.mccs.start()
|
|
|
- self.running = True
|
|
|
- self._started.set()
|
|
|
- try:
|
|
|
- while self.running:
|
|
|
- self.mccs.check_command(False)
|
|
|
- except Exception:
|
|
|
- pass
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- self.running = False
|
|
|
-
|
|
|
- def config_handler(self, new_config):
|
|
|
- return isc.config.create_answer(0)
|
|
|
-
|
|
|
- def command_handler(self, command, *args, **kwargs):
|
|
|
- self.got_command_name = command
|
|
|
- sdata = { 'queries.tcp': self.queries_tcp,
|
|
|
- 'queries.udp': self.queries_udp,
|
|
|
- 'queries.perzone' : self.queries_per_zone,
|
|
|
- 'nds_queries.perzone' : {
|
|
|
- 'test10.example': {
|
|
|
- 'queries.tcp': \
|
|
|
- isc.cc.data.find(
|
|
|
- self.nds_queries_per_zone,
|
|
|
- 'test10.example/queries.tcp')
|
|
|
- }
|
|
|
- },
|
|
|
- 'nds_queries.perzone/test10.example/queries.udp' :
|
|
|
- isc.cc.data.find(self.nds_queries_per_zone,
|
|
|
- 'test10.example/queries.udp')
|
|
|
- }
|
|
|
- if command == 'getstats':
|
|
|
- return isc.config.create_answer(0, sdata)
|
|
|
- return isc.config.create_answer(1, "Unknown Command")
|
|
|
|
|
|
class MyModuleCCSession(isc.config.ConfigData):
|
|
|
"""Mocked ModuleCCSession class.
|
|
@@ -505,9 +341,9 @@ class SimpleStats(stats.Stats):
|
|
|
# the default answer from faked recvmsg if _answers is empty
|
|
|
self.__default_answer = isc.config.ccsession.create_answer(
|
|
|
0, {'Init':
|
|
|
- json.loads(MockInit.spec_str)['module_spec']['statistics'],
|
|
|
+ json.loads(INIT_SPEC_STR)['module_spec']['statistics'],
|
|
|
'Auth':
|
|
|
- json.loads(MockAuth.spec_str)['module_spec']['statistics']
|
|
|
+ json.loads(AUTH_SPEC_STR)['module_spec']['statistics']
|
|
|
})
|
|
|
# setup faked auth statistics
|
|
|
self.__init_auth_stat()
|
|
@@ -616,8 +452,8 @@ class SimpleStatsHttpd(stats_httpd.StatsHttpd):
|
|
|
with open(stats.SPECFILE_LOCATION) as f:
|
|
|
stat_spec_str = f.read()
|
|
|
self.__default_spec_answer = {
|
|
|
- 'Init': json.loads(MockInit.spec_str)['module_spec']['statistics'],
|
|
|
- 'Auth': json.loads(MockAuth.spec_str)['module_spec']['statistics'],
|
|
|
+ 'Init': json.loads(INIT_SPEC_STR)['module_spec']['statistics'],
|
|
|
+ 'Auth': json.loads(AUTH_SPEC_STR)['module_spec']['statistics'],
|
|
|
'Stats': json.loads(stat_spec_str)['module_spec']['statistics']
|
|
|
}
|
|
|
self.__default_data_answer = {
|
|
@@ -720,102 +556,3 @@ class SimpleStatsHttpd(stats_httpd.StatsHttpd):
|
|
|
def run(self):
|
|
|
self._started.set()
|
|
|
self.start()
|
|
|
-
|
|
|
-class MyStats(stats.Stats):
|
|
|
-
|
|
|
- def __init__(self):
|
|
|
- self._started = threading.Event()
|
|
|
- stats.Stats.__init__(self)
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self._started.set()
|
|
|
- try:
|
|
|
- self.start()
|
|
|
- except Exception:
|
|
|
- pass
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- self.command_shutdown()
|
|
|
-
|
|
|
-class MyStatsHttpd(stats_httpd.StatsHttpd):
|
|
|
- ORIG_SPECFILE_LOCATION = stats_httpd.SPECFILE_LOCATION
|
|
|
- def __init__(self, *server_address):
|
|
|
- self._started = threading.Event()
|
|
|
- if server_address:
|
|
|
- stats_httpd.SPECFILE_LOCATION = self.create_specfile(*server_address)
|
|
|
- try:
|
|
|
- stats_httpd.StatsHttpd.__init__(self)
|
|
|
- finally:
|
|
|
- if hasattr(stats_httpd.SPECFILE_LOCATION, "close"):
|
|
|
- stats_httpd.SPECFILE_LOCATION.close()
|
|
|
- stats_httpd.SPECFILE_LOCATION = self.ORIG_SPECFILE_LOCATION
|
|
|
- else:
|
|
|
- stats_httpd.StatsHttpd.__init__(self)
|
|
|
-
|
|
|
- def create_specfile(self, *server_address):
|
|
|
- spec_io = open(self.ORIG_SPECFILE_LOCATION)
|
|
|
- try:
|
|
|
- spec = json.load(spec_io)
|
|
|
- spec_io.close()
|
|
|
- config = spec['module_spec']['config_data']
|
|
|
- for i in range(len(config)):
|
|
|
- if config[i]['item_name'] == 'listen_on':
|
|
|
- config[i]['item_default'] = \
|
|
|
- [ dict(address=a[0], port=a[1]) for a in server_address ]
|
|
|
- break
|
|
|
- return io.StringIO(json.dumps(spec))
|
|
|
- finally:
|
|
|
- spec_io.close()
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self._started.set()
|
|
|
- try:
|
|
|
- self.start()
|
|
|
- except Exception:
|
|
|
- pass
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- self.command_handler('shutdown', None)
|
|
|
-
|
|
|
-class BaseModules:
|
|
|
- def __init__(self):
|
|
|
- # MockMsgq
|
|
|
- self.msgq = ThreadingServerManager(MockMsgq)
|
|
|
- self.msgq.run()
|
|
|
- # Check whether msgq is ready. A SessionTimeout is raised here if not.
|
|
|
- isc.cc.session.Session().close()
|
|
|
- # MockCfgmgr
|
|
|
- self.cfgmgr = ThreadingServerManager(MockCfgmgr)
|
|
|
- self.cfgmgr.run()
|
|
|
- # MockInit
|
|
|
- self.b10_init = ThreadingServerManager(MockInit)
|
|
|
- self.b10_init.run()
|
|
|
- # MockAuth
|
|
|
- self.auth = ThreadingServerManager(MockAuth)
|
|
|
- self.auth.run()
|
|
|
- self.auth2 = ThreadingServerManager(MockAuth)
|
|
|
- self.auth2.run()
|
|
|
-
|
|
|
-
|
|
|
- def shutdown(self):
|
|
|
- # MockMsgq. We need to wait (blocking) for it, otherwise it'll wipe out
|
|
|
- # a socket for another test during its shutdown.
|
|
|
- self.msgq.shutdown(True)
|
|
|
-
|
|
|
- # We also wait for the others, but these are just so we don't create
|
|
|
- # too many threads in parallel.
|
|
|
-
|
|
|
- # MockAuth
|
|
|
- self.auth2.shutdown(True)
|
|
|
- self.auth.shutdown(True)
|
|
|
- # MockInit
|
|
|
- self.b10_init.shutdown(True)
|
|
|
- # MockCfgmgr
|
|
|
- self.cfgmgr.shutdown(True)
|
|
|
- # remove the unused socket file
|
|
|
- socket_file = self.msgq.server.msgq.socket_file
|
|
|
- try:
|
|
|
- if os.path.exists(socket_file):
|
|
|
- os.remove(socket_file)
|
|
|
- except OSError:
|
|
|
- pass
|