mirror of
https://github.com/atmoz/sftp.git
synced 2024-11-17 12:51:33 -05:00
98 lines
2.3 KiB
Bash
Executable file
98 lines
2.3 KiB
Bash
Executable file
#!/bin/bash
|
|
# See: https://github.com/djui/bashunit
|
|
|
|
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=$'&1'
|
|
fi
|
|
|
|
##############################################################################
|
|
|
|
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"
|
|
|
|
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 1 # wait for sftp server to get ready
|
|
}
|
|
|
|
function afterTest() {
|
|
if [ "$output" != "quiet" ]; then
|
|
docker logs "$sftpContainerName"
|
|
fi
|
|
|
|
if [ "$cleanup" == "cleanup" ]; then
|
|
docker rm -fv "$sftpContainerName" > "$redirect"
|
|
rm -rf "$tmpDir"
|
|
fi
|
|
}
|
|
|
|
function getSftpIp() {
|
|
docker inspect -f {{.NetworkSettings.IPAddress}} "$sftpContainerName"
|
|
}
|
|
|
|
function runSftpCommands() {
|
|
ip="$(getSftpIp)"
|
|
|
|
commands=""
|
|
for cmd in "$@"; do
|
|
commands="$commands$cmd"$'\n'
|
|
done
|
|
|
|
echo "$commands" | sftp \
|
|
-i "$scriptDir/id_rsa" \
|
|
-oStrictHostKeyChecking=no \
|
|
-oUserKnownHostsFile=/dev/null \
|
|
-b - test@$ip \
|
|
> "$redirect" 2>&1
|
|
}
|
|
|
|
##############################################################################
|
|
|
|
function testContainerIsRunning() {
|
|
ps="$(docker ps -q -f name="$sftpContainerName")"
|
|
assertNotEqual "$ps" ""
|
|
}
|
|
|
|
function testLoginUsingSshKey() {
|
|
runSftpCommands "exit"
|
|
assertReturn $? 0
|
|
}
|
|
|
|
function testWritePermission() {
|
|
runSftpCommands "cd share" "mkdir test" "exit"
|
|
test -d "$tmpDir/test"
|
|
assertReturn $? 0
|
|
}
|
|
|
|
##############################################################################
|
|
|
|
# Run tests
|
|
source "$scriptDir/bashunit.bash"
|
|
# Nothing happens after this
|