Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[tests] basic command-line api tests
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Jul 20, 2015
1 parent 7c3ed09 commit 422ffea
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/test-unit-text-only
@@ -0,0 +1,19 @@
#!/bin/bash

initial_dir=$(pwd)

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

if [ "$action" == "export" ]; then
trace "Exporting"
echo "first: $(date): $(uuidgen)" > $data_dir/1.txt
echo "second: $(date): $(uuidgen)" > $data_dir/2.txt
elif [ "$action" == "import" ]; then
trace "Importing"
for src in $data_dir/1.txt $data_dir/2.txt; do
[ -f $src ] || error 77 "There is no source file $src"
cp $src $home_dir || error 78 "Can't copy $src to $home_dir"
done
fi
168 changes: 168 additions & 0 deletions tests/test-vault-basic
@@ -0,0 +1,168 @@
#!/bin/bash

initial_dir=$(pwd)

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

create_test_dir

test_home=
storage=

user_name=User1
email=mail@user

function cd_vault_and_check_it {
cd $storage || failed 2 "No vault dir $storage"
init_root_and_enter
check_is_vault_storage $storage/.git
cd .git || failed 4 "No .git"
[ "$(git config user.name)" == "$user_name" ] || test_failed 5 "User name is wrong"
[ "$(git config user.email)" == "$email" ] || test_failed 5 "User email is wrong"
cd .. || test_failed 6 "Can't cd .."
}

NEXT_TEST "Create empty vault"
create_test_dirs 1
vault -V $storage -H $test_home -a init -g user.name=$user_name,user.email=$email \
|| test_failed 1 "Vault init is failed"
cd_vault_and_check_it

NEXT_TEST "Create empty vault, long options"
create_test_dirs 2
vault --vault $storage --home $test_home --action init --git-config user.name=$user_name,user.email=$email \
|| test_failed 1 "Vault init is failed"
cd_vault_and_check_it

function check_unit {
unit_name=$1
fname=$storage/.units/$unit_name.json
[ -f $fname ] || test_failed 7 "There is no unit conf file $fname"
}

function check_no_unit {
unit_name=$1
fname=$storage/.units/$unit_name.json
[ -f $fname ] && test_failed 7 "There shoudn't be unit conf file $fname"
}

unit=
unit_group=

function reg_unit {
ensure_param_count_exit_usage $# 3 "Use: reg_unit name group script"
unit=$1
unit_group=$2
script=$3
# move outside of repo to be sure it does not depend on location
cd $test_dir
vault -V $storage --H $test_home -a register \
-d name=$unit,group=$unit_group,icon=first-icon,script=$script \
|| test_failed 1 "Vault init is failed"
}

function unreg_unit {
unit=$1
# move outside of repo to be sure it does not depend on location
cd $test_dir
vault -V $storage --H $test_home -a unregister -M $unit \
|| test_failed 1 "Vault init is failed"
}

script=vault-test-script-generic

NEXT_TEST "Register local units"
reg_unit U0 G0 $script
check_unit $unit

reg_unit U1 G0 $script
check_unit $unit
check_unit U0

reg_unit U2 G1 $script
check_unit $unit
check_unit U0
check_unit U1

NEXT_TEST "Unregister local units"
unreg_unit U0
check_no_unit $unit
check_unit U1
check_unit U2

NEXT_TEST "Unit with readable data"
create_test_dirs text_unit
vault -V $storage -H $test_home -a init -g user.name=$user_name,user.email=$email \
|| test_failed 1 "Vault init is failed"
cd_vault_and_check_it
reg_unit UData GReadable $src/test-unit-text-only
vault -a export -M $unit -V $storage -H $test_home ||
test_failed 40 "Export of the unit $unit is failed"
[[ $(find $test_home | wc -l) -eq 1 ]] ||
test_failed 43 "There shouldn't be anything in the test home $test_home"

cd $storage
[[ $(git log --oneline | wc -l) -eq 3 ]] ||
test_failed 44 "Log should have 3 lines"
git_tag=$(git describe --tags HEAD)
[[ "$git_tag" =~ \>.* ]] || test_failed "Tag >... is expected, got $git_tag"
snap=$(get_snapshots $storage)
[[ "$snap" == "$git_tag" ]] || test_failed "Expected $snap == $git_tag"
#git_log=$(git log --oneline)
#echo "$git_log"
#[[ "$git_log" =~ ""]]

expected_data_dir=$(get_data_dir $storage $unit)
[[ $(find $expected_data_dir | wc -l) -eq 3 ]] ||
test_failed 43 "There shouldn't be files in the dir $expected_data_dir"

expected_blobs_dir=$(get_blobs_dir $storage $unit)
[[ $(find $expected_blobs_dir | wc -l) -eq 1 ]] ||
test_failed 43 "There shouldn't be anything in the dir $expected_blobs_dir"

vault -a import -M $unit -V $storage -H $test_home -t $snap ||
test_failed 40 "Import of the unit $unit is failed"

[[ $(find $test_home | wc -l) -eq 3 ]] ||
test_failed 43 "Check home $test_home contents"
expected_file=$test_home/1.txt
[ -f $expected_file ] || test_failed 41 "No file $expected_file"
[[ "$(cat $expected_file)" =~ first:.* ]] || test_failed "Check data in $expected_file"
expected_file=$test_home/2.txt
[ -f $expected_file ] || test_failed 42 "No file $expected_file"
[[ "$(cat $expected_file)" =~ second:.* ]] || test_failed "Check data in $expected_file"

NEXT_TEST "Second backup for the same unit"
vault -a export -M $unit -V $storage -H $test_home ||
test_failed 40 "Export of the unit $unit is failed"


snap_count=$(get_snapshots $storage | wc -l)
[[ $snap_count -eq 2 ]] || test_failed 43 "Expected 2 snapshots, got $snap_count"

home_copy=$test_dir/home_copy
cp -r $test_home $home_copy || test_failed 44 "Can't copy $test_home as $home_copy"

NEXT_TEST "Restore Second backup, get updated content"
snap=$(get_snapshots $storage | tail -n 1)
vault -a import -M $unit -V $storage -H $test_home -t $snap ||
test_failed 40 "Import of the unit $unit is failed"

ls $home_copy > $test_dir/before.txt
ls $test_home > $test_dir/after.txt
diff $test_dir/before.txt $test_dir/after.txt || test_failed 45 "ls should be the same"
diff -r $home_copy $test_home > $test_dir/diff.txt && test_failed 46 "content should change"

NEXT_TEST "Restore 1st backup, get content back"
snap_before=$(get_snapshots $storage | head -n 1)
[[ "$snap_before" != "$snap" ]] || test_failed 47 "Snapshot ids are equal: $snap_before $snap"
snap=$snap_before
vault -a import -M $unit -V $storage -H $test_home -t $snap ||
test_failed 40 "Import of the unit $unit is failed"
ls $home_copy > $test_dir/before.txt
ls $test_home > $test_dir/after.txt
diff $test_dir/before.txt $test_dir/after.txt || test_failed 48 "ls should be the same"
diff -r $home_copy $test_home > $test_dir/diff.txt || test_failed 49 "1st state should be restored"

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

initial_dir=$(pwd)

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

OPTS=$(getopt -o A:D:B:H: --long action:,dir:,bin-dir:,home-dir: -n "$0" -- "$@")
[ $? -eq 0 ] || error 3 "Error parsing options $@"

eval set -- "$OPTS"

while true; do
case "$1" in
-A | --action)
action=$2
shift 2
;;
-D | --dir)
data_dir=$2
shift 2
;;
-B | --bin-dir)
blobs_dir=$2
shift 2
;;
-H | --home-dir)
home_dir=$2
shift 2
;;
-- )
shift; break ;;
*)
shift; break ;;
esac
done

trace "Options: action=$action, data=$data_dir, blobs=$blobs_dir, home=$home_dir"

[[ "$action" =~ ^import|export$ ]] || error 31 "Unknown action: $action"
for d in data_dir blobs_dir home_dir; do
[ -d ${!d} ] || error 32 "Dir doesn't exist: $d=${!d}"
done

15 changes: 15 additions & 0 deletions tools/vault-misc
Expand Up @@ -222,6 +222,21 @@ function split_git_uri {
fi
}

function get_data_dir {
ensure_param_count_exit_usage $# 2 "get_data_dir <repo> <unit>"
echo $1/$2/data
}

function get_blobs_dir {
ensure_param_count_exit_usage $# 2 "get_blobs_dir <repo> <unit>"
echo $1/$2/blobs
}

function get_snapshots {
ensure_param_count_exit_usage $# 1 "get_snapshots <repo>"
vault -a list-snapshots -V $1
}

vault_misc=1
function get_vault_lock_cmd {
# wait for 30s
Expand Down

0 comments on commit 422ffea

Please sign in to comment.