#!/bin/bash
set -e
set -o pipefail
set -u
#
# run multiple jobs and verify that the pruning mechanism
# logs the jobs being pruned
#
#
TestName="$(basename "$(pwd)")"
export TestName

regularjob_name=backup-bareos-fd
failingjob_name=failingjob
smallvoljob_name=smallvoljob

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

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

"${rscripts}"/cleanup
"${rscripts}"/setup

# Fill ${BackupDirectory} with data.
setup_data
generate_test_data data

start_test

start_bareos

label_backup_log="$tmp"/label_backup_log.out
restore_log="$tmp"/restore_log.out
prune_volume_log="$tmp"/prune_volume_log.out
prune_all_log="$tmp"/prune_all_log.out

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $label_backup_log
label volume=TestVolume001 storage=File pool=Full
label volume=TestVolume002 storage=File pool=Full
label volume=TestVolume003 storage=File pool=Full
label volume=TestVolume004 storage=File pool=Full
label volume=TestVolumeIncremental storage=File pool=Incremental
status director
status client
status storage=File
wait
run job=$regularjob_name Level=Full yes
run job=$regularjob_name Level=Full yes
messages

@$out $restore_log
@#
@# now do a restore
@#
wait
restore client=bareos-fd fileset=SelfTest where=$tmp/bareos-restores select all done
yes
wait
messages

@$out $prune_volume_log
@#
@# now prune using the commandline
@#
@sleep 6
update volume=TestVolume001 volstatus=Used
prune volume=TestVolume001 yes
wait
quit
END_OF_DATA

run_bconsole

## prune all volumes

cat <<END_OF_DATA >$tmp/bconcmds
@$out $prune_all_log
@#
@# now do the same but using the prune all command
@#
@#

run job=$regularjob_name Level=Full yes
run job=$regularjob_name Level=Full yes
wait
update volume=TestVolume002 volstatus=Used

run job=$regularjob_name Level=Full yes
run job=$regularjob_name Level=Full yes
wait
update volume=TestVolume003 volstatus=Used

@sleep 6

messages
prune volume all yes
wait

run job=$regularjob_name Level=Full yes
wait

quit
END_OF_DATA

run_bconsole

# Prune all volumes of specific pool

prune_all_pool_log="$tmp"/prune_all_pool_log.out

touch $tmp/data/weird-files/newfilecreatedjustforthistest

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $prune_all_pool_log
@#
@# Prune all volumes of a given pool
@#
@#

run job=$regularjob_name Level=Incremental yes
wait

@sleep 6

messages
update volume=TestVolume004 volstatus=Used
update volume=TestVolumeIncremental volstatus=Used

prune volume all pool=Incremental yes

wait
quit
END_OF_DATA

run_bconsole

# Pruning jobs

job_pruning_log="$tmp"/job_pruning_log.out

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $job_pruning_log
run job=$regularjob_name Level=Full yes
run job=$regularjob_name Level=Full yes
wait
@sleep 2
messages
prune jobs pool=Full yes
wait
quit
END_OF_DATA

run_bconsole

# Pruning files

file_pruning_log="$tmp"/file_pruning_log.out

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $file_pruning_log
prune files pool=Full yes
wait
restore jobid=11
no
messages
quit
END_OF_DATA

run_bconsole

# Purging failed jobs

purging_failed_jobs="$tmp"/purging_failed_jobs.out

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $purging_failed_jobs
run job=$failingjob_name level=Full yes
run job=$failingjob_name level=Full yes
run job=$failingjob_name level=Full yes
wait
messages
purge jobs client=bareos-fd jobstatus=E yes
wait
quit
END_OF_DATA

run_bconsole

# Purging failed jobs in specific pool

purging_pool_failed_jobs="$tmp"/purging_pool_failed_jobs.out

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $purging_pool_failed_jobs
run job=$smallvoljob_name level=Full yes
run job=$smallvoljob_name level=Full yes
run job=$smallvoljob_name level=Full yes
run job=$failingjob_name level=Full yes
run job=$failingjob_name level=Full yes
run job=$failingjob_name level=Full yes
wait
messages
purge jobs jobstatus=E pool=SmallFull yes
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File
check_two_logs "$label_backup_log" "$restore_log"

## Prune volumes
expect_grep "Purging the following 2 JobIds: 1,2" \
  "$prune_volume_log" \
  "Pruned jobs from volume pruning not found" \
  "Purging the"

expect_grep "Volume \"TestVolume001\" contains no jobs after pruning." \
  "$prune_volume_log" \
  "Volume still contains jobs, or wrong volume pruned." \
  "Volume.*contains no jobs after pruning."

## Prune all volumes
expect_grep "Purging the following 2 JobIds: 4,5" \
  "$prune_all_log" \
  "Pruned jobs from volume all pruning not found" \
  "Purging the"

expect_grep "Volume \"TestVolume002\" contains no jobs after pruning." \
  "$prune_all_log" \
  "Volume still contains jobs, or wrong volume pruned." \
  "Volume.*contains no jobs after pruning."

expect_grep "Purging the following 2 JobIds: 6,7" \
  "$prune_all_log" \
  "Pruned jobs from volume all pruning not found" \
  "Purging the"

expect_grep "Volume \"TestVolume003\" contains no jobs after pruning." \
  "$prune_all_log" \
  "Volume still contains jobs, or wrong volume pruned." \
  "Volume.*contains no jobs after pruning."

## Prune all volumes from specific pool

expect_grep "Purging the following 1 JobIds: 9" \
  "$prune_all_pool_log" \
  "Pruned jobs from volume all pool pruning not found" \
  "Purging the"

expect_grep "Volume \"TestVolumeIncremental\" contains no jobs after pruning." \
  "$prune_all_pool_log" \
  "Volume still contains jobs, or wrong volume pruned." \
  "Volume.*contains no jobs after pruning."

## Pruning jobs
expect_grep "Purging the following 2 JobIds: 8,10" \
  "$job_pruning_log" \
  "Expected pruned jobids not found" \
  "Purging the"

expect_grep "Pruned 2 Jobs for client bareos-fd from catalog." \
  "$job_pruning_log" \
  "Expected number of jobs pruned not found"

## Pruning files
expect_grep "Pruned Files from 1 Jobs" \
  "$file_pruning_log" \
  "Expected number of files pruned not found"

expect_grep "Most likely your retention policy pruned the files." \
  "$file_pruning_log" \
  "Restore command found files, when they should have been pruned"

## Purging failed jobs
expect_grep "Found 3 Jobs for client \"bareos-fd\" in catalog \"MyCatalog\"" \
  "$purging_failed_jobs" \
  "Correct number of failed jobs to prune was not found."

expect_grep "Purging the following 3 JobIds: 12,13,14" \
  "$purging_failed_jobs" \
  "Expected pruned failed jobids not found" \
  "Purging the"

## Purging failed jobs from specific pool

expect_grep "3 Jobs on Pool \"SmallFull\" purged from catalog" \
  "$purging_pool_failed_jobs" \
  "Correct number of jobs with warnings to purge on specific pool was not found."

expect_grep "Purging the following 3 JobIds: 18,19,20" \
  "$purging_pool_failed_jobs" \
  "Expected purged jobids with warnings in specific pool not found" \
  "Purging the"

end_test
