Beispiel #1
0
    def test_host_only(self, mock_find_entrypoint, mock_ConnectionPool,
                       mock_StrictRedis):
        result = database.initialize(dict(host='10.0.0.1'))

        self.assertEqual(result, 'db_handle')
        self.assertFalse(mock_ConnectionPool.called)
        mock_StrictRedis.assert_called_once_with(host='10.0.0.1')
Beispiel #2
0
    def test_cpool_options(self, mock_find_entrypoint, mock_ConnectionPool,
                           mock_StrictRedis):
        entrypoints = self.make_entrypoints(
            mock_find_entrypoint,
            connection='connection_fake',
            parser='parser_fake',
        )

        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'db': '5',
            'password': '******',
            'socket_timeout': '600',
            'unix_socket_path': '/tmp/redis',
            'connection_pool.connection_class': 'connection',
            'connection_pool.max_connections': '50',
            'connection_pool.parser_class': 'parser',
            'connection_pool.other': 'value',
        })

        self.assertEqual(result, 'db_handle')
        mock_StrictRedis.assert_called_once_with(connection_pool='conn_pool')
        mock_ConnectionPool.assert_called_once_with(
            host='10.0.0.1',
            port=1234,
            db=5,
            password='******',
            socket_timeout=600,
            unix_socket_path='/tmp/redis',
            connection_class='connection_fake',
            max_connections=50,
            parser_class='parser_fake',
            other='value',
        )
Beispiel #3
0
    def test_host_only(self, mock_find_entrypoint, mock_ConnectionPool,
                       mock_StrictRedis):
        result = database.initialize(dict(host='10.0.0.1'))

        self.assertEqual(result, 'db_handle')
        self.assertFalse(mock_ConnectionPool.called)
        mock_StrictRedis.assert_called_once_with(host='10.0.0.1')
Beispiel #4
0
def parse_config(config):
    """
    Parses the connection configuration file.  Establishes a
    connection to the database and returns it, along with the limits
    key and the control channel name, as a tuple.

    :param config: Name of the configuration file, for connecting to
                   the Redis database.
    """

    # Instantiate the config parser
    cp = ConfigParser.SafeConfigParser()

    # Read the limits from the file
    cp.read(config)

    # Make sure we have a connection section
    if not cp.has_section('connection'):
        raise Exception("Missing [connection] section from %r" % config)

    # Get the database configuration...
    db_config = dict(cp.items('connection'))
    limits_key = db_config.pop('limits_key', 'limits')
    control_channel = db_config.pop('control_channel', 'control')

    # Get the database connection
    return database.initialize(db_config), limits_key, control_channel
Beispiel #5
0
    def test_cpool_options(self, mock_find_entrypoint, mock_ConnectionPool,
                           mock_StrictRedis):
        entrypoints = self.make_entrypoints(
            mock_find_entrypoint,
            connection='connection_fake',
            parser='parser_fake',
        )

        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'db': '5',
            'password': '******',
            'socket_timeout': '600',
            'unix_socket_path': '/tmp/redis',
            'connection_pool.connection_class': 'connection',
            'connection_pool.max_connections': '50',
            'connection_pool.parser_class': 'parser',
            'connection_pool.other': 'value',
        })

        self.assertEqual(result, 'db_handle')
        mock_StrictRedis.assert_called_once_with(connection_pool='conn_pool')
        mock_ConnectionPool.assert_called_once_with(
            host='10.0.0.1',
            port=1234,
            db=5,
            password='******',
            socket_timeout=600,
            unix_socket_path='/tmp/redis',
            connection_class='connection_fake',
            max_connections=50,
            parser_class='parser_fake',
            other='value',
        )
Beispiel #6
0
    def test_unixpath_only(self, mock_find_entrypoint, mock_ConnectionPool,
                           mock_StrictRedis):
        result = database.initialize(dict(unix_socket_path='/tmp/socket'))

        self.assertEqual(result, 'db_handle')
        self.assertFalse(mock_ConnectionPool.called)
        mock_StrictRedis.assert_called_once_with(
            unix_socket_path='/tmp/socket')
Beispiel #7
0
    def test_unixpath_only(self, mock_find_entrypoint, mock_ConnectionPool,
                           mock_StrictRedis):
        result = database.initialize(dict(unix_socket_path='/tmp/socket'))

        self.assertEqual(result, 'db_handle')
        self.assertFalse(mock_ConnectionPool.called)
        mock_StrictRedis.assert_called_once_with(
            unix_socket_path='/tmp/socket')
Beispiel #8
0
    def test_alt_client(self, mock_find_entrypoint, mock_ConnectionPool,
                        mock_StrictRedis):
        entrypoints = self.make_entrypoints(
            mock_find_entrypoint,
            client=mock.Mock(return_value='alt_handle'),
        )

        result = database.initialize(dict(host='10.0.0.1',
                                          redis_client='client'))

        self.assertEqual(result, 'alt_handle')
        self.assertFalse(mock_ConnectionPool.called)
        self.assertFalse(mock_StrictRedis.called)
        entrypoints['client'].assert_called_once_with(host='10.0.0.1')
Beispiel #9
0
    def test_alt_client(self, mock_find_entrypoint, mock_ConnectionPool,
                        mock_StrictRedis):
        entrypoints = self.make_entrypoints(
            mock_find_entrypoint,
            client=mock.Mock(return_value='alt_handle'),
        )

        result = database.initialize(
            dict(host='10.0.0.1', redis_client='client'))

        self.assertEqual(result, 'alt_handle')
        self.assertFalse(mock_ConnectionPool.called)
        self.assertFalse(mock_StrictRedis.called)
        entrypoints['client'].assert_called_once_with(host='10.0.0.1')
Beispiel #10
0
    def test_cpool_host(self, mock_find_entrypoint, mock_ConnectionPool,
                        mock_StrictRedis):
        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'connection_pool.other': 'value',
        })

        self.assertEqual(result, 'db_handle')
        mock_StrictRedis.assert_called_once_with(connection_pool='conn_pool')
        mock_ConnectionPool.assert_called_once_with(
            host='10.0.0.1',
            port=1234,
            other='value',
            connection_class=redis.Connection,
        )
Beispiel #11
0
    def test_cpool_host(self, mock_find_entrypoint, mock_ConnectionPool,
                        mock_StrictRedis):
        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'connection_pool.other': 'value',
        })

        self.assertEqual(result, 'db_handle')
        mock_StrictRedis.assert_called_once_with(connection_pool='conn_pool')
        mock_ConnectionPool.assert_called_once_with(
            host='10.0.0.1',
            port=1234,
            other='value',
            connection_class=redis.Connection,
        )
Beispiel #12
0
    def test_cpool_unixsock(self, mock_find_entrypoint, mock_ConnectionPool,
                            mock_StrictRedis):
        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'unix_socket_path': '/tmp/redis',
            'connection_pool.other': 'value',
        })

        self.assertEqual(result, 'db_handle')
        mock_StrictRedis.assert_called_once_with(connection_pool='conn_pool')
        mock_ConnectionPool.assert_called_once_with(
            path='/tmp/redis',
            other='value',
            connection_class=redis.UnixDomainSocketConnection,
        )
Beispiel #13
0
    def test_cpool_unixsock(self, mock_find_entrypoint, mock_ConnectionPool,
                            mock_StrictRedis):
        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'unix_socket_path': '/tmp/redis',
            'connection_pool.other': 'value',
        })

        self.assertEqual(result, 'db_handle')
        mock_StrictRedis.assert_called_once_with(connection_pool='conn_pool')
        mock_ConnectionPool.assert_called_once_with(
            path='/tmp/redis',
            other='value',
            connection_class=redis.UnixDomainSocketConnection,
        )
Beispiel #14
0
    def __init__(self, app, local_conf):
        """
        Initialize the turnstile middleware.  Saves the configuration
        and sets up the list of preprocessors, connects to the
        database, and initiates the control daemon thread.
        """

        # Save the application
        self.app = app
        self.limits = []
        self.mapper = None

        # Split up the configuration into groups of related variables
        self.config = {
            None: {
                'status': '413 Request Entity Too Large',
                },
            }
        for key, value in local_conf.items():
            outer, _sep, inner = key.partition('.')

            # Deal with prefix-less keys
            if not inner:
                outer, inner = None, outer

            # Make sure we have a place to put them
            self.config.setdefault(outer, {})
            self.config[outer][inner] = value

        # Set up request preprocessors
        self.preprocessors = []
        for preproc in self.config[None].get('preprocess', '').split():
            # Allow ImportError to bubble up
            self.preprocessors.append(utils.import_class(preproc))

        # Next, let's configure redis
        redis_args = self.config.get('redis', {})
        self.db = database.initialize(redis_args)

        # And start up the control daemon
        control_args = self.config.get('control', {})
        self.control_daemon = database.ControlDaemon(self.db, self,
                                                     control_args)
Beispiel #15
0
    def test_connection_pool_unix_custom_pool_custom_class(self):
        self.stubs.Set(redis, "ConnectionPool", tests.GenericFakeClass)
        config = {
            "host": "example.com",
            "port": "1234",
            "db": "5",
            "password": "******",
            "socket_timeout": "321",
            "unix_socket_path": "/tmp/redis",
            "connection_pool": "FakeConnectionPool",
            "connection_pool.connection_class": "FakeConnection",
            "connection_pool.max_connections": "100",
            "connection_pool.parser_class": "FakeParser",
            "connection_pool.arg1": "arg1",
            "connection_pool.arg2": "2",
        }
        db = database.initialize(config)

        self.assertEqual(db._args, ())
        self.assertEqual(len(db._kwargs), 1)
        self.assertIn("connection_pool", db._kwargs)
        self.assertIsInstance(db._kwargs["connection_pool"], FakeConnectionPool)

        conn_pool = db._kwargs["connection_pool"]
        self.assertEqual(conn_pool.args, ())
        self.assertEqual(
            conn_pool.kwargs,
            dict(
                host="example.com",
                port=1234,
                unix_socket_path="/tmp/redis",
                db=5,
                password="******",
                socket_timeout=321,
                connection_class=FakeConnection,
                max_connections=100,
                parser_class=FakeParser,
                arg1="arg1",
                arg2="2",
            ),
        )
Beispiel #16
0
    def test_all_options(self, mock_find_entrypoint, mock_ConnectionPool,
                         mock_StrictRedis):
        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'db': '5',
            'password': '******',
            'socket_timeout': '600',
            'unix_socket_path': '/tmp/redis',
        })

        self.assertEqual(result, 'db_handle')
        self.assertFalse(mock_ConnectionPool.called)
        mock_StrictRedis.assert_called_once_with(
            host='10.0.0.1',
            port=1234,
            db=5,
            password='******',
            socket_timeout=600,
            unix_socket_path='/tmp/redis',
        )
Beispiel #17
0
    def get_database(self, override=None):
        """
        Convenience function for obtaining a handle to the Redis
        database.  By default, uses the connection options from the
        '[redis]' section.  However, if the override parameter is
        given, it specifies a section containing overrides for the
        Redis connection info; the keys will all be prefixed with
        'redis.'.  For example, in the following configuration file:

            [redis]
            host = 10.0.0.1
            password = s3cureM3!

            [control]
            redis.host = 127.0.0.1

        A call to get_database() would return a handle for the redis
        database on 10.0.0.1, while a call to get_database('control')
        would return a handle for the redis database on 127.0.0.1; in
        both cases, the database password would be 's3cureM3!'.
        """

        # Grab the database connection arguments
        redis_args = self['redis']

        # If we have an override, read some overrides from that
        # section
        if override:
            redis_args = redis_args.copy()
            for key, value in self[override].items():
                if not key.startswith('redis.'):
                    continue
                key = key[len('redis.'):]
                if value:
                    redis_args[key] = value
                else:
                    redis_args.pop(key, None)

        # Return the redis database connection
        return database.initialize(redis_args)
Beispiel #18
0
    def get_database(self, override=None):
        """
        Convenience function for obtaining a handle to the Redis
        database.  By default, uses the connection options from the
        '[redis]' section.  However, if the override parameter is
        given, it specifies a section containing overrides for the
        Redis connection info; the keys will all be prefixed with
        'redis.'.  For example, in the following configuration file:

            [redis]
            host = 10.0.0.1
            password = s3cureM3!

            [control]
            redis.host = 127.0.0.1

        A call to get_database() would return a handle for the redis
        database on 10.0.0.1, while a call to get_database('control')
        would return a handle for the redis database on 127.0.0.1; in
        both cases, the database password would be 's3cureM3!'.
        """

        # Grab the database connection arguments
        redis_args = self['redis']

        # If we have an override, read some overrides from that
        # section
        if override:
            redis_args = redis_args.copy()
            for key, value in self[override].items():
                if not key.startswith('redis.'):
                    continue
                key = key[len('redis.'):]
                if value:
                    redis_args[key] = value
                else:
                    redis_args.pop(key, None)

        # Return the redis database connection
        return database.initialize(redis_args)
Beispiel #19
0
    def test_all_options(self, mock_find_entrypoint, mock_ConnectionPool,
                         mock_StrictRedis):
        result = database.initialize({
            'host': '10.0.0.1',
            'port': '1234',
            'db': '5',
            'password': '******',
            'socket_timeout': '600',
            'unix_socket_path': '/tmp/redis',
            'extra_config': 'extra_value',
        })

        self.assertEqual(result, 'db_handle')
        self.assertFalse(mock_ConnectionPool.called)
        mock_StrictRedis.assert_called_once_with(
            host='10.0.0.1',
            port=1234,
            db=5,
            password='******',
            socket_timeout=600,
            unix_socket_path='/tmp/redis',
            extra_config='extra_value',
        )
Beispiel #20
0
    def test_unix_connection(self):
        config = dict(unix_socket_path="/tmp/redis", db="5", password="******", socket_timeout="321")
        db = database.initialize(config)

        self.assertEqual(db._args, ())
        self.assertEqual(db._kwargs, dict(unix_socket_path="/tmp/redis", db=5, password="******", socket_timeout=321))
Beispiel #21
0
 def test_missing_connection(self):
     with self.assertRaises(redis.ConnectionError):
         db = database.initialize({})
Beispiel #22
0
    def test_host_connection(self):
        config = dict(host="example.com", port="1234", db="5", password="******", socket_timeout="321")
        db = database.initialize(config)

        self.assertEqual(db._args, ())
        self.assertEqual(db._kwargs, dict(host="example.com", port=1234, db=5, password="******", socket_timeout=321))
Beispiel #23
0
    def test_host_minimal(self):
        config = dict(host="example.com")
        db = database.initialize(config)

        self.assertEqual(db._args, ())
        self.assertEqual(db._kwargs, dict(host="example.com"))
Beispiel #24
0
    def test_unix_minimal(self):
        config = dict(unix_socket_path="/tmp/redis")
        db = database.initialize(config)

        self.assertEqual(db._args, ())
        self.assertEqual(db._kwargs, dict(unix_socket_path="/tmp/redis"))