diff --git a/tests/test_postfix.py b/tests/test_postfix.py index b7feb842eb34cfd7026ed1d1698e462ebb3f8cdb..dcba0ad01083f61fdbc3d249ddc55f46d8ca318b 100755 --- a/tests/test_postfix.py +++ b/tests/test_postfix.py @@ -7,15 +7,68 @@ Check that emails can be sent. import os import subprocess import sys +import random +import time +import imp +GREEN = '\033[92m' +RED = '\033[91m' +DEF = '\033[0m' + +def print_color(txt, col): + print('%s%s%s' % (col, txt, DEF)) + +def print_red(txt): + print_color(txt, RED) + +def print_green(txt): + print_color(txt, GREEN) + +all_ok = True + +def get_configured_relay(): + status, out = subprocess.getstatusoutput('grep relayhost /etc/postfix/main.cf') + if status == 0: + return out.split('relayhost =')[1] + +def check_relay(): + global all_ok + configured_relay = get_configured_relay() + + print('Checking if SMTP relay conforms to conf') + if os.path.isfile('../utils.py'): + es_utils = imp.load_source('es_utils', '../utils.py') + conf = es_utils.load_conf() + conf_relay = conf.get('EMAIL_SMTP_SERVER') + if conf_relay != configured_relay: + print_red('Configured STMP relay (%s) does not match the expected value (%s)' % (configured_relay, conf_relay)) + all_ok = False + +def send_test_email(): + global all_ok + email = "noreply+%s@ubicast.eu" % random.randint(0, 1000) + cmd = 'echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" %s' % email + status, out = subprocess.getstatusoutput(cmd) + time.sleep(5) + status, out = subprocess.getstatusoutput('grep %s /var/log/mail.log | grep bounced' % email) + if status == 0: + print_red('Sending mail failed') + all_ok = False + else: + print_green('Email sent') if not os.path.exists('/etc/postfix'): - print('Postfix dir does not exists, please install postfix.') - sys.exit(1) + print_red('Postfix dir does not exists, please install postfix.') + all_ok = False else: # check that postfix listens the port 25 correctly status, out = subprocess.getstatusoutput('netstat -pant | grep master | grep 127.0.0.1:25') if status != 0: - print('The port 25 is not listened by any process.') - sys.exit(1) - print('Postfix listening port: OK.') + print_red('The port 25 is not listened by any process.') + all_ok = False + else: + print_green('Postfix listening port: OK.') + check_relay() + send_test_email() + +sys.exit(int(not all_ok)) diff --git a/tests/test_send_email.sh b/tests/test_send_email.sh deleted file mode 100755 index 12483ea868ec48fe4ff631480c3fd4f112ceccc7..0000000000000000000000000000000000000000 --- a/tests/test_send_email.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -# Criticality: High -# Check that emails can be sent -RAND="$RANDOM" -EMAIL="noreply+$RAND@ubicast.eu" -RELAY=`grep relayhost /etc/postfix/main.cf` -echo "Sending email to $EMAIL using $RELAY" -echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" $EMAIL -sleep 10 -grep $EMAIL /var/log/mail.log | grep bounced > /dev/null -if [ $? -eq 0 ]; then - echo "Sending mail failed" - exit 1 -fi -echo "Sending mail succeeded" -exit 0 -# check un webservice qui recoit le mail