#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest creates a simple backup with
# the local-fileset plugin and checks that it
# works correctly.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd

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

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

if [ -f "${working}/${TestName}"-fd.trace ]; then
  rm "${working}/${TestName}"-fd.trace
fi

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out ${runner_tmp}/log1.out
@# toggle trace to make sure that bareos creates a new trace file
setdebug level=800 client trace=0
setdebug level=800 client trace=1
run job=$JobName level=Full yes
status director
status client
status storage=File
wait
messages
END_OF_DATA

run_bconsole "$@"
check_for_zombie_jobs storage=File

cat <<END_OF_DATA >$tmp/bconcmds
@#
@# now do a restore
@#
@$out ${runner_tmp}/log2.out
restore client=bareos-fd fileset=PluginTest where=${runner_tmp}/bareos-restores select all done yes
status client=bareos-fd
wait
messages
END_OF_DATA

run_bconsole "$@"
check_for_zombie_jobs storage=File

check_two_logs "${runner_tmp}/log1.out" "${runner_tmp}/log2.out"
check_restore_diff "${BackupDirectory}" "${runner_tmp}/bareos-restores"

expect_grep 'name="XATTR name 3", value="XATTR value 3"' \
  "${runner_tmp}/log2.out" \
  "could not find recovered XATTR in restore log"

expect_grep 'content="Hello ACL"' \
  "${runner_tmp}/log2.out" \
  "could not find recovered ACL in restore log"

expect_grep "Readin.*from file" \
  "${working}/${TestName}"-fd.trace \
  "io was not handled in python according to trace file."

end_test
