#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest tests the plugin functionality
# of the Bareos FD with the Python LDAP plugin.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd

#shellcheck source=../environment.in
. ./environment

#shellcheck source=../scripts/functions
. "${rscripts}"/functions

testdata_opts=( \
  --address  "$SYSTEMTEST_LDAP_ADDRESS" \
  --basedn   "$SYSTEMTEST_LDAP_BASEDN" \
  --binddn   "$SYSTEMTEST_LDAP_BINDDN" \
  --password "$SYSTEMTEST_LDAP_PASSWORD"
)

start_test

tar xzf ../../data/image.tgz
./testdata.py "${testdata_opts[@]}" \
  --clean \
  --populate \
  --dump-backup > backup-data-before.ldif

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
run job=$JobName yes
status director
status client
status storage=File
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
wait
restore client=bareos-fd fileset=PluginTest regexwhere=!ou=backup!ou=restore! select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole "$@"

check_for_zombie_jobs storage=File

check_two_logs

./testdata.py "${testdata_opts[@]}" \
  --dump-backup > backup-data-after.ldif
./testdata.py "${testdata_opts[@]}" \
  --dump-restore > restored-data.ldif

diff -u backup-data-before.ldif backup-data-after.ldif
diff -u backup-data-before.ldif restored-data.ldif

end_test
