Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    def test_assign(self):
        """Testing method / function assign."""
        # Add an entry to the database
        name = data.hashstring(str(random()))
        pair_xlate_group.insert_row(name)

        # Make sure it exists
        idx_pair_xlate_group = pair_xlate_group.exists(name)

        # Verify the index exists
        result = pair_xlate_group.idx_exists(idx_pair_xlate_group)
        self.assertTrue(result)

        # Prepare for adding an entry to the database
        agent_id = data.hashstring(str(random()))
        agent_target = data.hashstring(str(random()))
        agent_program = data.hashstring(str(random()))

        # Make sure it does not exist
        result = agent.exists(agent_id, agent_target)
        self.assertFalse(bool(result))

        # Add an entry to the database
        agent.insert_row(agent_id, agent_target, agent_program)
        idx_agent = agent.exists(agent_id, agent_target)

        # Assign
        agent.assign(idx_agent, idx_pair_xlate_group)

        # Get assigned idx_pair_xlate_group for the agent group
        with db.db_query(20099) as session:
            result = session.query(Agent.idx_pair_xlate_group).filter(
                Agent.idx_agent == idx_agent).one()
        idx_new = result.idx_pair_xlate_group
        self.assertEqual(idx_pair_xlate_group, idx_new)
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    def test_idx_pair_xlate_group(self):
        """Testing method / function idx_pair_xlate_group."""
        # Create a new PairXlateGroup entry
        translation = data.hashstring(str(random()))
        pair_xlate_group.insert_row(translation)
        idx_pair_xlate_group = pair_xlate_group.exists(translation)

        # Add an entry to the database
        translation = data.hashstring(str(random()))
        agent_group.insert_row(translation)

        # Make sure it exists
        idx_agent_group = agent_group.exists(translation)

        # Assign AgentGroup
        agent_group.assign(idx_agent_group, idx_pair_xlate_group)

        # Add an entry to the database
        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)

        # Assign Agent got AgentGroup
        agent.assign(idx_agent, idx_agent_group)

        # Test
        result = agent.idx_pair_xlate_group(agent_id)
        self.assertEqual(result, idx_pair_xlate_group)
Ejemplo n.º 8
0
    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])
Ejemplo n.º 9
0
    def test_insert_row(self):
        """Testing method / function insert_row."""
        # Add an entry to the database
        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)

        # Make sure it exists
        idx_agent = agent.exists(agent_id, agent_target)

        # Verify the index exists
        result = agent.idx_exists(idx_agent)
        self.assertTrue(result)
Ejemplo n.º 10
0
    def test_exists(self):
        """Testing method / function exists."""
        # Create a translation
        agent_id = data.hashstring(str(random()))
        agent_target = data.hashstring(str(random()))
        agent_program = data.hashstring(str(random()))

        # Make sure it does not exist
        result = agent.exists(agent_id, agent_target)
        self.assertFalse(bool(result))

        # Add database row
        agent.insert_row(agent_id, agent_target, agent_program)

        # Make sure it exists
        result = agent.exists(agent_id, agent_target)
        self.assertTrue(bool(result))
Ejemplo n.º 11
0
    def test_cli_show_dump(self):
        """Testing method / function cli_show_dump."""
        # Add an entry to the database
        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)

        # Make sure it exists
        idx_agent = agent.exists(agent_id, agent_target)

        result = agent.cli_show_dump()
        for item in result:
            if item.idx_agent == idx_agent:
                self.assertEqual(item.agent_target, agent_target)
                self.assertEqual(item.agent_program, agent_program)
                break
Ejemplo n.º 12
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))
Ejemplo n.º 13
0
    def setUpClass(self):
        """Setup tables in pattoo_unittest database"""

        # Setting up arpser to be able to parse import cli commands
        subparser = self.parser.add_subparsers(dest='action')
        _Show(subparser)

        # Creates new database tables for test cli_show module
        self.tables = [
            Agent.__table__, AgentXlate.__table__, Language.__table__,
            PairXlate.__table__, PairXlateGroup.__table__
        ]
        create_tables(self.tables)

        # Test Insertions
        setup_database._insert_language()
        setup_database._insert_pair_xlate_group()
        setup_database._insert_agent_xlate()

        agent.insert_row('agent_id', 'agent_test_target', 'agent_program')
        pair_xlate.insert_row('xlate_key', 'xlate_translation', 'xlate_units',
                              1, 1)
Ejemplo n.º 14
0
    def test_assign(self):
        """Testing method / function assign."""
        # Create a new AgentGroup entry
        translation = data.hashstring(str(random()))
        agent_group.insert_row(translation)
        idx_agent_group = agent_group.exists(translation)

        # Prepare for adding an entry to the database
        agent_id = data.hashstring(str(random()))
        agent_target = data.hashstring(str(random()))
        agent_program = data.hashstring(str(random()))

        # Make sure it does not exist
        result = agent.exists(agent_id, agent_target)
        self.assertFalse(bool(result))

        # Add an entry to the database
        agent.insert_row(agent_id, agent_target, agent_program)
        idx_agent = agent.exists(agent_id, agent_target)

        # Get current idx_agent for the agent
        with db.db_query(20001) as session:
            result = session.query(Agent.idx_agent_group).filter(
                Agent.idx_agent == idx_agent).one()
        idx_original = result.idx_agent_group
        self.assertNotEqual(idx_agent_group, idx_original)

        # Assign
        agent.assign(idx_agent, idx_agent_group)

        # Get current idx_agent_group for the agent group
        with db.db_query(20099) as session:
            result = session.query(Agent.idx_agent_group).filter(
                Agent.idx_agent == idx_agent).one()
        idx_new = result.idx_agent_group
        self.assertEqual(idx_agent_group, idx_new)