From e848fc10ac8aa517474ed748f7b47cff8c419c53 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 16 Oct 2023 18:13:14 +0200 Subject: [PATCH] introduce remotelog fixture for capturing systemd-unit logs --- online-tests/conftest.py | 30 ++++++++++++++++++------------ online-tests/test_0_basic.py | 4 ++++ online-tests/test_0_login.py | 2 +- online-tests/test_1_deltachat.py | 5 ++--- 4 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 online-tests/test_0_basic.py diff --git a/online-tests/conftest.py b/online-tests/conftest.py index 7b3815ee..cd2a2dbb 100644 --- a/online-tests/conftest.py +++ b/online-tests/conftest.py @@ -30,11 +30,8 @@ def maildomain(): @pytest.fixture -def chatmail_ssh(maildomain): - domain = os.environ.get("CHATMAIL_SSH") - if not domain: - domain = maildomain - return domain +def sshdomain(maildomain): + return os.environ.get("CHATMAIL_SSH", maildomain) def pytest_report_header(): @@ -51,6 +48,7 @@ def imap(maildomain): class ImapConn: AuthError = imaplib.IMAP4.error + logunit = "dovecot" def __init__(self, host): self.host = host @@ -71,6 +69,7 @@ def smtp(maildomain): class SmtpConn: AuthError = smtplib.SMTPAuthenticationError + logunit = "postfix" def __init__(self, host): self.host = host @@ -158,13 +157,20 @@ def cmfactory(request, maildomain, gencreds, tmpdir, data): @pytest.fixture -def dovelogreader(chatmail_ssh): - def remote_reader(): - popen = subprocess.Popen( - ["ssh", f"root@{chatmail_ssh}", "journalctl -f -u dovecot"], +def remotelog(sshdomain): + return RemoteLog(sshdomain) + + +class RemoteLog: + def __init__(self, sshdomain): + self.sshdomain = sshdomain + + def iter(self, unit=""): + getjournal = f"journalctl -f -u {unit}" if unit else "journalctl -f" + self.popen = subprocess.Popen( + ["ssh", f"root@{self.sshdomain}", getjournal], stdout=subprocess.PIPE, ) while 1: - yield popen.stdout.readline() - - return remote_reader + line = self.popen.stdout.readline() + yield line.decode().strip().lower() diff --git a/online-tests/test_0_basic.py b/online-tests/test_0_basic.py new file mode 100644 index 00000000..5d3a9648 --- /dev/null +++ b/online-tests/test_0_basic.py @@ -0,0 +1,4 @@ +def test_remotelog(remotelog, imap_or_smtp): + lineproducer = remotelog.iter(imap_or_smtp.logunit) + imap_or_smtp.connect() + assert imap_or_smtp.logunit in next(lineproducer) diff --git a/online-tests/test_0_login.py b/online-tests/test_0_login.py index ac1a4867..63388b01 100644 --- a/online-tests/test_0_login.py +++ b/online-tests/test_0_login.py @@ -17,7 +17,7 @@ def test_login_basic_functioning(imap_or_smtp, gencreds, lp): imap_or_smtp.connect() lp.sec("success") - lp.sec("reconnect and verify wrong password fails {user} ") + lp.sec(f"reconnect and verify wrong password fails {user} ") imap_or_smtp.connect() with pytest.raises(imap_or_smtp.AuthError): imap_or_smtp.login(user, password + "wrong") diff --git a/online-tests/test_1_deltachat.py b/online-tests/test_1_deltachat.py index 588751d8..977368d6 100644 --- a/online-tests/test_1_deltachat.py +++ b/online-tests/test_1_deltachat.py @@ -19,7 +19,7 @@ class TestEndToEndDeltaChat: assert msg2.text == "message0" @pytest.mark.slow - def test_exceed_quota(self, cmfactory, lp, tmpdir, dovelogreader): + def test_exceed_quota(self, cmfactory, lp, tmpdir, remotelog): """This is a very slow test as it needs to upload >100MB of mail data before quota is exceeded, and thus depends on the speed of the upload. """ @@ -48,8 +48,7 @@ class TestEndToEndDeltaChat: addr = ac2.get_config("addr").lower() saved_ok = 0 - for line in dovelogreader(): - line = line.decode().lower().strip() + for line in remotelog.iter("dovecot"): if addr not in line: # print(line) continue