def _idx_datapoint(): """Create a new DataPoint db entry. Args: value: Value to convert Returns: result: idx_datapoint value for new DataPoint """ # Initialize key variables polling_interval = 1 # Create a new Agent entry agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Create entry and check _checksum = data.hashstring(str(random())) result = datapoint.checksum_exists(_checksum) datapoint.insert_row(_checksum, DATA_FLOAT, polling_interval, idx_agent) result = datapoint.checksum_exists(_checksum) return result
def test_insert_rows(self): """Testing method / function insert_rows.""" # Initialize key variables polling_interval = 1 checksum = data.hashstring(str(random())) key = data.hashstring(str(random())) value = data.hashstring(str(random())) # Create a new Agent entry agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Insert values in tables pair.insert_rows((key, value)) idx_pair = pair.pair_exists(key, value) datapoint.insert_row(checksum, DATA_FLOAT, polling_interval, idx_agent) idx_datapoint = datapoint.checksum_exists(checksum) # Create entry and check result = glue.glue_exists(idx_datapoint, idx_pair) self.assertFalse(result) glue.insert_rows(idx_datapoint, idx_pair) result = glue.glue_exists(idx_datapoint, idx_pair) self.assertTrue(bool(result)) self.assertTrue(isinstance(result, int))
def test_idx_pairs(self): """Testing method / function idx_pairs.""" # Initialize key variables checksum = data.hashstring(str(random())) polling_interval = 1 keypairs = [] idx_pairs = [] for _ in range(0, 10): time.sleep(0.05) key = data.hashstring(str(random())) value = data.hashstring(str(random())) keypairs.append((key, value)) # Create a new Agent entry agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Insert values in tables pair.insert_rows(keypairs) datapoint.insert_row(checksum, DATA_FLOAT, polling_interval, idx_agent) idx_datapoint = datapoint.checksum_exists(checksum) # Test for key, value in keypairs: idx_pairs.append(pair.pair_exists(key, value)) glue.insert_rows(idx_datapoint, idx_pairs) result = glue.idx_pairs(idx_datapoint) self.assertEqual(len(result), len(idx_pairs)) for idx_pair in idx_pairs: self.assertTrue(idx_pair in result)
def test_chart_timestamp_args(self): """Testing function chart_timestamp_args.""" # Create a new Agent entry _pi = 1000 agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Create entry and check checksum = data.hashstring(str(random())) result = datapoint.checksum_exists(checksum) self.assertFalse(result) datapoint.insert_row(checksum, DATA_FLOAT, _pi, idx_agent) idx_datapoint = datapoint.checksum_exists(checksum) # Test values = [False, None] for value in values: now = normalized_timestamp(_pi, int(time.time() * 1000)) result = uri.chart_timestamp_args(idx_datapoint, value) self.assertEqual(result + 604800000, now) values = [-1, -6011, 1, 6011] for value in values: now = normalized_timestamp(_pi, int(time.time() * 1000)) result = uri.chart_timestamp_args(idx_datapoint, value) self.assertEqual(result + (abs(value) * 1000), now) values = ['foo', [None]] for value in values: now = normalized_timestamp(_pi, int(time.time() * 1000)) result = uri.chart_timestamp_args(idx_datapoint, value) self.assertEqual(result + 604800000, now)
def test_insert_row(self): """Testing method / function insert_row.""" # Initialize key variables result = datapoint.checksum_exists(-1) polling_interval = 1 self.assertFalse(result) # Create a new Agent entry agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Create entry and check checksum = data.hashstring(str(random())) result = datapoint.checksum_exists(checksum) self.assertFalse(result) datapoint.insert_row(checksum, DATA_FLOAT, polling_interval, idx_agent) result = datapoint.checksum_exists(checksum) self.assertTrue(bool(result)) self.assertTrue(isinstance(result, int))
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)