#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest tests the plugin functionality
# of the Bareos SD by using the module
#   bareos-sd-test.py
#
# This test will backup some files. While the
# backup some events of the storage daemon
# will be logged from the python module.
# This testrunner will check if all events
# have been logged.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd
#shellcheck source=../../environment.in
. ./environment

JobName=backup-bareos-fd
#shellcheck source=../../scripts/functions
. "${rscripts}"/functions
"${rscripts}"/cleanup
"${rscripts}"/setup

# Use a tgz to setup data to be backuped.
# Fill ${BackupDirectory} with data.
setup_data

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
run job=$JobName yes
status director
status client
status storage=File
wait
messages
quit
END_OF_DATA

run_bareos "$@"
check_for_zombie_jobs storage=File

events=(bsdEventJobStart
  bsdEventDeviceReserve
  bsdEventVolumeUnload
  bsdEventVolumeLoad
  bsdEventDeviceOpen
  bsdEventDeviceMount
  bsdEventLabelRead
  bsdEventLabelVerified
  bsdEventLabelWrite
  bsdEventSetupRecordTranslation
  bsdEventWriteRecordTranslation
  bsdEventDeviceUnmount
  bsdEventDeviceClose
  bsdEventJobEnd)

for i in "${events[@]}"; do
  if ! grep -q "$i" ${tmp}/test-plugin.log; then
    set_error "Failed to find logged event $i"
  fi
done

end_test
