示例#1
0
class TestGRPCServer:
    def __init__(self, manage_py, params=None):
        if params is None:
            params = {}
        params.setdefault('--port', 50000 + randint(0, 10000))
        self.port = params.get('--port')
        self.manage_py = manage_py
        self.process = TCPExecutor(['python', self.manage_py, 'grpcserver'] +
                                   list(self.flat_params(params)),
                                   host='localhost',
                                   port=self.port)

    @classmethod
    def flat_params(cls, dict_):
        for k, v in dict_.items():
            yield str(k)
            if str(v) != "":
                yield str(v)

    def addr(self):
        return "localhost:%s" % self.port

    def start(self):
        self.process.start()

    def stop(self):
        self.process.stop()
示例#2
0
def test_start_and_wait(caplog: LogCaptureFixture):
    """Test if executor await for process to accept connections."""
    caplog.set_level(logging.DEBUG, logger="mirakuru")
    executor = TCPExecutor(NC_COMMAND, "localhost", port=3000, timeout=5)
    executor.start()
    assert executor.running() is True
    executor.stop()
示例#3
0
def test_start_and_wait(timeout):
    """Test if executor await for process to accept connections."""
    command = 'bash -c "sleep 2 && nc -l 3000"'
    executor = TCPExecutor(command, 'localhost', port=3000, timeout=timeout)
    executor.start()

    assert executor.running() is True
    executor.stop()
示例#4
0
def test_start_and_wait():
    """Test if executor await for process to accept connections."""
    command = 'bash -c "sleep 2 && nc -l 3000"'
    executor = TCPExecutor(command, 'localhost', port=3000, timeout=5)
    executor.start()

    assert executor.running() is True
    executor.stop()

    # check proper __str__ and __repr__ rendering:
    assert 'TCPExecutor' in repr(executor)
    assert command in str(executor)
示例#5
0
def test_start_and_wait():
    """Test if executor await for process to accept connections."""
    command = 'bash -c "sleep 2 && nc -l 3000"'
    executor = TCPExecutor(command, 'localhost', port=3000, timeout=5)
    executor.start()

    assert executor.running() is True
    executor.stop()

    # check proper __str__ and __repr__ rendering:
    assert 'TCPExecutor' in repr(executor)
    assert command in str(executor)
示例#6
0
def memcached_server():
    path = shutil.which('memcached')

    if not path:
        raise RuntimeError("Could not find memcached executable")

    host = '127.0.0.1'
    port = port_for.select_random()

    executor = TCPExecutor(f'memcached -l {host} -p {port}', host, port)
    executor.start()

    yield executor

    executor.stop()
    executor.kill()
示例#7
0
def memcached_server():
    path = shutil.which('memcached')

    if not path:
        raise RuntimeError("Could not find memcached executable")

    host = '127.0.0.1'
    port = port_for.select_random()

    executor = TCPExecutor(f'memcached -l {host} -p {port}', host, port)
    executor.start()

    yield executor

    executor.stop()
    executor.kill()
示例#8
0
def local_matrix_server(use_matrix, use_local_matrix_server, local_matrix,
                        matrix_host, matrix_port):

    if not (use_matrix and use_local_matrix_server):
        yield None
        return

    assert local_matrix is not None, \
        "No command to start the local matrix server given. (--local-matrix option)"

    server = TCPExecutor(local_matrix,
                         host=matrix_host,
                         port=matrix_port,
                         timeout=120,
                         sleep=0.1,
                         shell=True)

    server.start()
    yield 'http://{}:{}'.format(matrix_host, matrix_port)
    server.stop()
示例#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
示例#10
0
def local_matrix_server(transport_config):

    if not transport_config.protocol == TransportProtocol.MATRIX:
        yield None
        return

    assert transport_config.parameters.command is not None, \
        'Missing --local-matrix setting. Cannot run Matrix version of integration test.'

    server = TCPExecutor(
        transport_config.parameters.command,
        host=transport_config.parameters.host,
        port=transport_config.parameters.port,
        timeout=120,
        sleep=0.1,
        shell=True,
    )

    server.start()
    yield f'http://{transport_config.parameters.host}:{transport_config.parameters.port}'
    server.stop()
示例#11
0
def oauth_redirect_server(temporary_path):
    config = temporary_path / 'config.yml'
    database = temporary_path / 'registered_clients'
    auth = uuid4().hex

    with config.open('w') as f:
        f.write('\n'.join(
            ('database: {}'.format(database), 'auth: {}'.format(auth))))

    command = 'oauth-redirect -h 127.0.0.1 -p 8000 -a {} -d {}'.format(
        auth, database)

    server = TCPExecutor(command, host='127.0.0.1', port=8000, shell=True)
    server.start()
    server.config = config
    server.database = database
    server.auth = auth
    server.url = 'http://127.0.0.1:8000'

    yield server

    server.stop()
示例#12
0
class LocalDynamoDB:
    def __init__(self, path='/tmp/dynamodb'):
        self._path = path
        self._path_dynamodb_jar = os.path.join(path, 'DynamoDBLocal.jar')

        self._port = self._get_open_port()
        self.executor = TCPExecutor(
            f'java -Djava.library.path=./DynamoDBLocal_lib -jar {self._path_dynamodb_jar} -inMemory -port {self._port}',
            host='localhost',
            port=self._port,
            timeout=60,
        )

        # Write random credentials into env
        self.aws_access_key = str(uuid4())
        self.aws_secret_access_key = str(uuid4())
        self.region = str(uuid4())

        os.environ['AWS_ACCESS_KEY_ID'] = self.aws_access_key
        os.environ['AWS_SECRET_ACCESS_KEY'] = self.aws_secret_access_key
        os.environ['AWS_DEFAULT_REGION'] = self.region

        self.__resources = set()

    def start(self):
        self._ensure_dynamodb_local()
        self.executor.start()
        return self

    def __enter__(self):
        self.start()
        return self.resource()

    def clear(self):
        for t in self.resource().tables.all():
            t.delete()

    def stop(self):
        # for resource in self.__resources:
        #     resource.
        try:
            self.executor.stop()
        except ProcessFinishedWithError:
            # Java exits with some strange code, ignore it, we wanted to stop it anyway
            pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop()

    def resource(self):
        dynamo_db = boto3.resource(
            'dynamodb',
            endpoint_url='http://{host}:{port}'.format(
                host='localhost',
                port=self._port,
            )
        )

        self.__resources.add(dynamo_db)
        return dynamo_db

    def credentials(self, table='table'):
        return {
            "access_key": self.aws_access_key,
            "region": self.region,
            "secret_access_key": self.aws_secret_access_key,
            "table": table
        }

    @staticmethod
    def _get_open_port():
        import socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(("", 0))
        s.listen(1)
        port = s.getsockname()[1]
        s.close()
        return port

    def _ensure_dynamodb_local(self):
        if os.path.exists(self._path_dynamodb_jar):
            print(f'Use existing DynamoDB setup in "{self._path}"')

        else:
            self._download_dynamodb()

    def _download_dynamodb(self):
        print(f'Download dynamodb local to "{self._path}"')

        if os.path.exists(self._path):
            print(f'Clean "{self._path}"')
            shutil.rmtree(self._path)

        with requests.get(LATEST_URL, stream=True) as r:
            r.raise_for_status()

            with tarfile.open(fileobj=r.raw, mode='r:gz') as tar:
                tar.extractall(self._path)

        for p in os.listdir(self._path):
            print(p)