#!/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" 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 testMinimalContainerStart() { $skipAllTests && skip && return 0 tmpContainerName="$sftpContainerName""_minimal" docker run \ --name "$tmpContainerName" \ -d "$sftpImageName" \ minimal::1111 \ > "$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 } 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 "exit" assertReturn $? 0 } function testWritePermission() { $skipAllTests && skip && return 0 runSftpCommands "cd share" "mkdir test" "exit" test -d "$tmpDir/test" assertReturn $? 0 } ############################################################################## # Run tests source "$scriptDir/bashunit.bash" # Nothing happens after this