0
0
Fork 0
mirror of https://github.com/atmoz/sftp.git synced 2024-11-24 12:59:24 -05:00
atmoz-sftp/tests/run

183 lines
4.4 KiB
Text
Raw Normal View History

2015-11-23 07:39:17 -05:00
#!/bin/bash
# See: https://github.com/djui/bashunit
2015-12-20 17:45:28 -05:00
skipAllTests=false
2015-11-23 07:39:17 -05:00
scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
buildDir="$scriptDir/.."
tmpDir="/tmp/atmoz_sftp_test"
build=${1:-"build"}
2015-12-07 04:10:24 -05:00
output=${2:-"quiet"}
cleanup=${3:-"cleanup"}
2015-11-23 07:39:17 -05:00
sftpImageName="atmoz/sftp_test"
sftpContainerName="atmoz_sftp_test"
2015-12-07 04:10:24 -05:00
if [ "$output" == "quiet" ]; then
redirect="/dev/null"
else
redirect="/dev/stdout"
2015-12-07 04:10:24 -05:00
fi
##############################################################################
2015-11-23 07:39:17 -05:00
function beforeTest() {
if [ "$build" == "build" ]; then
docker build --pull=true --tag "$sftpImageName" "$buildDir"
fi
# Private key can not be read by others
chmod go-rw "$scriptDir/id_rsa"
2015-11-23 07:39:17 -05:00
rm -rf "$tmpDir" # clean state
mkdir "$tmpDir"
2015-12-07 04:10:24 -05:00
echo "test::$(id -u):$(id -g)" >> "$tmpDir/users"
2015-11-23 07:39:17 -05:00
docker run \
2015-12-07 04:10:24 -05:00
-v "$tmpDir/users:/etc/sftp-users.conf:ro" \
2015-11-23 07:39:17 -05:00
-v "$scriptDir/id_rsa.pub":/home/test/.ssh/keys/id_rsa.pub:ro \
-v "$tmpDir":/home/test/share \
--name "$sftpContainerName" \
--expose 22 \
-d "$sftpImageName" \
2015-12-07 04:10:24 -05:00
> "$redirect"
2015-11-23 07:39:17 -05:00
sleep 1 # wait for sftp server to get ready
}
function afterTest() {
2015-12-07 04:10:24 -05:00
if [ "$output" != "quiet" ]; then
2015-12-20 19:37:42 -05:00
echo "Docker logs:"
2015-12-07 04:10:24 -05:00
docker logs "$sftpContainerName"
fi
if [ "$cleanup" == "cleanup" ]; then
docker rm -fv "$sftpContainerName" > "$redirect"
rm -rf "$tmpDir"
fi
2015-11-23 07:39:17 -05:00
}
function getSftpIp() {
docker inspect -f {{.NetworkSettings.IPAddress}} "$1"
2015-11-23 07:39:17 -05:00
}
function runSftpCommands() {
ip="$(getSftpIp $1)"
user="$2"
shift 2
2015-12-07 04:10:24 -05:00
commands=""
for cmd in "$@"; do
commands="$commands$cmd"$'\n'
done
echo "$commands" | sftp \
2015-11-23 07:39:17 -05:00
-i "$scriptDir/id_rsa" \
-oStrictHostKeyChecking=no \
-oUserKnownHostsFile=/dev/null \
-b - $user@$ip \
2015-12-07 04:10:24 -05:00
> "$redirect" 2>&1
2015-11-23 07:39:17 -05:00
}
2015-12-07 04:10:24 -05:00
##############################################################################
2015-12-20 17:45:28 -05:00
function testMinimalContainerStart() {
$skipAllTests && skip && return 0
tmpContainerName="$sftpContainerName""_minimal"
docker run \
--name "$tmpContainerName" \
-d "$sftpImageName" \
minimal \
2015-12-20 17:45:28 -05:00
> "$redirect"
sleep 1
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
}
2015-11-23 07:39:17 -05:00
function testContainerIsRunning() {
2015-12-20 17:45:28 -05:00
$skipAllTests && skip && return 0
2015-11-23 07:39:17 -05:00
ps="$(docker ps -q -f name="$sftpContainerName")"
assertNotEqual "$ps" ""
2015-12-20 17:45:28 -05:00
if [ -z "$ps" ]; then
skipAllTests=true
fi
2015-11-23 07:39:17 -05:00
}
function testLoginUsingSshKey() {
2015-12-20 17:45:28 -05:00
$skipAllTests && skip && return 0
runSftpCommands "$sftpContainerName" "test" "exit"
2015-11-23 07:39:17 -05:00
assertReturn $? 0
}
function testWritePermission() {
2015-12-20 17:45:28 -05:00
$skipAllTests && skip && return 0
runSftpCommands "$sftpContainerName" "test" "cd share" "mkdir test" "exit"
2015-11-23 07:39:17 -05:00
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 1
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
}
2015-12-07 04:10:24 -05:00
##############################################################################
2015-11-23 07:39:17 -05:00
# Run tests
source "$scriptDir/bashunit.bash"
# Nothing happens after this