#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Simple test to check if storage list is correctly handled for the LeastUsed policy
#

TestName="store-mngr-leastused"
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

# Limit per-device max concurrent jobs to 1
$bperl -e 'add_virtual_changer("vDisk1", 5)'
$bperl -e 'add_virtual_changer("vDisk2", 5)'
$bperl -e 'add_virtual_changer("vDisk3", 5)'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "LabelFormat", "Vol", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "CommCompression", "no", "Director")'
$bperl -e 'add_attribute("$conf/bacula-fd.conf", "CommCompression", "no", "FileDaemon")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "CommCompression", "no", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AllowCompression", "no", "Storage")'
sed 's/sparse=yes;//' $conf/bacula-dir.conf > $tmp/1
mv $tmp/1 $conf/bacula-dir.conf

echo $tmp/f > $tmp/file-list
dd if=/dev/zero of=$tmp/f count=10000

# Add two simple jobs (one of them with store group)
cat <<END_OF_DATA >> $bin/bacula-dir.conf

Job {
  Name = "SingleStoreJob"
  Client = $HOST-fd
  Messages = Standard
  Type = Backup
  Storage = vDisk1
  Pool = Default
  FileSet="Full Set"
  ClientRunBeforeJob = "sleep 3"
}
Job {
  Name = "StoreGroupJob"
  Type = Backup
  Client = $HOST-fd
  Messages = Standard
  Storage = vDisk1, vDisk2, vDisk3
  Storage Group Policy = LeastUsed
  Pool = Default
  FileSet = "Full Set"
  ClientRunBeforeJob = "sleep 3"
}
Job {
  Name = "StoreGroupJob2"
  Type = Backup
  Client = $HOST-fd
  Messages = Standard
  Storage = vDisk1, vDisk2, vDisk3
  Storage Group Policy = FreeSpace
  Pool = Default
  FileSet = "Full Set"
  ClientRunBeforeJob = "sleep 3"
}
Job {
  Name = "ByPool"
  Type = Backup
  Client = $HOST-fd
  Messages = Standard
  Storage = vDisk1, vDisk2
  Storage Group Policy = FreeSpace
  Pool = LeastUsed
  FileSet = "Full Set"
  ClientRunBeforeJob = "sleep 3"
}
Job {
  Name = "ByPool2"
  Type = Backup
  Client = $HOST-fd
  Messages = Standard
  Storage = vDisk1
  Pool = LeastUsed
  FileSet = "Full Set"
  ClientRunBeforeJob = "sleep 3"
}
Pool {
  Name = LeastUsed
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
  LabelFormat = vol
  Storage = vDisk2, vDisk3
  Storage Group Policy = LeastUsed
}
END_OF_DATA

$bperl -e 'set_global_maximum_concurrent_jobs(10)'

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out  ${cwd}/tmp/log1.out
setdebug level=0 trace=1 dir
setbandwidth limit=500kb/s client
label volume=TestVolume001 storage=File1 pool=Default slot=1 drive=0
label volume=TestVolume002 storage=File2 pool=Default slot=1 drive=0
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
.status dir running
run job=StoreGroupJob level=Full yes
@sleep 1
list jobs
wait
@$out  ${cwd}/tmp/log2.out
messages
@$out  ${cwd}/tmp/log3.out
run job=StoreGroupJob level=Full yes
wait
messages
@$out  ${cwd}/tmp/log4.out
setdebug level=200 trace=1 dir
run job=StoreGroupJob2 pool=LeastUsed level=Full yes
run job=ByPool level=Full yes
run job=ByPool2 level=Full yes
wait
messages
setdebug level=0 trace=1 dir
quit
END_OF_DATA

run_bacula

stop_bacula

nb=`grep 'was selected out of group of 2 available storages. StorageGroupPolicy "LeastUsed" used' $tmp/log4.out | wc -l`
if [ $nb != 3 ]; then
    print_debug "ERROR: Incorrect selection by Pool in $tmp/log4.out"
    estat=1
fi


# Each vDisk should get 3 jobs
awk '/Storage:/ { tab[$2]++ } END { for (t in tab) { if (tab[t] != 3) { print t ": ERROR found="  tab[t]} } }' $tmp/log2.out > $tmp/1
grep ERROR $tmp/1

if [ $? -eq 0 ]; then
    print_debug "ERROR: Found incorrect drive sequence in $tmp/log2.out"
    estat=1
fi

end_test
