Beispiel #1
0
    def elasticsearch_proc_fixture(
            request: FixtureRequest,
            tmp_path_factory: TempPathFactory) -> ElasticSearchExecutor:
        """Elasticsearch process starting fixture."""
        config = return_config(request)
        elasticsearch_host = host or config["host"]
        elasticsearch_executable = executable or config["executable"]

        elasticsearch_port = get_port(port) or get_port(config["port"])
        elasticsearch_transport_port = get_port(
            transport_tcp_port) or get_port(config["transport_tcp_port"])

        elasticsearch_cluster_name = (
            cluster_name or config["cluster_name"]
            or f"elasticsearch_cluster_{elasticsearch_port}")
        elasticsearch_index_store_type = index_store_type or config[
            "index_store_type"]
        elasticsearch_network_publish_host = network_publish_host or config[
            "network_publish_host"]
        tmpdir = tmp_path_factory.mktemp(
            f"pytest-elasticsearch-{request.fixturename}")

        logs_path = tmpdir / "logs"

        pidfile = tmpdir / f"elasticsearch.{elasticsearch_port}.pid"
        work_path = tmpdir / f"workdir_{elasticsearch_port}"

        elasticsearch_executor = ElasticSearchExecutor(
            elasticsearch_executable,
            elasticsearch_host,
            elasticsearch_port,
            elasticsearch_transport_port,
            pidfile,
            logs_path,
            work_path,
            elasticsearch_cluster_name,
            elasticsearch_network_publish_host,
            elasticsearch_index_store_type,
            timeout=60,
        )

        elasticsearch_executor.start()
        yield elasticsearch_executor
        try:
            elasticsearch_executor.stop()
        except ProcessExitedWithError:
            pass
        shutil.rmtree(work_path)
        shutil.rmtree(logs_path)
Beispiel #2
0
def test_redis_exec_configuration(request: FixtureRequest,
                                  tmp_path_factory: TempPathFactory, parameter,
                                  config_option, value):
    """
    Check if RedisExecutor properly processes configuration options.

    Improperly set options won't be set in redis,
    and we won't be able to read it out of redis.
    """
    config = get_config(request)
    tmpdir = tmp_path_factory.mktemp(
        f"pytest-redis-test-test_redis_exec_configuration")
    redis_exec = RedisExecutor(
        executable=config["exec"],
        databases=4,
        redis_timeout=config["timeout"],
        loglevel=config["loglevel"],
        port=get_port(None),
        host=config["host"],
        timeout=30,
        datadir=tmpdir,
        **parameter,
    )
    with redis_exec:
        redis_client = redis.StrictRedis(redis_exec.host, redis_exec.port, 0)
        assert redis_client.config_get(config_option) == {config_option: value}
Beispiel #3
0
    def redis_proc_fixture(request: FixtureRequest,
                           tmp_path_factory: TempPathFactory):
        """
        Fixture for pytest-redis.

        #. Get configs.
        #. Run redis process.
        #. Stop redis process after tests.

        :param request: fixture request object
        :param tmpdir_factory:
        :rtype: pytest_redis.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)
        redis_exec = executable or config["exec"]
        rdbcompression = config[
            "compression"] if compression is None else compression
        rdbchecksum = config["rdbchecksum"] if checksum is None else checksum

        if datadir:
            redis_datadir = Path(datadir)
        elif config["datadir"]:
            redis_datadir = Path(config["datadir"])
        else:
            redis_datadir = tmp_path_factory.mktemp(
                f"pytest-redis-{request.fixturename}")

        redis_executor = RedisExecutor(
            executable=redis_exec,
            databases=db_count or config["db_count"],
            redis_timeout=timeout or config["timeout"],
            loglevel=loglevel or config["loglevel"],
            rdbcompression=rdbcompression,
            rdbchecksum=rdbchecksum,
            syslog_enabled=syslog or config["syslog"],
            save=save or config["save"],
            host=host or config["host"],
            port=get_port(port) or get_port(config["port"]),
            timeout=60,
            datadir=redis_datadir,
        )
        redis_executor.start()
        request.addfinalizer(redis_executor.stop)

        return redis_executor
Beispiel #4
0
def test_too_long_unixsocket(request: FixtureRequest,
                             tmp_path_factory: TempPathFactory):
    """Check handling of misconfigured redis executable path."""
    config = get_config(request)
    tmpdir = tmp_path_factory.mktemp(f"x" * 110)
    with pytest.raises(UnixSocketTooLong):
        RedisExecutor(
            config["exec"],
            databases=4,
            redis_timeout=config["timeout"],
            loglevel=config["loglevel"],
            port=get_port(None),
            host=config["host"],
            timeout=30,
            datadir=tmpdir,
        ).start()
Beispiel #5
0
def test_not_existing_redis(request: FixtureRequest,
                            tmp_path_factory: TempPathFactory):
    """Check handling of misconfigured redis executable path."""
    config = get_config(request)
    tmpdir = tmp_path_factory.mktemp(
        f"pytest-redis-test-test_not_existing_redis")
    with pytest.raises(RedisMisconfigured):
        RedisExecutor(
            "/not/redis/here/redis-server",
            databases=4,
            redis_timeout=config["timeout"],
            loglevel=config["loglevel"],
            port=get_port(None),
            host=config["host"],
            timeout=30,
            datadir=tmpdir,
        ).start()
Beispiel #6
0
def test_old_redis_version(request: FixtureRequest,
                           tmp_path_factory: TempPathFactory, version):
    """Test how fixture behaves in case of old redis version."""
    config = get_config(request)
    tmpdir = tmp_path_factory.mktemp(
        f"pytest-redis-test-test_old_redis_version")
    with mock.patch("os.popen", lambda *args: StringIO(version)):
        with pytest.raises(RedisUnsupported):
            RedisExecutor(
                config["exec"],
                databases=4,
                redis_timeout=config["timeout"],
                loglevel=config["loglevel"],
                port=get_port(None),
                host=config["host"],
                timeout=30,
                datadir=tmpdir,
            ).start()
Beispiel #7
0
def test_unsupported_version(request: FixtureRequest) -> None:
    """Check that the error gets raised on unsupported postgres version."""
    config = get_config(request)
    port = get_port(config["port"])
    assert port is not None
    executor = PatchedPostgreSQLExecutor(
        executable=config["exec"],
        host=config["host"],
        port=port,
        datadir="/tmp/error",
        unixsocketdir=config["unixsocketdir"],
        logfile="/tmp/version.error.log",
        startparams=config["startparams"],
        dbname="random_name",
    )

    with pytest.raises(PostgreSQLUnsupported):
        executor.start()
Beispiel #8
0
def test_executor_init_with_password(
    request: FixtureRequest,
    monkeypatch: pytest.MonkeyPatch,
    tmp_path_factory: pytest.TempPathFactory,
    locale: str,
) -> None:
    """Test whether the executor initializes properly."""
    config = get_config(request)
    monkeypatch.setenv("LC_ALL", locale)
    port = get_port(config["port"])
    assert port is not None
    tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.node.name}")
    datadir = tmpdir / f"data-{port}"
    datadir.mkdir()
    logfile_path = tmpdir / f"postgresql.{port}.log"
    executor = PostgreSQLExecutor(
        executable=config["exec"],
        host=config["host"],
        port=port,
        datadir=str(datadir),
        unixsocketdir=config["unixsocketdir"],
        logfile=str(logfile_path),
        startparams=config["startparams"],
        password="******",
        dbname="somedatabase",
    )
    with executor:
        assert executor.running()
        psycopg.connect(
            dbname=executor.user,
            user=executor.user,
            password=executor.password,
            host=executor.host,
            port=executor.port,
        )
        with pytest.raises(psycopg.OperationalError):
            psycopg.connect(
                dbname=executor.user,
                user=executor.user,
                password="******",
                host=executor.host,
                port=executor.port,
            )
    assert not executor.running()
Beispiel #9
0
    def dynamodb_proc_fixture(request):
        """
        Process fixture for DynamoDB.

        It starts DynamoDB when first used and stops it at the end
        of the tests. Works on ``DynamoDBLocal.jar``.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)
        path_dynamodb_jar = os.path.join((dynamodb_dir or config["dir"]),
                                         "DynamoDBLocal.jar")

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

        dynamodb_port = get_port(port or config["port"])
        dynamodb_delay = ("-delayTransientStatuses"
                          if delay or config["delay"] else "")
        dynamodb_host = host or config["host"]
        dynamodb_executor = TCPExecutor(
            f"""java
            -Djava.library.path=./DynamoDBLocal_lib
            -jar {path_dynamodb_jar}
            -inMemory
            {dynamodb_delay}
            -port {dynamodb_port}""",
            host=dynamodb_host,
            port=dynamodb_port,
            timeout=60,
        )
        dynamodb_executor.start()
        yield dynamodb_executor
        try:
            dynamodb_executor.stop()
        except ProcessExitedWithError:
            pass
Beispiel #10
0
def test_redis_exec(request: FixtureRequest, tmp_path_factory: TempPathFactory,
                    parameter):
    """
    Check if RedisExecutor properly starts with these configuration options.

    Incorrect options won't even start redis.
    """
    config = get_config(request)
    tmpdir = tmp_path_factory.mktemp(f"pytest-redis-test-test_redis_exec")
    redis_exec = RedisExecutor(
        executable=config["exec"],
        databases=4,
        redis_timeout=config["timeout"],
        loglevel=config["loglevel"],
        port=get_port(None),
        host=config["host"],
        timeout=30,
        datadir=tmpdir,
        **parameter,
    )
    with redis_exec:
        assert redis_exec.running()
Beispiel #11
0
    def postgresql_proc_fixture(
            request: FixtureRequest,
            tmp_path_factory: TempPathFactory) -> Iterator[PostgreSQLExecutor]:
        """
        Process fixture for PostgreSQL.

        :param request: fixture request object
        :param tmp_path_factory: temporary path object (fixture)
        :returns: tcp executor
        """
        config = get_config(request)
        postgresql_ctl = executable or config["exec"]
        logfile_prefix = logs_prefix or config["logsprefix"]
        pg_dbname = dbname or config["dbname"]
        pg_load = load or config["load"]

        # 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")

        tmpdir = tmp_path_factory.mktemp(
            f"pytest-postgresql-{request.fixturename}")

        if logfile_prefix:
            warn(
                f"logfile_prefix and logsprefix config option is deprecated, "
                f"and will be dropped in future releases. All fixture related "
                f"data resides within {tmpdir}",
                DeprecationWarning,
            )

        pg_port = get_port(port) or get_port(config["port"])
        assert pg_port is not None
        datadir = tmpdir / f"data-{pg_port}"
        datadir.mkdir()
        logfile_path = tmpdir / f"{logfile_prefix}postgresql.{pg_port}.log"

        if platform.system() == "FreeBSD":
            with (datadir / "pg_hba.conf").open(mode="a") as conf_file:
                conf_file.write("host all all 0.0.0.0/0 trust\n")

        postgresql_executor = PostgreSQLExecutor(
            executable=postgresql_ctl,
            host=host or config["host"],
            port=pg_port,
            user=user or config["user"],
            password=password or config["password"],
            dbname=pg_dbname,
            options=options or config["options"],
            datadir=str(datadir),
            unixsocketdir=unixsocketdir or config["unixsocketdir"],
            logfile=str(logfile_path),
            startparams=startparams or config["startparams"],
            postgres_options=postgres_options or config["postgres_options"],
        )
        # start server
        with postgresql_executor:
            postgresql_executor.wait_for_postgres()
            template_dbname = f"{postgresql_executor.dbname}_tmpl"
            with DatabaseJanitor(
                    user=postgresql_executor.user,
                    host=postgresql_executor.host,
                    port=postgresql_executor.port,
                    dbname=template_dbname,
                    version=postgresql_executor.version,
                    password=postgresql_executor.password,
            ) as janitor:
                for load_element in pg_load:
                    janitor.load(load_element)
                yield postgresql_executor
Beispiel #12
0
    def mysql_proc_fixture(
        request: FixtureRequest, tmp_path_factory: TempPathFactory
    ):
        """
        Process fixture for MySQL server.

        #. 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
        :param tmp_path_factory: pytest fixture for temporary directories
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor

        """
        config = get_config(request)
        mysql_mysqld = mysqld_exec or config["mysqld"]
        mysql_admin_exec = admin_executable or config["admin"]
        mysql_mysqld_safe = mysqld_safe or config["mysqld_safe"]
        mysql_port = get_port(port) or get_port(config["port"])
        mysql_host = host or config["host"]
        mysql_params = params or config["params"]
        mysql_install_db = install_db or config["install_db"]

        tmpdir = py.path.local(
            tmp_path_factory.mktemp(f"pytest-mysql-{request.fixturename}")
        )

        if logs_prefix:
            warn(
                f"logfile_prefix factory argument is deprecated, "
                f"and will be dropped in future releases. All fixture related "
                f"data resides within {tmpdir}, and logs_prefix is only used, "
                f"if deprecated logsdir is configured",
                DeprecationWarning,
            )

        logsdir = config["logsdir"]
        if logsdir:
            warn(
                f"mysql_logsdir and --mysql-logsdir config option is "
                f"deprecated, and will be dropped in future releases. "
                f"All fixture related data resides within {tmpdir}",
                DeprecationWarning,
            )
            if logs_prefix:
                logfile_path = os.path.join(
                    logsdir,
                    f"{logs_prefix}mysql-server.{mysql_port}.log",
                )
        else:
            logfile_path = tmpdir.join(f"mysql-server.{port}.log")

        mysql_executor = MySQLExecutor(
            mysqld_safe=mysql_mysqld_safe,
            mysqld=mysql_mysqld,
            admin_exec=mysql_admin_exec,
            logfile_path=logfile_path,
            base_directory=tmpdir,
            params=mysql_params,
            user=user or config["user"] or "root",
            host=mysql_host,
            port=mysql_port,
            install_db=mysql_install_db,
        )
        with mysql_executor:
            yield mysql_executor