Commit 30ea193c authored by ale's avatar ale
Browse files

More thorough logging of all SMTP and IMAP connections

parent 94cd2b42
Pipeline #5848 passed with stages
in 2 minutes and 4 seconds
......@@ -25,12 +25,13 @@ class _SSLContext(ssl.SSLContext):
def imap_conn(server_name, server_addr=None):
def imap_conn(ctx, server_name, server_addr=None):
if not server_addr:
server_addr = server_name
ssl_ctx = _SSLContext(ssl.PROTOCOL_TLSv1_2)
ctx.log('making IMAP connection to %s (%s)', server_name, server_addr)
conn = imaplib.IMAP4_SSL(server_addr, ssl_context=ssl_ctx)
......@@ -46,7 +47,7 @@ class Timeout(Exception):
def wait_for_unique_email(conn, message_id, timeout=300):
def wait_for_unique_email(ctx, conn, message_id, timeout=300):
def _check_for_msg():
typ, data = conn.uid(
'search', None, 'HEADER', 'Message-ID', message_id)
......@@ -61,23 +62,28 @@ def wait_for_unique_email(conn, message_id, timeout=300):
return msg
return None
ctx.log('waiting %d seconds for message %s to appear',
timeout, message_id)
deadline = time.time() + timeout
while time.time() < deadline:
msg = _check_for_msg()
if msg:
ctx.log('message found and removed successfully')
return msg
ctx.log('timeout waiting for message to appear')
raise Timeout()
def smtp_conn(server_name, server_addr=None, port=587):
def smtp_conn(ctx, server_name, server_addr=None, port=587):
if not server_addr:
server_addr = server_name
ssl_ctx = _SSLContext(ssl.PROTOCOL_TLSv1_2)
ctx.log('making SMTP connection to %s (%s)', server_name, server_addr)
conn = smtplib.SMTP(server_addr, port, timeout=SMTP_TIMEOUT)
......@@ -100,7 +106,8 @@ def _create_msg(sender, rcpt):
return msg, message_id
def send_test_email(conn, sender, rcpt):
def send_test_email(ctx, conn, sender, rcpt):
ctx.log('sending test email %s -> %s', sender, rcpt)
msg, message_id = _create_msg(sender, rcpt)
conn.sendmail(sender, [rcpt], msg.as_string())
return message_id
......@@ -2,8 +2,7 @@ from ai_diagnostics import mail
def probe_imap(ctx):
ctx.log('connecting to %s', ctx['server'])
with mail.imap_conn(ctx['server'], ctx.get('addr')) as conn:
with mail.imap_conn(ctx, ctx['server'], ctx.get('addr')) as conn:
ctx.log('logging in as %s', ctx['credentials']['username'])
......@@ -8,15 +8,15 @@ def _delivery(ctx, smtp_server, imap_server, smtp_username, smtp_password,
Waits until the email is received by the target account.
ctx.log('sending mail from %s to %s', smtp_username, imap_username)
with mail.smtp_conn(smtp_server) as smtp:
with mail.smtp_conn(ctx, smtp_server) as smtp:
smtp.login(smtp_username, smtp_password)
msgid = mail.send_test_email(smtp, smtp_username, imap_username)
msgid = mail.send_test_email(ctx, smtp, smtp_username, imap_username)
ctx.log('waiting to receive email %s in %s/INBOX', msgid, imap_username)
with mail.imap_conn(imap_server) as imap:
with mail.imap_conn(ctx, imap_server) as imap:
imap.login(imap_username, imap_password)'INBOX')
mail.wait_for_unique_email(imap, msgid)
mail.wait_for_unique_email(ctx, imap, msgid)
def probe_mail_local_delivery(ctx):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment