Beispiel #1
0
    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))
Beispiel #2
0
    def test_process_cache(self):
        """Testing method / function process_cache."""
        # Initialize key variables
        polling_interval = 20
        _pi = polling_interval * 1000

        _ = create_cache()

        # Read data from directory
        cache = Cache()
        all_records = cache.records()

        # Test
        self.assertEqual(len(all_records), 1)
        self.assertEqual(len(all_records[0]), 1)
        pdbr = all_records[0][0]

        # Datapoint should not exist before ingest
        checksum = pdbr.pattoo_checksum
        timestamp = pdbr.pattoo_timestamp
        value = pdbr.pattoo_value
        self.assertFalse(datapoint.checksum_exists(checksum))

        # Ingest using process_cache
        result = files_test.process_cache(fileage=0)
        self.assertTrue(result)

        # Test (checksum should exist)
        idx_datapoint = datapoint.checksum_exists(checksum)
        self.assertTrue(bool(idx_datapoint))

        # Test (Single data entry should exist)
        obj = datapoint.DataPoint(idx_datapoint)
        result = obj.data(timestamp, timestamp)
        self.assertEqual(len(result), 1)
        key_pair = result[0]
        self.assertEqual(key_pair['timestamp'],
                         times.normalized_timestamp(_pi, timestamp))
        self.assertEqual(key_pair['value'], value)
Beispiel #3
0
    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)