def test_delete_node(self): """ Tests deleting of nodes in the database. """ config_logging(logging.ERROR) storage = self._init_database() system_data = self._create_system_data() self._create_objects(storage, system_data) # Delete object and check correct deletion. for node in system_data.get_nodes_list(): storage._open_connection() storage._delete_node(node.nodeId) storage._conn.commit() system_data.delete_node_by_id(node.nodeId) storage.synchronize_database_to_system_data() compare_nodes_content(self, system_data.get_nodes_list(), storage._system_data.get_nodes_list()) # Delete function of database also deletes all Alert, Manager, and Sensor objects. compare_alerts_content(self, system_data.get_alerts_list(), storage._system_data.get_alerts_list()) compare_managers_content(self, system_data.get_managers_list(), storage._system_data.get_managers_list()) compare_sensors_content(self, system_data.get_sensors_list(), storage._system_data.get_sensors_list())
def test_add_data(self): """ Tests adding of system information to the database. """ config_logging(logging.ERROR) storage = self._init_database() # Create database objects. system_data = self._create_system_data() storage.update_server_information(0, system_data.get_options_list(), system_data.get_nodes_list(), system_data.get_sensors_list(), system_data.get_alerts_list(), system_data.get_managers_list(), system_data.get_alert_levels_list(), []) storage._system_data = SystemData() storage.synchronize_database_to_system_data() compare_nodes_content(self, system_data.get_nodes_list(), storage._system_data.get_nodes_list()) compare_alerts_content(self, system_data.get_alerts_list(), storage._system_data.get_alerts_list()) compare_managers_content(self, system_data.get_managers_list(), storage._system_data.get_managers_list()) compare_sensors_content(self, system_data.get_sensors_list(), storage._system_data.get_sensors_list())
def test_add_node(self): """ Tests adding of nodes to the database. """ config_logging(logging.ERROR) storage = self._init_database() system_data = self._create_system_data() self._create_objects(storage, system_data) storage._system_data = SystemData() storage.synchronize_database_to_system_data() compare_nodes_content(self, system_data.get_nodes_list(), storage._system_data.get_nodes_list())
def test_update_node(self): """ Tests updating of nodes in the database. """ config_logging(logging.ERROR) storage = self._init_database() system_data = self._create_system_data() self._create_objects(storage, system_data) # Update local objects. ctr = 0 for node in system_data.get_nodes_list(): temp_node = ManagerObjNode().deepcopy(node) temp_node.hostname = "new_hostname_" + str(ctr + 1) temp_node.nodeType = ["alert", "manager", "sensor", "server"][ctr % 4] temp_node.instance = "new_instance_" + str(ctr + 1) temp_node.connected = (ctr % 2) temp_node.version = float(5 + ctr) temp_node.rev = ctr temp_node.username = "******" + str(ctr + 1) temp_node.persistent = (ctr % 2) system_data.update_node(temp_node) ctr += 1 # Update database objects. storage._open_connection() for node in system_data.get_nodes_list(): storage._update_node(node) storage._conn.commit() storage._system_data = SystemData() storage.synchronize_database_to_system_data() compare_nodes_content(self, system_data.get_nodes_list(), storage._system_data.get_nodes_list()) # Update function of database also deletes all Alert, Manager, and Sensor objects if node type changes. compare_alerts_content(self, system_data.get_alerts_list(), storage._system_data.get_alerts_list()) compare_managers_content(self, system_data.get_managers_list(), storage._system_data.get_managers_list()) compare_sensors_content(self, system_data.get_sensors_list(), storage._system_data.get_sensors_list())
def test_delete_profile(self): """ Tests deleting of profiles in system information data in the database. """ config_logging(logging.ERROR) storage = self._init_database() # Create database objects. system_data = self._create_system_data() storage.update_server_information(0, system_data.get_options_list(), system_data.get_profiles_list(), system_data.get_nodes_list(), system_data.get_sensors_list(), system_data.get_alerts_list(), system_data.get_managers_list(), system_data.get_alert_levels_list(), []) # Delete profile objects and check correct deletion. for profile in system_data.get_profiles_list(): system_data.delete_profile_by_id(profile.profileId) storage.update_server_information( 0, system_data.get_options_list(), system_data.get_profiles_list(), system_data.get_nodes_list(), system_data.get_sensors_list(), system_data.get_alerts_list(), system_data.get_managers_list(), system_data.get_alert_levels_list(), []) storage.synchronize_database_to_system_data() compare_profiles_content(self, system_data.get_profiles_list(), storage._system_data.get_profiles_list()) compare_nodes_content(self, system_data.get_nodes_list(), storage._system_data.get_nodes_list()) compare_alerts_content(self, system_data.get_alerts_list(), storage._system_data.get_alerts_list()) compare_managers_content(self, system_data.get_managers_list(), storage._system_data.get_managers_list()) compare_sensors_content(self, system_data.get_sensors_list(), storage._system_data.get_sensors_list()) compare_alert_levels_content( self, system_data.get_alert_levels_list(), storage._system_data.get_alert_levels_list())
def test_update(self): """ Tests updating of system information in the database. """ config_logging(logging.ERROR) storage = self._init_database() # Create database objects. system_data = self._create_system_data() storage.update_server_information(0, system_data.get_options_list(), system_data.get_nodes_list(), system_data.get_sensors_list(), system_data.get_alerts_list(), system_data.get_managers_list(), system_data.get_alert_levels_list(), []) # Update local objects. ctr = 0 for option in system_data.get_options_list(): temp_option = ManagerObjOption().deepcopy(option) temp_option.value = float(ctr) system_data.update_option(temp_option) ctr += 1 for alert_level in system_data.get_alert_levels_list(): temp_alert_level = ManagerObjAlertLevel().deepcopy(alert_level) temp_alert_level.name = "new_alert_level_" + str(ctr + 1) temp_alert_level.triggerAlways = (ctr % 2) temp_alert_level.instrumentation_active = (ctr % 2) == 0 if temp_alert_level.instrumentation_active: temp_alert_level.instrumentation_cmd = "alert_level_instrumentation_" + str( ctr + 1) temp_alert_level.instrumentation_timeout = 123 + ctr system_data.update_alert_level(temp_alert_level) ctr += 1 for node in system_data.get_nodes_list(): temp_node = ManagerObjNode().deepcopy(node) temp_node.hostname = "new_hostname_" + str(ctr + 1) temp_node.nodeType = ["alert", "manager", "sensor", "server"][ctr % 4] temp_node.instance = "new_instance_" + str(ctr + 1) temp_node.connected = (ctr % 2) temp_node.version = float(5 + ctr) temp_node.rev = ctr temp_node.username = "******" + str(ctr + 1) temp_node.persistent = (ctr % 2) system_data.update_node(temp_node) ctr += 1 for alert in system_data.get_alerts_list(): temp_alert = ManagerObjAlert().deepcopy(alert) temp_alert.description = "new_alert_" + str(ctr + 1) temp_alert.remoteAlertId = ctr # We started the alert levels in our test data with level 1. temp_alert.alertLevels = [ (ctr % len(system_data.get_alert_levels_list())) + 1 ] system_data.update_alert(temp_alert) ctr += 1 for manager in system_data.get_managers_list(): temp_manager = ManagerObjManager().deepcopy(manager) temp_manager.description = "new_manager_" + str(ctr + 1) system_data.update_manager(temp_manager) ctr += 1 for sensor in system_data.get_sensors_list(): temp_sensor = ManagerObjSensor().deepcopy(sensor) temp_sensor.description = "new_sensor_" + str(ctr + 1) temp_sensor.remoteSensorId = ctr temp_sensor.alertDelay = ctr + 10 temp_sensor.lastStateUpdated = ctr + 10 temp_sensor.state = ctr % 2 temp_sensor.dataType = SensorDataType.INT temp_sensor.data = ctr # We started the alert levels in our test data with level 1. temp_sensor.alertLevels = [ (ctr % len(system_data.get_alert_levels_list())) + 1 ] system_data.update_sensor(temp_sensor) ctr += 1 # Update database objects. storage.update_server_information(0, system_data.get_options_list(), system_data.get_nodes_list(), system_data.get_sensors_list(), system_data.get_alerts_list(), system_data.get_managers_list(), system_data.get_alert_levels_list(), []) storage._system_data = SystemData() storage.synchronize_database_to_system_data() compare_nodes_content(self, system_data.get_nodes_list(), storage._system_data.get_nodes_list()) compare_alerts_content(self, system_data.get_alerts_list(), storage._system_data.get_alerts_list()) compare_managers_content(self, system_data.get_managers_list(), storage._system_data.get_managers_list()) compare_sensors_content(self, system_data.get_sensors_list(), storage._system_data.get_sensors_list()) compare_alert_levels_content( self, system_data.get_alert_levels_list(), storage._system_data.get_alert_levels_list())
def test_content_update(self): """ Tests status update of content. """ global_data = GlobalData() global_data.system_data = self._create_system_data() event_handler = BaseManagerEventHandler(global_data) # Update local objects. ctr = 1 for option in self.options: option.value += ctr ctr += 1 for profile in self.profiles: profile.name += "_new_" + str(ctr) ctr += 1 for alert_level in self.alert_levels: alert_level.name += "_new_" + str(ctr) ctr += 1 for node in self.nodes: node.hostname += "_new_" + str(ctr) ctr += 1 for alert in self.alerts: alert.description += "_new_" + str(ctr) ctr += 1 for sensor in self.sensors: sensor.description += "_new_" + str(ctr) ctr += 1 for manager in self.managers: manager.description += "_new_" + str(ctr) ctr += 1 # Check stored and local objects differ. for stored_option in global_data.system_data.get_options_list(): found = False for local_option in self.options: if local_option.type == stored_option.type: found = True if local_option == stored_option: self.fail( "Local and stored object have not the same object." ) if local_option.value == stored_option.value: self.fail( "Local and stored object have the same content.") break if not found: self.fail("Stored Option object not found in local objects.") # Check stored and local objects differ. for stored_profile in global_data.system_data.get_profiles_list(): found = False for local_profile in self.profiles: if local_profile.profileId == stored_profile.profileId: found = True if local_profile == stored_profile: self.fail( "Local and stored object have not the same object." ) if local_profile.name == stored_profile.name: self.fail( "Local and stored object have the same content.") break if not found: self.fail("Stored Option object not found in local objects.") for stored_alert_level in global_data.system_data.get_alert_levels_list( ): found = False for local_alert_level in self.alert_levels: if local_alert_level.level == stored_alert_level.level: found = True if local_alert_level == stored_alert_level: self.fail( "Local and stored object are the same object.") if local_alert_level.name == stored_alert_level.name: self.fail( "Local and stored object have the same content.") break if not found: self.fail( "Stored Alert Level object not found in local objects.") for stored_node in global_data.system_data.get_nodes_list(): found = False for local_node in self.nodes: if local_node.nodeId == stored_node.nodeId: found = True if local_node == stored_node: self.fail( "Local and stored object are the same object.") if local_node.hostname == stored_node.hostname: self.fail( "Local and stored object have the same content.") break if not found: self.fail("Stored Node object not found in local objects.") for stored_alert in global_data.system_data.get_alerts_list(): found = False for local_alert in self.alerts: if local_alert.alertId == stored_alert.alertId: found = True if local_alert == stored_alert: self.fail( "Local and stored object are the same object.") if local_alert.description == stored_alert.description: self.fail( "Local and stored object have the same content.") break if not found: self.fail("Stored Alert object not found in local objects.") for stored_manager in global_data.system_data.get_managers_list(): found = False for local_manager in self.managers: if local_manager.managerId == stored_manager.managerId: found = True if local_manager == stored_manager: self.fail( "Local and stored object are the same object.") if local_manager.description == stored_manager.description: self.fail( "Local and stored object have the same content.") break if not found: self.fail("Stored Manager object not found in local objects.") for stored_sensor in global_data.system_data.get_sensors_list(): found = False for local_sensor in self.sensors: if local_sensor.sensorId == stored_sensor.sensorId: found = True if local_sensor == stored_sensor: self.fail( "Local and stored object are the same object.") if local_sensor.description == stored_sensor.description: self.fail( "Local and stored object have the same content.") break if not found: self.fail("Stored Sensor object not found in local objects.") if not event_handler.status_update( 1337, self.options, self.profiles, self.nodes, self.sensors, self.managers, self.alerts, self.alert_levels): self.fail("Status update failed.") if event_handler.msg_time != 1337: self.fail("Server time update failed.") # Check changes are stored. compare_options_content(self, self.options, global_data.system_data.get_options_list()) compare_profiles_content(self, self.profiles, global_data.system_data.get_profiles_list()) compare_alert_levels_content( self, self.alert_levels, global_data.system_data.get_alert_levels_list()) compare_nodes_content(self, self.nodes, global_data.system_data.get_nodes_list()) compare_alerts_content(self, self.alerts, global_data.system_data.get_alerts_list()) compare_managers_content(self, self.managers, global_data.system_data.get_managers_list()) compare_sensors_content(self, self.sensors, global_data.system_data.get_sensors_list())
def test_object_deletion(self): """ Tests status update of object deletion. """ global_data = GlobalData() global_data.system_data = self._create_system_data() event_handler = BaseManagerEventHandler(global_data) # Remove single local object # IMPORTANT: Ignore alert level/node/profile here, # since the deletion of a alert level/node/profile object also deletes other objects. ctr = 0 option_to_remove = self.options[ctr % len(self.options)] self.options.remove(option_to_remove) ctr += 1 alert_to_remove = self.alerts[ctr % len(self.alerts)] self.alerts.remove(alert_to_remove) ctr += 1 manager_to_remove = self.managers[ctr % len(self.managers)] self.managers.remove(manager_to_remove) ctr += 1 sensor_to_remove = self.sensors[ctr % len(self.sensors)] self.sensors.remove(sensor_to_remove) if len(self.options) != ( len(global_data.system_data.get_options_list()) - 1): self.fail("Local objects list connected to stored data.") if len(self.alerts) != ( len(global_data.system_data.get_alerts_list()) - 1): self.fail("Local objects list connected to stored data.") if len(self.managers) != ( len(global_data.system_data.get_managers_list()) - 1): self.fail("Local objects list connected to stored data.") if len(self.sensors) != ( len(global_data.system_data.get_sensors_list()) - 1): self.fail("Local objects list connected to stored data.") if not event_handler.status_update( 0, self.options, self.profiles, self.nodes, self.sensors, self.managers, self.alerts, self.alert_levels): self.fail("Status update failed.") if len(self.options) != len( global_data.system_data.get_options_list()): self.fail("Number of stored objects wrong.") if len(self.profiles) != len( global_data.system_data.get_profiles_list()): self.fail("Number of stored objects wrong.") if len(self.alert_levels) != len( global_data.system_data.get_alert_levels_list()): self.fail("Number of stored objects wrong.") if len(self.nodes) != len(global_data.system_data.get_nodes_list()): self.fail("Number of stored objects wrong.") if len(self.alerts) != len(global_data.system_data.get_alerts_list()): self.fail("Number of stored objects wrong.") if len(self.managers) != len( global_data.system_data.get_managers_list()): self.fail("Number of stored objects wrong.") if len(self.sensors) != len( global_data.system_data.get_sensors_list()): self.fail("Number of stored objects wrong.") # Check changes are stored. for stored_option in global_data.system_data.get_options_list(): if stored_option.type == option_to_remove.type: self.fail("Object not removed from store.") found = False for local_option in self.options: if local_option.type == stored_option.type: found = True if local_option == stored_option: self.fail( "Local and stored object are the same object.") break if not found: self.fail("Stored Option object not found in local objects.") for stored_alert in global_data.system_data.get_alerts_list(): if stored_alert.alertId == alert_to_remove.alertId: self.fail("Object not removed from store.") found = False for local_alert in self.alerts: if local_alert.alertId == stored_alert.alertId: found = True if local_alert == stored_alert: self.fail( "Local and stored object are the same object.") break if not found: self.fail("Stored Alert object not found in local objects.") for stored_manager in global_data.system_data.get_managers_list(): if stored_manager.managerId == manager_to_remove.managerId: self.fail("Object not removed from store.") found = False for local_manager in self.managers: if local_manager.managerId == stored_manager.managerId: found = True if local_manager == stored_manager: self.fail( "Local and stored object are the same object.") break if not found: self.fail("Stored Manager object not found in local objects.") for stored_sensor in global_data.system_data.get_sensors_list(): if stored_sensor.sensorId == sensor_to_remove.sensorId: self.fail("Object not removed from store.") found = False for local_sensor in self.sensors: if local_sensor.sensorId == stored_sensor.sensorId: found = True if local_sensor == stored_sensor: self.fail( "Local and stored object are the same object.") break if not found: self.fail("Stored Sensor object not found in local objects.") compare_profiles_content(self, self.profiles, global_data.system_data.get_profiles_list()) compare_alert_levels_content( self, self.alert_levels, global_data.system_data.get_alert_levels_list()) compare_nodes_content(self, self.nodes, global_data.system_data.get_nodes_list())