From 8902cd5aa66b24a01d2eebe9fa4689a4b0c1dd44 2024-09-06 15:02:01 From: Branko Majic Date: 2024-09-06 15:02:01 Subject: [PATCH] MAR-218: Improve mail server role test reliability: - Introduce sleep after sending out mails in order to ensure that Postfix has had enough time to process the messages. - Relevant only for tests that take care of parsing the mail log. - Solves the false negatives caused by timing issues. - It might be a good idea down the line to implement some kind of retry/backoff mechanism instead. --- diff --git a/roles/mail_server/molecule/default/tests/test_default.py b/roles/mail_server/molecule/default/tests/test_default.py index feee441b137226907dc6f61f16dc70cb3f16b20d..62017eb87d2569ebb20eb000a10901367cc2407f 100644 --- a/roles/mail_server/molecule/default/tests/test_default.py +++ b/roles/mail_server/molecule/default/tests/test_default.py @@ -1,5 +1,6 @@ import os import re +import time import uuid import defusedxml.ElementTree as ElementTree @@ -258,6 +259,9 @@ def test_postfix_delivery_to_dovecot(host): send = host.run('swaks --header %s --suppress-data --to john.doe@domain1 --server %s', "Message-Id: <%s>" % message_id, hostname) assert send.rc == 0 + # Wait for a little while for message to be processed. + time.sleep(5) + with host.sudo(): mail_log = host.file('/var/log/mail.log') pattern = r"dovecot: lda\(john.doe@domain1\)<\d+><.+?>: msgid=<%s>: saved mail to INBOX" % message_id @@ -534,6 +538,9 @@ def test_postfix_local_delivery(host): send = host.run('swaks --header %s --suppress-data --to localuser@localhost --server localhost', "Message-Id: <%s>" % message_id) assert send.rc == 0 + # Wait for a little while for message to be processed. + time.sleep(5) + with host.sudo(): mail_log = host.file('/var/log/mail.log') @@ -562,6 +569,9 @@ def test_postfix_sends_mails_without_tls_when_unavailable(host): send = host.run('swaks --header %s --suppress-data --to root@smtp-server-refusing-tls --server localhost', "Message-Id: <%s>" % message_id) assert send.rc == 0 + # Wait for a little while for message to be processed. + time.sleep(5) + with host.sudo(): mail_log = host.file('/var/log/mail.log') @@ -590,6 +600,9 @@ def test_postfix_sends_mails_over_tls_when_available(host): send = host.run('swaks --tls --header %s --suppress-data --to root@smtp-server-requiring-tls --server localhost', "Message-Id: <%s>" % message_id) assert send.rc == 0 + # Wait for a little while for message to be processed. + time.sleep(5) + with host.sudo(): mail_log = host.file('/var/log/mail.log') diff --git a/roles/mail_server/molecule/default/tests/test_optional.py b/roles/mail_server/molecule/default/tests/test_optional.py index 8f7aefba2b32b15ea5deee6317cfc064bf97e20c..36e2728619eecc877e6f55a3f514fb314ad19488 100644 --- a/roles/mail_server/molecule/default/tests/test_optional.py +++ b/roles/mail_server/molecule/default/tests/test_optional.py @@ -57,9 +57,11 @@ def test_local_aliases(host): message_id = "%s@localhost" % str(uuid.uuid4()) send = host.run('swaks --header %s --suppress-data --to root@localhost', "Message-Id: <%s>" % message_id) - time.sleep(1) assert send.rc == 0 + # Wait for a little while for message to be processed. + time.sleep(5) + with host.sudo(): mail_log = host.file('/var/log/mail.log') pattern = r"dovecot: lda\(john.doe@domain1\)<\d+><.+?>: msgid=<%s>: saved mail to INBOX" % message_id