Esempio n. 1
0
    def test_search_by_ip(self):
        """ Test the search using an ip method of Connection. """
        create_connections(self.session)

        connections = model.Connection.search(self.session,
                                              ip='127.0.0.2',
                                              active=True)
        self.assertEqual(len(connections), 0)
        self.assertEqual(connections, [])

        connections = model.Connection.search(self.session,
                                              ip='127.0.0.1',
                                              active=True)
        self.assertEqual(len(connections), 3)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('1' ip:'127.0.0.1'"))
        self.assertTrue(connections[1].__repr__().startswith(
            "<Connection('2' ip:'127.0.0.1'"))
        self.assertTrue(connections[2].__repr__().startswith(
            "<Connection('3' ip:'127.0.0.1'"))

        connections = model.Connection.search(self.session,
                                              ip='127.0.0.1',
                                              active=True,
                                              cnt=True)
        self.assertEqual(connections, 3)
Esempio n. 2
0
    def test_by_db_name(self):
        """ Test the by_db_name method of Connection. """
        create_connections(self.session)

        connection = model.Connection.by_db_name(self.session, 'db2')
        self.assertTrue(connection.__repr__().startswith(
            "<Connection('2' ip:'127.0.0.1'"))
Esempio n. 3
0
    def test_by_db_name(self):
        """ Test the by_db_name method of Connection. """
        create_connections(self.session)

        connection = model.Connection.by_db_name(self.session, 'db2')
        self.assertTrue(
            connection.__repr__().startswith("<Connection('2' ip:'127.0.0.1'"))
Esempio n. 4
0
    def test_older_than(self):
        """ Test the older_than method of Connection. """
        create_connections(self.session)

        limit = datetime.utcnow() - timedelta(minutes=30)
        connections = model.Connection.older_than(self.session, limit)
        self.assertEqual(len(connections), 1)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('3' ip:'127.0.0.1'"))
Esempio n. 5
0
    def test_older_than(self):
        """ Test the older_than method of Connection. """
        create_connections(self.session)

        limit = datetime.utcnow() - timedelta(minutes=30)
        connections = model.Connection.older_than(self.session, limit)
        self.assertEqual(len(connections), 1)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('3' ip:'127.0.0.1'"))
Esempio n. 6
0
    def test_get_stats(self):
        """ Test the get_stats method of faitoutlib. """
        create_connections(self.session)

        output = faitoutlib.get_stats(self.session)
        self.assertEqual(
            output,
            {
                'total_connections': 4,
                'active_connections': 3,
                'unique_ip': 2,
            }
        )
Esempio n. 7
0
    def test_get_stats(self):
        """ Test the get_stats method of faitoutlib. """
        create_connections(self.session)

        output = faitoutlib.get_stats(self.session)
        self.assertEqual(
            output,
            {
                'total_connections': 4,
                'active_connections': 3,
                'unique_ip': 2,
            }
        )
Esempio n. 8
0
    def test_get_ip_stats(self):
        """ Test the get_ip_stats method of faitoutlib. """
        create_connections(self.session)

        output = faitoutlib.get_ip_stats(self.session, '127.0.0.1')
        self.assertEqual(
            output.keys(), ['total_connections', 'active_connections'])
        self.assertEqual(
            output['total_connections'], 3)
        self.assertEqual(
            len(output['active_connections']), 3)

        output = faitoutlib.get_ip_stats(self.session, '127.0.0.2')
        self.assertEqual(
            output.keys(), ['total_connections', 'active_connections'])
        self.assertEqual(
            output['total_connections'], 1)
        self.assertEqual(
            len(output['active_connections']), 0)
Esempio n. 9
0
    def test_drop_connection(self):
        """ Test the drop_connection method of faitoutlib. """
        create_connections(self.session)

        faitoutlib.drop_database = mock.MagicMock()

        # Fails as dbTest does not exists
        self.assertRaises(
            faitoutlib.NoDatabaseException,
            faitoutlib.drop_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            db_name='dbTest'
            )

        # Fails as db4 is not active
        self.assertRaises(
            faitoutlib.NoDatabaseException,
            faitoutlib.drop_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            db_name='db4'
            )

        # Fails as db1 was requested by 127.0.0.1
        self.assertRaises(
            faitoutlib.WrongOriginException,
            faitoutlib.drop_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            db_name='db1'
            )

        output = faitoutlib.drop_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            db_name='db1'
            )
        self.assertEqual(output, 'Database db1 has been dropped')
Esempio n. 10
0
    def test_drop_connection(self):
        """ Test the drop_connection method of faitoutlib. """
        create_connections(self.session)

        faitoutlib.drop_database = mock.MagicMock()

        # Fails as dbTest does not exists
        self.assertRaises(
            faitoutlib.NoDatabaseException,
            faitoutlib.drop_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            db_name='dbTest'
            )

        # Fails as db4 is not active
        self.assertRaises(
            faitoutlib.NoDatabaseException,
            faitoutlib.drop_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            db_name='db4'
            )

        # Fails as db1 was requested by 127.0.0.1
        self.assertRaises(
            faitoutlib.WrongOriginException,
            faitoutlib.drop_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            db_name='db1'
            )

        output = faitoutlib.drop_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            db_name='db1'
            )
        self.assertEqual(output, 'Database db1 has been dropped')
Esempio n. 11
0
    def test_by_ip(self):
        """ Test the by_ip method of Connection. """
        create_connections(self.session)

        connections = model.Connection.by_ip(self.session, '127.0.0.2')
        self.assertEqual(len(connections), 0)
        self.assertEqual(connections, [])

        connections = model.Connection.by_ip(self.session, '127.0.0.1')
        self.assertEqual(len(connections), 3)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('1' ip:'127.0.0.1'"))
        self.assertTrue(connections[1].__repr__().startswith(
            "<Connection('2' ip:'127.0.0.1'"))
        self.assertTrue(connections[2].__repr__().startswith(
            "<Connection('3' ip:'127.0.0.1'"))

        connections = model.Connection.by_ip(
            self.session, '127.0.0.1', cnt=True)
        self.assertEqual(connections, 3)
Esempio n. 12
0
    def test_search(self):
        """ Test the search method of Connection. """
        create_connections(self.session)

        connections = model.Connection.search(self.session, active=True)
        self.assertEqual(len(connections), 3)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('1' ip:'127.0.0.1'"))
        self.assertTrue(connections[1].__repr__().startswith(
            "<Connection('2' ip:'127.0.0.1'"))
        self.assertTrue(connections[2].__repr__().startswith(
            "<Connection('3' ip:'127.0.0.1'"))

        connections = model.Connection.search(self.session, active=False)
        self.assertEqual(len(connections), 1)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('4' ip:'127.0.0.2'"))

        connections = model.Connection.search(
            self.session, active=True, cnt=True)
        self.assertEqual(connections, 3)
Esempio n. 13
0
    def test_search(self):
        """ Test the search method of Connection. """
        create_connections(self.session)

        connections = model.Connection.search(self.session, active=True)
        self.assertEqual(len(connections), 3)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('1' ip:'127.0.0.1'"))
        self.assertTrue(connections[1].__repr__().startswith(
            "<Connection('2' ip:'127.0.0.1'"))
        self.assertTrue(connections[2].__repr__().startswith(
            "<Connection('3' ip:'127.0.0.1'"))

        connections = model.Connection.search(self.session, active=False)
        self.assertEqual(len(connections), 1)
        self.assertTrue(connections[0].__repr__().startswith(
            "<Connection('4' ip:'127.0.0.2'"))

        connections = model.Connection.search(self.session,
                                              active=True,
                                              cnt=True)
        self.assertEqual(connections, 3)
Esempio n. 14
0
    def test_get_new_connection(self):
        """ Test the get_new_connection method of faitoutlib. """
        create_connections(self.session)

        faitoutlib.create_database = mock.MagicMock()

        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.1', active=True, cnt=True), 3)
        # Fails as 127.0.0.1 already has 3 active connections
        self.assertRaises(
            faitoutlib.TooManyConnectionException,
            faitoutlib.get_new_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='text',
            unlimited=False
            )
        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.1', active=True, cnt=True), 3)

        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.2', active=True, cnt=True), 0)
        connection = faitoutlib.get_new_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='text',
            unlimited=False
            )
        self.assertTrue(connection.startswith('postgresql://'))
        self.assertTrue('localhost:5432' in connection)
        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.2', active=True, cnt=True), 1)

        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.2', active=True, cnt=True), 1)
        connection = faitoutlib.get_new_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='json',
            unlimited=False
            )
        self.assertEqual(
            sorted(connection.keys()),
            ['dbname', 'host', 'password', 'port', 'username'])
        self.assertEqual(connection['host'], 'localhost')
        self.assertEqual(connection['port'], 5432)
        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.2', active=True, cnt=True), 2)

        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.1', active=True, cnt=True), 3)
        connection = faitoutlib.get_new_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='json',
            unlimited=True
            )
        self.assertEqual(model.Connection.search(
            self.session, ip='127.0.0.1', active=True, cnt=True), 4)
        self.assertEqual(
            sorted(connection.keys()),
            ['dbname', 'host', 'password', 'port', 'username'])
        self.assertEqual(connection['host'], 'localhost')
        self.assertEqual(connection['port'], 5432)
Esempio n. 15
0
    def test_cnt_unique_ip(self):
        """ Test the cnt_unique_ip method of Connection. """
        create_connections(self.session)

        unique_ip = model.Connection.cnt_unique_ip(self.session)
        self.assertEqual(unique_ip, 2)
Esempio n. 16
0
    def test_get_new_connection(self):
        """ Test the get_new_connection method of faitoutlib. """
        create_connections(self.session)

        faitoutlib.create_database = mock.MagicMock()

        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.1', cnt=True), 3)
        # Fails as 127.0.0.1 already has 3 active connections
        self.assertRaises(
            faitoutlib.TooManyConnectionException,
            faitoutlib.get_new_connection,
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='text',
            unlimited=False
            )
        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.1', cnt=True), 3)

        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.2', cnt=True), 0)
        connection = faitoutlib.get_new_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='text',
            unlimited=False
            )
        self.assertTrue(connection.startswith('postgresql://'))
        self.assertTrue('localhost:5432' in connection)
        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.2', cnt=True), 1)

        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.2', cnt=True), 1)
        connection = faitoutlib.get_new_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.2',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='json',
            unlimited=False
            )
        self.assertEqual(
            sorted(connection.keys()),
            ['dbname', 'host', 'password', 'port', 'username'])
        self.assertEqual(connection['host'], 'localhost')
        self.assertEqual(connection['port'], 5432)
        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.2', cnt=True), 2)

        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.1', cnt=True), 3)
        connection = faitoutlib.get_new_connection(
            self.session,
            admin_engine=None,
            remote_ip='127.0.0.1',
            host='localhost',
            port=5432,
            max_con=3,
            outformat='json',
            unlimited=True
            )
        self.assertEqual(model.Connection.by_ip(
            self.session, '127.0.0.1', cnt=True), 4)
        self.assertEqual(
            sorted(connection.keys()),
            ['dbname', 'host', 'password', 'port', 'username'])
        self.assertEqual(connection['host'], 'localhost')
        self.assertEqual(connection['port'], 5432)
Esempio n. 17
0
    def test_cnt_unique_ip(self):
        """ Test the cnt_unique_ip method of Connection. """
        create_connections(self.session)

        unique_ip = model.Connection.cnt_unique_ip(self.session)
        self.assertEqual(unique_ip, 2)