Esempio n. 1
0
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
Esempio n. 2
0
    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)
Esempio n. 3
0
 def test___init__(self):
     """Testing function __init__."""
     # Test with non existent DeviceIDX
     record = db_device.GetDevice('bogus')
     self.assertEqual(record.exists(), False)
     self.assertEqual(record.devicename(), None)
     self.assertEqual(record.enabled(), None)
     self.assertEqual(record.description(), None)
     self.assertEqual(record.idx_device(), None)
Esempio n. 4
0
    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
Esempio n. 5
0
    def valid(self):
        """Check if data keys are OK.

        Args:
            None

        Returns:
            valid: True if valid

        """
        # Initialize key variables
        valid = True

        # Return if instantiation tests have failed
        if self._valid is False:
            valid = False
            return valid

        # Assign other values
        timestamp = int(self.data['timestamp'])
        id_agent = self.data['id_agent']
        devicename = self.data['devicename']

        # Check if there is a duplicate entry for this id_agent
        if db_agent.id_agent_exists(id_agent) is not False:
            idx_agent = db_agent.GetIDAgent(id_agent).idx_agent()

            # Check if device exists
            if db_device.devicename_exists(devicename) is True:
                idx_device = db_device.GetDevice(devicename).idx_device()

                # Check for device / agent entry existence
                if db_deviceagent.device_agent_exists(
                        idx_device, idx_agent) is True:
                    # Check if this device / agent has been updated before
                    last_timesamp = db_deviceagent.GetDeviceAgent(
                        idx_device, idx_agent).last_timestamp()

                    # Validate
                    if timestamp <= last_timesamp:
                        log_message = (
                            'Data for id_agent %s, devicename %s '
                            'at timestamp %s '
                            'is already found in database.'
                            '') % (id_agent, devicename, timestamp)
                        log.log2warning(1113, log_message)
                        valid = False

        # Return
        return valid
Esempio n. 6
0
def devicename_agents(devicename, id_agent):
    """Get last contact data from the DB.

    Args:
        devicename: Device table devicename
        id_agent: Agent table id_agent

    Returns:
        data: JSON data for the selected agent

    """
    # Initialize key variables
    data = []

    # Get starting timestamp
    secondsago = general.integerize(request.args.get('secondsago'))
    timestamp = general.integerize(request.args.get('ts_start'))
    ts_start = _ts_start(secondsago, timestamp)

    # Get idx_device and idx_agent
    device = db_device.GetDevice(devicename)
    if device.exists() is True:
        # Device Found
        idx_device = device.idx_device()

        # Now find idx_agent
        agent = db_agent.GetIDAgent(id_agent)
        if agent.exists() is True:
            idx_agent = agent.idx_agent()

        # Now get the idx_deviceagent
        deviceagent = db_deviceagent.GetDeviceAgent(idx_device, idx_agent)
        if deviceagent.exists() is True:
            idx_deviceagent = deviceagent.idx_deviceagent()

            # Now get the data
            data = db_data.last_contacts_by_device(
                int(idx_deviceagent), int(ts_start))

    # Return
    return jsonify(data)
Esempio n. 7
0
class TestGetDevice(unittest.TestCase):
    """Checks all functions and methods."""

    #########################################################################
    # General object setup
    #########################################################################

    # Setup database based on the config
    database = unittest_setup_db.TestData()

    # Define expected values
    expected = {}
    expected['idx_device'] = database.idx_device()
    expected['devicename'] = database.devicename()
    expected['description'] = database.device_description()
    expected['enabled'] = True
    expected['exists'] = True

    # Create device object
    good_device = db_device.GetDevice(expected['devicename'])

    def test___init__(self):
        """Testing function __init__."""
        # Test with non existent DeviceIDX
        record = db_device.GetDevice('bogus')
        self.assertEqual(record.exists(), False)
        self.assertEqual(record.devicename(), None)
        self.assertEqual(record.enabled(), None)
        self.assertEqual(record.description(), None)
        self.assertEqual(record.idx_device(), None)

    def test_idx_device(self):
        """Testing method idx_device."""
        # Testing with known good value
        result = self.good_device.idx_device()
        self.assertEqual(result, self.expected['idx_device'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_device.devicename()
        self.assertNotEqual(result, expected)

    def test_exists(self):
        """Testing function exists."""
        # Testing with known good value
        result = self.good_device.exists()
        self.assertEqual(result, True)

    def test_description(self):
        """Testing function description."""
        # Testing with known good value
        result = self.good_device.description()
        self.assertEqual(result, self.expected['description'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_device.description()
        self.assertNotEqual(result, expected)

    def test_enabled(self):
        """Testing function enabled."""
        # Testing with known good value
        result = self.good_device.enabled()
        self.assertEqual(result, self.expected['enabled'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_device.enabled()
        self.assertNotEqual(result, expected)

    def test_everything(self):
        """Testing method everything."""
        # Testing with known good value
        result = self.good_device.everything()
        for key, _ in self.expected.items():
            self.assertEqual(result[key], self.expected[key])

        # Test the number and names of keys
        keys = ['idx_device', 'devicename', 'description', 'enabled', 'exists']
        self.assertEqual(len(result), len(keys))
        for key in keys:
            self.assertEqual(key in result, True)