2
0
mirror of https://github.com/deajan/osync synced 2024-11-19 03:25:37 +00:00
osync/dev/tests/run_tests.sh

471 lines
13 KiB
Bash
Raw Normal View History

2016-09-05 20:21:25 +00:00
#!/usr/bin/env bash
# osync test suite 2016111305
2016-09-05 20:21:25 +00:00
2016-09-06 17:26:37 +00:00
# 4 tests:
# quicklocal
# quickremote
# conflocal
# confremote
# for each test:
# files with spaces, subdirs
2016-11-13 19:22:52 +00:00
# largefileset (...large ?)
2016-09-06 17:26:37 +00:00
# exclusions
2016-11-13 19:22:52 +00:00
# conflict resolution initiator with backups / multiple backups
# conflict resolution target with backups / multiple backups
2016-09-06 17:26:37 +00:00
# deletion propagation
# lock checks
2016-10-17 14:45:02 +00:00
# file attribute tests
#TODO: conflict resolution missing
#TODO: lock checks missing
2016-10-23 17:17:25 +00:00
#TODO: skip deletion tests
2016-11-13 19:22:52 +00:00
#TODO: daemon mode tests
2016-09-06 17:26:37 +00:00
2016-09-16 07:51:29 +00:00
LARGE_FILESET_URL="http://ftp.drupal.org/files/projects/drupal-8.1.9.tar.gz"
2016-09-05 20:21:25 +00:00
OSYNC_DIR="$(pwd)"
OSYNC_DIR=${OSYNC_DIR%%/dev*}
DEV_DIR="$OSYNC_DIR/dev"
TESTS_DIR="$DEV_DIR/tests"
2016-10-23 17:17:25 +00:00
LOCAL_CONF="local.conf"
REMOTE_CONF="remote.conf"
OLD_CONF="old.conf"
TMP_OLD_CONF="tmp.old.conf"
2016-09-05 20:21:25 +00:00
OSYNC_EXECUTABLE="osync.sh"
2016-10-23 17:17:25 +00:00
OSYNC_UPGRADE="upgrade-v1.0x-v1.2x.sh"
2016-09-06 17:26:37 +00:00
TMP_FILE="$DEV_DIR/tmp"
2016-09-05 20:21:25 +00:00
2016-10-23 17:17:25 +00:00
2016-09-05 20:21:25 +00:00
if [ "$TRAVIS_RUN" == true ]; then
echo "Running with travis settings"
CONF_DIR="$TESTS_DIR/conf-travis"
SSH_PORT=22
else
echo "Running with local settings"
CONF_DIR="$TESTS_DIR/conf-local"
SSH_PORT=49999
fi
2016-10-23 17:17:25 +00:00
INITIATOR_DIR="${HOME}/osync-tests/initiator"
TARGET_DIR="${HOME}/osync-tests/target"
2016-09-16 15:19:04 +00:00
OSYNC_WORKDIR=".osync_workdir"
OSYNC_STATE_DIR="$OSYNC_WORKDIR/state"
2016-10-17 14:45:02 +00:00
OSYNC_DELETE_DIR="$OSYNC_WORKDIR/deleted"
OSYNC_BACKUP_DIR="$OSYNC_WORKDIR/backup"
2016-09-05 20:21:25 +00:00
2016-09-06 17:26:37 +00:00
# Setup an array with all function modes
2016-09-16 07:51:29 +00:00
declare -Ag osyncParameters
2016-09-06 17:26:37 +00:00
2016-11-13 14:11:06 +00:00
osyncParameters[quicklocal]="--initiator=$INITIATOR_DIR --target=$TARGET_DIR --instance-id=quicklocal"
osyncParameters[quickRemote]="--initiator=$INITIATOR_DIR --target=ssh://localhost:$SSH_PORT/$TARGET_DIR --rsakey=${HOME}/.ssh/id_rsa_local --instance-id=quickremote"
2016-11-13 19:22:52 +00:00
osyncParameters[confLocal]="$CONF_DIR/$LOCAL_CONF"
osyncParameters[confRemote]="$CONF_DIR/$REMOTE_CONF"
#osyncParameters[daemonlocal]="$CONF_DIR/$LOCAL_CONF --on-changes"
#osyncParameters[daemonlocal]="$CONF_DIR/$REMOTE_CONF --on-changes"
2016-09-06 17:26:37 +00:00
2016-09-05 20:21:25 +00:00
function SetStableToYes () {
if grep "^IS_STABLE=YES" "$OSYNC_DIR/$OSYNC_EXECUTABLE" > /dev/null; then
IS_STABLE=yes
else
IS_STABLE=no
sed -i.tmp 's/^IS_STABLE=no/IS_STABLE=yes/' "$OSYNC_DIR/$OSYNC_EXECUTABLE"
assertEquals "Set stable to yes" "0" $?
fi
}
function SetStableToOrigin () {
if [ "$IS_STABLE" == "no" ]; then
sed -i.tmp 's/^IS_STABLE=yes/IS_STABLE=no/' "$OSYNC_DIR/$OSYNC_EXECUTABLE"
assertEquals "Set stable to origin value" "0" $?
fi
}
function SetupSSH {
echo -e 'y\n'| ssh-keygen -t rsa -b 2048 -N "" -f "${HOME}/.ssh/id_rsa_local"
cat "${HOME}/.ssh/id_rsa_local.pub" >> "${HOME}/.ssh/authorized_keys"
chmod 600 "${HOME}/.ssh/authorized_keys"
# Add localhost to known hosts so self connect works
if [ -z $(ssh-keygen -F localhost) ]; then
ssh-keyscan -H localhost >> ~/.ssh/known_hosts
fi
}
2016-09-16 07:51:29 +00:00
function DownloadLargeFileSet() {
local destinationPath="${1}"
cd "$OSYNC_DIR"
wget -q "$LARGE_FILESET_URL" > /dev/null
assertEquals "Download [$LARGE_FILESET_URL]." "0" $?
2016-10-17 14:45:02 +00:00
tar xvf "$(basename $LARGE_FILESET_URL)" -C "$destinationPath" > /dev/null
2016-09-16 07:51:29 +00:00
assertEquals "Extract $(basename $LARGE_FILESET_URL)" "0" $?
2016-10-17 14:45:02 +00:00
rm -f "$(basename $LARGE_FILESET_URL)"
2016-09-16 07:51:29 +00:00
}
function CreateOldFile () {
2016-11-13 13:12:51 +00:00
local drive
2016-09-16 07:51:29 +00:00
local filePath="${1}"
2016-11-13 13:12:51 +00:00
touch "$filePath"
assertEquals "touch [$filePath]" "0" $?
# Get current drive
drive=$(df "$OSYNC_DIR" | tail -1 | awk '{print $1}')
# modify ctime on ext4 so osync thinks it has to delete the old files
2016-11-13 15:23:04 +00:00
debugfs -w -R 'set_inode_field "'$filePath'" ctime 201001010101' $drive
2016-09-16 07:51:29 +00:00
assertEquals "CreateOldFile [$filePath]" "0" $?
2016-11-13 13:12:51 +00:00
# force update of inodes (ctimes)
echo 3 > /proc/sys/vm/drop_caches
2016-09-16 07:51:29 +00:00
assertEquals "Drop caches" "0" $?
}
function PrepareLocalDirs () {
# Remote dirs are the same as local dirs, so no problem here
2016-09-05 20:21:25 +00:00
if [ -d "$INITIATOR_DIR" ]; then
rm -rf "$INITIATOR_DIR"
fi
mkdir -p "$INITIATOR_DIR"
if [ -d "$TARGET_DIR" ]; then
rm -rf "$TARGET_DIR"
fi
mkdir -p "$TARGET_DIR"
}
2016-09-05 20:25:25 +00:00
function oneTimeSetUp () {
source "$DEV_DIR/ofunctions.sh"
SetupSSH
}
function oneTimeTearDown () {
SetStableToOrigin
}
2016-10-23 17:17:25 +00:00
function setUp () {
rm -rf "$INITIATOR_DIR"
rm -rf "$TARGET_DIR"
}
2016-09-05 20:21:25 +00:00
function test_Merge () {
cd "$DEV_DIR"
./merge.sh
assertEquals "Merging code" "0" $?
SetStableToYes
}
2016-09-16 07:51:29 +00:00
function test_LargeFileSet () {
for i in "${osyncParameters[@]}"; do
cd "$OSYNC_DIR"
PrepareLocalDirs
DownloadLargeFileSet "$INITIATOR_DIR"
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
assertEquals "LargeFileSet test with parameters [$i]." "0" $?
2016-11-13 19:22:52 +00:00
[ -d "$INITIATOR_DIR/$OSYNC_STATE_DIR" ]
assertEquals "Initiator state dir exists" "0" $?
[ -d "$TARGET_DIR/$OSYNC_STATE_DIR" ]
assertEquals "Target state dir exists" "0" $?
2016-09-16 07:51:29 +00:00
done
}
function test_Exclusions () {
2016-11-13 13:12:51 +00:00
# Will sync except php files
# RSYNC_EXCLUDE_PATTERN="*.php" is set at runtime for quicksync and in config files for other runs
2016-09-16 07:51:29 +00:00
local numberOfPHPFiles
local numberOfExcludedFiles
2016-09-16 08:16:18 +00:00
local numberOfInitiatorFiles
local numberOfTargetFiles
2016-09-16 07:51:29 +00:00
for i in "${osyncParameters[@]}"; do
cd "$OSYNC_DIR"
PrepareLocalDirs
DownloadLargeFileSet "$INITIATOR_DIR"
2016-09-16 15:19:04 +00:00
numberOfPHPFiles=$(find "$INITIATOR_DIR" ! -wholename "$INITIATOR_DIR/$OSYNC_WORKDIR*" -name "*.php" | wc -l)
2016-09-16 07:51:29 +00:00
2016-09-16 15:23:25 +00:00
REMOTE_HOST_PING=no RSYNC_EXCLUDE_PATTERN="*.php" ./$OSYNC_EXECUTABLE $i
2016-09-16 07:51:29 +00:00
assertEquals "Exclusions with parameters [$i]." "0" $?
2016-10-17 14:45:02 +00:00
#WIP Add exclusion from file tests here
2016-09-16 15:19:04 +00:00
numberOfInitiatorFiles=$(find "$INITIATOR_DIR" ! -wholename "$INITIATOR_DIR/$OSYNC_WORKDIR*" | wc -l)
numberOfTargetFiles=$(find "$TARGET_DIR" ! -wholename "$TARGET_DIR/$OSYNC_WORKDIR*" | wc -l)
2016-09-16 08:16:18 +00:00
numberOfExcludedFiles=$((numberOfInitiatorFiles-numberOfTargetFiles))
2016-09-16 07:51:29 +00:00
assertEquals "Number of php files: $numberOfPHPFiles - Number of excluded files: $numberOfExcludedFiles" $numberOfPHPFiles $numberOfExcludedFiles
done
}
2016-10-17 14:45:02 +00:00
function test_Deletetion () {
local iFile1="$INITIATOR_DIR/ific"
local iFile2="$INITIATOR_DIR/ifoc"
local tFile1="$TARGET_DIR/tfic"
local tFile2="$TARGET_DIR/tfoc"
for i in "${osyncParameters[@]}"; do
cd "$OSYNC_DIR"
PrepareLocalDirs
touch "$iFile1"
touch "$iFile2"
touch "$tFile1"
touch "$tFile2"
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
assertEquals "First deletion run with parameters [$i]." "0" $?
rm -f "$iFile1"
rm -f "$tFile1"
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
assertEquals "Second deletion run with parameters [$i]." "0" $?
2016-11-13 19:22:52 +00:00
[ -f "$TARGET_DIR/$OSYNC_DELETE_DIR/$(basename $iFile1)" ]
assertEquals "File [$TARGET_DIR/$OSYNC_DELETE_DIR/$(basename $iFile1)] has been soft deleted on target" "0" $?
2016-10-17 14:45:02 +00:00
[ -f "$iFile1" ]
assertEquals "File [$iFile1] is still in initiator" "1" $?
[ -f "${iFile1/$INITIATOR_DIR/TARGET_DIR}" ]
assertEquals "File [${iFile1/$INITIATOR_DIR/TARGET_DIR}] is still in target" "1" $?
2016-11-13 19:22:52 +00:00
[ -f "$INITIATOR_DIR/$OSYNC_DELETE_DIR/$(basename $tFile1)" ]
assertEquals "File [$INITIATOR_DIR/$OSYNC_DELETE_DIR/$(basename $tFile1)] has been soft deleted on initiator" "0" $?
2016-10-17 14:45:02 +00:00
[ -f "$tFile1" ]
assertEquals "File [$tFile1] is still in target" "1" $?
[ -f "${tFile1/$TARGET_DIR/INITIATOR_DIR}" ]
assertEquals "File [${tFile1/$TARGET_DIR/INITIATOR_DIR}] is still in initiator" "1" $?
done
}
2016-11-13 13:12:51 +00:00
function test_softdeletion_cleanup () {
2016-11-13 19:22:52 +00:00
declare -A files
2016-11-13 13:12:51 +00:00
2016-11-13 19:22:52 +00:00
files[deletedFileInitiator]="$INITIATOR_DIR/$OSYNC_DELETE_DIR/someDeletedFileInitiator"
files[deletedFileTarget]="$TARGET_DIR/$OSYNC_DELETE_DIR/someDeletedFileTarget"
files[backedUpFileInitiator]="$INITIATOR_DIR/$OSYNC_BACKUP_DIR/someBackedUpFileInitiator"
files[backedUpFileTarget]="$TARGET_DIR/$OSYNC_BACKUP_DIR/someBackedUpFileTarget"
2016-11-13 15:23:04 +00:00
2016-11-13 13:12:51 +00:00
for i in "${osyncParameters[@]}"; do
cd "$OSYNC_DIR"
PrepareLocalDirs
# First run
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
assertEquals "First deletion run with parameters [$i]." "0" $?
# Get current drive
drive=$(df "$OSYNC_DIR" | tail -1 | awk '{print $1}')
# Create some deleted & backed up files, some new and some old
for file in "${files[@]}"; do
# Create directories first if they don't exist (deletion dir is created by osync, backup dir is created by rsync only when needed)
if [ ! -d "$(dirname $file)" ]; then
mkdir --parents "$(dirname $file)"
fi
touch "$file.new"
2016-11-13 14:11:06 +00:00
if [ "$TRAVIS_RUN" != true ]; then
CreateOldFile "$file.old"
else
echo "Skipping changing ctime on file because travis does not support debugfs"
2016-11-13 14:11:06 +00:00
fi
2016-11-13 13:12:51 +00:00
done
2016-11-13 19:22:52 +00:00
# Second run
2016-11-13 13:12:51 +00:00
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
# Check file presence
for file in "${files[@]}"; do
[ -f "$file.new" ]
assertEquals "New softdeleted / backed up file [$file.new] exists." "0" $?
2016-11-13 15:23:04 +00:00
2016-11-13 14:11:06 +00:00
if [ "$TRAVIS_RUN" != true ]; then
2016-11-13 15:23:04 +00:00
[ ! -f "$file.old" ]
2016-11-13 14:11:06 +00:00
assertEquals "Old softdeleted / backed up file [$file.old] is deleted permanently." "1" $?
else
2016-11-13 15:23:04 +00:00
[ ! -f "$file.old" ]
2016-11-13 14:11:06 +00:00
assertEquals "Old softdeleted / backed up file [$file.old] is deleted permanently." "0" $?
fi
2016-11-13 13:12:51 +00:00
done
done
}
2016-11-13 19:22:52 +00:00
function test_FileAttributePropagation () {
2016-10-23 17:17:25 +00:00
if [ "$TRAVIS_RUN" == true ]; then
echo "Skipping FileAttributePropagation tests as travis does not support getfacl / setfacl"
return 0
fi
2016-11-13 19:22:52 +00:00
for i in "${osyncParameters[@]}"; do
cd "$OSYNC_DIR"
PrepareLocalDirs
2016-10-23 17:17:25 +00:00
2016-11-13 19:22:52 +00:00
FileA="FileA"
FileB="FileB"
2016-09-05 20:21:25 +00:00
2016-11-13 19:22:52 +00:00
touch "$INITIATOR_DIR/$FileA"
touch "$TARGET_DIR/$FileB"
2016-09-05 20:21:25 +00:00
2016-11-13 19:22:52 +00:00
# First run
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
assertEquals "First deletion run with parameters [$i]." "0" $?
2016-09-05 20:21:25 +00:00
2016-11-13 19:22:52 +00:00
sleep 1
2016-09-05 20:21:25 +00:00
2016-11-13 19:22:52 +00:00
getfacl -p "$INITIATOR_DIR/$FileA" | grep "other::r--" > /dev/null
assertEquals "Check getting ACL on initiator." "0" $?
getfacl -p "$TARGET_DIR/$FileB" | grep "other::r--" > /dev/null
assertEquals "Check getting ACL on target." "0" $?
2016-10-23 17:17:25 +00:00
2016-11-13 19:22:52 +00:00
setfacl -m o:r-x "$INITIATOR_DIR/$FileA"
assertEquals "Set ACL on initiator" "0" $?
setfacl -m o:-w- "$TARGET_DIR/$FileB"
assertEquals "Set ACL on target" "0" $?
2016-10-23 17:17:25 +00:00
2016-11-13 19:22:52 +00:00
# Second run
REMOTE_HOST_PING=no ./$OSYNC_EXECUTABLE $i
assertEquals "First deletion run with parameters [$i]." "0" $?
2016-09-05 20:21:25 +00:00
2016-11-13 19:22:52 +00:00
getfacl -p "$TARGET_DIR/$FileA" | grep "other::r-x" > /dev/null
assertEquals "ACLs matched original value on target." "0" $?
2016-09-05 20:21:25 +00:00
2016-11-13 19:22:52 +00:00
getfacl -p "$INITIATOR_DIR/$FileB" | grep "other::-w-" > /dev/null
assertEquals "ACLs matched original value on initiator." "0" $?
getfacl -p "$TARGET_DIR/$FileA"
getfacl -p "$INITIATOR_DIR/$FileB"
done
2016-09-05 20:21:25 +00:00
}
function test_WaitForTaskCompletion () {
2016-09-06 17:26:37 +00:00
local pids
2016-09-05 20:21:25 +00:00
# Tests if wait for task completion works correctly
# Standard wait
sleep 1 &
pids="$!"
sleep 2 &
pids="$pids;$!"
WaitForTaskCompletion $pids 0 0 ${FUNCNAME[0]} true 0
assertEquals "WaitForTaskCompletion test 1" "0" $?
# Standard wait with warning
sleep 2 &
pids="$!"
sleep 5 &
pids="$pids;$!"
WaitForTaskCompletion $pids 3 0 ${FUNCNAME[0]} true 0
assertEquals "WaitForTaskCompletion test 2" "0" $?
# Both pids are killed
sleep 5 &
pids="$!"
sleep 5 &
pids="$pids;$!"
WaitForTaskCompletion $pids 0 2 ${FUNCNAME[0]} true 0
assertEquals "WaitForTaskCompletion test 3" "2" $?
# One of two pids are killed
sleep 2 &
pids="$!"
sleep 10 &
pids="$pids;$!"
WaitForTaskCompletion $pids 0 3 ${FUNCNAME[0]} true 0
assertEquals "WaitForTaskCompletion test 4" "1" $?
# Count since script begin, the following should output two warnings and both pids should get killed
sleep 20 &
pids="$!"
sleep 20 &
pids="$pids;$!"
WaitForTaskCompletion $pids 3 5 ${FUNCNAME[0]} false 0
assertEquals "WaitForTaskCompletion test 5" "2" $?
}
function test_ParallelExec () {
2016-09-06 17:26:37 +00:00
local cmd
# Test if parallelExec works correctly in array mode
2016-09-05 20:21:25 +00:00
cmd="sleep 2;sleep 2;sleep 2;sleep 2"
ParallelExec 4 "$cmd"
assertEquals "ParallelExec test 1" "0" $?
cmd="sleep 2;du /none;sleep 2"
ParallelExec 2 "$cmd"
assertEquals "ParallelExec test 2" "1" $?
cmd="sleep 4;du /none;sleep 3;du /none;sleep 2"
ParallelExec 3 "$cmd"
assertEquals "ParallelExec test 3" "2" $?
2016-09-06 17:26:37 +00:00
# Test if parallelExec works correctly in file mode
echo "sleep 2" > "$TMP_FILE"
echo "sleep 2" >> "$TMP_FILE"
echo "sleep 2" >> "$TMP_FILE"
echo "sleep 2" >> "$TMP_FILE"
2016-09-08 20:57:57 +00:00
ParallelExec 4 "$TMP_FILE" true
2016-09-06 17:26:37 +00:00
assertEquals "ParallelExec test 4" "0" $?
echo "sleep 2" > "$TMP_FILE"
echo "du /nome" >> "$TMP_FILE"
echo "sleep 2" >> "$TMP_FILE"
2016-09-08 20:57:57 +00:00
ParallelExec 2 "$TMP_FILE" true
2016-09-06 17:26:37 +00:00
assertEquals "ParallelExec test 5" "1" $?
echo "sleep 4" > "$TMP_FILE"
echo "du /none" >> "$TMP_FILE"
echo "sleep 3" >> "$TMP_FILE"
echo "du /none" >> "$TMP_FILE"
echo "sleep 2" >> "$TMP_FILE"
2016-09-08 20:57:57 +00:00
ParallelExec 3 "$TMP_FILE" true
2016-09-06 17:26:37 +00:00
assertEquals "ParallelExec test 6" "2" $?
2016-09-05 20:21:25 +00:00
}
2016-10-23 17:17:25 +00:00
function test_UpgradeConfRun () {
# Basic return code tests. Need to go deep into file presence testing
cd "$OSYNC_DIR"
PrepareLocalDirs
# Make a security copy of the old config file
cp "$CONF_DIR/$OLD_CONF" "$CONF_DIR/$TMP_OLD_CONF"
./$OSYNC_UPGRADE "$CONF_DIR/$TMP_OLD_CONF"
assertEquals "Conf file upgrade" "0" $?
./$OSYNC_EXECUTABLE "$CONF_DIR/$TMP_OLD_CONF"
assertEquals "Upgraded conf file execution test" "0" $?
rm -f "$CONF_DIR/$TMP_OLD_CONF"
rm -f "$CONF_DIR/$TMP_OLD_CONF.save"
}
2016-09-05 20:21:25 +00:00
. "$TESTS_DIR/shunit2/shunit2"