def run_special_commands(self, p_arguments): if p_arguments.stamp_databases: self.basic_init(p_full_startup=False) self.stamp_databases(p_alembic_version=p_arguments.stamp_databases) return True if p_arguments.create_databases: self.basic_init(p_full_startup=False) self._persistence.check_schema(p_create_tables=False) if p_arguments.upgrade_databases: self.basic_init(p_full_startup=False) db_mig = db_migrations.DatabaseMigrations( p_logger=self._logger, p_persistence=self._persistence) db_mig.upgrade_databases( p_alembic_version=p_arguments.upgrade_databases) return False
def test_process_statistics_get_empty_stat_infos(self): start_time = datetime.datetime.utcnow() rule_set_configs = test_data.get_dummy_ruleset_configs( p_ruleset_config=test_data.RULESET_CONFIGS_USER1_ALL_RESTRICTIONS) dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence( self._logger) session_context = persistence.SessionContext( p_persistence=dummy_persistence) dummy_persistence.add_new_user(p_session_context=session_context, p_username=test_data.USER_1) migrator = db_migrations.DatabaseMigrations( p_logger=self._logger, p_persistence=dummy_persistence) migrator.migrate_ruleset_configs(p_ruleset_configs=rule_set_configs) sis = process_statistics.get_empty_stat_infos( p_user_map=dummy_persistence.user_map(session_context), p_reference_time=start_time, p_max_lookback_in_days=5, p_min_activity_duration=30) self.assertIsNotNone(sis)
def test_priority(self): dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(self._logger) session_context = persistence.SessionContext(p_persistence=dummy_persistence) a_rule_handler = self.create_dummy_rule_handler(p_persistence=dummy_persistence) migrator = db_migrations.DatabaseMigrations(p_logger=self._logger, p_persistence=dummy_persistence) migrator.migrate_ruleset_configs(self.create_dummy_ruleset_configs()) active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context, p_username=TEST_USER, p_reference_date=NORMAL_DAY_1) self.assertIsNotNone(active_rule_set) self.assertEqual(active_rule_set.context, simple_context_rule_handlers.DEFAULT_CONTEXT_RULE_HANDLER_NAME) active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context, p_username=TEST_USER, p_reference_date=WEEKEND_DAY_1) self.assertIsNotNone(active_rule_set) self.assertEqual(active_rule_set.context, simple_context_rule_handlers.WEEKPLAN_CONTEXT_RULE_HANDLER_NAME) active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context, p_username=TEST_USER, p_reference_date=WEEKEND_DAY_2) self.assertIsNotNone(active_rule_set) self.assertEqual(active_rule_set.context, simple_context_rule_handlers.WEEKPLAN_CONTEXT_RULE_HANDLER_NAME) active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context, p_username=TEST_USER, p_reference_date=VACATION_DAY_1) self.assertIsNotNone(active_rule_set) self.assertEqual(active_rule_set.context, german_vacation_context_rule_handler.CALENDAR_CONTEXT_RULE_HANDLER_NAME) active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context, p_username=TEST_USER, p_reference_date=VACATION_DAY_2) self.assertIsNotNone(active_rule_set) self.assertEqual(active_rule_set.context, german_vacation_context_rule_handler.CALENDAR_CONTEXT_RULE_HANDLER_NAME) active_rule_set = a_rule_handler.get_active_ruleset(p_session_context=session_context, p_username=TEST_USER, p_reference_date=WEEKEND_DAY_3) self.assertIsNotNone(active_rule_set) self.assertEqual(active_rule_set.context, simple_context_rule_handlers.WEEKPLAN_CONTEXT_RULE_HANDLER_NAME)
def check_migrations(self): db_mig = db_migrations.DatabaseMigrations( p_logger=self._logger, p_persistence=self._persistence) current_version = db_mig.get_current_version() msg = "Database is on alembic version {version}" self._logger.info(msg.format(version=current_version)) if db_mig.check_if_version_is_active( p_version=alembic_version_gui.revision): session = self._persistence.get_session() rows = session.query(persistence.User).count() if rows == 0: # if there are no users in the database yet we assume that the migration has never run yet db_mig.migrate_ruleset_configs( p_ruleset_configs=self._rule_set_section_handler. rule_set_configs) db_mig.migrate_client_device_configs( p_client_device_configs=self. _client_device_section_handler.client_device_configs) else: if len(self._rule_set_section_handler.rule_set_configs) > 0: msg = "Remove {count} migrated ruleset sections 'RuleSet*'" self._logger.warning( msg.format(count=len( self._rule_set_section_handler.rule_set_configs))) if len(self._client_device_section_handler. client_device_configs) > 0: msg = "Remove {count} migrated device sections 'Device*'" self._logger.warning( msg.format( count=len(self._client_device_section_handler. client_device_configs))) session.close()
def test_existing_host(self): config = client_device_handler.ClientDeviceHandlerConfigModel() device_config = client_device_handler.ClientDeviceConfigModel( p_section_name="my_device") device_config.username = test_data.USER_1 device_config.hostname = "localhost" device_config.min_activity_duration = 0 device_configs = {device_config.section_name: device_config} dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence( self._logger) session_context = persistence.SessionContext( p_persistence=dummy_persistence) dummy_persistence.add_new_user(p_session_context=session_context, p_username=test_data.USER_1) migrator = db_migrations.DatabaseMigrations( p_logger=self._logger, p_persistence=dummy_persistence) migrator.migrate_client_device_configs(device_configs) process_handler = client_device_handler.ClientDeviceHandler( p_config=config, p_persistence=dummy_persistence) process_handler.scan_processes( p_session_context=session_context, p_server_group=None, p_login_mapping=None, p_host_name=None, p_process_regex_map=None, p_reference_time=datetime.datetime.now()) events = process_handler.scan_processes( p_session_context=session_context, p_server_group=None, p_login_mapping=None, p_host_name=None, p_process_regex_map=None, p_reference_time=datetime.datetime.now()) self.check_list_has_n_elements(p_list=events, p_n=1)
def create_dummy_persistence(p_logger): sqlite_file = os.path.join(SQLITE_DIR, SQLITE_FILE) if os.path.exists(sqlite_file): os.unlink(sqlite_file) config = persistence.PersistenceConfigModel() config.sqlite_filename = SQLITE_FILE config.sqlite_dir = SQLITE_DIR config.database_driver = persistence.DATABASE_DRIVER_SQLITE config.database_user = None config.database_admin = None a_persistence = persistence.Persistence(p_config=config, p_reuse_session=False) a_persistence.check_schema(p_create_tables=False) db_mig = db_migrations.DatabaseMigrations(p_logger, p_persistence=a_persistence) db_mig.upgrade_databases() return a_persistence