mirror of
https://github.com/atmoz/sftp.git
synced 2024-11-17 12:51:33 -05:00
188 lines
4.6 KiB
Bash
Executable file
188 lines
4.6 KiB
Bash
Executable file
#!/bin/bash
|
|
# See: https://github.com/djui/bashunit
|
|
|
|
skipAllTests=false
|
|
|
|
scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
buildDir="$scriptDir/.."
|
|
tmpDir="/tmp/atmoz_sftp_test"
|
|
|
|
build=${1:-"build"}
|
|
output=${2:-"quiet"}
|
|
cleanup=${3:-"cleanup"}
|
|
sftpImageName="atmoz/sftp_test"
|
|
sftpContainerName="atmoz_sftp_test"
|
|
|
|
if [ "$output" == "quiet" ]; then
|
|
redirect="/dev/null"
|
|
else
|
|
redirect="/dev/stdout"
|
|
fi
|
|
|
|
##############################################################################
|
|
|
|
function beforeTest() {
|
|
if [ "$build" == "build" ]; then
|
|
docker build --pull=true --tag "$sftpImageName" "$buildDir"
|
|
if [ $? -gt 0 ]; then
|
|
echo "Build failed"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Private key can not be read by others
|
|
chmod go-rw "$scriptDir/id_rsa"
|
|
|
|
rm -rf "$tmpDir" # clean state
|
|
mkdir "$tmpDir"
|
|
|
|
echo "test::$(id -u):$(id -g)" >> "$tmpDir/users"
|
|
docker run \
|
|
-v "$tmpDir/users:/etc/sftp-users.conf:ro" \
|
|
-v "$scriptDir/id_rsa.pub":/home/test/.ssh/keys/id_rsa.pub:ro \
|
|
-v "$tmpDir":/home/test/share \
|
|
--name "$sftpContainerName" \
|
|
--expose 22 \
|
|
-d "$sftpImageName" \
|
|
> "$redirect"
|
|
sleep 2 # wait for sftp server to get ready
|
|
}
|
|
|
|
function afterTest() {
|
|
if [ "$output" != "quiet" ]; then
|
|
echo "Docker logs:"
|
|
docker logs "$sftpContainerName"
|
|
fi
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
|
docker rm -fv "$sftpContainerName" > "$redirect"
|
|
rm -rf "$tmpDir"
|
|
fi
|
|
}
|
|
|
|
function getSftpIp() {
|
|
docker inspect -f {{.NetworkSettings.IPAddress}} "$1"
|
|
}
|
|
|
|
function runSftpCommands() {
|
|
ip="$(getSftpIp $1)"
|
|
user="$2"
|
|
shift 2
|
|
|
|
commands=""
|
|
for cmd in "$@"; do
|
|
commands="$commands$cmd"$'\n'
|
|
done
|
|
|
|
echo "$commands" | sftp \
|
|
-i "$scriptDir/id_rsa" \
|
|
-oStrictHostKeyChecking=no \
|
|
-oUserKnownHostsFile=/dev/null \
|
|
-b - $user@$ip \
|
|
> "$redirect" 2>&1
|
|
|
|
sleep 1 # wait for command to finish
|
|
}
|
|
|
|
##############################################################################
|
|
|
|
function testMinimalContainerStart() {
|
|
$skipAllTests && skip && return 0
|
|
|
|
tmpContainerName="$sftpContainerName""_minimal"
|
|
|
|
docker run \
|
|
--name "$tmpContainerName" \
|
|
-d "$sftpImageName" \
|
|
minimal \
|
|
> "$redirect"
|
|
sleep 2
|
|
|
|
ps="$(docker ps -q -f name="$tmpContainerName")"
|
|
assertNotEqual "$ps" ""
|
|
|
|
if [ -z "$ps" ]; then
|
|
skipAllTests=true
|
|
fi
|
|
|
|
if [ "$output" != "quiet" ]; then
|
|
docker logs "$tmpContainerName"
|
|
fi
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
|
docker rm -fv "$tmpContainerName" > "$redirect"
|
|
fi
|
|
}
|
|
|
|
function testContainerIsRunning() {
|
|
$skipAllTests && skip && return 0
|
|
|
|
ps="$(docker ps -q -f name="$sftpContainerName")"
|
|
assertNotEqual "$ps" ""
|
|
|
|
if [ -z "$ps" ]; then
|
|
skipAllTests=true
|
|
fi
|
|
}
|
|
|
|
function testLoginUsingSshKey() {
|
|
$skipAllTests && skip && return 0
|
|
|
|
runSftpCommands "$sftpContainerName" "test" "exit"
|
|
assertReturn $? 0
|
|
}
|
|
|
|
function testWritePermission() {
|
|
$skipAllTests && skip && return 0
|
|
|
|
runSftpCommands "$sftpContainerName" "test" "cd share" "mkdir test" "exit"
|
|
test -d "$tmpDir/test"
|
|
assertReturn $? 0
|
|
}
|
|
|
|
# Bind-mount folder using script in /etc/sftp.d/
|
|
function testCustomContainerStart() {
|
|
$skipAllTests && skip && return 0
|
|
|
|
tmpContainerName="$sftpContainerName""_custom"
|
|
|
|
mkdir -p "$tmpDir/custom/bindmount"
|
|
echo "mkdir -p /home/custom/bindmount && \
|
|
chown custom /home/custom/bindmount && \
|
|
mount --bind /custom /home/custom/bindmount" \
|
|
> "$tmpDir/mount.sh"
|
|
chmod +x "$tmpDir/mount.sh"
|
|
|
|
docker run \
|
|
--privileged=true \
|
|
--name "$tmpContainerName" \
|
|
-v "$scriptDir/id_rsa.pub":/home/custom/.ssh/keys/id_rsa.pub:ro \
|
|
-v "$tmpDir/custom/bindmount":/custom \
|
|
-v "$tmpDir/mount.sh":/etc/sftp.d/mount.sh \
|
|
--expose 22 \
|
|
-d "$sftpImageName" \
|
|
custom:123 \
|
|
> "$redirect"
|
|
sleep 2
|
|
|
|
ps="$(docker ps -q -f name="$tmpContainerName")"
|
|
assertNotEqual "$ps" ""
|
|
|
|
runSftpCommands "$tmpContainerName" "custom" "cd bindmount" "mkdir test" "exit"
|
|
test -d "$tmpDir/custom/bindmount/test"
|
|
assertReturn $? 0
|
|
|
|
if [ "$output" != "quiet" ]; then
|
|
docker logs "$tmpContainerName"
|
|
fi
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
|
docker rm -fv "$tmpContainerName" > "$redirect"
|
|
fi
|
|
}
|
|
|
|
##############################################################################
|
|
|
|
# Run tests
|
|
source "$scriptDir/bashunit.bash"
|
|
# Nothing happens after this
|