#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest tests the plugin functionality
# of the Bareos FD by using the supplied module
#   bareos-fd-local-fileset.py
#
# The module will backup some files.
# This plugin is not intended for production,
# but is only a minimal example that shows
# how to use the python plugin interface.
# File attributes like uses and times will not be saved.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd

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

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

# Fill ${BackupDirectory} with data.
setup_data

# this test does not work with links, because
# python 2 does not support setting mtime on links
# they are restored properly but with wrong timestamps
# This is supposed to work with the future plugin-fd interface
# using Python3
find ${tmp}/data/weird-files -type l -exec rm {} \;
find tmp/data/weird-files -links +1 -type f -exec rm {} \;
rm tmp/data/weird-files/*utf*
print_debug "$(locale)"

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=800 client trace=1
label volume=TestVolume001 storage=File pool=Full
run job=$JobName fileset=PluginTest_do_io_in_core yes
status director
status client
status storage=File
wait
messages
END_OF_DATA

run_bareos "$@"
if is_debug; then
    cat ${working_dir}/*.trace
fi
check_for_zombie_jobs storage=File
stop_bareos

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

run_bareos "$@"
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff ${BackupDirectory}

expect_grep "bread handled in core"\
  working/py?plug-fd-local-fileset-basic-fd.trace \
  "io was not handled in core according to trace file."



end_test
bin/bareos stop
