def test_zero_max_logs(self): # Test if max_logs is set to zero max_logs = 0 print("Creating enginelog with max_logs = ", max_logs) enlog = enginelog.EngineLog(max_logs=max_logs) logs = _fill_logs(enlog, 10) self.assertEqual(len(logs), 0)
def test_add_error(self): error_message = "test error message" enlog = enginelog.EngineLog() enlog.add_error(error_message) log = enlog.get_logs()[0] log_type = log.get("type") log_entry = log.get("entry") self.assertEqual(log_type, "error") self.assertEqual(log_entry.get("message"), error_message)
def __init__(self, *args, **kwargs): super(TestEngine, self).__init__(*args, **kwargs) # Initialize with defaults and to enable linting/ac # Test should call _setup_engine() to override any of these attributes self.loop = _get_event_loop() self.config = config.EngineConfig() self.clock = clock.EngineClock(self.config.tz, self.loop) self.persist_mgr = persistence.PersistenceManager( self.config.json_rules_dir) self.enlog = enginelog.EngineLog() self.engine_obj = engine.OttoEngine(self.config, self.loop, self.clock, self.persist_mgr, self.enlog)
def test_add_event(self): event_name = "test event" event_data = {"a": 1, "b": 2} enlog = enginelog.EngineLog() enlog.add_event(event_name, event_data=event_data) log = enlog.get_logs()[0] log_type = log.get("type") log_entry = log.get("entry") self.assertEqual(log_type, "event") self.assertEqual(log_entry.get("event"), event_name) self.assertEqual( log_entry.get("event_data").get("a"), event_data.get("a")) self.assertEqual( log_entry.get("event_data").get("b"), event_data.get("b"))
def test_log_retention(self): max_logs = 20 print("Creating enginelog with max_logs = ", max_logs) enlog = enginelog.EngineLog(max_logs=max_logs) # Test log holds all entires when not filled num_add_logs = max_logs - 5 logs = _fill_logs(enlog, num_add_logs) self.assertEqual(len(logs), num_add_logs) # Test log holds only max_logs when over filled num_add_logs = max_logs + 5 logs = _fill_logs(enlog, num_add_logs) self.assertEqual(len(logs), max_logs) # Test log holds exactly max_logs when same number are added num_add_logs = max_logs logs = _fill_logs(enlog, num_add_logs) self.assertEqual(len(logs), num_add_logs)
def test_resize_log(self): max_logs_1 = 20 print("Creating enginelog with max_logs = ", max_logs_1) enlog = enginelog.EngineLog(max_logs=max_logs_1) # Overfill first logs = _fill_logs(enlog, max_logs_1 + 7) self.assertEqual(len(logs), max_logs_1) # Then increase the size of the log: should have same number of logs max_logs_2 = max_logs_1 + 10 enlog.set_max_logs(max_logs_2) logs = enlog.get_logs() self.assertAlmostEqual(len(logs), max_logs_1) # Then decrease the size of the log smaller than initial logs # The log should trim to the new max_log size max_logs_3 = max_logs_1 - 10 enlog.set_max_logs(max_logs_3) logs = enlog.get_logs() self.assertAlmostEqual(len(logs), max_logs_3)
# Setup the logging utils.setup_logging(config.log_level) _LOG = logging.getLogger("run_otto") # Set Test Websocket to Start if needed if len(sys.argv) > 1: if sys.argv[1].lower() == 'test': _LOG.info("Test Websocket server requested by command-line option") config.test_websocket_port = 8123 # Initialize the engine loop = asyncio.get_event_loop() clock = clock.EngineClock(config.tz, loop=loop) persistence_mgr = persistence.PersistenceManager(config.json_rules_dir) engine_log = enginelog.EngineLog() engine_obj = engine.OttoEngine(config, loop, clock, persistence_mgr, engine_log) # Start the Flask web server _LOG.info("Starting web thread") restapi.engine_obj = engine_obj web_thread = threading.Thread(target=restapi.run_server) web_thread.start() # Start the engine's BaseLoop engine_obj.start_engine() # Shutdown the webui urllib.request.urlopen("http://localhost:{}/shutdown".format(config.rest_port))