示例#1
0
def skip_if_no_local_network():
    """
    Helper function to check for existing local network

    Returns:
        str: The reason for the skip.
        None: Should not be skipped.
    """
    check_port = ports.get_unused_localhost_port()
    has_local_network = False
    try:
        with contextlib.closing(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as pubsock:
            pubsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            pubsock.bind(("", check_port))
        has_local_network = True
    except OSError:
        # I wonder if we just have IPV6 support?
        try:
            with contextlib.closing(
                    socket.socket(socket.AF_INET6,
                                  socket.SOCK_STREAM)) as pubsock:
                pubsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                pubsock.bind(("", check_port))
            has_local_network = True
        except OSError:
            # Let's continue
            pass
    if has_local_network is False:
        return "No local network was detected"
示例#2
0
def get_unused_localhost_port(use_cache=False):
    """
    :keyword bool use_cache:
        If ``use_cache`` is ``True``, consecutive calls to this function will never return the cached port.

    Return a random unused port on localhost
    """
    if not isinstance(use_cache, bool):
        raise pytest.UsageError(
            "The value of 'use_cache' needs to be an boolean, not {}".format(
                type(use_cache)))

    with contextlib.closing(
            socket.socket(family=socket.AF_INET,
                          type=socket.SOCK_STREAM)) as usock:
        usock.bind(("127.0.0.1", 0))
        port = usock.getsockname()[1]

    if use_cache:
        try:
            cached_ports = get_unused_localhost_port.__cached_ports__
        except AttributeError:
            cached_ports = get_unused_localhost_port.__cached_ports__ = set()
        if port in cached_ports:
            return get_unused_localhost_port(use_cache=use_cache)
        cached_ports.add(port)

    return port
示例#3
0
def skip_if_no_remote_network():
    """
    Helper function to check for existing remote network(internet)

    Returns:
        str: The reason for the skip.
        None: Should not be skipped.
    """

    # We are using the google.com DNS records as numerical IPs to avoid
    # DNS look ups which could greatly slow down this check
    has_remote_network = False
    for addr in (
            "172.217.17.14",
            "172.217.16.238",
            "173.194.41.198",
            "173.194.41.199",
            "173.194.41.200",
            "173.194.41.201",
            "173.194.41.206",
            "173.194.41.192",
            "173.194.41.193",
            "173.194.41.194",
            "173.194.41.195",
            "173.194.41.196",
            "173.194.41.197",
            "216.58.201.174",
    ):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(0.25)
            sock.connect((addr, 80))
            sock.close()
            # We connected? Stop the loop
            has_remote_network = True
            break
        except OSError:
            # Let's check the next IP
            continue

    if has_remote_network is False:
        return "No internet network connection was detected"
示例#4
0
def get_connectable_ports(ports):
    """
    :param ~collections.abc.Iterable ports: An iterable of ports to try and connect to
    :rtype: set
    :return: Returns a set of the ports where connection was successful
    """
    connectable_ports = set()
    ports = set(ports)

    for port in set(ports):
        with contextlib.closing(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
            conn = sock.connect_ex(("localhost", port))
            try:
                if conn == 0:
                    log.debug("Port %s is connectable!", port)
                    connectable_ports.add(port)
                    sock.shutdown(socket.SHUT_RDWR)
            except OSError:
                continue
    return connectable_ports
def test_spawn_container(docker_container, echo_server_port):
    message = b"Hello!\n"
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        client.connect(("127.0.0.1", echo_server_port))
        client.settimeout(0.1)
        # Get any welcome message from the server
        while True:
            try:
                client.recv(4096)
            except socket.timeout:
                break
        client.send(message)
        response = None
        while True:
            try:
                response = client.recv(4096)
            except socket.timeout:
                break
        assert response is not None
        assert response == message
    finally:
        client.close()