test: Refactor test-cases for admin/health endpoint
This commit is contained in:
committed by
Wolfgang Walther
parent
b9c497a3ab
commit
efc7a21427
+20
-22
@@ -69,7 +69,8 @@ class PostgrestSession(requests_unixsocket.Session):
|
|||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class PostgrestProcess:
|
class PostgrestProcess:
|
||||||
"Running PostgREST process and its corresponding endpoint."
|
"Running PostgREST process and its corresponding main and admin endpoints."
|
||||||
|
admin: object
|
||||||
process: object
|
process: object
|
||||||
session: object
|
session: object
|
||||||
|
|
||||||
@@ -135,7 +136,7 @@ def dumpconfig(configpath=None, env=None, stdin=None):
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def run(configpath=None, stdin=None, env=None, port=None):
|
def run(configpath=None, stdin=None, env=None, port=None, adminport=None):
|
||||||
"Run PostgREST and yield an endpoint that is ready for connections."
|
"Run PostgREST and yield an endpoint that is ready for connections."
|
||||||
env = env or {}
|
env = env or {}
|
||||||
|
|
||||||
@@ -149,6 +150,12 @@ def run(configpath=None, stdin=None, env=None, port=None):
|
|||||||
env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile)
|
env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile)
|
||||||
baseurl = "http+unix://" + urllib.parse.quote_plus(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
|
||||||
|
|
||||||
command = [POSTGREST_BIN]
|
command = [POSTGREST_BIN]
|
||||||
env["HPCTIXFILE"] = hpctixfile()
|
env["HPCTIXFILE"] = hpctixfile()
|
||||||
|
|
||||||
@@ -165,7 +172,11 @@ def run(configpath=None, stdin=None, env=None, port=None):
|
|||||||
|
|
||||||
wait_until_ready(baseurl)
|
wait_until_ready(baseurl)
|
||||||
|
|
||||||
yield PostgrestProcess(process=process, session=PostgrestSession(baseurl))
|
yield PostgrestProcess(
|
||||||
|
process=process,
|
||||||
|
session=PostgrestSession(baseurl),
|
||||||
|
admin=PostgrestSession(adminurl) if adminurl else None,
|
||||||
|
)
|
||||||
finally:
|
finally:
|
||||||
process.terminate()
|
process.terminate()
|
||||||
try:
|
try:
|
||||||
@@ -736,14 +747,11 @@ def test_admin_healthy_w_channel(defaultenv):
|
|||||||
|
|
||||||
env = {
|
env = {
|
||||||
**defaultenv,
|
**defaultenv,
|
||||||
"PGRST_ADMIN_SERVER_PORT": "3001",
|
|
||||||
"PGRST_DB_CHANNEL_ENABLED": "true",
|
"PGRST_DB_CHANNEL_ENABLED": "true",
|
||||||
}
|
}
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
with run(env=env, adminport=freeport()) as postgrest:
|
||||||
response = requests.get(
|
response = postgrest.admin.get("/health")
|
||||||
f"http://localhost:{env['PGRST_ADMIN_SERVER_PORT']}/health"
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
@@ -752,27 +760,17 @@ def test_admin_healthy_wo_channel(defaultenv):
|
|||||||
|
|
||||||
env = {
|
env = {
|
||||||
**defaultenv,
|
**defaultenv,
|
||||||
"PGRST_ADMIN_SERVER_PORT": "3001",
|
|
||||||
"PGRST_DB_CHANNEL_ENABLED": "false",
|
"PGRST_DB_CHANNEL_ENABLED": "false",
|
||||||
}
|
}
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
with run(env=env, adminport=freeport()) as postgrest:
|
||||||
response = requests.get(
|
response = postgrest.admin.get("/health")
|
||||||
f"http://localhost:{env['PGRST_ADMIN_SERVER_PORT']}/health"
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
def test_admin_not_found(defaultenv):
|
def test_admin_not_found(defaultenv):
|
||||||
"Should get a not found from the admin server"
|
"Should get a not found from the admin server"
|
||||||
|
|
||||||
env = {
|
with run(env=defaultenv, adminport=freeport()) as postgrest:
|
||||||
**defaultenv,
|
response = postgrest.admin.get("/notfound")
|
||||||
"PGRST_ADMIN_SERVER_PORT": "3001",
|
|
||||||
}
|
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
|
||||||
response = requests.get(
|
|
||||||
f"http://localhost:{env['PGRST_ADMIN_SERVER_PORT']}/notfound"
|
|
||||||
)
|
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|||||||
Reference in New Issue
Block a user