def test_getinfo(self):
        """Testing function getinfo."""
        # Drop the database and create tables
        unittest_db.initialize_db()

        # Test with valid data
        result = validate.ValidateCache(data=self.data)
        data_dict = result.getinfo()

        # Check main keys
        for key, _ in self.data.items():
            self.assertEqual(self.data[key], data_dict[key])
    def test_valid(self):
        """Testing function valid."""
        # Drop the database and create tables
        unittest_db.initialize_db()

        # Test with valid data
        result = validate.ValidateCache(data=self.data)
        self.assertEqual(result.valid(), True)

        # Test with invalid data (string)
        result = validate.ValidateCache(data='string')
        self.assertEqual(result.valid(), False)
Ejemplo n.º 3
0
class TestGetDeviceAgent(unittest.TestCase):
    """Checks all functions and methods."""

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

    # Initiazlize key variables
    data = {}
    data['devicename'] = general.randomstring()
    data['id_agent'] = general.randomstring()
    data['agent'] = general.randomstring()
    data['timestamp'] = int(time.time())

    # Setup database
    unittest_db.initialize_db()
    (idx_device_good, idx_agent_good) = unittest_db.setup_db_deviceagent(data)

    # Create device object
    good_device = db_deviceagent.GetDeviceAgent(
        idx_device_good, idx_agent_good)

    def test___init__(self):
        """Testing method __init__."""
        # Test with non existent IDXDevice
        record = db_deviceagent.GetDeviceAgent('bogus', 'bogus')
        self.assertEqual(record.exists(), False)

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

        # Test with known bad value
        expected = db_deviceagent.GetDeviceAgent(None, None)
        result = expected.exists()
        self.assertEqual(result, False)

    def test_enabled(self):
        """Testing method enabled."""
        # Testing with known good value
        result = self.good_device.enabled()
        self.assertEqual(result, True)

    def test_last_timestamp(self):
        """Testing method last_timestamp."""
        # Testing with known good value
        result = self.good_device.last_timestamp()
        self.assertEqual(result, self.data['timestamp'])

    def test_idx_deviceagent(self):
        """Testing method idx_deviceagent."""
        # Testing with known good value
        result = self.good_device.idx_deviceagent()
        self.assertEqual(result, 1)
Ejemplo n.º 4
0
class TestFunctions(unittest.TestCase):
    """Checks all functions."""

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

    # Initiazlize key variables
    data = {}
    data['devicename'] = general.randomstring()
    data['id_agent'] = general.randomstring()
    data['agent'] = general.randomstring()
    data['timestamp'] = int(time.time())

    # Setup database
    unittest_db.initialize_db()
    (idx_device_good, idx_agent_good) = unittest_db.setup_db_deviceagent(data)

    def test_device_agent_exists(self):
        """Testing function device_agent_exists."""
        # Testing with known good value
        result = db_deviceagent.device_agent_exists(
            self.idx_device_good, self.idx_agent_good)
        self.assertEqual(result, True)

        # Testing with known good value
        result = db_deviceagent.device_agent_exists(None, None)
        self.assertEqual(result, False)

    def test_all_device_indices(self):
        """Testing function all_device_indices."""
        # Testing with known good value
        result = db_deviceagent.all_device_indices()
        self.assertEqual(result, [1])

    def test_device_indices(self):
        """Testing function device_indices."""
        # Testing with known good value
        result = db_deviceagent.device_indices(self.idx_agent_good)
        self.assertEqual(result, [1])

    def test_agent_indices(self):
        """Testing function agent_indices."""
        # Testing with known good value
        result = db_deviceagent.agent_indices(
            self.idx_agent_good)
        self.assertEqual(result, [1])

    def test_get_all_device_agents(self):
        """Testing function get_all_device_agents."""
        results = db_deviceagent.get_all_device_agents()
        for result in results:
            for key, _ in result.items():
                self.assertEqual(result[key], 1)
Ejemplo n.º 5
0
    def test_valid(self):
        """Testing function valid."""
        # Initialize key variables
        devicename = self.data['devicename']
        id_agent = self.data['id_agent']
        agent_name = self.data['agent']
        last_timestamp = self.data['timestamp']

        # Drop the database and create tables
        unittest_db.initialize_db()

        # Add record to the database
        record = Agent(id_agent=general.encode(id_agent),
                       name=general.encode(agent_name))
        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, 1021)

        # 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)
    def test___init__(self):
        """Testing function __init__."""
        # Initialize key variables
        directory = tempfile.mkdtemp()
        id_agent = self.data['id_agent']
        last_timestamp = self.data['timestamp']
        filepath = ('%s/%s_%s_%s.json') % (
            directory,
            last_timestamp,
            id_agent,
            general.hashstring(general.randomstring()))

        # Drop the database and create tables
        unittest_db.initialize_db()

        # Test with valid data
        result = validate.ValidateCache(data=self.data)
        self.assertEqual(result.valid(), True)

        # Test with invalid data (string)
        result = validate.ValidateCache(data='string')
        self.assertEqual(result.valid(), False)

        # Test with invalid data (string)
        data_dict = copy.deepcopy(self.data)
        data_dict.pop('agent', None)
        result = validate.ValidateCache(data=data_dict)
        self.assertEqual(result.valid(), False)

        # Write good data to file and test
        with open(filepath, 'w') as f_handle:
            json.dump(self.data, f_handle)
        result = validate.ValidateCache(filepath=filepath)
        self.assertEqual(result.valid(), True)

        #################################################################
        #################################################################
        # Add record to DeviceAgent table and test for validity with the
        # same data, which should be False
        #################################################################
        #################################################################

        unittest_db.setup_db_deviceagent(self.data)

        # Attempting to insert duplicate data should fail
        with open(filepath, 'w') as f_handle:
            json.dump(self.data, f_handle)
        result = validate.ValidateCache(filepath=filepath)
        self.assertEqual(result.valid(), False)

        #################################################################
        #################################################################
        # Test with invalid data in file
        #################################################################
        #################################################################

        # Drop the database and create tables
        unittest_db.initialize_db()

        # Write bad data to file and test
        data_dict = copy.deepcopy(self.data)
        data_dict.pop('agent', None)
        with open(filepath, 'w') as f_handle:
            json.dump(data_dict, f_handle)
        result = validate.ValidateCache(filepath=filepath)
        self.assertEqual(result.valid(), False)

        # Cleanup
        os.remove(filepath)
        os.removedirs(directory)