#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# This script will test prune algo 
#
. scripts/functions

TestName="prune-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

setup_shstore # simulate shared storage if enabled

start_test

echo $PWD/build/po > tmp/file-list

# We want to have pruning only when doing prune command
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AutoPrune", "No", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Job Retention", "1s", "Client")'

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName level=full yes
wait
messages
@exec "touch $cwd/build/po/fr.po"
run job=$JobName level=incremental yes
wait
messages
@exec "touch $cwd/build/po/de.po"
run job=$JobName level=incremental yes
wait
messages
@sleep 2
@$out $tmp/log3.out
@################################################################
@# Should not prune anything
list jobs
prune jobs yes
list jobs
@$out $tmp/log4.out
@################################################################
@# Should prune the two firsts incrementals
run job=$JobName level=differential yes
wait
messages
@exec "touch $cwd/build/po/es.po"
run job=$JobName level=incremental yes
wait
messages
@sleep 2
list jobs
prune jobs yes
list jobs
@$out $tmp/log5.out
@################################################################
@# Should prune the first Full+Diff job
run job=$JobName level=full yes
wait
messages
@sleep 2
list jobs
prune jobs yes
list jobs
@$out $tmp/log6.out
@################################################################
run job=$JobName level=incremental yes
wait
messages
@$out $tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select all storage=File done
yes
wait
messages
@sleep 2
@$out $tmp/log6.out
@################################################################
@# Should prune the restore job and the last incremental job (jobfiles=0)
list jobs
prune jobs yes
list jobs
quit
EOF

run_bacula

check_for_zombie_jobs storage=File
stop_bacula

check_two_logs

estat=0

###############################################################################
# Now we will verify that the pruning code is working as expected.  Each time,
# we run 'list jobs', 'prune', 'list jobs'. check_prune_list ensures that jobids
# in argument are present in the first 'list jobs', the 'prune' command deletes
# them (same number), and checks that the last 'list jobs' doesn't contain them
# anymore. See scripts/functions.pm for details.

# nothing should be pruned
$bperl -e "check_prune_list('$tmp/log3.out')"
estat=$(($estat + $?))

# jobids 2 and 3 should be pruned
# (F I I D) -> (F D)
$bperl -e "check_prune_list('$tmp/log4.out',2,3)"
estat=$(($estat + $?))

# (F D I F) -> (F)
$bperl -e "check_prune_list('$tmp/log5.out',1,4,5)"
estat=$(($estat + $?))

# (F I R) -> (F)     -- Incremental have 0 file
$bperl -e "check_prune_list('$tmp/log6.out',7,8)"
estat=$(($estat + $?))

end_test
