Skip to content

Commit

Permalink
test: Make io tests use admin/ready to wait for startup
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Jan 7, 2022
1 parent 6bc965e commit df5d940
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions test/io-tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def dumpconfig(configpath=None, env=None, stdin=None):


@contextlib.contextmanager
def run(configpath=None, stdin=None, env=None, port=None, adminport=None):
def run(configpath=None, stdin=None, env=None, port=None):
"Run PostgREST and yield an endpoint that is ready for connections."
env = env or {}
env["PGRST_DB_POOL"] = "1"
Expand All @@ -153,11 +153,9 @@ def run(configpath=None, stdin=None, env=None, port=None, adminport=None):
env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile)
baseurl = "http+unix://" + urllib.parse.quote_plus(str(socketfile))

if adminport:
env["PGRST_ADMIN_SERVER_PORT"] = str(adminport)
adminurl = f"http://localhost:{adminport}"
else:
adminurl = None
adminport = freeport()
env["PGRST_ADMIN_SERVER_PORT"] = str(adminport)
adminurl = f"http://localhost:{adminport}"

command = [POSTGREST_BIN]
env["HPCTIXFILE"] = hpctixfile()
Expand All @@ -179,14 +177,14 @@ def run(configpath=None, stdin=None, env=None, port=None, adminport=None):
process.stdin.write(stdin or b"")
process.stdin.close()

wait_until_ready(baseurl)
wait_until_ready(adminurl + "/ready")

process.stdout.read()

yield PostgrestProcess(
process=process,
session=PostgrestSession(baseurl),
admin=PostgrestSession(adminurl) if adminurl else None,
admin=PostgrestSession(adminurl),
)
finally:
remaining_output = process.stdout.read()
Expand Down Expand Up @@ -750,7 +748,7 @@ def test_admin_ready_w_channel(defaultenv):
"PGRST_DB_CHANNEL_ENABLED": "true",
}

with run(env=env, adminport=freeport()) as postgrest:
with run(env=env) as postgrest:
response = postgrest.admin.get("/ready")
assert response.status_code == 200

Expand All @@ -763,7 +761,7 @@ def test_admin_ready_wo_channel(defaultenv):
"PGRST_DB_CHANNEL_ENABLED": "false",
}

with run(env=env, adminport=freeport()) as postgrest:
with run(env=env) as postgrest:
response = postgrest.admin.get("/ready")
assert response.status_code == 200

Expand All @@ -780,7 +778,7 @@ def test_admin_ready_includes_schema_cache_state(defaultenv):
"PGRST_DB_ANON_ROLE": "limited_authenticator",
}

with run(env=env, adminport=freeport()) as postgrest:
with run(env=env) as postgrest:

# make it impossible to load the schema cache
response = postgrest.session.post(
Expand All @@ -802,15 +800,15 @@ def test_admin_ready_includes_schema_cache_state(defaultenv):
def test_admin_not_found(defaultenv):
"Should get a not found from a undefined endpoint on the admin server"

with run(env=defaultenv, adminport=freeport()) as postgrest:
with run(env=defaultenv) as postgrest:
response = postgrest.admin.get("/notfound")
assert response.status_code == 404


def test_admin_ready_dependent_on_main_app(defaultenv):
"Should get a failure from the admin ready endpoint if the main app also fails"

with run(env=defaultenv, adminport=freeport()) as postgrest:
with run(env=defaultenv) as postgrest:
# delete the unix socket to make the main app fail
os.remove(defaultenv["PGRST_SERVER_UNIX_SOCKET"])
response = postgrest.admin.get("/ready")
Expand All @@ -820,15 +818,15 @@ def test_admin_ready_dependent_on_main_app(defaultenv):
def test_admin_live_good(defaultenv):
"Should get a success from the admin live endpoint if the main app is running"

with run(env=defaultenv, port=freeport(), adminport=freeport()) as postgrest:
with run(env=defaultenv, port=freeport()) as postgrest:
response = postgrest.admin.get("/live")
assert response.status_code == 200


def test_admin_live_dependent_on_main_app(defaultenv):
"Should get a failure from the admin live endpoint if the main app also fails"

with run(env=defaultenv, adminport=freeport()) as postgrest:
with run(env=defaultenv) as postgrest:
# delete the unix socket to make the main app fail
os.remove(defaultenv["PGRST_SERVER_UNIX_SOCKET"])
response = postgrest.admin.get("/live")
Expand Down

0 comments on commit df5d940

Please sign in to comment.