#!/bin/bash
# ----------------------------------------------------------------------
# File: eos-fusex-certify
# Author: Andreas-Joachim Peters - CERN
# ----------------------------------------------------------------------

# ************************************************************************
# * EOS - the CERN Disk Storage System                                   *
# * Copyright (C) 2017 CERN/Switzerland                                  *
# *                                                                      *
# * This program is free software: you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation, either version 3 of the License, or    *
# * (at your option) any later version.                                  *
# *                                                                      *
# * This program is distributed in the hope that it will be useful,      *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
# * GNU General Public License for more details.                         *
# *                                                                      *
# * You should have received a copy of the GNU General Public License    *
# * along with this program.  If not, see <http://www.gnu.org/licenses/>.*

wdir="certify.$RANDOM";
mkdir $wdir
cd $wdir
echo "===================================================================="
echo "--- ... working-dir = $PWD"
##################################################

if [ "$1" = "1" ] || [ "$1" = "" ]; then
time (
if [ -e "/usr/sbin/fusex-benchmark" ]; then
echo "===================================================================="
echo "001 ... fusex-benchmark"
mkdir 001-fusex-benchmark
cd 001-fusex-benchmark
/usr/sbin/fusex-benchmark >&/tmp/001-fusex-benchmark.log ||  exit 1
cd ../
fi
) || exit 1
fi
##################################################
if [ "$1" = "2a" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "002a ... rename-test"
mkdir 002-rename
cd 002-rename
for name in `seq 1 1000`; do unlink b 2>/dev/null && echo damn b exists; touch a; mv a b ; stat b>/dev/null; touch a; mv -f a b ; unlink b; ls | grep b && echo failed && exit 2 ; done;  exit 0;
) || exit 2
fi
##################################################
if [ "$1" = "2b" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "002b ... mkdir-rmdir-test"
mkdir 002-mkdir-rmdir
cd 002-mkdir-rmdir
for name in `seq 1 1000`; do  mkdir asdf || exit 2; rmdir asdf; done;  exit 0;
) || exit 2
fi
##################################################
if [ "$1" = "2c" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "002c ... rename-overwrite-test"
(
    mkdir 002-rename-overwrite
    cd 002-rename-overwrite
    for name in `seq 1 1000`; do
	echo $name
	mkdir a  || exit 1
	mkdir a/a
	mkdir b
	mkdir b/b
	mv -T a/a b/b
	ls -la a/
	ls -la b/
	find b
	rm -rf a || exit 9
	rm -rf b || exit 10
    done
) >& /tmp/002c-rename-overwrite-test.log
) || exit 2
fi
##################################################
if [ "$1" = "3" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "003 ... git-clone-test"
mkdir 003-git
cd 003-git
git clone https://github.com/gbitzes/quarkdb quarkdb-2 >& /tmp/003-git-clone-test.log || exit 3
cd quarkdb-2
git submodule update --recursive --init >> /tmp/003-git-clone-test.log 2>&1 || exit 3
cd ../../
) || exit 3
fi
##################################################
if [ "$1" = "4" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "004 ... xrootd-compilation"
mkdir 004-xrootd
cd 004-xrootd
tar xvzf /var/eos/test/fuse/untar/xrootd.tgz >& /tmp/004-xrootd-compilation.log
cd xrootd
mkdir build
cd build
cmake3 ../ >> /tmp/004-xrootd-compilation.log 2>&1 || exit 4
time make -j 4  >> /tmp/004-xrootd-compilation.log 2>&1 || exit 4
cd ../../../
) || exit 4
fi
##################################################
if [ "$1" = "5" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "005 ... client-tests"
mkdir 005-eosclient-tests
cd 005-eosclient-tests
git clone https://gitlab.cern.ch/dss/eosclient-tests.git >& /tmp/005-eos-clienttests.log
cd eosclient-tests/
echo "005a... micro-tests"
python run.py --workdir=$PWD microbench >& /tmp/005-eos-clienttests.log 2>&1 || exit 5
cd functional_tests/
echo "005b... zlib-compile"
./zlib-compile-selftest.sh $PWD >& /tmp/005-eos-clienttests.log 2>&1 || exit 5
echo "005c... git-clone"
./git-clone.sh $PWD  >& /tmp/005-eos-clienttests.log 2>&1 || exit 5
echo "005d... rsync"
./rsync.sh $PWD  >& /tmp/005-eos-clienttests.log 2>&1 || exit 5
echo "005d... sqlite"
./test_sqlite.py $PWD >& /tmp/005-eos-clienttests.log 2>&1|| exit 5
) || exit 5
fi
##################################################
if [ "$1" = "6" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "006 ... eos-rpm-test"
mkdir 006-eos-rpm-test
cd 006-eos-rpm-test
echo "006 ... clone eos"
git clone https://:@gitlab.cern.ch:8443/dss/eos.git >& /tmp/006-eos-rpm-test.log 2>&1 || exit 6
cd eos
echo "006 ... submodule update"
git submodule update --init --recursive >& /tmp/006-eos-rpm-test.log 2>&1 || exit 6
echo "006 ... cmake"
mkdir build
cd build
uname -a | grep ".el6." >& /dev/null
if [ $? -eq 0 ] ; then
    scl enable devtoolset-8 "cmake3 ../ -DPACKAGEONLY=1" >& /tmp/006-eos-rpm-test.log 2>&1 || exit 6
else
    scl enable devtoolset-8 "cmake3 ../ -DPACKAGEONLY=1" >& /tmp/006-eos-rpm-test.log 2>&1 || exit 6
fi

make srpm >& /tmp/006-eos-rpm-test.log 2>&1 || exit 6
rpmbuild --rebuild --define "_topdir $(pwd)/build" --define "debug_package %{nil}" SRPMS/eos-*.src.rpm >& /tmp/006-eos-rpm-test.log 2>&1 || exit 6
for name in `seq 1 15`; do echo $name / 15 ; sleep 10; done
cd ../../
rm -rf eos
) || exit 6
fi
##################################################
if [ "$1" = "7" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "007 ... eos-write-recovery-test"
mkdir 007-eos-write-recovery-test
cd 007-eos-write-recovery-test
echo "007 ... recovery-on-flush"
dd if=/dev/zero of=512M.1\#err_sim_flush\# bs=1M count=512 || exit 7
cat 512M.1\#err_sim_flush\# > /dev/null || exit 7
echo "007 ... recovery-on-flusher"
dd if=/dev/zero of=512M.2\#err_sim_flusher\# bs=1M count=512 || exit 7
cat 512M.2\#err_sim_flusher\# > /dev/null || exit 7
rm -rf 512M.1\#err_sim_flush\#
rm -rf 512M.2\#err_sim_flusher\#
) || exit 7
fi
##################################################
if [ "$1" = "8" ] || [ "$1" = "" ]; then
time (
echo "===================================================================="
echo "008 ... eos-hardlink-tests"
mkdir 008-eos-hardlink-tests
cd 008-eos-hardlink-tests
echo "008 ... create hardlinks"
touch /tmp/008-eos-hardlink-tests
cp /etc/passwd .
for name in `seq 2 128`; do
  ln passwd hardlink.$name >> /tmp/008-eos-hardlink-tests
  link_source=`stat -c %h passwd`;
  link_target=`stat -c %h hardlink.$name`;
  ino_source=`stat -c %i passwd`;
  ino_target=`stat -c %i hardlink.$name`;
  test "$name" == "$link_source" || exit 8
  test "$name" == "$link_target" || exit 8
  test "$ino_source" == "$ino_target" || exit 8
  echo $name $link_source $link_target >> /tmp/008-eos-hardlink-tests
done
echo "008 ... remove hardlinks"
for name in `seq 128 -1 2`; do
  link_source=`stat -c %h passwd`;
  link_target=`stat -c %h hardlink.$name`;
  echo $name $link_source $link_target >> /tmp/008-eos-hardlink-tests
  test "$name" == "$link_source" || exit 8
  test "$name" == "$link_target" || exit 8
  unlink hardlink.$name >> /tmp/008-eos-hardlink-tests
done

rm passwd
) || exit 8
fi


if [ "$1" = "9" ] ; then
time (
echo "===================================================================="
echo "009 ... inlined-files test"
echo "    ... this test works only with uncompressed inlined files and max-size= <4096"
mkdir 009-eos-inlined-files-tests
cd 009-eos-inlined-files-tests
echo "009 creating inlined file"
touch inlined
# create an inlined file
date +%s >> inlined;
# we should see the inline buffer
getfattr --only-values -d -n system.eos.md inlined | grep sys.file.buf || exit 9
echo ". 1"

# truncate of the limit
truncate -s 4097 inlined;
# file should not be inlined anymore
getfattr --only-values -d -n system.eos.md inlined | grep sys.file.buf && exit 9
echo ". 2"

truncate -s 0 inlined;
# file should have an empty inline buffer
getfattr --only-values -d -n system.eos.md inlined | grep sys.file.buf || exit 9;
echo ". 3"

# create an inlined file
date +%s >> inlined;
# we should see the inline buffer
getfattr --only-values -d -n system.eos.md inlined | grep sys.file.buf || exit 9
echo ". 4"

for name in `seq 1 400`; do
    date +%s >> inlined ;
done
getfattr --only-values -d -n system.eos.md inlined | grep sys.file.buf && exit 9
echo ". 6"

) || exit 9
fi


if [ "$1" = "10" ] ; then
time (
echo "===================================================================="
echo "010 ... non UTF-8 files test"
mkdir 010-eos-non-utf8-test
cd 010-eos-non-utf8-test
echo "010 creating funny filenames"
touch $'\007' || exit 10

touch $'\xa9' || exit 10

echo "010 stat'ing funny filenames"
stat $'\007' || exit 10
stat $'\xa9' || exit 10

echo "010 listinging funny filenames"
ls | grep $'\007' || exit 10
ls | grep $'\xa9' || exit 10

echo "010 deleting funny filenames"
rm $'\007' || exit 10
rm $'\xa9' || exit 10
echo "010 all done"
) || exit 10
fi

if [ "$1" = "11" ] ; then
time (
echo "===================================================================="
echo "011 ... chmod test"
mkdir 011-eos-chmod-test
cd 011-eos-chmod-test
mkdir newdir || exit 11
chmod a-w newdir || exit 11
mkdir newdir/forbidden >& /dev/null
if [ $? -ne 0 ] ; then echo OK > /dev/null; else exit 11; fi
chmod a+w newdir || exit 11
mkdir newdir/forbidden >& /dev/null
if [ $? -ne 0 ] ; then exit 11; else echo OK > /dev/null; fi
rmdir newdir/forbidden || exit 11;
rmdir newdir || exit 11;
) || exit 11
fi

if [ "$1" = "12" ]; then
time (
echo "===================================================================="
echo "012 ... rm-rf eternal loop test"
mkdir 012-eos-rmrf-test
cd 012-eos-rmrf-test
for name in `seq 1 100`; do mkdir name; rm -rf name; stat name 2>/dev/null  && exit 12;  done; exit 0;
) || exit 12
fi

if [ "$1" = "13" ] ; then
time (
echo "===================================================================="
echo "013 ... IO verify 512 files"
mkdir 013-eos-ioverify-test
cd 013-eos-ioverify-test

eos-fusex-ioverify $PWD 512 0 >& /tmp/013-eos-clienttests.log
if [ $? -eq 0 ] ; then echo OK > /dev/null; else exit 13; fi
echo "3" > /proc/sys/vm/drop_caches
eos-fusex-ioverify $PWD 512 1000 >> /tmp/013-eos-clienttests.log 2>&1
if [ $? -eq 0 ] ; then echo OK > /dev/null; else exit 13; fi
rm -rf pattern.* >> /tmp/032-eos-clienttests.log 2>&1
if [ $? -eq 0 ] ; then echo OK > /dev/null; else exit 13; fi
) || exit 13
fi


if [ "$1" = "14" ] ; then
time (
echo "===================================================================="
echo "014 ... flock test"
mkdir 014-flock-test
cd 014-flock-test

FLOCK=$PWD/flocktest
flock --verbose "$FLOCK" -c '/bin/sleep 5' > /dev/null &
sleep 1;
# the lock should not work now
flock --verbose --nonblock "$FLOCK" -c '/bin/sleep 5' > /dev/null 2>&1
if [ $? -ne 0 ]; then echo OK > /dev/null; else exit 14; fi
sleep 5;
# the lock should work now
flock --verbose --nonblock "$FLOCK" -c '/bin/sleep 5' > /dev/null
if [ $? -eq 0 ]; then echo OK > /dev/null; else exit 14; fi
exit 0;
) || exit 14;
fi

echo "===================================================================="
echo "... :-) passed !"
echo "===================================================================="
