Skip to content

Commit

Permalink
functional tests refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Jul 20, 2015
1 parent a759a24 commit 7c3ed09
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 86 deletions.
62 changes: 16 additions & 46 deletions tools/test-vault-move → tests/test-vault-move
Expand Up @@ -4,62 +4,32 @@ initial_dir=$(pwd)

src=$(dirname $0)
src=$(cd $src;pwd)
source $src/vault-misc $src

cur_test="Begin"
function next_test {
trace $@
cur_test=$1
}

function test_failed {
echo 'stdout {' >&2;
cat $test_out >&2;
echo '} stderr {' >&2;
cat $test_err >&2;
echo '}' >&2;
error $1 "Test: $cur_test." ${@:2}
}

function check_is_vault_storage {
d=$1
[ -d $d ] || test_failed 7 "Not a dir $d"
[ -f $d/config ] || test_failed 7 "Not a git storage"
[ -d $d/blobs ] || test_failed 7 "Not a vault storage $d?"
}
source $src/testing-common-vault.sh

trace "Test run in $TOP_PID"

empty_repo_archive=$src/../tests/empty-repo.tar.gz
[ -f $empty_repo_archive ] || test_failed 1 "No test vault archive"

is_success=true

test_dir=$(mktemp -t -d vault.XXXXXXXXXX)
test_out=$test_dir/stdout.txt
test_err=$test_dir/stderr.txt
cd $test_dir || test_failed 1 "Can't enter $test_dir"
normal_cleanup="$normal_cleanup; echo 'OK' 1>&2; cd $initial_dir; rm -rf $test_dir"
failed_cleanup="warn 'Dir $test_dir is left';$failed_cleanup;"

create_test_dir

r1=$test_dir/v1
rout=$test_dir/external1
rout2=$test_dir/external2

next_test "Prepare $test_dir"
NEXT_TEST "Prepare $test_dir"
mkdir $r1 || test_failed 2 "Can't create $r1"
tar xf $empty_repo_archive -C $r1 || test_failed 4 "Can't extract archive"
cd $r1 || test_failed 5 "Can't enter $r1"
init_root_and_enter
storage_now=$r1/.git

next_test "Move to exiting dir"
NEXT_TEST "Move to exiting dir"
mkdir $rout || test_failed 10 "Can't create $rout"
git-vault-move $rout 1>$test_out 2>$test_err \
&& test_failed 6 "Move to existing $rout shouldn't succeed"

next_test "Move to read-only dir"
NEXT_TEST "Move to read-only dir"
chmod ug-w $rout || test_failed 12 "Can't chown $rout"
ro_dir=$rout/inaccessible
git-vault-move $ro_dir 1>$test_out 2>$test_err \
Expand All @@ -68,53 +38,53 @@ git-vault-move $ro_dir 1>$test_out 2>$test_err \
check_is_vault_storage $storage_now
rmdir $rout || test_failed 11 "Dir $rout is not empty?"

next_test "Move out"
NEXT_TEST "Move out"
git-vault-move $rout \
|| test_failed 6 "Move to $rout"
storage_now=$rout
[ -f $r1/.git ] || test_failed 7 "Not a file $r1/.git"
check_is_vault_storage $storage_now

next_test "Move out-of-tree dir to itself"
NEXT_TEST "Move out-of-tree dir to itself"
git-vault-move $rout 1>$test_out 2>$test_err \
&& test_failed 14 "Moving into self shouldn't succeed for $rout"

next_test "Move out to another dir"
NEXT_TEST "Move out to another dir"
git-vault-move $rout2 \
|| test_failed 6 "Move to $rout2"
storage_now=$rout2
[ -f $r1/.git ] || test_failed 7 "Not a file $r1/.git"
check_is_vault_storage $storage_now

next_test "Move to read-only in-tree .git"
NEXT_TEST "Move to read-only in-tree .git"
chmod ug-w $r1/.git || test_failed 12 "Can't chown $r1/.git"
git-vault-move $r1/.git 1>$test_out 2>$test_err \
&& test_failed 16 "Move to RO $r1/.git shouldn't succeed"
[ -f $r1/.git ] || test_failed 7 "Not a file $r1/.git"
check_is_vault_storage $storage_now
chmod ug+w $r1/.git || test_failed 12 "Can't chown back $r1/.git"

next_test "Move in"
NEXT_TEST "Move in"
git-vault-move $r1/.git \
|| test_failed 6 "Move to $r1/.git"
storage_now=$r1/.git
[ -d $rout ] && test_failed 7 "Dir $rout is left"
[ -d $r1/.git ] || test_failed 7 "Not a dir $r1/.git"
check_is_vault_storage $storage_now

next_test "Move in-tree dir to itself"
NEXT_TEST "Move in-tree dir to itself"
git-vault-move $r1 1>$test_out 2>$test_err \
&& test_failed 8 "Moving into self shouldn't succeed $r1.git"

next_test "Setup to check moving between filesystems"
NEXT_TEST "Setup to check moving between filesystems"
other_fs=/dev/shm
[ -d /dev/shm ] || error 20 "Can't test further w/o /dev/shm used as other fs"
other_test_dir=$other_fs/$(basename $test_dir)
mkdir $other_test_dir || error 20 "Can't create $other_test_dir"
normal_cleanup="$normal_cleanup; rm -rf $other_test_dir"
failed_cleanup="warn 'Dir $other_test_dir is left';$failed_cleanup"

next_test "Move to other fs"
NEXT_TEST "Move to other fs"

out_repo=$other_test_dir/v1

Expand All @@ -124,22 +94,22 @@ storage_now=$out_repo
[ -f $r1/.git ] || test_failed 7 "Not a file $r1/.git"
check_is_vault_storage $storage_now

next_test "Move from other fs"
NEXT_TEST "Move from other fs"
git-vault-move $r1/.git \
|| test_failed 6 "Move from $out_repo"
[ -d $out_repo ] && error 23 "Dir should not be left: $out_repo"
storage_now=$r1/.git
[ -d $r1/.git ] || test_failed 7 "Not a dir $r1/.git"
check_is_vault_storage $storage_now

next_test "Move back to other fs"
NEXT_TEST "Move back to other fs"
git-vault-move $out_repo \
|| test_failed 6 "Move to $out_repo"
storage_now=$out_repo
[ -f $r1/.git ] || test_failed 7 "Not a file $r1/.git"
check_is_vault_storage $storage_now

next_test "Move from other fs to out-of-tree"
NEXT_TEST "Move from other fs to out-of-tree"
git-vault-move $rout \
|| test_failed 6 "Move to $rout"
storage_now=$rout
Expand Down
6 changes: 2 additions & 4 deletions tools/test-vault-split-uri → tests/test-vault-split-uri
Expand Up @@ -3,7 +3,7 @@ exec=./vault-clone-blobs

src=$(dirname $0)
src=$(cd $src;pwd)
source $src/vault-misc $src
source $src/vault-testing.sh

function assert_split {
pattern=$1
Expand All @@ -26,7 +26,7 @@ function assert_split {
*)
error 4 "Unsupported uri: $uri"
esac

if [ "$res" != "$2" ]; then
error "FOR '$pattern': $res != $2"
exit 1
Expand All @@ -47,5 +47,3 @@ assert_split user0@host1:12/home/dir2 "SRC:user=user0,host=host1,port=12,path=/h
assert_split user0@host1.org:/home/dir2 "SRC:user=user0,host=host1.org,port=,path=/home/dir2"
assert_split host1.org:/home/dir2 "SRC:user=$(whoami),host=host1.org,port=,path=/home/dir2"
assert_split host1.org\\:/home/dir2 "SRC:path=host1.org\\:/home/dir2"


64 changes: 64 additions & 0 deletions tests/testing-common-vault.sh
@@ -0,0 +1,64 @@
#!/bin/bash

initial_dir=$(pwd)

src=$(dirname $0)
src=$(cd $src;pwd)
lib_dir=$src
if [ "x$VAULT_LIB_DIR" != "x" ]; then
if ! [ -d $VAULT_LIB_DIR ]; then
echo "No such dir $VAULT_LIB_DIR"
exit 1
fi
lib_dir="$VAULT_LIB_DIR"
elif [ -d "$src/../tools" ]; then
export VAULT_LIB_DIR=$src/../tools
lib_dir="$VAULT_LIB_DIR"
fi
source $lib_dir/vault-misc

cur_test="Begin"
function NEXT_TEST {
trace $@
cur_test=$1
}

function test_failed {
echo 'stdout {' >&2;
cat $test_out >&2;
echo '} stderr {' >&2;
cat $test_err >&2;
echo '}' >&2;
error $1 "Test: $cur_test." ${@:2}
}

function check_is_vault_storage {
d=$1
[ -d $d ] || test_failed 7 "Not a dir $d"
[ -f $d/config ] || test_failed 7 "Not a git storage"
[ -d $d/blobs ] || test_failed 7 "Not a vault storage $d?"
}


function create_test_dir {
test_dir=$(mktemp -t -d vault.XXXXXXXXXX)
test_out=$test_dir/stdout.txt
test_err=$test_dir/stderr.txt
touch $test_out
touch $test_err
cd $test_dir || test_failed 1 "Can't enter $test_dir"
normal_cleanup="$normal_cleanup; echo 'OK' 1>&2; cd $initial_dir; rm -rf $test_dir"
failed_cleanup="warn 'Dir $test_dir is left';$failed_cleanup;"
}

test_home=
storage=

function create_test_dirs {
ensure_param_count_exit_usage $# 1 "create_test_dirs prefix"
prefix=$1
test_home=$test_dir/$prefix/home
storage=$test_dir/$prefix/vault
mkdir -p $test_home || test_failed 1 "Can't create $test_home"
mkdir -p $storage || test_failed 1 "Can't create $storage"
}
29 changes: 4 additions & 25 deletions tools/git-vault-root
@@ -1,27 +1,6 @@
#!/bin/bash

src=`dirname $0`
src=`cd $src;pwd`
source $src/vault-misc $src

root=$(git rev-parse --show-toplevel)
rc=$?

if [ $rc -ne 0 ] || [ "x$root" == "x" ]; then
error 1 "Not a git `pwd`"
fi

if ! [ -d "$root" ]; then
error 2 "Wrong location, not a git repo? `pwd`"
fi

if [ -f $root/.vault ]; then
echo $root
else
if [ root == "`dirname /`" ]; then
error 3 "Reached /, vault is not found"
fi
root=`dirname $root`
res=$(cd $root && git vault-root) || exit 34
echo $res
fi
src=$(dirname $0)
src=$(cd $src;pwd)
source $src/vault-misc
get_root $(pwd)
48 changes: 37 additions & 11 deletions tools/vault-misc
Expand Up @@ -70,14 +70,22 @@ function error {
kill -s TERM $TOP_PID
}

if [ $# -eq 1 ]; then
src=$1
else
src=`dirname $0`
src=`cd $src;pwd`
fi
trace "SCRIPTS DIR: $src"
export PATH=$src:$PATH
update_path () {
local my_path=$(dirname $1)
my_path=$(readlink -f $(cd $my_path;pwd))
if ! (echo $PATH | grep "^$my_path:.*" 1>/dev/null); then
export PATH=$my_path:$PATH
fi
if [ "x$VAULT_LIB_DIR" != "x" ]; then
[ -d $VAULT_LIB_DIR ] || error 6 "No such dir $VAULT_LIB_DIR"
local lib_path=$(readlink -f $VAULT_LIB_DIR)
if [ "$my_path" != "$lib_path" ]; then
export PATH=$lib_path:$PATH
fi
fi
trace "SCRIPTS PATH: $PATH"
}
update_path $0

function ensure_param_count_op_exit_usage {
[ $# -ge 3 ] || error 22 "Call as Num#0 OP Num#1"
Expand Down Expand Up @@ -109,9 +117,28 @@ function units_path {
echo $1/.units/
}

function get_root {
current_root=$1
while true; do
current_root=$(cd $current_root && git rev-parse --show-toplevel)
if [ $? -ne 0 ] || [ "x$current_root" == "x" ] ||
! [ -d "$current_root" ] ; then
error 1 "Not a git $1"
fi
if [ -f $current_root/.vault ]; then
echo $current_root
break
else
[ "$(readlink -f $current_root)" == "/" ] &&
error 33 "Reached /, vault is not found"
current_root=$(dirname $current_root)
fi
done
}

function init_root {
if [ "x$root" == "x" ]; then
root=$(git vault-root)
root=$(get_root $(pwd))
if [ $? -ne 0 ]; then
error $? "Not inside vault? $(pwd)"
fi
Expand All @@ -132,7 +159,6 @@ function init_root {
function init_root_and_enter {
init_root
cd $root || error 2 "Can't enter $root"
echo $root
}

function get_vid {
Expand Down Expand Up @@ -174,7 +200,7 @@ function units {
}

function split_git_uri {
uri=$1
local uri=$1
trace "split $uri"
# parsing is too dumb now, it should be stricter
ssh_re='(ssh://)?((.*)@)?(.*[^\\]):([0-9]*)(.*)'
Expand Down

0 comments on commit 7c3ed09

Please sign in to comment.