#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest tests the vmware plugin functionality
# of the Bareos FD by using the supplied module
#   BareosFdPluginProxmox.py
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-proxmox-vm
#shellcheck source=../../environment.in
. ./environment

export PATH="${current_test_directory}/sbin:${current_test_directory}/bin:$PATH"

JobName=backup-proxmox-vm
#shellcheck source=../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions

BACKGUESTID=999010
RESTGUESTID=999011
GUESTNAME=plugtestvm

qm stop "${BACKGUESTID}" || :
qm destroy "${BACKGUESTID}" || :
qm create "${BACKGUESTID}" --ostype l26 --memory 512 --name "${GUESTNAME}" --scsi0 ZFS:2
qm start "${BACKGUESTID}"

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=100 client=bareos-fd trace=1
label volume=TestVolume001 storage=File pool=Full
run job=$JobName level=Full yes
status director
status client
status storage=File
wait
messages
END_OF_DATA

run_bconsole "$@"

qm stop "${BACKGUESTID}"
qm destroy "${BACKGUESTID}"

# make sure BACKGUESTID was destroyed
qm list | grep -v "${BACKGUESTID}"

cat <<END_OF_DATA2 >$tmp/bconcmds
@#
@# now do a restore to guest
@#
@$out $tmp/log2.out
wait
setdebug client=bareos-fd level=100 trace=1
restore client=bareos-fd fileset=ProxmoxTestVM where=/ select all done  pluginoptions=python:guestid=${RESTGUESTID}
yes
wait
messages
END_OF_DATA2


run_bconsole "$@"

qm start "${RESTGUESTID}"

# make sure RESTGUESTID was recovered
qm list | grep "${RESTGUESTID}"

# cleanup
qm stop "${RESTGUESTID}"
qm destroy "${RESTGUESTID}"


cat <<END_OF_DATA3 >$tmp/bconcmds
@#
@# now do a restore to file
@#
@$out $tmp/log3.out
wait
setdebug client=bareos-fd level=100 trace=1
restore client=bareos-fd fileset=ProxmoxTestVM where=/ select all done  pluginoptions=python:guestid=${RESTGUESTID}:restoretodisk=yes
yes
wait
messages
END_OF_DATA3


run_bconsole "$@"

check_for_zombie_jobs storage=File

RESTOREFILE=$(grep -o "/var/lib/vz/dump.*vma" "${tmp}/log3.out")

head "$RESTOREFILE"  > "${tmp}/diskhead"

expect_grep "name: ${GUESTNAME}" \
 "${tmp}/diskhead" \
 "did not find expected vm name in vma dump file"

check_log "${tmp}/log1.out"
check_log "${tmp}/log2.out"
check_log "${tmp}/log3.out"

end_test
