def _setup_db_deviceagent(data): """Create the database for DeviceAgent table testing. Args: None Returns: result: Tuple of (idx_device, idx_agent) """ # Initialize key variables devicename = data['devicename'] description = data['device_description'] id_agent = data['id_agent'] agent_name = data['agent'] last_timestamp = data['timestamp'] # Add AgentName record to the database record = AgentName(name=general.encode(agent_name)) database = db.Database() database.add(record, 1031) # Add Agent record to the database record = Agent(id_agent=general.encode(id_agent)) database = db.Database() database.add(record, 1031) # Get idx_agent value from database agent_info = db_agent.GetIDAgent(id_agent) idx_agent = agent_info.idx_agent() # Add record to the database dev_record = Device(description=general.encode(description), devicename=general.encode(devicename)) database = db.Database() database.add(dev_record, 1034) # Get idx of newly added device device_info = db_device.GetDevice(devicename) idx_device = device_info.idx_device() # Update DeviceAgent table if hagent.device_agent_exists(idx_device, idx_agent) is False: # Add to DeviceAgent table da_record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent) database = db.Database() database.add(da_record, 1020) # Update DeviceAgent table with timestamp database = db.Database() session = database.session() record = session.query(DeviceAgent).filter( and_(DeviceAgent.idx_device == idx_device, DeviceAgent.idx_agent == idx_agent)).one() record.last_timestamp = last_timestamp database.commit(session, 1042) # Return result = (idx_device, idx_agent) return result
def _insert_agent_device(self): """Insert first agent and device in the database. Args: None Returns: None """ # Initialize key variables idx_agent = 1 idx_device = 1 # Add agent if db_agent.idx_agent_exists(idx_agent) is False: # Generate a UID and add a record in the database record = Agent(id_agent=general.encode(self.reserved), name=general.encode(self.reserved)) database = db.Database() database.add(record, 1109) # Add device if db_device.idx_device_exists(idx_device) is False: record = Device(description=general.encode(self.reserved), devicename=general.encode(self.reserved)) database = db.Database() database.add(record, 1106) # Add to Agent / Device table if db_deviceagent.device_agent_exists(idx_device, idx_agent) is False: record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent) database = db.Database() database.add(record, 1107)
def test_valid(self): """Testing function valid.""" # Initialize key variables devicename = self.data['devicename'] id_agent = self.data['id_agent'] last_timestamp = self.data['timestamp'] # Drop the database and create tables unittest_setup_db.TestData() # Add record to the database record = Agent(id_agent=general.encode(id_agent)) database = db.Database() database.add(record, 1040) # Test must be good as DeviceAgent last_timestamp not updated result = validate._CheckDuplicates(self.data) self.assertEqual(result.valid(), True) # Get idx_agent value from database data = db_agent.GetIDAgent(id_agent) idx_agent = data.idx_agent() # Add record to the database record = Device(devicename=general.encode(devicename)) database = db.Database() database.add(record, 1024) # Test must be good as DeviceAgent last_timestamp not updated result = validate._CheckDuplicates(self.data) self.assertEqual(result.valid(), True) # Get idx of newly added device device_info = db_device.GetDevice(devicename) idx_device = device_info.idx_device() # Update DeviceAgent table if hagent.device_agent_exists(idx_device, idx_agent) is False: # Add to DeviceAgent table record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent) database = db.Database() database.add(record, 1055) # Test must be good as DeviceAgent last_timestamp not updated result = validate._CheckDuplicates(self.data) self.assertEqual(result.valid(), True) # Update database with timestamp database = db.Database() session = database.session() record = session.query(DeviceAgent).filter( and_(DeviceAgent.idx_device == idx_device, DeviceAgent.idx_agent == idx_agent)).one() record.last_timestamp = last_timestamp database.commit(session, 1044) # Test must fail as DeviceAgent last_timestamp not updated result = validate._CheckDuplicates(self.data) self.assertEqual(result.valid(), False)
def idx_device(self): """Insert new device into database if necessary. Args: None Returns: idx_device: Index value for device record """ # Initialize key variables devicename = self.agent_data['devicename'] # Get information on agent from database device = db_device.GetDevice(devicename) # Determine index value for device if device.exists() is True: idx_device = device.idx_device() else: # Add record to the database record = Device(devicename=general.encode(devicename)) database = db.Database() database.add(record, 1080) # Get idx of newly added device device_info = db_device.GetDevice(devicename) idx_device = device_info.idx_device() # Update DeviceAgent table idx_agent = self._idx_agent if db_deviceagent.device_agent_exists(idx_device, idx_agent) is False: # Add to DeviceAgent table new_record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent) database = db.Database() database.add(new_record, 1038) # Return return idx_device