Skip to content

Commit

Permalink
test: use postgrest.read_stdout in io tests (#3412)
Browse files Browse the repository at this point in the history
It's easier to maintain this way in case there are new log lines.
  • Loading branch information
steve-chavez authored Apr 15, 2024
1 parent 69c6ce9 commit 9d1dc78
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions test/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,16 +772,8 @@ def test_admin_works_with_host_special_values(specialhostvalue, defaultenv):
assert response.status_code == 200


@pytest.mark.parametrize(
"level, has_output",
[
("info", [True, True, True]),
("warn", [False, True, True]),
("error", [False, False, True]),
("crit", [False, False, False]),
],
)
def test_log_level(level, has_output, defaultenv):
@pytest.mark.parametrize("level", ["crit", "error", "warn", "info"])
def test_log_level(level, defaultenv):
"log_level should filter request logging"

env = {**defaultenv, "PGRST_LOG_LEVEL": level}
Expand All @@ -791,29 +783,49 @@ def test_log_level(level, has_output, defaultenv):
headers = jwtauthheader(claim, SECRET)

with run(env=env) as postgrest:
response = postgrest.session.get("/", headers=headers)
assert response.status_code == 500

response = postgrest.session.get("/unknown")
assert response.status_code == 404

response = postgrest.session.get("/")
assert response.status_code == 200
if has_output[0]:

output = sorted(postgrest.read_stdout(nlines=3))

if level == "crit":
assert len(output) == 0
elif level == "error":
assert re.match(
r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 - "" "python-requests/.+"',
postgrest.process.stdout.readline().decode(),
r'- - - \[.+\] "GET / HTTP/1.1" 500 - "" "python-requests/.+"',
output[0],
)
assert len(output) == 1
elif level == "warn":
assert re.match(
r'- - - \[.+\] "GET / HTTP/1.1" 500 - "" "python-requests/.+"',
output[0],
)

response = postgrest.session.get("/unknown")
assert response.status_code == 404
if has_output[1]:
assert re.match(
r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 - "" "python-requests/.+"',
postgrest.process.stdout.readline().decode(),
output[1],
)

response = postgrest.session.get("/", headers=headers)
assert response.status_code == 500
if has_output[2]:
assert len(output) == 2
else:
assert re.match(
r'- - - \[.+\] "GET / HTTP/1.1" 500 - "" "python-requests/.+"',
postgrest.process.stdout.readline().decode(),
output[0],
)
assert re.match(
r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 - "" "python-requests/.+"',
output[1],
)
assert re.match(
r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 - "" "python-requests/.+"',
output[2],
)
assert len(output) == 3


def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
Expand Down Expand Up @@ -1074,10 +1086,9 @@ def test_log_postgrest_version(defaultenv):
with run(env=defaultenv, no_startup_stdout=False) as postgrest:
version = postgrest.session.head("/").headers["Server"].split("/")[1]

assert (
"Starting PostgREST %s..." % version
in postgrest.process.stdout.readline().decode()
)
output = sorted(postgrest.read_stdout(nlines=5))

assert "Starting PostgREST %s..." % version in output[3]


def test_succeed_w_role_having_superuser_settings(defaultenv):
Expand Down

0 comments on commit 9d1dc78

Please sign in to comment.