0
0
Fork 0
mirror of https://github.com/atmoz/sftp.git synced 2024-11-17 12:51:33 -05:00

Tests need sudo on some systems (like mine) and wait on server

This commit is contained in:
Adrian Dvergsdal 2017-06-05 13:30:16 +02:00
parent 06eafb8e1f
commit 11ac015ed4

View file

@ -7,6 +7,9 @@ scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
buildDir="$scriptDir/.." buildDir="$scriptDir/.."
tmpDir="/tmp/atmoz_sftp_test" tmpDir="/tmp/atmoz_sftp_test"
sudo="sudo"
cache="--no-cache"
build=${1:-"build"} build=${1:-"build"}
output=${2:-"quiet"} output=${2:-"quiet"}
cleanup=${3:-"cleanup"} cleanup=${3:-"cleanup"}
@ -25,10 +28,10 @@ buildOptions="--tag $sftpImageName"
function beforeTest() { function beforeTest() {
if [ "$build" == "build" ]; then if [ "$build" == "build" ]; then
buildOptions="$buildOptions --no-cache --pull=true" buildOptions="$buildOptions $cache --pull=true"
fi fi
docker build $buildOptions "$buildDir" $sudo docker build $buildOptions "$buildDir"
if [ $? -gt 0 ]; then if [ $? -gt 0 ]; then
echo "Build failed" echo "Build failed"
exit 1 exit 1
@ -41,7 +44,7 @@ function beforeTest() {
mkdir "$tmpDir" mkdir "$tmpDir"
echo "test::$(id -u):$(id -g):dir1,dir2" >> "$tmpDir/users" echo "test::$(id -u):$(id -g):dir1,dir2" >> "$tmpDir/users"
docker run \ $sudo docker run \
-v "$tmpDir/users:/etc/sftp-users.conf:ro" \ -v "$tmpDir/users:/etc/sftp-users.conf:ro" \
-v "$scriptDir/id_rsa.pub":/home/test/.ssh/keys/id_rsa.pub:ro \ -v "$scriptDir/id_rsa.pub":/home/test/.ssh/keys/id_rsa.pub:ro \
-v "$tmpDir":/home/test/share \ -v "$tmpDir":/home/test/share \
@ -49,23 +52,24 @@ function beforeTest() {
--expose 22 \ --expose 22 \
-d "$sftpImageName" \ -d "$sftpImageName" \
> "$redirect" > "$redirect"
sleep 2 # wait for sftp server to get ready
waitForServer $sftpContainerName
} }
function afterTest() { function afterTest() {
if [ "$output" != "quiet" ]; then if [ "$output" != "quiet" ]; then
echo "Docker logs:" echo "Docker logs:"
docker logs "$sftpContainerName" $sudo docker logs "$sftpContainerName"
fi fi
if [ "$cleanup" == "cleanup" ]; then if [ "$cleanup" == "cleanup" ]; then
docker rm -fv "$sftpContainerName" > "$redirect" $sudo docker rm -fv "$sftpContainerName" > "$redirect"
rm -rf "$tmpDir" rm -rf "$tmpDir"
fi fi
} }
function getSftpIp() { function getSftpIp() {
docker inspect -f {{.NetworkSettings.IPAddress}} "$1" $sudo docker inspect -f {{.NetworkSettings.IPAddress}} "$1"
} }
function runSftpCommands() { function runSftpCommands() {
@ -85,6 +89,27 @@ function runSftpCommands() {
-b - $user@$ip \ -b - $user@$ip \
> "$redirect" 2>&1 > "$redirect" 2>&1
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
} }
############################################################################## ##############################################################################
@ -92,7 +117,7 @@ function runSftpCommands() {
function testContainerIsRunning() { function testContainerIsRunning() {
$skipAllTests && skip && return 0 $skipAllTests && skip && return 0
ps="$(docker ps -q -f name="$sftpContainerName")" ps="$($sudo docker ps -q -f name="$sftpContainerName")"
assertNotEqual "$ps" "" assertNotEqual "$ps" ""
if [ -z "$ps" ]; then if [ -z "$ps" ]; then
@ -103,17 +128,14 @@ function testContainerIsRunning() {
function testLoginUsingSshKey() { function testLoginUsingSshKey() {
$skipAllTests && skip && return 0 $skipAllTests && skip && return 0
runSftpCommands "$sftpContainerName" \ runSftpCommands "$sftpContainerName" "test" "exit"
"test" \
"exit"
assertReturn $? 0 assertReturn $? 0
} }
function testWritePermission() { function testWritePermission() {
$skipAllTests && skip && return 0 $skipAllTests && skip && return 0
runSftpCommands "$sftpContainerName" \ runSftpCommands "$sftpContainerName" "test" \
"test" \
"cd share" \ "cd share" \
"mkdir test" \ "mkdir test" \
"exit" "exit"
@ -124,8 +146,7 @@ function testWritePermission() {
function testDir() { function testDir() {
$skipAllTests && skip && return 0 $skipAllTests && skip && return 0
runSftpCommands "$sftpContainerName" \ runSftpCommands "$sftpContainerName" "test" \
"test" \
"cd dir1" \ "cd dir1" \
"mkdir test-dir1" \ "mkdir test-dir1" \
"get -rf test-dir1 $tmpDir/" \ "get -rf test-dir1 $tmpDir/" \
@ -144,14 +165,15 @@ function testMinimalContainerStart() {
tmpContainerName="$sftpContainerName""_minimal" tmpContainerName="$sftpContainerName""_minimal"
docker run \ $sudo docker run \
--name "$tmpContainerName" \ --name "$tmpContainerName" \
-d "$sftpImageName" \ -d "$sftpImageName" \
minimal \ minimal \
> "$redirect" > "$redirect"
sleep 2
ps="$(docker ps -q -f name="$tmpContainerName")" waitForServer $tmpContainerName
ps="$($sudo docker ps -q -f name="$tmpContainerName")"
assertNotEqual "$ps" "" assertNotEqual "$ps" ""
if [ -z "$ps" ]; then if [ -z "$ps" ]; then
@ -159,11 +181,11 @@ function testMinimalContainerStart() {
fi fi
if [ "$output" != "quiet" ]; then if [ "$output" != "quiet" ]; then
docker logs "$tmpContainerName" $sudo docker logs "$tmpContainerName"
fi fi
if [ "$cleanup" == "cleanup" ]; then if [ "$cleanup" == "cleanup" ]; then
docker rm -fv "$tmpContainerName" > "$redirect" $sudo docker rm -fv "$tmpContainerName" > "$redirect"
fi fi
} }
@ -180,7 +202,7 @@ function testCustomContainerStart() {
> "$tmpDir/mount.sh" > "$tmpDir/mount.sh"
chmod +x "$tmpDir/mount.sh" chmod +x "$tmpDir/mount.sh"
docker run \ $sudo docker run \
--privileged=true \ --privileged=true \
--name "$tmpContainerName" \ --name "$tmpContainerName" \
-v "$scriptDir/id_rsa.pub":/home/custom/.ssh/keys/id_rsa.pub:ro \ -v "$scriptDir/id_rsa.pub":/home/custom/.ssh/keys/id_rsa.pub:ro \
@ -190,21 +212,26 @@ function testCustomContainerStart() {
-d "$sftpImageName" \ -d "$sftpImageName" \
custom:123 \ custom:123 \
> "$redirect" > "$redirect"
sleep 2
ps="$(docker ps -q -f name="$tmpContainerName")" waitForServer $tmpContainerName
ps="$($sudo docker ps -q -f name="$tmpContainerName")"
assertNotEqual "$ps" "" assertNotEqual "$ps" ""
runSftpCommands "$tmpContainerName" "custom" "cd bindmount" "mkdir test" "exit" runSftpCommands "$tmpContainerName" "custom" \
"cd bindmount" \
"mkdir test" \
"exit"
test -d "$tmpDir/custom/bindmount/test" test -d "$tmpDir/custom/bindmount/test"
assertReturn $? 0 assertReturn $? 0
if [ "$output" != "quiet" ]; then if [ "$output" != "quiet" ]; then
docker logs "$tmpContainerName" $sudo docker logs "$tmpContainerName"
fi fi
if [ "$cleanup" == "cleanup" ]; then if [ "$cleanup" == "cleanup" ]; then
docker rm -fv "$tmpContainerName" > "$redirect" $sudo docker rm -fv "$tmpContainerName" > "$redirect"
fi fi
} }