|
21 | 21 | ) |
22 | 22 |
|
23 | 23 |
|
| 24 | +def match_log(output, matchers): |
| 25 | + ito = iter(output) |
| 26 | + itm = iter(matchers) |
| 27 | + nextMatcher = next(itm, None) |
| 28 | + while nextMatcher is not None and (line := next(ito, None)) is not None: |
| 29 | + if re.match(nextMatcher, line) is not None: |
| 30 | + nextMatcher = next(itm, None) |
| 31 | + if nextMatcher is not None: |
| 32 | + raise AssertionError( |
| 33 | + f"Expected log line matching {nextMatcher} not found in output" |
| 34 | + ) |
| 35 | + |
| 36 | + |
24 | 37 | def test_connect_with_dburi(dburi, defaultenv): |
25 | 38 | "Connecting with db-uri instead of LIPQ* environment variables should work." |
26 | 39 | defaultenv_without_libpq = { |
@@ -701,54 +714,44 @@ def test_log_level(level, defaultenv): |
701 | 714 | response = postgrest.session.get("/") |
702 | 715 | assert response.status_code == 200 |
703 | 716 |
|
704 | | - output = sorted(postgrest.read_stdout(nlines=7)) |
| 717 | + output = postgrest.read_stdout(nlines=7) |
705 | 718 |
|
706 | 719 | if level == "crit": |
707 | 720 | assert len(output) == 0 |
708 | 721 | elif level == "error": |
709 | | - assert re.match( |
710 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
711 | | - output[0], |
| 722 | + match_log( |
| 723 | + output, |
| 724 | + [r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"'], |
712 | 725 | ) |
713 | 726 | assert len(output) == 1 |
714 | 727 | elif level == "warn": |
715 | | - assert re.match( |
716 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
717 | | - output[0], |
718 | | - ) |
719 | | - assert re.match( |
720 | | - r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
721 | | - output[1], |
| 728 | + match_log( |
| 729 | + output, |
| 730 | + [ |
| 731 | + r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
| 732 | + r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
| 733 | + ], |
722 | 734 | ) |
723 | 735 | assert len(output) == 2 |
724 | 736 | elif level == "info": |
725 | | - assert re.match( |
726 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
727 | | - output[0], |
728 | | - ) |
729 | | - assert re.match( |
730 | | - r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
731 | | - output[1], |
732 | | - ) |
733 | | - assert re.match( |
734 | | - r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
735 | | - output[2], |
| 737 | + match_log( |
| 738 | + output, |
| 739 | + [ |
| 740 | + r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
| 741 | + r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
| 742 | + r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
| 743 | + ], |
736 | 744 | ) |
737 | 745 | assert len(output) == 3 |
738 | 746 | elif level == "debug": |
739 | | - assert re.match( |
740 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
741 | | - output[0], |
| 747 | + match_log( |
| 748 | + output, |
| 749 | + [ |
| 750 | + r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
| 751 | + r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
| 752 | + r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
| 753 | + ], |
742 | 754 | ) |
743 | | - assert re.match( |
744 | | - r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
745 | | - output[1], |
746 | | - ) |
747 | | - assert re.match( |
748 | | - r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
749 | | - output[2], |
750 | | - ) |
751 | | - |
752 | 755 | assert len(output) == 7 |
753 | 756 | assert any("Connection" and "is available" in line for line in output) |
754 | 757 | assert any("Connection" and "is used" in line for line in output) |
@@ -1386,16 +1389,21 @@ def test_db_error_logging_to_stderr(level, defaultenv, metapostgrest): |
1386 | 1389 | assert response.status_code == 500 |
1387 | 1390 |
|
1388 | 1391 | # ensure the message appears on the logs |
1389 | | - output = sorted(postgrest.read_stdout(nlines=6)) |
| 1392 | + output = postgrest.read_stdout(nlines=6) |
1390 | 1393 |
|
1391 | 1394 | if level == "crit": |
1392 | 1395 | assert len(output) == 0 |
1393 | 1396 | elif level == "debug": |
1394 | | - assert " 500 " in output[0] |
1395 | | - assert "canceling statement due to statement timeout" in output[5] |
| 1397 | + match_log( |
| 1398 | + output, |
| 1399 | + [ |
| 1400 | + r".*canceling statement due to statement timeout.*", |
| 1401 | + r".*500.*", |
| 1402 | + ], |
| 1403 | + ) |
1396 | 1404 | else: |
1397 | | - assert " 500 " in output[0] |
1398 | | - assert "canceling statement due to statement timeout" in output[1] |
| 1405 | + assert " 500 " in output[1] |
| 1406 | + assert "canceling statement due to statement timeout" in output[0] |
1399 | 1407 |
|
1400 | 1408 | reset_statement_timeout(metapostgrest, role) |
1401 | 1409 |
|
@@ -1589,18 +1597,17 @@ def test_log_pool_req_observation(level, defaultenv): |
1589 | 1597 |
|
1590 | 1598 | headers = jwtauthheader({"role": "postgrest_test_author"}, SECRET) |
1591 | 1599 |
|
1592 | | - pool_req = "Trying to borrow a connection from pool" |
1593 | | - pool_req_fullfill = "Borrowed a connection from the pool" |
| 1600 | + pool_req = r".*Trying to borrow a connection from pool.*" |
| 1601 | + pool_req_fullfill = r".*Borrowed a connection from the pool.*" |
1594 | 1602 |
|
1595 | 1603 | with run(env=env) as postgrest: |
1596 | 1604 |
|
1597 | 1605 | postgrest.session.get("/authors_only", headers=headers) |
1598 | 1606 |
|
1599 | 1607 | if level == "debug": |
1600 | | - output = postgrest.read_stdout(nlines=5) |
1601 | | - assert pool_req in output[1] |
1602 | | - assert pool_req_fullfill in output[4] |
1603 | | - assert len(output) == 5 |
| 1608 | + output = postgrest.read_stdout(nlines=7) |
| 1609 | + assert len(output) == 6 |
| 1610 | + match_log(output, [pool_req, pool_req_fullfill]) |
1604 | 1611 | elif level == "info": |
1605 | 1612 | output = postgrest.read_stdout(nlines=4) |
1606 | 1613 | assert len(output) == 1 |
|
0 commit comments