Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions test/io/postgrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def run(
env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile)
baseurl = "http+unix://" + urllib.parse.quote_plus(str(socketfile))

adminport = freeport(port)
adminport = freeport(used_ports=[port])
env["PGRST_ADMIN_SERVER_PORT"] = str(adminport)
adminhost = f"[{host}]" if host and is_ipv6(host) else localhost
adminurl = f"http://{adminhost}:{adminport}"
Expand Down Expand Up @@ -165,14 +165,14 @@ def run(
process.wait()


def freeport(used_port=None):
"Find a free port on localhost."
def freeport(used_ports=None):
"Find an unused free port on localhost."
while True:
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = s.getsockname()[1]
if port != used_port:
if used_ports is None or port not in used_ports:
return port


Expand Down
8 changes: 6 additions & 2 deletions test/io/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def test_jwt_secret_min_length(defaultenv):
assert "The JWT secret must be at least 32 characters long." in error


# TODO: Improve readability of "--ready" healthcheck tests
@pytest.mark.parametrize("host", ["127.0.0.1", "::1"], ids=["IPv4", "IPv6"])
def test_cli_ready_flag_success(host, defaultenv):
"test PostgREST ready flag succeeds when ready"
Expand Down Expand Up @@ -340,8 +341,11 @@ def test_cli_ready_flag_fail_with_http_exception(defaultenv):

# when healthcheck process sends the request to a wrong endpoint
with run(env=defaultenv, port=port) as postgrest:
# we set it to some freeport where admin is not running
postgrest.config["PGRST_ADMIN_SERVER_PORT"] = str(freeport())
# we set it to some freeport where server and admin server is not running
admin_port = int(postgrest.config["PGRST_ADMIN_SERVER_PORT"])
used_ports = [port, admin_port]

postgrest.config["PGRST_ADMIN_SERVER_PORT"] = str(freeport(used_ports))
output = cli(["--ready"], env=postgrest.config, expect_error=True)
(admin_host, admin_port) = get_admin_host_and_port_from_config(postgrest.config)

Expand Down