def setUp(self): k = '' for line in open(TESTDATADIR + '/dkim/testfuglu.org.public'): if line.startswith('---'): continue k = k + line.strip() record = "v=DKIM1; k=rsa; p=%s" % k fuglu.lib.patcheddkimlib.dnstxt = mock.Mock(return_value=record) self.config = RawConfigParser() self.config.read([TESTDATADIR + '/endtoendtest.conf']) self.config.set('main', 'incomingport', str(DKIMTestCase.FUGLU_PORT)) self.config.set('main', 'outgoinghost', str(DKIMTestCase.FUGLU_HOST)) self.config.set('main', 'outgoingport', str(DKIMTestCase.DUMMY_PORT)) self.config.set('main', 'controlport', str(DKIMTestCase.FUGLUCONTROL_PORT)) guess_clamav_socket(self.config) # init core self.mc = MainController(self.config) # start listening smtp dummy server to get fuglus answer self.smtp = DummySMTPServer(self.config, self.config.getint('main', 'outgoingport'), DKIMTestCase.FUGLU_HOST) dkdss = threading.Thread(target=self.smtp.serve, args=()) dkdss.daemon = True dkdss.start() # start fuglu's listening server fls = threading.Thread(target=self.mc.startup, args=()) fls.daemon = True fls.start()
def setUp(self): self.config = RawConfigParser() self.config.read([TESTDATADIR + '/endtoendtest.conf']) self.config.set('main', 'incomingport', str(EndtoEndTestTestCase.FUGLU_PORT)) self.config.set('main', 'outgoinghost', str(EndtoEndTestTestCase.FUGLU_HOST)) self.config.set('main', 'outgoingport', str(EndtoEndTestTestCase.DUMMY_PORT)) self.config.set('main', 'controlport', str(EndtoEndTestTestCase.FUGLUCONTROL_PORT)) guess_clamav_socket(self.config) # init core self.mc = MainController(self.config) # start listening smtp dummy server to get fuglus answer self.smtp = DummySMTPServer(self.config, EndtoEndTestTestCase.DUMMY_PORT, EndtoEndTestTestCase.FUGLU_HOST) e2edss = threading.Thread(target=self.smtp.serve, args=()) e2edss.daemon = True e2edss.start() # start fuglu's listening server fls = threading.Thread(target=self.mc.startup, args=()) fls.daemon = True fls.start()
def setUp(self): config = RawConfigParser() config.read([CONFDIR + '/fuglu.conf.dist']) config.set('main', 'disablebounces', '1') guess_clamav_socket(config) self.mc = MainController(config) self.tempfiles = []
def test_backend_reload(self): """Test reload with different backends""" config = RawConfigParser() config.add_section('performance') # minimum scanner threads config.set('performance', 'minthreads', 2) # maximum scanner threads config.set('performance', 'maxthreads', 40) # Method for parallelism, either 'thread' or 'process' config.set('performance', 'backend', 'process') # Initial number of processes when backend='process'. # If 0 (the default), automatically selects twice the number of available virtual cores. # Despite its 'initial'-name, this number currently is not adapted automatically. config.set('performance', 'initialprocs', 10) config.set('performance', 'join_timeout', 2.0) mc = MainController(config) mc.propagate_core_defaults() # usually the backend is loaded by "startup()" which is run # in a separate thread because it goes to the event loop. I'll just # directly start a the threadpool here... mc.threadpool = mc._start_threadpool() time.sleep(0.1) try: self.assertIsNone(mc.procpool) self.assertIsNotNone(mc.threadpool) except AttributeError: # Python 2.6 self.assertTrue(mc.procpool is None) self.assertTrue(mc.threadpool is not None) # now reload will replace the threadpool by a procpool mc.reload() time.sleep(0.1) try: self.assertIsNone(mc.threadpool) self.assertIsNotNone(mc.procpool) except AttributeError: # Python 2.6 self.assertTrue(mc.threadpool is None) self.assertTrue(mc.procpool is not None) config.set('performance', 'backend', 'thread') # now reload will replace the procpool by a threadpool mc.reload() time.sleep(0.1) try: self.assertIsNone(mc.procpool) self.assertIsNotNone(mc.threadpool) except AttributeError: # Python 2.6 self.assertTrue(mc.procpool is None) self.assertTrue(mc.threadpool is not None) mc.shutdown()
def test_alldefault_lint(self): """Test lint command for default config""" lc = logConfig(lint=True) lc.configure() root = logging.getLogger() root.setLevel(logging.ERROR) config = RawConfigParser() mc = MainController(config) errors = mc.lint() self.assertEqual( 2, errors, "With all default, 1 error for SAPlugin and 1 error for ClamavPlugin are expected" )
def test_multiple_mcs(self): """Just start multiple controllers """ config = RawConfigParser() mclist = [] for i in range(10): mc = MainController(config) mc.propagate_core_defaults() mclist.append(mc) for mc in mclist: # usually the backend is loaded by "startup()" which is run # in a separate thread because it goes to the event loop. I'll just # directly start a the threadpool here... mc.threadpool = mc._start_threadpool() time.sleep(0.1) for mc in mclist: mc.shutdown()
def test_lint_nodefault(self): """Plugin with missing required var without default""" lc = logConfig(lint=True) lc.configure() root = logging.getLogger() root.setLevel(logging.ERROR) config = RawConfigParser() # -------------# # config: main # # -------------# config.add_section("main") config.set('main', 'plugins', 'controller_tests.Dummy3') config.set('main', 'prependers', '') config.set('main', 'appenders', '') mc = MainController(config) errors = mc.lint() self.assertEqual(1, errors)
def test_lint_noproblems(self): """Test lint where config is all fine""" lc = logConfig(lint=True) lc.configure() root = logging.getLogger() root.setLevel(logging.ERROR) config = RawConfigParser() # -------------# # config: main # # -------------# config.add_section("main") config.set('main', 'plugins', 'controller_tests.Dummy2') config.set('main', 'prependers', '') config.set('main', 'appenders', '') mc = MainController(config) errors = mc.lint() self.assertEqual(0, errors)
def test_prepender(self): """Test prepender plugin raising exception""" config = RawConfigParser() # -------------# # config: main # # -------------# config.add_section("main") config.set('main', 'plugins', '') config.set('main', 'prependers', 'sessionhandler_test.RaiseExceptionPrepender') config.set('main', 'appenders', '') # ------------------- # # config: performance # # ------------------- # config.add_section("performance") # minimum scanner threads config.set('performance', 'minthreads', 1) # maximum scanner threads config.set('performance', 'maxthreads', 1) # Method for parallelism, either 'thread' or 'process' config.set('performance', 'backend', 'process') mc = MainController(config) mc.propagate_core_defaults() ok = mc.load_plugins() suspect = Suspect('*****@*****.**', '*****@*****.**', '/dev/null') shandler = SessionHandler(None, config, mc.prependers, mc.plugins, mc.appenders, 0) pluglist, applist = shandler.run_prependers(suspect) shandler.run_plugins(suspect, pluglist) ptags = suspect.get_tag("processingerrors") self.assertEqual([ 'Prepender RaiseExceptionPrepender failed: Prepender Plugin not implemented' ], ptags)
def test_lint_nosec(self): """Test if plugin with section=None triggers error by default""" lc = logConfig(lint=True) lc.configure() root = logging.getLogger() root.setLevel(logging.ERROR) config = RawConfigParser() # -------------# # config: main # # -------------# config.add_section("main") config.set('main', 'plugins', 'controller_tests.Dummy,controller_tests.Dummy2') config.set('main', 'prependers', '') config.set('main', 'appenders', '') mc = MainController(config) errors = mc.lint() self.assertEqual(1, errors)
def test_multiple_mcs_reload(self): """ Even if there are multiple MainControllers they should not cause crashes as long as they don't start control servers... """ config = RawConfigParser() config.add_section('performance') # minimum scanner threads config.set('performance', 'minthreads', 2) # maximum scanner threads config.set('performance', 'maxthreads', 40) # Method for parallelism, either 'thread' or 'process' config.set('performance', 'backend', 'process') # Initial number of processes when backend='process'. # If 0 (the default), automatically selects twice the number of available virtual cores. # Despite its 'initial'-name, this number currently is not adapted automatically. config.set('performance', 'initialprocs', 0) config = RawConfigParser() mclist = [] for i in range(3): mc = MainController(config) mc.propagate_core_defaults() mclist.append(mc) for mc in mclist: # usually the backend is loaded by "startup()" which is run # in a separate thread because it goes to the event loop. I'll just # directly start a the threadpool here... mc.threadpool = mc._start_threadpool() time.sleep(0.1) for mc in mclist: mc.reload() time.sleep(0.1) for mc in mclist: mc.shutdown()
def setUp(self): logger = logging.getLogger("setUp") logger.info("setup config") self.config = RawConfigParser() self.config.read([TESTDATADIR + '/endtoendbasetest.conf']) # ------------ # # config: main # # ------------ # self.config.set('main', 'incomingport', str(ReloadUnderLoadTest.FUGLU_PORT)) self.config.set('main', 'outgoinghost', str(ReloadUnderLoadTest.FUGLU_HOST)) self.config.set('main', 'outgoingport', str(ReloadUnderLoadTest.DUMMY_PORT)) self.config.set('main', 'controlport', str(ReloadUnderLoadTest.FUGLUCONTROL_PORT)) # ------------------- # # config: performance # # ------------------- # # minimum scanner threads self.config.set('performance', 'minthreads', 1) # maximum scanner threads self.config.set('performance', 'maxthreads', 1) # Method for parallelism, either 'thread' or 'process' self.config.set('performance', 'backend', 'process') # Initial number of processes when backend='process'. # If 0 (the default), automatically selects twice the number of available virtual cores. # Despite its 'initial'-name, this number currently is not adapted automatically. self.config.set('performance', 'initialprocs', ReloadUnderLoadTest.num_procs) # set timeout for joining the workers. Since the DummySMTPServer receives sequentially, # we need at least number_of_procs*delayPlugin self.config.set( 'performance', 'join_timeout', 10.0 * float(ReloadUnderLoadTest.num_procs) * ReloadUnderLoadTest.delay_by) self.config.set('main', 'plugins', 'fuglu.plugins.delay.DelayPlugin') # -------------------- # # config: delay plugin # # -------------------- # self.config.add_section("DelayPlugin") # the delay created by this plugn self.config.set("DelayPlugin", 'delay', ReloadUnderLoadTest.delay_by) self.config.set("DelayPlugin", 'logfrequency', ReloadUnderLoadTest.delay_by) # -------------- # # MainController # # -------------- # # init core logger.info("setup MainController") self.mc = MainController(self.config) # ----------------- # # Dummy SMTP Server # # ----------------- # logger.info("setup Dummy SMTP Server and start thread") # start listening smtp dummy server to get fuglus answer self.dsmtp = DummySMTPServer(self.config, ReloadUnderLoadTest.DUMMY_PORT, ReloadUnderLoadTest.FUGLU_HOST, stayalive=True) self.thread_dsmtp = threading.Thread(name="DummySMTPServer", target=self.dsmtp.serve, args=()) self.thread_dsmtp.daemon = True self.thread_dsmtp.start() # -- # start fuglu's listening server (MainController.startup) # -- logger.info("setup Fuglu and start thread") self.thread_fls = threading.Thread(name="MainController", target=self.mc.startup, args=()) self.thread_fls.daemon = True self.thread_fls.start() # give fuglu time to start listener time.sleep(1) setup_module()
if opts.plugindirs != None: for plugindir in opts.plugindirs: if plugindir not in sys.path: sys.path.insert(0, plugindir) # prepare config config = ConfigParser.ConfigParser() config.add_section('main') prependers = [] scanners = [] appenders = [] # autodetect plugin type tempmc = MainController(config) for plugin in pluginlist: try: pluginstance = tempmc._load_component(plugin) except Exception as e: print("Could not load plugin %s: %s" % (plugin, str(e))) sys.exit(1) if isinstance(pluginstance, ScannerPlugin): scanners.append(plugin) elif isinstance(pluginstance, PrependerPlugin): prependers.append(plugin) elif isinstance(pluginstance, AppenderPlugin): appenders.append(plugin) else: print("%s doesn't seem to be a fuglu plugin - ignoring" % plugin)