def test_random_port_exception(request, redis_proc, postgresql_proc):
    """
    Check if PortForException is raised when we try to start
    next fixture on already used ports.
    """

    with pytest.raises(port_for.exceptions.PortForException):
        get_port('%s,%s' % (redis_proc.port, postgresql_proc.port))
def memcached_proc(port='?', memcached='/usr/bin/memcached', host='localhost'):
    '''
        Starts Memcached as a subprocess.

        :param int|str port: exact server port (e.g. '8000')
            or randomly selected port:
                '?' - any random available port
                '2000-3000' - random available port from a given range
                '4002,4003' - random of 4002 or 4003 ports
        :returns pytest fixture with Memcached process executor
    '''

    port = get_port(port)

    @pytest.fixture(scope='session')
    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

    return memcached_proc_fixture
Example #3
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 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
Example #4
0
    def rabbitmq_proc_fixture(request):
        """
        #. Get config.
        #. Make a temporary directory.
        #. Setup required environment variables:
        #.  * RABBITMQ_LOG_BASE
        #.  * RABBITMQ_MNESIA_BASE
        #.  * RABBITMQ_ENABLED_PLUGINS_FILE
        #.  * RABBITMQ_NODE_PORT
        #.  * RABBITMQ_NODENAME
        #. Start a rabbit server
            `<http://www.rabbitmq.com/man/rabbitmq-server.1.man.html>`_
        #. Stop rabbit server and remove temporary files after tests.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor of running rabbitmq-server
        """

        config = get_config(request)
        rabbit_confpath = config_file or request.config.getvalue('rabbit_conf')

        with open(rabbit_confpath) as configuration:
            environ = dict(
                line[:-1].split('=') for line in configuration.readlines()
            )

        rabbit_ctl = rabbit_ctl_file or config.rabbit.rabbit_ctl
        rabbit_server = server or config.rabbit.rabbit_server
        rabbit_host = host or config.rabbit.host
        rabbit_port = get_port(port or config.rabbit.port)

        rabbit_path = path('/tmp/rabbitmq.{0}/'.format(rabbit_port))
        rabbit_log = rabbit_path + 'log'
        rabbit_mnesia = rabbit_path + 'mnesia'
        rabbit_plugins = rabbit_path + 'plugins'

        environ['RABBITMQ_LOG_BASE'] = rabbit_log
        environ['RABBITMQ_MNESIA_BASE'] = rabbit_mnesia
        environ['RABBITMQ_ENABLED_PLUGINS_FILE'] = rabbit_plugins
        environ['RABBITMQ_NODE_PORT'] = str(rabbit_port)

        if node_name:
            environ['RABBITMQ_NODENAME'] = node_name

        rabbit_executor = RabbitMqExecutor(
            rabbit_server,
            rabbit_host,
            rabbit_port,
            rabbit_ctl,
            environ
        )

        request.addfinalizer(rabbit_executor.stop)

        rabbit_executor.start()

        return rabbit_executor
    def postgresql_proc_fixture(request):
        """
        #. Get config.
        #. Initialize postgresql data directory
        #. `Start a postgresqld server
            <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_
        #. Stop server and remove directory after tests.
            `See <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)
        postgresql_ctl = executable or config.postgresql.postgresql_ctl
        # check if that executable exists, as it's no on system PATH
        # only replace if executable isn't passed manually
        if not os.path.exists(postgresql_ctl) and executable is None:
            pg_bindir = subprocess.check_output(
                ['pg_config', '--bindir'], universal_newlines=True
            ).strip()
            postgresql_ctl = os.path.join(pg_bindir, 'pg_ctl')

        pg_host = host or config.postgresql.host
        pg_port = get_port(port or config.postgresql.port)
        datadir = '/tmp/postgresqldata.{0}'.format(pg_port)
        logsdir = path(request.config.getvalue('logsdir'))
        logfile_path = logsdir / '{prefix}postgresql.{port}.log'.format(
            prefix=logs_prefix,
            port=pg_port
        )

        init_postgresql_directory(
            postgresql_ctl, config.postgresql.user, datadir
        )
        postgresql_executor = PostgreSQLExecutor(
            pg_ctl=postgresql_ctl,
            host=pg_host,
            port=pg_port,
            datadir=datadir,
            unixsocketdir=config.postgresql.unixsocketdir,
            logfile=logfile_path,
            startparams=config.postgresql.startparams,
        )

        def stop_server_and_remove_directory():
            postgresql_executor.stop()
            remove_postgresql_directory(datadir)

        request.addfinalizer(stop_server_and_remove_directory)

        # start server
        postgresql_executor.start()
        if '-w' in config.postgresql.startparams:
            wait_for_postgres(logfile_path, START_INFO)

        return postgresql_executor
def test_ports_parsing(ports, ports_set):
    assert parse_ports(ports) == ports_set

    try:
        port = get_port(ports)
        if ports_set:
            assert port in ports_set
    except port_for.exceptions.PortForException:
        pass  # it may happen that some of the ports are already in use
    def elasticsearch_proc_fixture(request):
        """
        Elasticsearch process starting fixture.
        """
        config = get_config(request)
        elasticsearch_port = get_port(port)

        pidfile = '/tmp/elasticsearch.{0}.pid'.format(elasticsearch_port)
        home_path = '/tmp/elasticsearch_{0}'.format(elasticsearch_port)
        logsdir = path(request.config.getvalue('logsdir'))
        logs_path = logsdir / '{prefix}elasticsearch_{port}_logs'.format(
            prefix=logs_prefix,
            port=elasticsearch_port
        )
        work_path = '/tmp/elasticsearch_{0}_tmp'.format(elasticsearch_port)
        cluster = cluster_name or 'dbfixtures.{0}'.format(elasticsearch_port)
        multicast_enabled = str(discovery_zen_ping_multicast_enabled).lower()

        command_exec = '''
            {deamon} -p {pidfile} --http.port={port}
            --path.home={home_path}  --default.path.logs={logs_path}
            --default.path.work={work_path}
            --default.path.conf=/etc/elasticsearch
            --cluster.name={cluster}
            --network.publish_host='{network_publish_host}'
            --discovery.zen.ping.multicast.enabled={multicast_enabled}
            --index.store.type={index_store_type}
            '''.format(
            deamon=config.elasticsearch.deamon,
            pidfile=pidfile,
            port=elasticsearch_port,
            home_path=home_path,
            logs_path=logs_path,
            work_path=work_path,
            cluster=cluster,
            network_publish_host=network_publish_host,
            multicast_enabled=multicast_enabled,
            index_store_type=index_store_type

        )

        elasticsearch_executor = HTTPExecutor(
            command_exec, 'http://{host}:{port}'.format(
                host=host,
                port=elasticsearch_port
            ),
        )

        elasticsearch_executor.start()

        def finalize_elasticsearch():
            elasticsearch_executor.stop()
            shutil.rmtree(home_path)

        request.addfinalizer(finalize_elasticsearch)
        return elasticsearch_executor
Example #8
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
Example #9
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
Example #10
0
def test_ports_invalid_def(ports):
    with pytest.raises(InvalidPortsDefinition) as excinfo:
        get_port(ports)
    assert ports in str(excinfo)
Example #11
0
def test_ports_parsing_passthrough(ports, ports_set):
    assert get_port(ports) == ports_set
Example #12
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