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"
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
sudo="sudo"
|
|
|
|
cache="--no-cache"
|
|
|
|
|
2015-11-23 07:39:17 -05:00
|
|
|
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
|
2015-12-16 05:54:57 -05:00
|
|
|
redirect="/dev/stdout"
|
2015-12-07 04:10:24 -05:00
|
|
|
fi
|
|
|
|
|
2016-09-26 14:13:53 -04:00
|
|
|
buildOptions="--tag $sftpImageName"
|
|
|
|
|
2015-12-07 04:10:24 -05:00
|
|
|
##############################################################################
|
|
|
|
|
2015-11-23 07:39:17 -05:00
|
|
|
function beforeTest() {
|
|
|
|
if [ "$build" == "build" ]; then
|
2017-06-05 07:30:16 -04:00
|
|
|
buildOptions="$buildOptions $cache --pull=true"
|
2016-09-26 14:13:53 -04:00
|
|
|
fi
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker build $buildOptions "$buildDir"
|
2016-09-26 14:13:53 -04:00
|
|
|
if [ $? -gt 0 ]; then
|
|
|
|
echo "Build failed"
|
|
|
|
exit 1
|
2015-11-23 07:39:17 -05:00
|
|
|
fi
|
|
|
|
|
2015-11-23 08:02:41 -05:00
|
|
|
# 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"
|
|
|
|
|
2016-08-18 06:45:59 -04:00
|
|
|
echo "test::$(id -u):$(id -g):dir1,dir2" >> "$tmpDir/users"
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo 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"
|
2017-06-05 07:30:16 -04:00
|
|
|
|
|
|
|
waitForServer $sftpContainerName
|
2015-11-23 07:39:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker logs "$sftpContainerName"
|
2015-12-07 04:10:24 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker rm -fv "$sftpContainerName" > "$redirect"
|
2015-12-07 04:10:24 -05:00
|
|
|
rm -rf "$tmpDir"
|
|
|
|
fi
|
2015-11-23 07:39:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSftpIp() {
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker inspect -f {{.NetworkSettings.IPAddress}} "$1"
|
2015-11-23 07:39:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function runSftpCommands() {
|
2016-03-17 16:27:41 -04:00
|
|
|
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 \
|
2016-03-17 16:27:41 -04:00
|
|
|
-b - $user@$ip \
|
2015-12-07 04:10:24 -05:00
|
|
|
> "$redirect" 2>&1
|
2016-06-02 08:56:30 -04:00
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
status=$?
|
|
|
|
sleep 1 # wait for commands to finish
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForServer() {
|
|
|
|
containerName="$1"
|
|
|
|
echo -n "Waiting for $containerName to open port 22 "
|
|
|
|
|
|
|
|
for i in {1..30}; do
|
|
|
|
sleep 1
|
|
|
|
ip="$(getSftpIp $containerName)"
|
|
|
|
echo -n "."
|
|
|
|
if nc -z $ip 22; then
|
|
|
|
echo " OK"
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo " FAIL"
|
|
|
|
return 1
|
2015-11-23 07:39:17 -05:00
|
|
|
}
|
|
|
|
|
2015-12-07 04:10:24 -05:00
|
|
|
##############################################################################
|
|
|
|
|
2016-08-12 10:21:07 -04:00
|
|
|
function testContainerIsRunning() {
|
|
|
|
$skipAllTests && skip && return 0
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
ps="$($sudo docker ps -q -f name="$sftpContainerName")"
|
2016-08-12 10:21:07 -04:00
|
|
|
assertNotEqual "$ps" ""
|
|
|
|
|
|
|
|
if [ -z "$ps" ]; then
|
|
|
|
skipAllTests=true
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function testLoginUsingSshKey() {
|
|
|
|
$skipAllTests && skip && return 0
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
runSftpCommands "$sftpContainerName" "test" "exit"
|
2016-08-12 10:21:07 -04:00
|
|
|
assertReturn $? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWritePermission() {
|
|
|
|
$skipAllTests && skip && return 0
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
runSftpCommands "$sftpContainerName" "test" \
|
2016-08-12 10:21:07 -04:00
|
|
|
"cd share" \
|
|
|
|
"mkdir test" \
|
|
|
|
"exit"
|
|
|
|
test -d "$tmpDir/test"
|
|
|
|
assertReturn $? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDir() {
|
|
|
|
$skipAllTests && skip && return 0
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
runSftpCommands "$sftpContainerName" "test" \
|
2016-08-12 14:45:21 -04:00
|
|
|
"cd dir1" \
|
|
|
|
"mkdir test-dir1" \
|
|
|
|
"get -rf test-dir1 $tmpDir/" \
|
|
|
|
"cd ../dir2" \
|
|
|
|
"mkdir test-dir2" \
|
|
|
|
"get -rf test-dir2 $tmpDir/" \
|
2016-08-12 10:21:07 -04:00
|
|
|
"exit"
|
2016-08-12 14:45:21 -04:00
|
|
|
test -d "$tmpDir/test-dir1"
|
|
|
|
assertReturn $? 0
|
|
|
|
test -d "$tmpDir/test-dir2"
|
2016-08-12 10:21:07 -04:00
|
|
|
assertReturn $? 0
|
|
|
|
}
|
|
|
|
|
2015-12-20 17:45:28 -05:00
|
|
|
function testMinimalContainerStart() {
|
|
|
|
$skipAllTests && skip && return 0
|
|
|
|
|
|
|
|
tmpContainerName="$sftpContainerName""_minimal"
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker run \
|
2015-12-20 17:45:28 -05:00
|
|
|
--name "$tmpContainerName" \
|
|
|
|
-d "$sftpImageName" \
|
2016-03-17 16:27:41 -04:00
|
|
|
minimal \
|
2015-12-20 17:45:28 -05:00
|
|
|
> "$redirect"
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
waitForServer $tmpContainerName
|
|
|
|
|
|
|
|
ps="$($sudo docker ps -q -f name="$tmpContainerName")"
|
2015-12-20 17:45:28 -05:00
|
|
|
assertNotEqual "$ps" ""
|
|
|
|
|
|
|
|
if [ -z "$ps" ]; then
|
|
|
|
skipAllTests=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$output" != "quiet" ]; then
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker logs "$tmpContainerName"
|
2015-12-20 17:45:28 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker rm -fv "$tmpContainerName" > "$redirect"
|
2015-12-20 17:45:28 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:27:41 -04:00
|
|
|
# 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"
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker run \
|
2016-03-17 16:27:41 -04:00
|
|
|
--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"
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
waitForServer $tmpContainerName
|
|
|
|
|
|
|
|
ps="$($sudo docker ps -q -f name="$tmpContainerName")"
|
2016-03-17 16:27:41 -04:00
|
|
|
assertNotEqual "$ps" ""
|
|
|
|
|
2017-06-05 07:30:16 -04:00
|
|
|
runSftpCommands "$tmpContainerName" "custom" \
|
|
|
|
"cd bindmount" \
|
|
|
|
"mkdir test" \
|
|
|
|
"exit"
|
|
|
|
|
2016-03-17 16:27:41 -04:00
|
|
|
test -d "$tmpDir/custom/bindmount/test"
|
|
|
|
assertReturn $? 0
|
|
|
|
|
|
|
|
if [ "$output" != "quiet" ]; then
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker logs "$tmpContainerName"
|
2016-03-17 16:27:41 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
2017-06-05 07:30:16 -04:00
|
|
|
$sudo docker rm -fv "$tmpContainerName" > "$redirect"
|
2016-03-17 16:27:41 -04:00
|
|
|
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
|