#!/bin/bash
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/lftp/Sanity/sftp
# Description: Basic test for SFTP file transfer
# Author: Martin Frodl <mfrodl@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/share/beakerlib/beakerlib.sh || exit 1
PACKAGES="lftp"
rlJournalStart
rlPhaseStartSetup
rlAssertRpm "$PACKAGES"
rlRun "useradd lftp" 0 "Creating user lftp"
rlRun "passwd --stdin lftp <<< lftp" 0 "Setting password"
rlRun "REMOTE=\$(mktemp -d)" 0 "Creating remote directory"
rlRun "touch ${REMOTE}/file1" 0 "Creating remote file"
rlRun "mkdir ${REMOTE}/dir" 0 "Creating remote subdirectory"
rlRun "touch ${REMOTE}/dir/file2" 0 "Creating another remote file"
rlRun "chmod -R 0755 ${REMOTE}" 0 "Allowing access to remote directory for user lftp"
rlRun "LOCAL=\$(mktemp -d)" 0 "Creating local directory"
rlRun "pushd ${LOCAL}"
if ! rlIsRHEL '<8'; then
if ! test -e ~/.ssh/known_hosts; then
rlRun "mkdir -p ~/.ssh" 0 "Create ~/.ssh"
fi
rlRun "ssh-keyscan $(hostname) >> ~/.ssh/known_hosts" 0 "Add localhost to known_hosts"
fi
rlPhaseEnd
rlPhaseStartTest "Mirroring files and directories"
rlRun "lftp -u lftp,lftp -e 'mirror ${REMOTE} ${LOCAL}; exit' sftp://${HOSTNAME}" \
0 "Mirroring ${REMOTE} to ${LOCAL}"
rlAssertExists "file1"
rlAssertExists "dir"
rlAssertExists "dir/file2"
rlRun "rm -rf *" 0 "Cleaning local directory"
rlPhaseEnd
rlPhaseStartTest "Mirroring symlinks [BZ#1305235]"
rlRun "TARGET=\$(mktemp -d)" 0 "Creating another directory"
rlRun "touch ${TARGET}/file3" 0 "Creating file inside it"
rlRun "ln -s ${TARGET} ${REMOTE}/symlink1" 0 "Creating symlink to it"
rlRun "chmod -R 0755 ${TARGET}" 0 "Allowing access to remote directory for user lftp"
rlRun "lftp -u lftp,lftp -e 'mirror -L ${REMOTE} ${LOCAL}; exit' sftp://${HOSTNAME}" \
0 "Mirroring ${REMOTE} to ${LOCAL} (follow symlinks)"
rlAssertExists "symlink1"
rlAssertExists "symlink1/file3"
rlRun "rm -rf *" 0 "Cleaning local directory"
rlPhaseEnd
rlPhaseStartTest "Mirroring circular symlinks"
rlRun "ln -s ${REMOTE} ${TARGET}/symlink2" 0 "Creating symlink cycle"
rlRun "lftp -u lftp,lftp -e 'mirror -L ${REMOTE} ${LOCAL}; exit' sftp://${HOSTNAME}" \
0,41,1 "Mirroring ${REMOTE} to ${LOCAL} (follow symlinks)"
rlAssertExists "symlink1"
rlAssertExists "symlink1/file3"
rlAssertExists "symlink1/symlink2"
rlAssertExists "symlink1/symlink2/file1"
rlRun "rm -rf *" 0 "Cleaning local directory"
rlPhaseEnd
rlPhaseStartCleanup
rlRun "popd"
rlRun "rm -r ${REMOTE} ${LOCAL} ${TARGET}" 0 "Removing test directories"
rlRun "userdel -rf lftp" 0 "Deleting user lftp"
rlPhaseEnd
rlJournalPrintText
rlJournalEnd