#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test truncate command. Reproduce a bug when a volume is used in two different
# devices
#
# We run a job on a device, then we truncate the volume and we run
# a new job more or less at the same time.
#
#
TestName="truncate-race-bug-test"
JobName=NightlySave
. scripts/functions

cwd=`pwd`
scripts/cleanup
scripts/copy-2disk-virtual-confs

echo $src > $tmp/file-list

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "ActionOnPurge", "Truncate", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SpoolData", "No", "Job")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Volume Size", "500M", "Device")'
start_test

WHEN=`date --date="15 sec" "+%Y-%m-%d %H:%M:%S"`

cat >tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out $tmp/log1.out
label storage=Virtual volume=TestVolume001 drive=0 slot=0 pool=Default
@########################################################
@# Run a first job on TestVolume001
@########################################################
run level=full job=$JobName storage=Virtual yes
wait
messages
purge volume=TestVolume001
setbandwidth limit="1000 kb/s" client
setdebug level=100 tags=volume storage
@# start a job 15secs after the truncate command (that takes 30s)
run when="$WHEN" level=full job=$JobName storage=Virtual yes
list volumes
truncate volume=TestVolume001 storage=vDrive-2 allpools
list volumes
setdebug level=0 tags= storage
messages
status client
wait
setbandwidth limit="1000000 kb/s" client
messages
@$out $tmp/log2.out
restore select all done yes where=$tmp/bacula-restores
wait
messages
quit
END_OF_DATA

run_bacula

check_for_zombie_jobs storage=File

stop_bacula

check_two_logs

check_restore_diff

end_test
