postfix/Sanity/basic/runtest.sh

219 lines
6.4 KiB
Bash
Executable file

#!/bin/bash
#shellcheck disable=SC1091
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/postfix/Sanity/basic
# Description: Basic function
# Author: Alois Mahdal <amahdal@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2018 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1
PACKAGE="postfix"
TEST_HOME=$PWD
PID="$$"
mkesmtp() {
#
# Make ESMTP mail send commands
#
echo 'helo localhost'
echo 'mail from: <postfixtest@localhost>'
echo 'rcpt to: <root@localhost>'
echo 'data'
echo 'From: Postfixtest postfixtest@localhost>'
echo 'To: root@localhost'
echo 'Subject: Test2'
echo 'Date: Tue, 18 Jun 0102 05:12:09 +0500'
echo 'MIME-Version: 1.0'
echo 'Content-Type: text/plain;'
echo 'charset="us-ascii"'
echo 'Content-Transfer-Encoding: 7bit'
echo 'Content-Disposition: inline'
echo ''
mkmsg
echo '.'
}
distribution_xcase__enum() {
#
# Enumerate test cases
#
# Test cases:
#
# * via_mail_cmd - just send the e-mail via `mail` command
# using default config.
#
# * via_nc_esmtp - send the e-mail via `nc` command and use
# ESMTP, ie. compose the actual ESMTP stream.
#
# * via_gnutls_esmtps - enable TLS (wrapper mode, ie. dedicated
# port; no STARTTLS), and send the ESMTP stream via gnutls
# client.
#
echo via_mail_cmd
echo via_nc_esmtp
rlIsRHEL 6 \
|| echo via_gnutls_esmtps
#FIXME: ^^ maybe figure out how to make gnutls-cli trust our CA, then re-enable
# ... or just disable the whole test on RHEL-6, as RHEL-6 gets older
}
mkmsg() {
#
# Compose a message to send
#
echo "text from '$(distribution_xcase__id)'"
}
mkclientcmd() {
#
# Compose the server start command
#
echo -n "gnutls-cli"
echo -n " -V"
echo -n " --no-ca-verification"
echo -n " -p 465"
echo -n " localhost"
}
setup_postfix() {
#
# Set up postfix
#
#shellcheck disable=SC1004
rlFileBackup --clean /etc/pki/tls/certs/localhost-$PID.pem
cp "$TEST_HOME/localhost.pem" "/etc/pki/tls/certs/localhost-$PID.pem"
sed -i -e '
/#smtps/ {
s/^#//
a \
-o smtpd_tls_cert_file='"/etc/pki/tls/certs/localhost-$PID.pem"'\
-o smtpd_tls_key_file='"/etc/pki/tls/certs/localhost-$PID.pem"'\
-o smtpd_tls_loglevel=2\
-o smtpd_tls_wrappermode=yes\
-o smtpd_tls_security_level=encrypt\
-o smtpd_use_tls=yes
}
' \
/etc/postfix/master.cf
}
master_on() {
#
# True if 'master' is already listening on port $1
#
local port=$1
ss -ntlp | grep '"master"' | grep ":$port"'\>'
}
distribution_xcase__setup() {
rlFileBackup --clean /var/spool/mail/root
rlFileBackup /etc/postfix/master.cf
rlRun "rm -f /var/spool/mail/root"
case $(distribution_xcase__id) in
via_mail_cmd)
rlRun "mkmsg | mail -s test1 root@localhost" \
0 "send e-mail to root"
rlRun "postqueue -f " \
0 "flushing mail queue, forcing mail delivery"
;;
via_nc_esmtp)
rlRun "mkesmtp | while read line ; do sleep 1s ; echo "$line" ; done | nc localhost 25 > nc.out" \
0 "send mail via ESMTP nc connection"
;;
via_gnutls_esmtps)
rlRun "setup_postfix"
rlServiceStart postfix
rlWaitForCmd "master_on 465" || {
rlLogError "master will not listen on 465"
rlFail "(TEST ERROR)"
return 3
}
rlRun "mkesmtp | while read line ; do sleep 1s ; echo "$line" ; done | $(mkclientcmd) >client.out 2>client.err" \
0 "send mail via ESMTP client connection"
;;
*) distribution_xcase__id_error ;;
esac
rlWaitForCmd "mail <<<type >/dev/null"
rlRun "mail <<<type >mail.out"
}
distribution_xcase__test() {
case $(distribution_xcase__id) in
via_mail_cmd)
rlRun "grep 'test1' mail.out" \
0 "mail recieved"
;;
via_nc_esmtp)
rlRun "grep 'ESMTP Postfix' nc.out" \
0 "ESMTP postfix support confirmed"
;;
via_gnutls_esmtps)
rlRun "grep 'ESMTP Postfix' client.out" \
0 "ESMTP postfix support confirmed"
;;
*) distribution_xcase__id_error ;;
esac
rlRun "grep -x 'text from .$(distribution_xcase__id).' mail.out" \
0 "it's the correct mail"
}
distribution_xcase__diag() {
distribution_dump__file -s -S mail.out nc.out client.out client.err
mailq | distribution_dump__pipe MAILQ
ss -ntlp | distribution_dump__pipe SSNTLP
}
distribution_xcase__cleanup() {
rlFileRestore
}
rlJournalStart
rlPhaseStartSetup init
rlAssertRpm $PACKAGE
rlImport "distribution/dump"
rlImport "distribution/xcase"
rlPhaseEnd
rlPhaseStartSetup
rlCheckRpm "sendmail" && {
rlServiceStop "sendmail"
rlRun "alternatives --set mta /usr/sbin/sendmail.postfix" 0 "set postfix as default MTA"
}
rlServiceStart "postfix"
rlPhaseEnd
distribution_xcase__run
rlPhaseStartCleanup
rlServiceRestore "postfix"
rlCheckRpm "sendmail" && {
rlRun "alternatives --set mta /usr/sbin/sendmail.sendmail" 0 "setting sendmail as default mailer"
rlServiceRestore "sendmail"
}
rlPhaseEnd
rlJournalPrintText
rlJournalEnd