refactor: DRY pytest no pool connection logic
This commit is contained in:
committed by
Steve Chavez
parent
4ab5e63e58
commit
71a5748718
+19
-39
@@ -140,7 +140,7 @@ def dumpconfig(configpath=None, env=None, stdin=None):
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def run(configpath=None, stdin=None, env=None, port=None, host=None):
|
def run(configpath=None, stdin=None, env=None, port=None, host=None, no_pool_connection_available=False):
|
||||||
"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 {}
|
||||||
env["PGRST_DB_POOL"] = "1"
|
env["PGRST_DB_POOL"] = "1"
|
||||||
@@ -190,6 +190,9 @@ def run(configpath=None, stdin=None, env=None, port=None, host=None):
|
|||||||
admin=PostgrestSession(adminurl),
|
admin=PostgrestSession(adminurl),
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
|
if no_pool_connection_available:
|
||||||
|
sleep_pool_connection(baseurl, 10)
|
||||||
|
|
||||||
remaining_output = process.stdout.read()
|
remaining_output = process.stdout.read()
|
||||||
if remaining_output:
|
if remaining_output:
|
||||||
print(remaining_output.decode())
|
print(remaining_output.decode())
|
||||||
@@ -230,6 +233,17 @@ def wait_until_ready(url):
|
|||||||
raise PostgrestTimedOut()
|
raise PostgrestTimedOut()
|
||||||
|
|
||||||
|
|
||||||
|
def sleep_pool_connection(url, seconds):
|
||||||
|
"Sleep a pool connection by calling an RPC that uses pg_sleep"
|
||||||
|
session = requests_unixsocket.Session()
|
||||||
|
|
||||||
|
# The try/except is a hack for not waiting for the response,
|
||||||
|
# taken from https://stackoverflow.com/a/45601591/4692662
|
||||||
|
try:
|
||||||
|
session.get(url + f"/rpc/sleep?seconds={seconds}", timeout=0.1)
|
||||||
|
except requests.exceptions.ReadTimeout:
|
||||||
|
pass
|
||||||
|
|
||||||
def authheader(token):
|
def authheader(token):
|
||||||
"Bearer token HTTP authorization header."
|
"Bearer token HTTP authorization header."
|
||||||
return {"Authorization": f"Bearer {token}"}
|
return {"Authorization": f"Bearer {token}"}
|
||||||
@@ -946,20 +960,7 @@ def test_log_level(level, has_output, defaultenv):
|
|||||||
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
|
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
|
||||||
"no pool connection should be consumed for failing on invalid http logic"
|
"no pool connection should be consumed for failing on invalid http logic"
|
||||||
|
|
||||||
env = {
|
with run(env=defaultenv, no_pool_connection_available=True) as postgrest:
|
||||||
**defaultenv,
|
|
||||||
"PGRST_DB_POOL": "1",
|
|
||||||
}
|
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
|
||||||
# First we retain the only pool connection available
|
|
||||||
# The try/except is a hack for not waiting for the response, taken from https://stackoverflow.com/a/45601591/4692662
|
|
||||||
try:
|
|
||||||
postgrest.session.get("/rpc/sleep?seconds=50", timeout=0.1)
|
|
||||||
except requests.exceptions.ReadTimeout:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Then the following requests should succeed rapidly
|
|
||||||
|
|
||||||
# not found nested route shouldn't require opening a connection
|
# not found nested route shouldn't require opening a connection
|
||||||
response = postgrest.session.head("/path/notfound")
|
response = postgrest.session.head("/path/notfound")
|
||||||
@@ -975,20 +976,7 @@ def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
|
|||||||
def test_no_pool_connection_required_on_options(defaultenv):
|
def test_no_pool_connection_required_on_options(defaultenv):
|
||||||
"no pool connection should be consumed for OPTIONS requests"
|
"no pool connection should be consumed for OPTIONS requests"
|
||||||
|
|
||||||
env = {
|
with run(env=defaultenv, no_pool_connection_available=True) as postgrest:
|
||||||
**defaultenv,
|
|
||||||
"PGRST_DB_POOL": "1",
|
|
||||||
}
|
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
|
||||||
# First we retain the only pool connection available
|
|
||||||
# The try/except is a hack for not waiting for the response, taken from https://stackoverflow.com/a/45601591/4692662
|
|
||||||
try:
|
|
||||||
postgrest.session.get("/rpc/sleep?seconds=50", timeout=0.1)
|
|
||||||
except requests.exceptions.ReadTimeout:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Then the following OPTIONS requests should succeed rapidly
|
|
||||||
|
|
||||||
# OPTIONS on a table shouldn't require opening a connection
|
# OPTIONS on a table shouldn't require opening a connection
|
||||||
response = postgrest.session.options("/projects")
|
response = postgrest.session.options("/projects")
|
||||||
@@ -1002,17 +990,9 @@ def test_no_pool_connection_required_on_options(defaultenv):
|
|||||||
def test_no_pool_connection_required_on_bad_jwt_claim(defaultenv):
|
def test_no_pool_connection_required_on_bad_jwt_claim(defaultenv):
|
||||||
"no pool connection should be consumed for failing on invalid jwt"
|
"no pool connection should be consumed for failing on invalid jwt"
|
||||||
|
|
||||||
env = {**defaultenv, "PGRST_DB_POOL": "1", "PGRST_JWT_SECRET": SECRET}
|
env = {**defaultenv, "PGRST_JWT_SECRET": SECRET}
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
with run(env=env, no_pool_connection_available=True) as postgrest:
|
||||||
# First we retain the only pool connection available
|
|
||||||
# The try/except is a hack for not waiting for the response, taken from https://stackoverflow.com/a/45601591/4692662
|
|
||||||
try:
|
|
||||||
postgrest.session.get("/rpc/sleep?seconds=50", timeout=0.1)
|
|
||||||
except requests.exceptions.ReadTimeout:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Then the following requests should succeed rapidly
|
|
||||||
|
|
||||||
# A JWT with an invalid signature shouldn't open a connection
|
# A JWT with an invalid signature shouldn't open a connection
|
||||||
headers = jwtauthheader({"role": "postgrest_test_author"}, "Wrong Secret")
|
headers = jwtauthheader({"role": "postgrest_test_author"}, "Wrong Secret")
|
||||||
|
|||||||
Reference in New Issue
Block a user