Skip to content
Snippets Groups Projects
Commit a66a7d52 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Changed timeout handling for email test.

parent f849ac57
No related branches found
No related tags found
No related merge requests found
...@@ -89,26 +89,31 @@ def send_test_email(): ...@@ -89,26 +89,31 @@ def send_test_email():
cmd = 'echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" %s' % email cmd = 'echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" %s' % email
subprocess.getstatusoutput(cmd) subprocess.getstatusoutput(cmd)
timeout = 120 timeout = 120
waited = 2 waited = 1
delay = 2
print('Timeout to find email sending log is set to %s seconds.' % timeout)
out = '' out = ''
if os.path.isfile('/var/log/mail.log'): if os.path.isfile('/var/log/mail.log'):
cmd = 'grep "%s" /var/log/mail.log' % email cmd = 'grep "%s" /var/log/mail.log' % email
else: else:
print('/var/log/mail.log not found, trying journalctl') print('/var/log/mail.log not found, trying journalctl')
cmd = 'journalctl -u postfix | grep %s' % email cmd = 'journalctl -u postfix | grep %s' % email
time.sleep(1)
while True: while True:
status, out = subprocess.getstatusoutput(cmd) status, out = subprocess.getstatusoutput(cmd)
if status == 0: if status == 0:
out = out.strip().split('\n')[-1] out = out.strip().split('\n')[-1]
if 'status=deferred' not in out: if 'status=deferred' not in out:
print('Log entry found after %s seconds.' % waited)
break break
if waited >= timeout: if waited >= timeout:
print_red('Failed to send email.') print_red('Failed to send email.')
print('No log entry found after %s seconds using command: %s' % (waited, cmd)) print('No log entry found after %s seconds using command: %s' % (waited, cmd))
return 1 return 1
print('Waiting %s seconds for email sending status.' % (timeout - waited)) print('No log entry found after %s seconds, waiting %s more seconds...' % (waited, delay))
time.sleep(waited) time.sleep(delay)
waited *= 2 waited += delay
delay *= 2
if 'bounced' not in out or 'The email account that you tried to reach does not exist.' in out: if 'bounced' not in out or 'The email account that you tried to reach does not exist.' in out:
print_green('Email sent.') print_green('Email sent.')
return 0 return 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment