def idx_datapoint(pattoo_db_record): """Get the db DataPoint.idx_datapoint value for a PattooDBrecord object. Args: pattoo_db_record: PattooDBrecord object Returns: _idx_datapoint: DataPoint._idx_datapoint value. None if unsuccessful """ # Initialize key variables checksum = pattoo_db_record.pattoo_checksum data_type = pattoo_db_record.pattoo_data_type polling_interval = pattoo_db_record.pattoo_agent_polling_interval agent_id = pattoo_db_record.pattoo_agent_id agent_target = pattoo_db_record.pattoo_agent_polled_target agent_program = pattoo_db_record.pattoo_agent_program # Create an entry in the database Checksum table _idx_datapoint = checksum_exists(checksum) if bool(_idx_datapoint) is False: # Create a record in the Agent table idx_agent = agent.idx_agent(agent_id, agent_target, agent_program) # Create a record in the DataPoint table insert_row(checksum, data_type, polling_interval, idx_agent) _idx_datapoint = checksum_exists(checksum) # Return return _idx_datapoint
def test_process(self): """Tests assign argument process function""" # Testing for invalid args.qualifier args = self.parser.parse_args([]) args.qualifier = '' self.assertIsNone(process(args)) #################################################################### # # Testing for proper _process_agent execution # #################################################################### # Agent information agent_id = 'test_agent_id_one' agent_target = 'test_agent_target_one' agent_program = 'test_agent_program_one' # Inserting agent data and getting agent idx_agent value agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.idx_agent(agent_id, agent_target, agent_program) # Derfining expected data and command line entries to be passed to # process function expected = {'idx_agent': idx_agent, 'idx_pair_xlate_group': '1'} cmd_args = [ 'assign', 'agent', '--idx_agent', str(idx_agent), '--idx_pair_xlate_group', expected['idx_pair_xlate_group'] ] self.assign_fn(expected, cmd_args, Agent, process, True)
def test__process_agent(self): """Tests _process_agent""" # Agent information agent_id = 'test_process_agent_one' agent_target = 'test_process_agent_one' agent_program = 'test_process_agent_progam_one' # Inserting agent data and getting agent idx_agent value agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.idx_agent(agent_id, agent_target, agent_program) # Derfining expected data and command line entries to be passed to # process function expected = {'idx_agent': idx_agent, 'idx_pair_xlate_group': '1'} cmd_args = [ 'assign', 'agent', '--idx_agent', str(idx_agent), '--idx_pair_xlate_group', expected['idx_pair_xlate_group'] ] self.assign_fn(expected, cmd_args, Agent, _process_agent, False) # Asserting that appropriate log message is ran if idx_pair_xlate_group # does not exist args = self.parser.parse_args([]) args.idx_pair_xlate_group = '' expected_included_str = ('''\ idx_pair_xlate_group "{}" not found.'''.format(args.idx_pair_xlate_group)) with self.assertLogs(self.log_obj.stdout(), level='INFO') as cm: print('Exception thrown testing test__process_agent: ') with self.assertRaises(SystemExit): _process_agent(args) print(cm.output[0]) print('') self.assertIn(expected_included_str, cm.output[0]) # Asserting that appropriate log message is ran if idx_agent does not # exist args = self.parser.parse_args([]) args.idx_pair_xlate_group = '1' args.idx_agent = '' expected_included_str = ('''\ idx_agent "{}" not found.'''.format(args.idx_agent)) with self.assertLogs(self.log_obj.stdout(), level='INFO') as cm: print('Exception thrown testing test__process_agent: ') with self.assertRaises(SystemExit): _process_agent(args) print(cm.output[0]) print('') self.assertIn(expected_included_str, cm.output[0])
def idx_datapoint(pattoo_db_record): """Get the db DataPoint.idx_datapoint value for a PattooDBrecord object. Args: pattoo_db_record: PattooDBrecord object Returns: _idx_datapoint: DataPoint._idx_datapoint value. None if unsuccessful """ # Initialize key variables checksum = pattoo_db_record.pattoo_checksum data_type = pattoo_db_record.pattoo_data_type polling_interval = pattoo_db_record.pattoo_agent_polling_interval agent_id = pattoo_db_record.pattoo_agent_id agent_target = pattoo_db_record.pattoo_agent_polled_target agent_program = pattoo_db_record.pattoo_agent_program # Create an entry in the database Checksum table _idx_datapoint = checksum_exists(checksum) if bool(_idx_datapoint) is False: # Create a record in the Agent table idx_agent = agent.idx_agent(agent_id, agent_target, agent_program) # Create a record in the DataPoint table insert_row(checksum, data_type, polling_interval, idx_agent) _idx_datapoint = checksum_exists(checksum) # Create a record in the Chart table chart_checksum = data_.hashstring('{}{}'.format( random.random(), checksum)) chart.insert_row( DbRowChart(name='', checksum=chart_checksum, enabled=1)) idx_chart = chart.exists(chart_checksum) # Create a record in the ChartDataPoint table chart_datapoint.insert_row( DbRowChartDataPoint(idx_chart=idx_chart, idx_datapoint=_idx_datapoint, enabled=1)) # Return return _idx_datapoint
def test_checksums(self): """Testing method / function checksums.""" # Initialize key variables expected = {} polling_interval = 1 pattoo_agent_polled_target = 'panda_bear' pattoo_agent_program = 'koala_bear' pattoo_agent_hostname = 'grizzly_bear' # Insert an entry in the agent table agent_id = data.hashstring(str(random())) idx_agent = agent.idx_agent(agent_id, pattoo_agent_polled_target, pattoo_agent_program) # Populate database with key-value pairs for data_index in range(0, 10): time.sleep(0.1) # Add the checksum to the database checksum = data.hashstring(str(random())) datapoint.insert_row(checksum, DATA_FLOAT, polling_interval, idx_agent) idx_datapoint = datapoint.checksum_exists(checksum) # Define what we expect from the test function expected[checksum] = ChecksumLookup( idx_datapoint=idx_datapoint, polling_interval=polling_interval, last_timestamp=1) # Add key-pairs to the database record = PattooDBrecord( pattoo_checksum=checksum, pattoo_key='key', pattoo_agent_polling_interval=polling_interval, pattoo_agent_id=agent_id, pattoo_timestamp=int(time.time() * 1000), pattoo_data_type=DATA_FLOAT, pattoo_value=(data_index * 10), pattoo_agent_polled_target=pattoo_agent_polled_target, pattoo_agent_program=pattoo_agent_program, pattoo_agent_hostname=pattoo_agent_hostname, pattoo_metadata=[]) pairs = get.key_value_pairs(record) pair.insert_rows(pairs) idx_pairs = pair.idx_pairs(pairs) # Create glue entry glue.insert_rows(idx_datapoint, idx_pairs) ####################################################################### # This is added to verify that we only get a subset of results ####################################################################### # Insert an entry in the agent table fake_agent_id = data.hashstring(str(random())) idx_agent = agent.idx_agent(fake_agent_id, pattoo_agent_polled_target, pattoo_agent_program) # Populate database with key-value pairs for data_index in range(0, 17): time.sleep(0.1) # Add the checksum to the database checksum = data.hashstring(str(random())) datapoint.insert_row(checksum, DATA_FLOAT, polling_interval, idx_agent) idx_datapoint = datapoint.checksum_exists(checksum) # Add key-pairs to the database record = PattooDBrecord( pattoo_checksum=checksum, pattoo_key='key', pattoo_agent_polling_interval=polling_interval, pattoo_agent_id=fake_agent_id, pattoo_timestamp=int(time.time() * 1000), pattoo_data_type=DATA_FLOAT, pattoo_value=(data_index * 10), pattoo_agent_polled_target=pattoo_agent_polled_target, pattoo_agent_program=pattoo_agent_program, pattoo_agent_hostname=pattoo_agent_hostname, pattoo_metadata=[]) pairs = get.key_value_pairs(record) pair.insert_rows(pairs) idx_pairs = pair.idx_pairs(pairs) # Create glue entry glue.insert_rows(idx_datapoint, idx_pairs) # Test result = misc.agent_checksums(agent_id) self.assertTrue(bool(result)) self.assertTrue(isinstance(result, dict)) self.assertEqual(len(result), len(expected)) for key, value in result.items(): self.assertEqual(value.idx_datapoint, expected[key].idx_datapoint) self.assertEqual(value.polling_interval, expected[key].polling_interval) self.assertEqual(value.last_timestamp, expected[key].last_timestamp)