Exemple #1
0
    def mysql_proc_fixture(request):
        """
        #. Get config.
        #. Initialize MySQL data directory
        #. `Start a mysqld server
            <https://dev.mysql.com/doc/refman/5.0/en/mysqld-safe.html>`_
        #. Stop server and remove directory after tests.
            `See <https://dev.mysql.com/doc/refman/5.6/en/mysqladmin.html>`_

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor

        """
        config = get_config(request)
        mysql_exec = executable or config.mysql.mysql_server
        mysql_admin_exec = admin_executable or config.mysql.mysql_admin
        mysql_init = init_executable or config.mysql.mysql_init
        mysql_port = get_port(port or config.mysql.port)
        mysql_host = host or config.mysql.host
        mysql_params = params or config.mysql.params

        datadir = '/tmp/mysqldata_{port}'.format(port=mysql_port)
        pidfile = '/tmp/mysql-server.{port}.pid'.format(port=mysql_port)
        unixsocket = '/tmp/mysql.{port}.sock'.format(port=mysql_port)
        logsdir = path(request.config.getvalue('logsdir'))
        logfile_path = logsdir / '{prefix}mysql-server.{port}.log'.format(
            prefix=logs_prefix, port=mysql_port)

        init_mysql_directory(mysql_init, datadir)

        mysql_executor = TCPExecutor(
            '''
            {mysql_server} --datadir={datadir} --pid-file={pidfile}
            --port={port} --socket={socket} --log-error={logfile_path}
            --skip-syslog {params}
            '''.format(
                mysql_server=mysql_exec,
                port=mysql_port,
                datadir=datadir,
                pidfile=pidfile,
                socket=unixsocket,
                logfile_path=logfile_path,
                params=mysql_params,
            ),
            host=mysql_host,
            port=mysql_port,
        )
        mysql_executor.start()

        def stop_server_and_remove_directory():
            shutdown_server = (mysql_admin_exec, '--socket=%s' % unixsocket,
                               '--user=%s' % config.mysql.user, 'shutdown')
            subprocess.check_output(' '.join(shutdown_server), shell=True)
            mysql_executor.stop()
            remove_mysql_directory(datadir)

        request.addfinalizer(stop_server_and_remove_directory)

        return mysql_executor
Exemple #2
0
    def redis_proc_fixture(request):
        """
        #. Get configs.
        #. Run redis process.
        #. Stop redis process after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)

        redis_exec = executable or config.redis.redis_exec
        redis_params = params or config.redis.params
        redis_conf = config_file or request.config.getvalue('redis_conf')
        redis_host = host or config.redis.host
        redis_port = get_port(port) or get_port(config.redis.port)

        pidfile = 'redis-server.{port}.pid'.format(port=redis_port)
        unixsocket = 'redis.{port}.sock'.format(port=redis_port)
        dbfilename = 'dump.{port}.rdb'.format(port=redis_port)
        logsdir = path(request.config.getvalue('logsdir'))
        logfile_path = logsdir / '{prefix}redis-server.{port}.log'.format(
            prefix=logs_prefix,
            port=redis_port
        )

        redis_executor = TCPExecutor(
            '''{redis_exec} {config}
            --pidfile {pidfile} --unixsocket {unixsocket}
            --dbfilename {dbfilename} --logfile {logfile_path}
            --port {port} --dir {tmpdir} {params}'''
            .format(
                redis_exec=redis_exec,
                params=redis_params,
                config=redis_conf,
                pidfile=pidfile,
                unixsocket=unixsocket,
                dbfilename=dbfilename,
                logfile_path=logfile_path,
                port=redis_port,
                tmpdir=gettempdir(),
            ),
            host=redis_host,
            port=redis_port,
        )
        redis_version = extract_version(
            os.popen('{0} --version'.format(redis_exec)).read()
        )
        cv_result = compare_version(redis_version, REQUIRED_VERSION)
        if redis_version and cv_result < 0:
            raise RedisUnsupported(
                'Your version of Redis is not supported. '
                'Consider updating to Redis {0} at least. '
                'The currently installed version of Redis: {1}.'
                .format(REQUIRED_VERSION, redis_version))
        redis_executor.start()
        request.addfinalizer(redis_executor.stop)

        return redis_executor
    def redis_proc_fixture(request):
        """
        #. Get configs.
        #. Run redis process.
        #. Stop redis process after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)

        redis_exec = executable or config.redis.redis_exec
        redis_params = params or config.redis.params
        redis_conf = config_file or request.config.getvalue('redis_conf')
        redis_host = host or config.redis.host
        redis_port = get_port(port or config.redis.port)

        pidfile = 'redis-server.{port}.pid'.format(port=redis_port)
        unixsocket = 'redis.{port}.sock'.format(port=redis_port)
        dbfilename = 'dump.{port}.rdb'.format(port=redis_port)
        logsdir = path(request.config.getvalue('logsdir'))
        logfile_path = logsdir / '{prefix}redis-server.{port}.log'.format(
            prefix=logs_prefix,
            port=redis_port
        )

        redis_executor = TCPExecutor(
            '''{redis_exec} {config}
            --pidfile {pidfile} --unixsocket {unixsocket}
            --dbfilename {dbfilename} --logfile {logfile_path}
            --port {port} {params}'''
            .format(
                redis_exec=redis_exec,
                params=redis_params,
                config=redis_conf,
                pidfile=pidfile,
                unixsocket=unixsocket,
                dbfilename=dbfilename,
                logfile_path=logfile_path,
                port=redis_port
            ),
            host=redis_host,
            port=redis_port,
        )
        redis_version = extract_version(
            os.popen('{0} --version'.format(redis_exec)).read()
        )
        cv_result = compare_version(redis_version, REQUIRED_VERSION)
        if redis_version and cv_result < 0:
            raise RedisUnsupported(
                'Your version of Redis is not supported. '
                'Consider updating to Redis {0} at least. '
                'The currently installed version of Redis: {1}.'
                .format(REQUIRED_VERSION, redis_version))
        redis_executor.start()
        request.addfinalizer(redis_executor.stop)

        return redis_executor
Exemple #4
0
    def mongo_proc_fixture(request):
        """
        #. Get config.
        #. Run a ``mongod`` process.
        #. Stop ``mongod`` process after tests.

        .. note::
            `mongod <http://docs.mongodb.org/v2.2/reference/mongod/>`_

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)

        # make a temporary directory for tests and delete it
        # if tests have been finished
        tmp = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tmp')
        if not os.path.exists(tmp):
            os.mkdir(tmp)
        tmpdir = path(mkdtemp(prefix='mongo_pytest_fixture', dir=tmp))
        request.addfinalizer(lambda: tmpdir.exists() and tmpdir.rmtree())

        mongo_exec = executable or config.mongo.mongo_exec
        mongo_params = params or config.mongo.params

        mongo_host = host or config.mongo.host
        mongo_port = get_port(port or config.mongo.port)

        logsdir = path(request.config.getvalue('logsdir'))
        mongo_logpath = logsdir / '{prefix}mongo.{port}.log'.format(
            prefix=logs_prefix,
            port=mongo_port
        )

        mongo_executor = TCPExecutor(
            '{mongo_exec} --bind_ip {host} --port {port} --dbpath {dbpath} --logpath {logpath} {params}'.format(  # noqa
                mongo_exec=mongo_exec,
                params=mongo_params,
                host=mongo_host,
                port=mongo_port,
                dbpath=tmpdir,
                logpath=mongo_logpath,
            ),
            host=mongo_host,
            port=mongo_port,
        )
        mongo_executor.start()

        request.addfinalizer(mongo_executor.stop)

        return mongo_executor
Exemple #5
0
    def mongo_proc_fixture(request):
        """
        #. Get config.
        #. Run a ``mongod`` process.
        #. Stop ``mongod`` process after tests.

        .. note::
            `mongod <http://docs.mongodb.org/v2.2/reference/mongod/>`_

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)

        # make a temporary directory for tests and delete it
        # if tests have been finished
        tmp = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tmp')
        if not os.path.exists(tmp):
            os.mkdir(tmp)
        tmpdir = path(mkdtemp(prefix='mongo_pytest_fixture', dir=tmp))
        request.addfinalizer(lambda: tmpdir.exists() and tmpdir.rmtree())

        mongo_exec = executable or config.mongo.mongo_exec
        mongo_params = params or config.mongo.params

        mongo_host = host or config.mongo.host
        mongo_port = get_port(port) or get_port(config.mongo.port)

        logsdir = path(request.config.getvalue('logsdir'))
        mongo_logpath = logsdir / '{prefix}mongo.{port}.log'.format(
            prefix=logs_prefix, port=mongo_port)

        mongo_executor = TCPExecutor(
            '{mongo_exec} --bind_ip {host} --port {port} --dbpath {dbpath} --logpath {logpath} {params}'
            .format(  # noqa
                mongo_exec=mongo_exec,
                params=mongo_params,
                host=mongo_host,
                port=mongo_port,
                dbpath=tmpdir,
                logpath=mongo_logpath,
            ),
            host=mongo_host,
            port=mongo_port,
        )
        mongo_executor.start()

        request.addfinalizer(mongo_executor.stop)

        return mongo_executor
    def memcached_proc_fixture(request):
        command = "%s -p %s" % (memcached, port)
        memcached_executor = TCPExecutor(
            command,
            host,
            port,
        )

        request.addfinalizer(memcached_executor.stop)

        memcached_executor.start()

        return memcached_executor
Exemple #7
0
    def redis_proc_fixture(request):
        """
        #. Get configs.
        #. Run redis process.
        #. Stop redis process after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)

        redis_exec = executable or config.redis.redis_exec
        redis_params = params or config.redis.params
        redis_conf = config_file or request.config.getvalue('redis_conf')
        redis_host = host or config.redis.host
        redis_port = port or config.redis.port

        pidfile = 'redis-server.{port}.pid'.format(port=redis_port)
        unixsocket = 'redis.{port}.sock'.format(port=redis_port)
        dbfilename = 'dump.{port}.rdb'.format(port=redis_port)
        logfile = 'redis-server.{port}.log'.format(port=redis_port)

        redis_executor = TCPExecutor(
            '''{redis_exec} {config}
            --pidfile {pidfile} --unixsocket {unixsocket}
            --dbfilename {dbfilename} --logfile {logfile}
            --port {port} {params}'''
            .format(
                redis_exec=redis_exec,
                params=redis_params,
                config=redis_conf,
                pidfile=pidfile,
                unixsocket=unixsocket,
                dbfilename=dbfilename,
                logfile=logfile,
                port=redis_port
            ),
            host=redis_host,
            port=redis_port,
        )
        redis_executor.start()

        request.addfinalizer(redis_executor.stop)

        return redis_executor
Exemple #8
0
    def redis_proc_fixture(request):
        """
        #. Get configs.
        #. Run redis process.
        #. Stop redis process after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)

        redis_exec = executable or config.redis.redis_exec
        redis_params = params or config.redis.params
        redis_conf = config_file or request.config.getvalue('redis_conf')
        redis_host = host or config.redis.host
        redis_port = port or config.redis.port

        pidfile = 'redis-server.{port}.pid'.format(port=redis_port)
        unixsocket = 'redis.{port}.sock'.format(port=redis_port)
        dbfilename = 'dump.{port}.rdb'.format(port=redis_port)
        logfile = 'redis-server.{port}.log'.format(port=redis_port)

        redis_executor = TCPExecutor(
            '''{redis_exec} {config}
            --pidfile {pidfile} --unixsocket {unixsocket}
            --dbfilename {dbfilename} --logfile {logfile}
            --port {port} {params}'''.format(
                redis_exec=redis_exec,
                params=redis_params,
                config=redis_conf,
                pidfile=pidfile,
                unixsocket=unixsocket,
                dbfilename=dbfilename,
                logfile=logfile,
                port=redis_port
            ),
            host=redis_host,
            port=redis_port,
        )
        redis_executor.start()

        request.addfinalizer(redis_executor.stop)

        return redis_executor
    def dynamodb_proc_fixture(request):
        """
        #. Run a ``DynamoDBLocal.jar`` process.
        #. Stop ``DynamoDBLocal.jar`` process after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        path_dynamodb_jar = path(
            dynamodb_dir or request.config.getvalue('dynamodbdir')
        ) / 'DynamoDBLocal.jar'

        if not path_dynamodb_jar.exists():
            raise JarPathException(
                'You have to provide a path to the dir with dynamodb jar file.'
            )

        dynamodb_port = get_port(port)
        delay_arg = '-delayTransientStatuses' if delay else ''
        dynamodb_executor = TCPExecutor(
            '''java
            -Djava.library.path=./DynamoDBLocal_lib
            -jar {path_dynamodb_jar}
            -inMemory
            {delay}
            -port {port}'''
            .format(
                path_dynamodb_jar=path_dynamodb_jar,
                port=dynamodb_port,
                delay=delay_arg,
            ),
            host=host,
            port=dynamodb_port,
        )
        dynamodb_executor.start()
        request.addfinalizer(dynamodb_executor.stop)
        return dynamodb_executor
Exemple #10
0
    def dynamodb_proc_fixture(request):
        """
        #. Run a ``DynamoDBLocal.jar`` process.
        #. Stop ``DynamoDBLocal.jar`` process after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        path_dynamodb_jar = path(
            dynamodb_dir or request.config.getvalue('dynamodbdir')
        ) / 'DynamoDBLocal.jar'

        if not path_dynamodb_jar.exists():
            raise JarPathException(
                'You have to provide a path to the dir with dynamodb jar file.'
            )

        dynamodb_port = get_port(port)
        delay_arg = '-delayTransientStatuses' if delay else ''
        dynamodb_executor = TCPExecutor(
            '''java
            -Djava.library.path=./DynamoDBLocal_lib
            -jar {path_dynamodb_jar}
            -inMemory
            {delay}
            -port {port}'''
            .format(
                path_dynamodb_jar=path_dynamodb_jar,
                port=dynamodb_port,
                delay=delay_arg,
            ),
            host=host,
            port=dynamodb_port,
        )
        dynamodb_executor.start()
        request.addfinalizer(dynamodb_executor.stop)
        return dynamodb_executor
Exemple #11
0
 def start(self):
     self.set_environ()
     TCPExecutor.start(self)
Exemple #12
0
 def start(self):
     self.set_environ()
     TCPExecutor.start(self)
Exemple #13
0
    def mysql_proc_fixture(request):
        """
        #. Get config.
        #. Initialize MySQL data directory
        #. `Start a mysqld server
            <https://dev.mysql.com/doc/refman/5.0/en/mysqld-safe.html>`_
        #. Stop server and remove directory after tests.
            `See <https://dev.mysql.com/doc/refman/5.6/en/mysqladmin.html>`_

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor

        """
        config = get_config(request)
        mysql_exec = executable or config.mysql.mysql_server
        mysql_admin_exec = admin_executable or config.mysql.mysql_admin
        mysql_init = init_executable or config.mysql.mysql_init
        mysql_port = get_port(port or config.mysql.port)
        mysql_host = host or config.mysql.host
        mysql_params = params or config.mysql.params

        datadir = '/tmp/mysqldata_{port}'.format(port=mysql_port)
        pidfile = '/tmp/mysql-server.{port}.pid'.format(port=mysql_port)
        unixsocket = '/tmp/mysql.{port}.sock'.format(port=mysql_port)
        logsdir = path(request.config.getvalue('logsdir'))
        logfile_path = logsdir / '{prefix}mysql-server.{port}.log'.format(
            prefix=logs_prefix,
            port=mysql_port
        )

        init_mysql_directory(mysql_init, datadir)

        mysql_executor = TCPExecutor(
            '''
            {mysql_server} --datadir={datadir} --pid-file={pidfile}
            --port={port} --socket={socket} --log-error={logfile_path}
            --skip-syslog {params}
            '''
            .format(
                mysql_server=mysql_exec,
                port=mysql_port,
                datadir=datadir,
                pidfile=pidfile,
                socket=unixsocket,
                logfile_path=logfile_path,
                params=mysql_params,
            ),
            host=mysql_host,
            port=mysql_port,
        )
        mysql_executor.start()

        def stop_server_and_remove_directory():
            shutdown_server = (
                mysql_admin_exec,
                '--socket=%s' % unixsocket,
                '--user=%s' % config.mysql.user,
                'shutdown'
            )
            subprocess.check_output(' '.join(shutdown_server), shell=True)
            mysql_executor.stop()
            remove_mysql_directory(datadir)

        request.addfinalizer(stop_server_and_remove_directory)

        return mysql_executor