#!/bin/bash
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/bash/Sanity/login-shell
# Description: the test creates user with bash as login shell and verified the user can log in and get th
# Author: Martin Kyral <mkyral@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2016 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/bin/rhts-environment.sh || exit 1
. /usr/share/beakerlib/beakerlib.sh || exit 1
PACKAGES=${PACKAGES:-"bash"}
SH_BIN=${SH_BIN:-"bash"}
rlJournalStart
rlPhaseStartSetup
rlAssertRpm --all
rlAssertBinaryOrigin $SH_BIN
rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory"
rlRun "cp script.exp ssh.exp tst.sh $TmpDir"
rlRun "pushd $TmpDir"
#USER=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
USER="user${RANDOM}"
rlRun "useradd -s /bin/${SH_BIN} -d /home/${USER} ${USER}"
rlRun "echo krava | passwd --stdin ${USER}"
rlRun "sed -i 's/shelluser/${USER}/g' script.exp"
rlRun "sed -i 's/shelluser/${USER}/g' ssh.exp"
rlRun "sed -i 's/shelluser/${USER}/g' tst.sh"
rlRun "sed -i 's/shellbin/${SH_BIN}/g' script.exp"
rlRun "sed -i 's/shellbin/${SH_BIN}/g' ssh.exp"
rlRun "sed -i 's/shellbin/${SH_BIN}/g' tst.sh"
rlRun "cp tst.sh /home/${USER}/"
rlRun "chown ${USER} /home/${USER}/tst.sh"
rlRun "mv /etc/profile.d/tps-cd.sh /root/tps-cd.sh" 0,1 "Backing tps-cd.sh up"
rlPhaseEnd
rlPhaseStartTest
rlLog "::::: 'su -c' test :::::"
rlRun "su ${USER} -c 'echo something' | tee test1.log" 0 "echo something test"
rlLog "test1.log: $(cat test1.log)"
rlAssertGrep "something" "test1.log"
rlRun "su ${USER} -c 'echo \$0 | tee /home/${USER}/ps1.log'" 0 "shell detection test"
rlLog "ps1.log: $(cat /home/${USER}/ps1.log)"
rlAssertGrep "$SH_BIN" "/home/${USER}/ps1.log"
# the following does not work with pdksh for some reason
# not a big deal, ssh test can substitute this
[ "$SH_BIN" != "pdksh" ] && {
rlLog "::::: 'su -l' test :::::"
expect script.exp &
sleep 20
pgrep expect && pkill expect
rlLog "test2.log: $(cat /home/${USER}/test2.log)"
rlAssertGrep "something" "/home/${USER}/test2.log"
rlLog "ps2.log: $(cat /home/${USER}/ps2.log)"
rlAssertGrep "$SH_BIN" "/home/${USER}/ps2.log"
}
rlLog "::::: ssh login test :::::"
expect ssh.exp &
sleep 20
pgrep expect && pkill expect
rlLog "ps3.log: $(cat /home/${USER}/ps3.log)"
rlAssertGrep "$SH_BIN" "/home/${USER}/ps3.log"
rlAssertExists "/home/${USER}/tst.dat"
rlPhaseEnd
rlPhaseStartCleanup
rlRun "mv /root/tps-cd.sh /etc/profile.d/tps-cd.sh" 0,1 "Restoring tps-cd.sh"
rlRun "su -c 'kill -9 -1' ${USER}" 0 "Killing all processes of ${USER}"
rlRun "userdel -r ${USER}"
rlRun "popd"
rlRun "rm -r $TmpDir" 0 "Removing tmp directory"
rlPhaseEnd
rlJournalPrintText
rlJournalEnd