|
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 = { |
@@ -661,54 +674,44 @@ def test_log_level(level, defaultenv): |
661 | 674 | response = postgrest.session.get("/") |
662 | 675 | assert response.status_code == 200 |
663 | 676 |
|
664 | | - output = sorted(postgrest.read_stdout(nlines=7)) |
| 677 | + output = postgrest.read_stdout(nlines=7) |
665 | 678 |
|
666 | 679 | if level == "crit": |
667 | 680 | assert len(output) == 0 |
668 | 681 | elif level == "error": |
669 | | - assert re.match( |
670 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
671 | | - output[0], |
| 682 | + match_log( |
| 683 | + output, |
| 684 | + [r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"'], |
672 | 685 | ) |
673 | 686 | assert len(output) == 1 |
674 | 687 | elif level == "warn": |
675 | | - assert re.match( |
676 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
677 | | - output[0], |
678 | | - ) |
679 | | - assert re.match( |
680 | | - r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
681 | | - output[1], |
| 688 | + match_log( |
| 689 | + output, |
| 690 | + [ |
| 691 | + r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
| 692 | + r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
| 693 | + ], |
682 | 694 | ) |
683 | 695 | assert len(output) == 2 |
684 | 696 | elif level == "info": |
685 | | - assert re.match( |
686 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
687 | | - output[0], |
688 | | - ) |
689 | | - assert re.match( |
690 | | - r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
691 | | - output[1], |
692 | | - ) |
693 | | - assert re.match( |
694 | | - r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
695 | | - output[2], |
| 697 | + match_log( |
| 698 | + output, |
| 699 | + [ |
| 700 | + r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
| 701 | + r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
| 702 | + r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
| 703 | + ], |
696 | 704 | ) |
697 | 705 | assert len(output) == 3 |
698 | 706 | elif level == "debug": |
699 | | - assert re.match( |
700 | | - r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
701 | | - output[0], |
| 707 | + match_log( |
| 708 | + output, |
| 709 | + [ |
| 710 | + r'- - - \[.+\] "GET / HTTP/1.1" 500 \d+ "" "python-requests/.+"', |
| 711 | + r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
| 712 | + r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
| 713 | + ], |
702 | 714 | ) |
703 | | - assert re.match( |
704 | | - r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"', |
705 | | - output[1], |
706 | | - ) |
707 | | - assert re.match( |
708 | | - r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"', |
709 | | - output[2], |
710 | | - ) |
711 | | - |
712 | 715 | assert len(output) == 7 |
713 | 716 | assert any("Connection" and "is available" in line for line in output) |
714 | 717 | assert any("Connection" and "is used" in line for line in output) |
@@ -1346,16 +1349,21 @@ def test_db_error_logging_to_stderr(level, defaultenv, metapostgrest): |
1346 | 1349 | assert response.status_code == 500 |
1347 | 1350 |
|
1348 | 1351 | # ensure the message appears on the logs |
1349 | | - output = sorted(postgrest.read_stdout(nlines=6)) |
| 1352 | + output = postgrest.read_stdout(nlines=6) |
1350 | 1353 |
|
1351 | 1354 | if level == "crit": |
1352 | 1355 | assert len(output) == 0 |
1353 | 1356 | elif level == "debug": |
1354 | | - assert " 500 " in output[0] |
1355 | | - assert "canceling statement due to statement timeout" in output[5] |
| 1357 | + match_log( |
| 1358 | + output, |
| 1359 | + [ |
| 1360 | + r".*canceling statement due to statement timeout.*", |
| 1361 | + r".*500.*", |
| 1362 | + ], |
| 1363 | + ) |
1356 | 1364 | else: |
1357 | | - assert " 500 " in output[0] |
1358 | | - assert "canceling statement due to statement timeout" in output[1] |
| 1365 | + assert " 500 " in output[1] |
| 1366 | + assert "canceling statement due to statement timeout" in output[0] |
1359 | 1367 |
|
1360 | 1368 | reset_statement_timeout(metapostgrest, role) |
1361 | 1369 |
|
@@ -1549,18 +1557,17 @@ def test_log_pool_req_observation(level, defaultenv): |
1549 | 1557 |
|
1550 | 1558 | headers = jwtauthheader({"role": "postgrest_test_author"}, SECRET) |
1551 | 1559 |
|
1552 | | - pool_req = "Trying to borrow a connection from pool" |
1553 | | - pool_req_fullfill = "Borrowed a connection from the pool" |
| 1560 | + pool_req = r".*Trying to borrow a connection from pool.*" |
| 1561 | + pool_req_fullfill = r".*Borrowed a connection from the pool.*" |
1554 | 1562 |
|
1555 | 1563 | with run(env=env) as postgrest: |
1556 | 1564 |
|
1557 | 1565 | postgrest.session.get("/authors_only", headers=headers) |
1558 | 1566 |
|
1559 | 1567 | if level == "debug": |
1560 | | - output = postgrest.read_stdout(nlines=5) |
1561 | | - assert pool_req in output[1] |
1562 | | - assert pool_req_fullfill in output[4] |
1563 | | - assert len(output) == 5 |
| 1568 | + output = postgrest.read_stdout(nlines=7) |
| 1569 | + assert len(output) == 6 |
| 1570 | + match_log(output, [pool_req, pool_req_fullfill]) |
1564 | 1571 | elif level == "info": |
1565 | 1572 | output = postgrest.read_stdout(nlines=4) |
1566 | 1573 | assert len(output) == 1 |
|
0 commit comments