예제 #1
0
def get_free_port_with_range(low, high):

    import port_for
    port_for.select_random(ports=set(range(low, high)))
    port = low  # First port.
    while low <= port <= high:  # Port high is last port you can access.
        try:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                                  0)  # Create a
                # socket.
            except:
                print("Error: Can't open socket!\n")
                break  # If can't open socket, exit the loop.
            s.connect(("127.0.0.1", port))
            # Try connect the port. If port is not listening, throws ConnectionRefusedError.
            connected = True
        except ConnectionRefusedError:
            connected = False
        finally:
            if connected and port != s.getsockname()[1]:  # If connected,
                print("{}:{} Open \n".format("127.0.0.1", port))  # print port.
                return port
            port = port + 1  # Increase port.
            s.close()  # Close socket.
    raise Exception('no port available')
예제 #2
0
 def test_change_port(self):
     # changing app ports is not supported.
     port = self.store.bind_port('foo')
     another_port = port_for.select_random()
     assert port != another_port
     self.assertRaises(port_for.PortForException, self.store.bind_port,
                       'foo', another_port)
예제 #3
0
파일: conftest.py 프로젝트: bsandyy/PyDAS
def start_redis_container(docker_client):
    def wait_for_redis(port, timeout=5.0):
        start_time = time.perf_counter()
        redis_client = redis.Redis(port=port, db=0)
        while True:
            try:
                redis_client.ping()
                break
            except redis.connection.ConnectionError:
                time.sleep(0.01)
                if time.perf_counter() - start_time >= timeout:
                    raise TimeoutError(
                        'Waited too long for Redis to start accepting connections.'
                    )

    redis_port = port_for.select_random()
    host_config = docker_client.create_host_config(
        port_bindings={
            DEFAULT_REDIS_PORT: redis_port,
        })
    container_id = docker_client.create_container(
        REDIS_IMAGE, host_config=host_config)['Id']

    docker_client.start(container_id)
    wait_for_redis(redis_port)
    return container_id, redis_port
예제 #4
0
def start_redis_container(docker_client: docker.DockerClient):
    free_port = port_for.select_random()
    container = docker_client.containers.run(
        IMAGE,
        detach=True,
        ports={'6379/tcp': free_port})
    return container, free_port
예제 #5
0
 def __init__(
     self,
     user="******",
     passwd="password",
     port=None,
     host="localhost",
     chainname=None,
     datadir=None,
     create=False,
 ):
     self.rpcuser = user
     self.rpcpasswd = passwd
     self.rpcport = port
     self.rpchost = host
     self.chainname = chainname
     self.datadir = datadir
     self.create = bool(create)
     if self.rpcport is None:
         self.rpcport = port_for.select_random()
     if self.chainname is None:
         self.chainname = "chain" + str(round(time.time()))
     if self.datadir is None:
         self.datadir = tempfile.mkdtemp()
         self._handle_exit(
             lambda: shutil.rmtree(self.datadir, ignore_errors=True))
     super().__init__(
         self.rpcuser,
         self.rpcpasswd,
         self.rpchost,
         self.rpcport,
         self.chainname.split("@")[0],
     )
     self._handle_exit(sys.exit)
예제 #6
0
    def add_imposter_simple(
            self,
            port=None,
            method='GET',  # pylint: disable=too-many-arguments
            path='/',
            status_code=200,
            response='',
            record_requests=False):
        """Adds an imposter with a single HTTP service stub to Mountebank instance.
        Takes a simplified configuration.

        Args:
            port (int): Port the imposter will listen on. If none, a random port will be selected.
            method (str): HTTP method that the imposter will wait for. GET by default.
            path (str): HTTP path the imposter will wait for. '/' by default.
            status_code (int): HTTP status code the imposter will return. 200 by default.
            response (str): body of the imposters response. Empty string by default.
            record_requests (bool): Instructs the imposter to record the requests.

        Returns:
            `Imposter`: The newly created imposter.
        """
        if port is None:
            port = port_for.select_random()

        return self.add_multi_stub_imposter_simple(
            port, record_requests,
            [HttpStub(method, path, status_code, response)])
예제 #7
0
 def _create(self):
     if self.create == True:
         if "@" not in self.chainname:
             logger.info("creating blockchain: %s" % self.chainname)
             self._execute_command(
                 "multichain-util create %s -datadir=%s -anyone-can-connect=true -anyone-can-send=true -anyone-can-receive=true -anyone-can-mine=true -target-block-time=5 -mining-turnover=1.0"
                 % (self.chainname, self.datadir))
         logger.info("starting blockchain daemon: %s at %s:%s" %
                     (self.chainname, self.rpchost, self.rpcport))
         self._execute_command(
             "multichaind %s -rpcuser=%s -rpcpassword=%s -rpcport=%s -rpchost=%s -datadir=%s -port=%s"
             % (
                 self.chainname,
                 self.rpcuser,
                 self.rpcpasswd,
                 self.rpcport,
                 self.rpchost,
                 self.datadir,
                 port_for.select_random(),
             ),
             daemon=True,
         )
         logger.info("waiting for blockchain daemon")
         self._execute_command(
             "multichain-cli %s -rpcuser=%s -rpcpassword=%s -rpcport=%s -rpchost=%s -datadir=%s -rpcwait getinfo"
             % (
                 self.chainname,
                 self.rpcuser,
                 self.rpcpasswd,
                 self.rpcport,
                 self.rpchost,
                 self.datadir,
             ))
         logger.info("blockchain daemon started")
예제 #8
0
    def test_random_port(self, port_is_used):
        ports = set([1, 2, 3])
        used = {1: True, 2:False, 3:True}
        port_is_used.side_effect = lambda port: used[port]

        for x in range(100):
            self.assertEqual(port_for.select_random(ports), 2)
예제 #9
0
    def test_random_port(self, port_is_used):
        ports = set([1, 2, 3])
        used = {1: True, 2: False, 3: True}
        port_is_used.side_effect = lambda port: used[port]

        for x in range(100):
            self.assertEqual(port_for.select_random(ports), 2)
예제 #10
0
    def zookeeper_process(request: 'SubRequest') -> Tuple[Popen, int]:
        """Configure and start a Zookeeper service."""
        used_zk_port = port_for.select_random() if zk_port is None else zk_port
        tempdir_path = request.getfixturevalue(_get_tmpdir_fixture_name(scope))

        zk_dir = tempdir_path / 'zookeeper-{}'.format(used_zk_port)
        zk_data_dir = zk_dir / 'data'
        zk_data_dir.mkdir(parents=True)
        zk_config_path = zk_dir / 'zookeeper.properties'

        _write_config(zk_config_template,
                      zk_config_path,
                      zk_port=used_zk_port,
                      zk_data_dir=zk_data_dir)

        zk_proc = Popen(
            [zk_bin, str(zk_config_path)],
            start_new_session=True,
        )

        request.addfinalizer(lambda: teardown_fn(zk_proc))

        # Kafka will wait for zookeeper, not need to poll it here.
        # If you use the zookeeper fixure alone, I'm sorry.
        return zk_proc, used_zk_port
예제 #11
0
def test_service_start_and_cleanup():
    service_port = port_for.select_random()

    with HttpService(SERVICE_COMMAND, service_port) as service:
        assert requests.get(service.url).status_code == 200

    with pytest.raises(requests.exceptions.ConnectionError):
        requests.get(service.url)
예제 #12
0
def test_mountebank_impostor_reset():
    test_port = port_for.select_random()
    with Mountebank() as mb:
        imposters_url = "http://localhost:{}/imposters".format(mb.port)
        mb.add_imposter_simple(port=test_port, method="GET", path="/", status_code=200, response="")
        assert requests.get(imposters_url).json()["imposters"] != []
        mb.reset()
        assert requests.get(imposters_url).json()["imposters"] == []
예제 #13
0
def giveme_a_port():
    """Return a random port.

    Using the port-for method to guess a free port on the localhost.

    """
    port = port_for.select_random()
    return port
예제 #14
0
def test_service_start_and_cleanup():
    service_port = port_for.select_random()

    with HttpService(SERVICE_COMMAND, service_port) as service:
        assert requests.get(service.url).status_code == 200

    with pytest.raises(requests.exceptions.ConnectionError):
        requests.get(service.url)
예제 #15
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    srcdir = os.path.realpath(args.sourcedir)
    outdir = os.path.realpath(args.outdir)

    build_args = []
    for arg, meta in SPHINX_BUILD_OPTIONS:
        val = getattr(args, arg)
        if not val:
            continue
        opt = '-{0}'.format(arg)
        if meta is None:
            build_args.extend([opt] * val)
        else:
            for v in val:
                build_args.extend([opt, v])

    build_args.extend([srcdir, outdir])
    build_args.extend(args.filenames)

    ignored = args.ignore
    if args.w:  # Logfile
        ignored.append(os.path.realpath(args.w[0]))
    if args.d:  # Doctrees
        ignored.append(os.path.realpath(args.d[0]))

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    re_ignore = args.re_ignore + DEFAULT_IGNORE_REGEX

    if args.port != 0:
        portn = args.port
    else:
        portn = port_for.select_random()

    builder = SphinxBuilder(outdir, build_args, ignored, re_ignore)
    server = Server(
        watcher=LivereloadWatchdogWatcher(use_polling=args.use_polling), )

    server.watch(srcdir, builder)
    for dirpath in args.additional_watched_dirs:
        dirpath = os.path.realpath(dirpath)
        server.watch(dirpath, builder)
    server.watch(outdir)

    if args.initial_build:
        builder.build()

    if args.openbrowser is True:
        server.serve(port=portn,
                     host=args.host,
                     root=outdir,
                     open_url_delay=args.delay)
    else:
        server.serve(port=portn, host=args.host, root=outdir)
예제 #16
0
def test_service_start_and_cleanup():
    service_port = port_for.select_random()
    service_url = TEST_SERVICE_URL_PATTERN.format(service_port)

    with HttpService(TEST_SERVICE_COMMAND, service_port):
        assert requests.get(service_url).status_code == 200

    with pytest.raises(requests.exceptions.ConnectionError):
        requests.get(service_url)
예제 #17
0
 def __init__(self, process_command, port=None, env=None, copy_parent_env=True):
     if port is None:
         self.port = port_for.select_random()
     else:
         self.port = port
     self.url = 'http://localhost:{}'.format(self.port)
     self._process_command = self._format_process_command(process_command, self.port)
     self._service_env = self._format_process_env(copy_parent_env, env, self.port)
     self._service_proc = None
예제 #18
0
파일: utils.py 프로젝트: zilder/testgres
def reserve_port():
    """
    Generate a new port and add it to 'bound_ports'.
    """

    port = port_for.select_random(exclude_ports=bound_ports)
    bound_ports.add(port)

    return port
예제 #19
0
def get_port(ports):
    """
    Return a random available port.

    If there's only one port passed (e.g. 5000 or '5000') function
    does not check if port is available.
    it there's -1 passed as an argument, function returns None.
    When a range or list of ports is passed `port_for` external package
    is used in order to find a free port.

    :param str|int|tuple|set|list port:
        exact port (e.g. '8000', 8000)
        randomly selected port (None) - any random available port
        [(2000,3000)] or (2000,3000) - random available port from a given range
        [{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports
        [(2000,3000), {4002,4003}] -random of given orange and set
    :returns: a random free port
    :raises: ValueError
    """
    if ports == -1:
        return None
    if not ports:
        return port_for.select_random(None)

    try:
        return int(ports)
    except TypeError:
        pass

    ports_set = set()

    try:
        if not isinstance(ports, list):
            ports = [ports]
        ranges = port_for.utils.ranges_to_set(filter_by_type(ports, tuple))
        nums = set(filter_by_type(ports, int))
        sets = set(chain.from_iterable(filter_by_type(ports,
                                                      (set, frozenset))))
        ports_set = ports_set.union(ranges, sets, nums)
    except ValueError as exc:
        raise InvalidPortsDefinition from exc

    return port_for.select_random(ports_set)
예제 #20
0
def get_port(ports):
    """
    Retun a random available port.

    If there's only one port passed (e.g. 5000 or '5000') function
    does not check if port is available.
    it there's -1 passed as an argument, function returns None.
    When a range or list of ports is passed `port_for` external package
    is used in order to find a free port.

    :param str|int|tuple|set|list port:
        exact port (e.g. '8000', 8000)
        randomly selected port (None) - any random available port
        [(2000,3000)] or (2000,3000) - random available port from a given range
        [{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports
        [(2000,3000), {4002,4003}] -random of given orange and set
    :returns: a random free port
    :raises: ValueError
    """
    if ports == -1:
        return None
    if not ports:
        return port_for.select_random(None)

    try:
        return int(ports)
    except TypeError:
        pass

    ports_set = set()

    try:
        if not isinstance(ports, list):
            ports = [ports]
        ranges = port_for.utils.ranges_to_set(filter_by_type(ports, tuple))
        nums = set(filter_by_type(ports, int))
        sets = set(chain(*filter_by_type(ports, (set, frozenset))))
        ports_set = ports_set.union(ranges, sets, nums)
    except ValueError:
        raise InvalidPortsDefinition

    return port_for.select_random(ports_set)
예제 #21
0
def test_mountebank_simple_impostor_add():
    test_port = port_for.select_random()
    test_response = "Just some reponse body (that I used to know)"

    with Mountebank() as mb:
        mb.add_imposter_simple(
            port=test_port, method="POST", path="/some-path", status_code=201, response=test_response
        )
        response = requests.post("http://localhost:{}/some-path".format(test_port))
        assert response.status_code == 201
        assert response.text == test_response
예제 #22
0
 def __init__(self, host='localhost', port=None, cwd=None, shell=False,
              stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL):
     self.host = host
     self.port = port or port_for.select_random()
     self.proc = None
     self.shell = shell
     self.cwd = cwd
     self.stdin = stdin
     self.stdout = stdout
     self.stderr = stderr
     self.arguments = [
         sys.executable, '-u', '-m', 'SimpleHTTPServer', str(self.port)
     ]
예제 #23
0
def test_service_env_config():
    example_service_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        'example_service.py'
    )
    service_port = port_for.select_random()

    service = HttpService(
        [sys.executable, example_service_path],
        port=service_port,
        env={'TEST_APP_PORT': '{port}'})
    with service:
        assert requests.get(service.base_url).text == 'Just some text.'
예제 #24
0
 def __init__(self, host='localhost', port=None, cwd=None, shell=False,
              stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL):
     self.host = host
     self.port = port or port_for.select_random()
     self.proc = None
     self.shell = shell
     self.cwd = cwd
     self.stdin = stdin
     self.stdout = stdout
     self.stderr = stderr
     self.arguments = [
         sys.executable, '-u', '-m', 'SimpleHTTPServer', str(self.port)
     ]
예제 #25
0
def test_existing_mountebank_simple_imposter():
    mb_port = port_for.select_random()
    test_port = port_for.select_random()
    test_response = 'Just some reponse body (that I used to know)'
    test_body = 'Some test message body'

    mb_process = subprocess.Popen(get_mb_command() + ['--mock', '--port', str(mb_port)])
    with ExistingMountebank('localhost', mb_port) as mb:
        imposter = mb.add_imposter_simple(
            port=test_port,
            method='POST',
            path='/some-path',
            status_code=201,
            response=test_response
        )

        response = requests.post('http://localhost:{}/some-path'.format(test_port), data=test_body)
        assert response.status_code == 201
        assert response.text == test_response
        assert imposter.wait_for_requests()[0].body == test_body

    mb_process.terminate()
예제 #26
0
def container():
    port = port_for.select_random()
    name = 'suitable-container-%s' % uuid4().hex

    subprocess.check_call((
        'docker', 'run', '-d', '--rm', '-it', '--name', name,
        '-p', '127.0.0.1:%d:22/tcp' % port,
        'rastasheep/ubuntu-sshd:18.04'
    ))

    yield Container('127.0.0.1', port, 'root', 'root')

    subprocess.call(('docker', 'stop', name))
예제 #27
0
def test_existing_mountebank_simple_imposter():
    mb_port = port_for.select_random()
    test_port = port_for.select_random()
    test_response = 'Just some reponse body (that I used to know)'
    test_body = 'Some test message body'

    mb_process = subprocess.Popen(get_mb_command() + ['--mock', '--port', str(mb_port)])
    with ExistingMountebank('localhost', mb_port) as mb:
        imposter = mb.add_imposter_simple(
            port=test_port,
            method='POST',
            path='/some-path',
            status_code=201,
            response=test_response
        )

        response = requests.post('http://localhost:{}/some-path'.format(test_port), data=test_body)
        assert response.status_code == 201
        assert response.text == test_response
        assert imposter.wait_for_requests()[0].body == test_body

    mb_process.terminate()
def test_execute_check_tcp(delay):
    """Check the executor with the TCP check."""
    port = port_for.select_random()
    check = check_tcp(port)

    assert check() is False
    process = execute(
        [SERVICE, '--delay', str(delay), 'tcp', '--port', str(port)],
        [check_tcp(port)],
        timeout=1 + delay)
    assert check() is True
    assert process.poll() is None  # Still running.
    process.kill()
예제 #29
0
def test_mountebank_reset():
    test_port = port_for.select_random()
    with Mountebank() as mb:
        imposters_url = 'http://localhost:{}/imposters'.format(mb.port)
        mb.add_imposter_simple(
            port=test_port,
            method='GET',
            path='/',
            status_code=200,
            response=''
        )
        assert requests.get(imposters_url).json()['imposters'] != []
        mb.reset()
        assert requests.get(imposters_url).json()['imposters'] == []
예제 #30
0
def test_execute_check_tcp(delay):
    """Check the executor with the TCP check."""
    port = port_for.select_random()
    check = check_tcp(port)

    assert check() is False
    process = execute(
        [SERVICE, '--delay',
         str(delay), 'tcp', '--port',
         str(port)], [check_tcp(port)],
        timeout=1 + delay)
    assert check() is True
    assert process.poll() is None  # Still running.
    process.kill()
예제 #31
0
def test_mountebank_impostor_reset():
    test_port = port_for.select_random()
    with Mountebank() as mb:
        imposters_url = 'http://localhost:{}/imposters'.format(mb.port)
        mb.add_imposter_simple(
            port=test_port,
            method='GET',
            path='/',
            status_code=200,
            response=''
        )
        assert requests.get(imposters_url).json()['imposters'] != []
        mb.reset()
        assert requests.get(imposters_url).json()['imposters'] == []
예제 #32
0
def es_process(es_binary, es_version):
    port = port_for.select_random()
    pid = es_binary + '.pid'

    command = f"{es_binary} -p {pid} -E http.port={port}"
    url = f'http://127.0.0.1:{port}/_cluster/health?wait_for_status=green'

    executor = HTTPExecutor(command, url, method='GET')
    executor.start()

    yield executor

    executor.stop()
    executor.kill()
예제 #33
0
def browser(webdriver, webdriver_options, webdriver_executable_path,
            browser_extension):

    config = {
        'executable_path': webdriver_executable_path,
        'options': webdriver_options,

        # preselect a port as selenium picks it in a way that triggers the
        # macos firewall to display a confirmation dialog
        'port': port_for.select_random()
    }

    with browser_extension.spawn(Browser, webdriver, **config) as browser:
        yield browser
예제 #34
0
    def __init__(self, host='localhost', port=None, cwd=None, shell=False,
                 stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL):
        self.host = host
        self.port = port or port_for.select_random()
        self.proc = None
        self.shell = shell
        self.cwd = cwd
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr

        self.arguments = [
            'flask', 'run', '-p', str(self.port)
        ]
예제 #35
0
def browser(webdriver, webdriver_options, webdriver_executable_path,
            browser_extension):

    config = {
        'executable_path': webdriver_executable_path,
        'options': webdriver_options,

        # preselect a port as selenium picks it in a way that triggers the
        # macos firewall to display a confirmation dialog
        'port': port_for.select_random()
    }

    with browser_extension.spawn(Browser, webdriver, **config) as browser:
        yield browser
예제 #36
0
 def __init__(self,
              process_command,
              port=None,
              env=None,
              copy_parent_env=True):
     if port is None:
         self.port = port_for.select_random()
     else:
         self.port = port
     self.url = 'http://localhost:{}'.format(self.port)
     self._process_command = self._format_process_command(
         process_command, self.port)
     self._service_env = self._format_process_env(copy_parent_env, env,
                                                  self.port)
     self._service_proc = None
예제 #37
0
파일: mountepy.py 프로젝트: bbyars/mountepy
 def __init__(self, process_command, port=None):
     """
     Initializes the object without starting the service process.
     :param process_command: Command that will start the service.
     It is a string or a list of strings (like parameters to subprocess.Popen).
     Command strings may contain '{port}' - it will be filled with the provided port.
     :param int port: Port on which the service will listen.
     If not provided then the service will run on a random free port.
     """
     if port is None:
         self.port = port_for.select_random()
     else:
         self.port = port
     self._process_command = self._format_process_command(process_command, self.port)
     self._service_proc = None
예제 #38
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()
예제 #39
0
def get_port(ports):
    """
    Retuns a random available port. If there's only one port passed
    (e.g. 5000 or '5000') function does not check if port is available.
    When a range or list of ports is passed `port_for` external package
    is used in order to find a free port.

    :param int|str ports: e.g. 3000, '3000', '3000-3100', '3000,3002', '?'
    :returns: a random free port
    """
    try:
        return int(ports)
    except ValueError:
        pass

    return port_for.select_random(parse_ports(ports))
예제 #40
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()
예제 #41
0
    def add_imposter_simple(self, port=None, method='GET', path='/', status_code=200, response=''):
        """
        Adds a HTTP service stub (imposter) to Mountebank instance.
        Takes a simplified configuration.
        :param int port: port the imposter will listen on. If none, a random port will be selected.
        :param str method: HTTP method that the imposter will wait for. GET by default.
        :param str path: HTTP path the imposter will wait for. '/' by default.
        :param int status_code: HTTP status code the imposter will return. 200 by default.
        :param str response: body of the imposters response. Empty string by default.
        :return: The created service stub.
        :rtype: Imposter
        """
        if port == None:
            port = port_for.select_random()

        imposter_config = {
            'port': port,
            'protocol': 'http',
            'stubs': [
                {
                    'responses': [
                        {
                            'is': {
                                'statusCode': status_code,
                                'headers': {
                                    'Content-Type': 'application/json'
                                },
                                'body': response
                            }
                        }
                    ],
                    'predicates': [
                        {
                            'and': [
                                {
                                    'equals': {
                                        'path': path,
                                        'method': method,
                                    }
                                },
                            ]
                        }
                    ]
                }
            ]
        }
        return self.add_imposter(imposter_config)
def test_execute_same_service_sequentially():
    """Check if the pre-checks detect remains of the previous process."""
    port = port_for.select_random()
    command = [SERVICE, 'tcp', '--port', str(port)]

    process = execute(command, [check_tcp(port)])

    with pytest.raises(PreChecksFailed):
        # The previous process still in place.
        execute(command, [check_tcp(port)], timeout=1)

    process.terminate()  # The process will take >2 seconds to tear down.

    # Now, as the previous process is dead, the `execute` should succeed.
    another_process = execute(command, [check_tcp(port)], timeout=5)

    another_process.kill()
예제 #43
0
    def kafka_server(request: 'SubRequest') -> Tuple[Popen, int]:
        """Configure and start a Kafka server."""
        _, zk_port = request.getfixturevalue(zookeeper_fixture_name)
        used_kafka_port = port_for.select_random(
        ) if kafka_port is None else kafka_port
        tempdir_path = request.getfixturevalue(_get_tmpdir_fixture_name(scope))

        kafka_dir = tempdir_path / 'kafka-server-{}'.format(used_kafka_port)
        kafka_log_dir = kafka_dir / 'logs'
        kafka_log_dir.mkdir(parents=True)
        kafka_config_path = kafka_dir / 'kafka-server.properties'

        _write_config(kafka_config_template,
                      kafka_config_path,
                      zk_port=zk_port,
                      kafka_port=used_kafka_port,
                      kafka_log_dir=kafka_log_dir)

        kafka_proc = Popen(
            [kafka_bin, str(kafka_config_path)],
            start_new_session=True,
        )

        request.addfinalizer(lambda: teardown_fn(kafka_proc))

        def kafka_started():
            assert kafka_proc.poll(
            ) is None, 'Kafka process must not terminate'
            try:
                producer = KafkaProducer(
                    bootstrap_servers='localhost:{}'.format(used_kafka_port))
                producer.close()
            except NoBrokersAvailable:
                return False
            return True

        # Silence kafka errors when polling.
        kafka_logger = logging.getLogger('kafka.producer.kafka')
        prev_propagate = kafka_logger.propagate
        try:
            kafka_logger.propagate = False
            _wait_until(kafka_started)
        finally:
            kafka_logger.propagate = prev_propagate

        return kafka_proc, used_kafka_port
예제 #44
0
 def test_files_io(self):
     from agutil.security import SecureConnection, SecureServer
     server_payload = lambda x:None
     warnings.simplefilter('ignore', ResourceWarning)
     server_thread = None
     found_port = -1
     for attempt in range(10):
         found_port = pf.select_random()
         server_thread = threading.Thread(target=server_comms_files, args=(SecureServer, found_port, server_payload), name='Server thread', daemon=True)
         server_thread.start()
         server_thread.join(1)
         if server_thread.is_alive():
             break
     warnings.resetwarnings()
     self.assertTrue(server_thread.is_alive(), "Failed to bind to any ports after 10 attempts")
     client_payload = lambda x:None
     client_thread = threading.Thread(target=client_comms_files, args=(SecureConnection, found_port, client_payload), daemon=True)
     client_thread.start()
     extra = 30 if TRAVIS else 0
     server_thread.join(60+extra)
     self.assertFalse(server_thread.is_alive(), "Server thread still running")
     client_thread.join(60+extra)
     self.assertFalse(client_thread.is_alive(), "Client thread still running")
     self.assertRaises(FileNotFoundError, client_payload.sock.sendfile, 'blarg')
     server_payload.sock.close()
     time.sleep(.25)
     client_payload.sock.close()
     self.assertEqual(client_payload.comms_check, '+')
     self.assertTrue(server_payload.exception)
     self.assertTrue(client_payload.exception)
     self.assertEqual(len(server_payload.intake), len(client_payload.output))
     self.assertEqual(len(server_payload.output), len(client_payload.intake))
     for i in range(len(server_payload.intake)):
         self.assertEqual(len(server_payload.intake[i]), len(client_payload.output[i]))
         if len(server_payload.intake[i])>2048:
             self.assertEqual(hash(server_payload.intake[i]), hash(client_payload.output[i]))
         else:
             self.assertEqual(server_payload.intake[i], client_payload.output[i])
     for i in range(len(client_payload.intake)):
         self.assertEqual(len(client_payload.intake[i]), len(server_payload.output[i]))
         if len(client_payload.intake[i])>2048:
             self.assertEqual(hash(client_payload.intake[i]), hash(server_payload.output[i]))
         else:
             self.assertEqual(client_payload.intake[i], server_payload.output[i])
     self.assertRaises(IOError, client_payload.sock.sendfile, 'fish')
     self.assertRaises(IOError, client_payload.sock.savefile, 'fish')
예제 #45
0
def test_execute_same_service_sequentially():
    """Check if the pre-checks detect remains of the previous process."""
    port = port_for.select_random()
    command = [SERVICE, 'tcp', '--port', str(port)]

    process = execute(command, [check_tcp(port)])

    with pytest.raises(PreChecksFailed):
        # The previous process still in place.
        execute(command, [check_tcp(port)], timeout=1)

    process.terminate()  # The process will take >2 seconds to tear down.

    # Now, as the previous process is dead, the `execute` should succeed.
    another_process = execute(command, [check_tcp(port)], timeout=5)

    another_process.kill()
예제 #46
0
 def test_server_bind_and_communication(self):
     # warnings.simplefilter('error', ResourceWarning)
     from agutil.io import SocketServer
     from agutil.io import QueuedSocket
     from agutil.io import Socket
     from agutil.security import SecureSocket
     ss = None
     warnings.simplefilter('ignore', ResourceWarning)
     for attempt in range(10):
         try:
             ss = SocketServer(pf.select_random())
         except OSError:
             if ss!=None:
                 ss.close()
         if ss!=None:
             break
     warnings.resetwarnings()
     self.assertIsInstance(ss, SocketServer, "Failed to bind to any ports after 10 attempts")
     startup_lock.acquire()
     server_payload = lambda x:None
     client_payload = lambda x:None
     server_thread = threading.Thread(target=server_comms, args=(SecureSocket, QueuedSocket, ss, server_payload), daemon=True)
     client_thread = threading.Thread(target=client_comms, args=(SecureSocket, QueuedSocket, Socket, ss.port, client_payload), daemon=True)
     server_thread.start()
     client_thread.start()
     extra = 30 if TRAVIS else 0
     server_thread.join(60+extra)
     self.assertFalse(server_thread.is_alive(), "Server thread still running")
     client_thread.join(60+extra)
     self.assertFalse(client_thread.is_alive(), "Client thread still running")
     self.assertRaises(TypeError, client_payload.sock.sendRAW, 13, 'test')
     self.assertRaises(TypeError, client_payload.sock.send, 13, 'test')
     self.assertRaises(TypeError, client_payload.sock.sendAES, 13)
     self.assertRaises(TypeError, client_payload.sock.sendAES, 'blorg', 'test', 13)
     self.assertRaises(TypeError, client_payload.sock.sendAES, 'blorg', 'test', True, 13)
     server_payload.sock.close()
     client_payload.sock.close()
     self.assertTrue(server_payload.exception1)
     self.assertTrue(server_payload.exception2)
     self.assertTrue(client_payload.exception)
     self.assertEqual(len(server_payload.intake), len(client_payload.output))
     self.assertEqual(len(server_payload.output), len(client_payload.intake))
     self.assertListEqual(server_payload.intake, client_payload.output)
     self.assertListEqual(server_payload.output, client_payload.intake)
     self.assertRaises(IOError, client_payload.sock.send, "fish")
예제 #47
0
def test_mountebank_simple_impostor():
    test_port = port_for.select_random()
    test_response = 'Just some reponse body (that I used to know)'
    test_body = 'Some test message body'

    with Mountebank() as mb:
        imposter = mb.add_imposter_simple(
            port=test_port,
            method='POST',
            path='/some-path',
            status_code=201,
            response=test_response
        )

        response = requests.post('http://localhost:{}/some-path'.format(test_port), data=test_body)
        assert response.status_code == 201
        assert response.text == test_response
        assert imposter.wait_for_requests()[0].body == test_body
예제 #48
0
 def _create(self):
     datadir = tempfile.mkdtemp()
     self._handle_exit(lambda: shutil.rmtree(datadir, ignore_errors=True))
     self._execute_command(
         "etcd --listen-client-urls=http://%s:%s --advertise-client-urls=http://%s:%s --data-dir=%s --listen-peer-urls=http://localhost:%s"
         % (
             self.host,
             self.port,
             self.host,
             self.port,
             datadir,
             port_for.select_random(),
         ),
         daemon=True,
     )
     self._execute_command(
         "etcdctl --endpoints=http://%s:%s endpoint status" %
         (self.host, self.port), )
예제 #49
0
def test_mountebank_simple_impostor():
    test_port = port_for.select_random()
    test_response = 'Just some reponse body (that I used to know)'
    test_body = 'Some test message body'

    with Mountebank() as mb:
        imposter = mb.add_imposter_simple(
            port=test_port,
            method='POST',
            path='/some-path',
            status_code=201,
            response=test_response
        )

        response = requests.post('http://localhost:{}/some-path'.format(test_port), data=test_body)
        assert response.status_code == 201
        assert response.text == test_response
        assert imposter.wait_for_requests()[0].body == test_body
예제 #50
0
 def test_server_bind_and_communication(self):
     # warnings.simplefilter('error', ResourceWarning)
     from agutil.io import SocketServer
     from agutil.io import MPlexSocket
     from agutil.io import Socket
     ss = None
     warnings.simplefilter('ignore', ResourceWarning)
     for attempt in range(10):
         try:
             ss = SocketServer(pf.select_random())
         except OSError:
             if ss!=None:
                 ss.close()
         if ss!=None:
             break
     warnings.resetwarnings()
     self.assertIsInstance(ss, SocketServer, "Failed to bind to any ports after 10 attempts")
     startup_lock.acquire()
     server_payload = lambda x:None
     client_payload = lambda x:None
     server_thread = threading.Thread(target=server_comms, args=(MPlexSocket, ss, server_payload, self.CHANNELS), daemon=True)
     client_thread = threading.Thread(target=client_comms, args=(MPlexSocket, Socket, ss.port, client_payload, self.CHANNELS), daemon=True)
     server_thread.start()
     client_thread.start()
     extra = 30 if TRAVIS else 0
     if sys.version_info==(3,3):
         extra+=5
     server_thread.join(10+extra)
     self.assertFalse(server_thread.is_alive(), "Server thread still running")
     client_thread.join(10+extra)
     self.assertFalse(client_thread.is_alive(), "Client thread still running")
     self.assertRaises(ValueError, client_payload.sock.send, 'fish', 'ta^cos')
     self.assertRaises(TypeError, client_payload.sock.send, 13, 'test')
     server_payload.sock.close()
     client_payload.sock.close()
     self.assertTrue(server_payload.exception)
     # self.assertTrue(client_payload.exception)
     self.assertEqual(len(server_payload.intake), len(client_payload.output))
     self.assertEqual(len(server_payload.output), len(client_payload.intake))
     self.assertListEqual(server_payload.intake, client_payload.output)
     self.assertListEqual(server_payload.output, client_payload.intake)
     self.assertRaises(IOError, client_payload.sock.send, "hi")
     self.assertRaises(IOError, client_payload.sock.recv)
def test_execute_failing_checks():
    """Test if failing checks result in ``PostChecksFailed`` being thrown."""
    port = check_tcp(port_for.select_random())
    tcp_check = check_tcp(port)
    unix_check = check_unix('no_such_sock')
    http_check = check_http('http://127.0.0.1:%s' % port)

    with pytest.raises(PostChecksFailed):
        execute('sleep 10', [tcp_check], timeout=1)

    with pytest.raises(PostChecksFailed):
        execute('sleep 10', [unix_check], timeout=1)

    with pytest.raises(PostChecksFailed):
        execute('sleep 10', [http_check], timeout=1)

    with pytest.raises(PostChecksFailed):
        # 3 failing checks at once.
        execute('sleep 10', [http_check, unix_check, tcp_check], timeout=1)
예제 #52
0
def test_execute_failing_checks():
    """Test if failing checks result in ``PostChecksFailed`` being thrown."""
    port = check_tcp(port_for.select_random())
    tcp_check = check_tcp(port)
    unix_check = check_unix('no_such_sock')
    http_check = check_http('http://127.0.0.1:%s' % port)

    with pytest.raises(PostChecksFailed):
        execute('sleep 10', [tcp_check], timeout=1)

    with pytest.raises(PostChecksFailed):
        execute('sleep 10', [unix_check], timeout=1)

    with pytest.raises(PostChecksFailed):
        execute('sleep 10', [http_check], timeout=1)

    with pytest.raises(PostChecksFailed):
        # 3 failing checks at once.
        execute('sleep 10', [http_check, unix_check, tcp_check], timeout=1)
def test_execute_process_killed():
    """
    Check if the process gets killed after post checks fail.

    We spawn a process that listens TCP on some port and supply a failing check so that the process should get killed.
    We check if the process got killed by checking if nothing listens on TCP after PostChecksFailed is raised.
    """
    port = port_for.select_random()
    passing_check = check_tcp(port)

    def failing_check_for_testing_purposes():
        return False

    with pytest.raises(PostChecksFailed) as checks_failed:
        execute([SERVICE, 'tcp', '--port', str(port)], [passing_check, failing_check_for_testing_purposes], timeout=1)

    assert 'function failing_check_for_testing_purposes' in str(checks_failed.value)

    # The process will be killed and nothing will be listening on that port.
    wait_until(lambda: passing_check() is False)
def test_execute_check_http(delay):
    """Check the executor with the HTTP HEAD request check."""
    port = port_for.select_random()
    good_path = '/good_path'

    check = check_http('http://127.0.0.1:%s%s' % (port, good_path))
    check_bad_path = check_http('http://127.0.0.1:%s/bad_path' % port)  # This request will not return 200.

    assert check() is False
    assert check_bad_path() is False

    process = execute(
        [SERVICE, '--delay', str(delay), 'http', '--port', str(port), '--path', good_path],
        [check],
        timeout=1 + delay)
    assert check() is True
    assert check_bad_path() is False

    assert process.poll() is None
    process.kill()
예제 #55
0
def test_mountebank_multiple_simple_impostors():
    test_port = port_for.select_random()
    test_response_1 = 'Just some response body (that I used to know)'
    test_response_2 = '{"Hey": "a JSON!"}'
    stub_1 = HttpStub(method='PUT', path='/path-1', status_code=201, response=test_response_1)
    stub_2 = HttpStub(method='POST', path='/path-2', status_code=202, response=test_response_2)

    with Mountebank() as mb:
        mb.add_imposters_simple(
            port=test_port,
            stubs=[stub_1, stub_2]
        )

        # TODO get rid of the code duplication
        response_1 = requests.put('http://localhost:{}/path-1'.format(test_port))
        assert response_1.status_code == 201
        assert response_1.text == test_response_1
        response_2 = requests.post('http://localhost:{}/path-2'.format(test_port))
        assert response_2.status_code == 202
        assert response_2.text == test_response_2
예제 #56
0
def test_mountebank_multiple_simple_impostors():
    test_port = port_for.select_random()
    test_response_1 = 'Just some response body (that I used to know)'
    test_response_2 = '{"Hey": "a JSON!"}'
    stub_1 = HttpStub(method='PUT', path='/path-1', status_code=201, response=test_response_1)
    stub_2 = HttpStub(method='POST', path='/path-2', status_code=202, response=test_response_2)

    with Mountebank() as mb:
        mb.add_multi_stub_imposter_simple(
            port=test_port,
            stubs=[stub_1, stub_2]
        )

        # TODO get rid of the code duplication
        response_1 = requests.put('http://localhost:{}/path-1'.format(test_port))
        assert response_1.status_code == 201
        assert response_1.text == test_response_1
        response_2 = requests.post('http://localhost:{}/path-2'.format(test_port))
        assert response_2.status_code == 202
        assert response_2.text == test_response_2