sendmail/functionality/selftest/runtest.sh
František Hrdina 66b86fa382 Fix tests
2025-02-25 14:31:24 +01:00

238 lines
8.4 KiB
Bash
Executable file

#!/bin/bash
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/sendmail/functionality/selftest
# Description: Sendmail - sanity checking, there are three users on one machine, sending mails each other, checking sending mail, incoming mail, e-mail parser
# Author: Petr Sklenar <psklenar@redhat.com>
# Rewriten to beakerlib syntax by: Patrik Mosko <pmosko@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2008 Red Hat, Inc. All rights reserved. This copyrighted material
# is made available to anyone wishing to use, modify, copy, or
# redistribute it subject to the terms and conditions of the GNU General
# Public License v.2.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1
PACKAGE=sendmail
USER1="testuser"
rlJournalStart
########## SETUP PHASE ##########
rlPhaseStartSetup
rlAssertRpm sendmail
rlAssertRpm sendmail-cf
rlAssertRpm procmail
rlAssertRpm m4
if (rlIsRHELLike '<=8'); then
rlAssertRpm mailx
rlAssertRpm procps-ng
fi
rlFileBackup --clean --missing-ok "/etc/mail/sendmail.mc" `readlink -f /etc/mail/sendmail.mc`
rlFileBackup --clean --missing-ok "/etc/mail/access" `readlink -f /etc/mail/access`
rlFileBackup --clean --missing-ok "/etc/aliases" `readlink -f /etc/aliases`
rlFileBackup --clean "/var/lib/alternatives/mta" "/etc/mail/aliasesdb-stamp"
rlFileBackup "/etc/alternatives"
# hostname package is going to be removed from RHEL9 (probably)...
# replace call of `hostname` with appropriate replacement (should be hostnamectl)
(rlIsRHELLike '<9') && HOSTNAME=`hostname` || HOSTNAME=`hostnamectl status --static`
rlRun "adduser $USER1"
rlRun "DIR=\$(mktemp -d)" 0 "Creating tmp directory for messages/outputs"
rlLog "changing configuration file:"
rlRun "sed -i -e \"s;DAEMON_OPTIONS.* ;dnl ##### \\\0 ;\" /etc/mail/sendmail.mc"
rlRun "sed -i -e \"s;.*FEATURE(\\\`relay_based_on_MX\\\').*;dnl ##### \\\0 ;\" /etc/mail/sendmail.mc"
rlRun "sed -i -e \"s;.*FEATURE(\\\`accept_unresolvable_domains\\\').*;dnl ##### \\\0 ;\" /etc/mail/sendmail.mc"
rlRun "echo $HOSTNAME RELAY >> /etc/mail/access"
rlRun "rm /var/spool/mail/$USER1 -rf"
pkill master
service postfix status --no-pager && rlServiceStop postfix
service sendmail status --no-pager && rlServiceStop sendmail
rlRun "alternatives --set mta $(rpm --eval '%{_sbindir}')/sendmail.sendmail"
rlRun "pushd /etc/mail"
rlRun "make"
rlServiceStart "sendmail"
sleep 10
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest "Test the Internet Delivery"
cat > $DIR/to_internet << EOF
To: angle@somewhere.out
From: petr@here.cz
Subject: Ein Test
Its a big test with header!
EOF
rlRun "cat $DIR/to_internet | /usr/lib/sendmail -bm -t -v > $DIR/to_internet.out"; sleep 5
##debug
cat $DIR/to_internet.out
rlAssertGrep "EHLO" $DIR/to_internet.out
rlAssertGrep "354 Enter\|354 End data" $DIR/to_internet.out
rlAssertGrep "250.*[oO][kK]" $DIR/to_internet.out
rlAssertGrep "Sent" $DIR/to_internet.out
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest
cat > $DIR/to_us << EOF
To: $USER1@localhost.localdomain
From: petr@here.cz
Subject: Ein Test
Its a big test with header!
EOF
rlRun "cat $DIR/to_us | /usr/lib/sendmail -bm -t -v > $DIR/to_us.out"; sleep 5
##debug
cat $DIR/to_us.out
rlAssertGrep 'EHLO' $DIR/to_us.out
rlAssertGrep 'MAIL From:<root@' $DIR/to_us.out
rlAssertGrep "354 Enter\|354 End data" $DIR/to_us.out
rlAssertGrep '250.*[oO][kK]' $DIR/to_us.out
rlAssertGrep "$USER1@localhost.localdomain... Sent" $DIR/to_us.out
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest "There is mail from previous staff"
sleep 10
if (rlIsRHELLike '>=9' || rlIsFedora); then
cat /var/mail/$USER1 > $DIR/mail.temp
else
mail -u $USER1 << EOF > $DIR/mail.temp
quit
EOF
fi
rlAssertGrep "Subject.*Ein Test" $DIR/mail.temp
rlAssertGrep 'Its a big test with header\!' $DIR/mail.temp
if rlIsRHEL '<=6'; then
rlRun "timeout 120 /usr/lib/sendmail -d0.12 -bt >$DIR/sendmail.info" 0,124
else
rlRun "timeout --preserve-status 120 /usr/lib/sendmail -d0.12 -bt >$DIR/sendmail.info"
fi
##debug
cat $DIR/sendmail.info
rlAssertGrep "Compiled with" $DIR/sendmail.info
rlAssertGrep "a.k.a.:" $DIR/sendmail.info
rlAssertGrep "(canonical domain name) .* = $HOSTNAME" $DIR/sendmail.info
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest
MACROS_NUMBER=$(/usr/lib/sendmail -d35.9 -bt < /dev/null | grep "define" | wc -l)
if [[ $MACROS_NUMBER -ge 10 ]]; then
rlPass "There are at least 10 macros!"
else
rlFail "There are too less macros!"
/usr/lib/sendmail -d35.9 -bt < /dev/null
fi
rlLog "running: /usr/lib/sendmail -bp"
rlAssertGrep "/var/spool/mqueue" <(/usr/lib/sendmail -bp)
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest "Testing sendmail.mx"
rlLog "If sendmail can play with mx record"
/usr/lib/sendmail -bt << EOF > $DIR/sendmail.mx
/mx redhat.com
EOF
MX_NUMBER=$(grep "redhat.com" $DIR/sendmail.mx | wc -l)
##debug
if [[ $MX_NUMBER -ge 2 ]]; then
rlPass "Testing /mx redhat.com record with '-bt'"
else
# Workaround for FEDORA CI
grep "redhat" $HOSTNAME
if [[ $? -eq 0 ]]; then
rlFail "Testing /mx redhat.com record with '-bt'"
fi
fi
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest "Testing sendmail parsing mail address"
/usr/lib/sendmail -bt << EOF > $DIR/sendmail.parser
0,4 psklenar@redhat.com
EOF
rlAssertGrep "final .*returns: .*local .*psklenar .*redhat .*com" $DIR/sendmail.parser
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest "testing right header"
/usr/lib/sendmail -bt << EOF > $DIR/sendmail.header
/tryflags HS
/try smtp petr
EOF
##debug
cat $DIR/sendmail.header
rlLog "Testing sendmail creating header"
rlAssertGrep "petr@$HOSTNAME" $DIR/sendmail.header
rlPhaseEnd
#################### TEST PHASE ####################
rlPhaseStartTest "checking users in /etc/aliases"
rlRun "echo 'psklenar: root' >> /etc/aliases"
rlRun "/usr/lib/sendmail -bi" 0 "initialize the alias db"
rlRun "/usr/lib/sendmail -bv -v non_exists_user" 67 "Validation: non-existing user"
rlRun "/usr/lib/sendmail -bv -v psklenar" 0 "Validation: existing user"
rlPhaseEnd
########## CLEANUP PHASE ##########
rlPhaseStartCleanup
rlRun "popd"
# restore MTA's
service sendmail status --no-pager && rlServiceStop sendmail
service postfix status --no-pager && rlServiceStop postfix
pkill master
rlFileRestore
rlRun "alternatives --auto mta"
rlServiceRestore "sendmail"
rlServiceRestore "postfix"
rlRun "userdel -rfZ $USER1"
rlPhaseEnd
rlJournalEnd