#!/bin/sh
#
# Copyright (C) 2000-2021 Kern Sibbald
# Copyright (C) 2021-2026 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Cloud test. We run backups and restore with the "truncate cache" command in-between
#
TestName="cloud-query-test"
JobName=NightlySave
. scripts/functions

require_cloud

#config is required for cloud cleanup
scripts/copy-test-confs
scripts/cleanup

FORCE_FILE_SET=${FORCE_FILE_SET:-"${cwd}/build"}
echo "$FORCE_FILE_SET" >${cwd}/tmp/file-list

start_test

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumPartSize", "10000000", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumUploadBandwidth", "4MB/s", "Cloud")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumDownloadBandwidth", "4MB/s", "Cloud")'


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=Vol1
END_OF_DATA

# do label
run_bacula

# test with driver directive

# use the cloud configuration information to setup aws cli
REGION=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'Region')") 
ACCESSKEY=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'AccessKey')") 
SECRETKEY=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'SecretKey')") 
DRIVER=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'Driver')")
HOSTNAME=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'HostName')")
BUCKET=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'BucketName')")

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: driver=$DRIVER accesskey=$ACCESSKEY secretkey=$SECRETKEY hostname=$HOSTNAME region=$REGION" parameter=buckets
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error":0' ${tmp}/log1.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error while listing buckets with driver=$DRIVER"
    estat=1
fi
grep '"error_msg":""' ${tmp}/log1.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error_msg while listing buckets with driver=$DRIVER"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log2.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: driver=$DRIVER accesskey=$ACCESSKEY secretkey=$SECRETKEY hostname=$HOSTNAME region=$REGION bucket=$BUCKET path=Vol1" parameter=contents
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error":0' ${tmp}/log2.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error while listing contents with driver=$DRIVER"
    estat=1
fi
grep '"error_msg":""' ${tmp}/log2.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error_msg while listing contents with driver=$DRIVER"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log3.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: driver=$DRIVER accesskey=$ACCESSKEY secretkey=$SECRETKEY hostname=$HOSTNAME region=$REGION bucket=cloud-query-test-bucket" parameter=createbucket
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error_msg":""' ${tmp}/log3.out 2>&1 >/dev/null
if test $? -ne 0; then
    # error 409 (already exists) is OK
    grep "409" ${tmp}/log3.out 2>&1 >/dev/null
    if test $? -ne 0; then
        echo "Error: got an error_msg while createbucket with driver=$DRIVER"
        estat=1
    fi
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log4.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: driver=$DRIVER accesskey=$ACCESSKEY secretkey=$SECRETKEY hostname=$HOSTNAME" parameter=regions
END_OF_DATA

run_bconsole

if [ "${CLOUD_NAME}" != "AzureCloud" ]; then
   # must report error=0 and error_msg= (empty)
   grep '"error":0' ${tmp}/log4.out 2>&1 >/dev/null
   if test $? -ne 0; then
      echo "Error: got an error while listing regions with driver=$DRIVER"
      estat=1
   fi
   grep '"error_msg":""' ${tmp}/log4.out 2>&1 >/dev/null
   if test $? -ne 0; then
      echo "Error: got an error_msg while listing regions with driver=$DRIVER"
      estat=1
   fi
else
   echo "AzureCloud detected: the region listing is not verified. FIXME."
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log5.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: driver=$DRIVER accesskey=$ACCESSKEY secretkey=$SECRETKEY hostname=$HOSTNAME" parameter=storageclasses
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error":0' ${tmp}/log5.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error while listing storageclasses with driver=$DRIVER"
    estat=1
fi
grep '"error_msg":""' ${tmp}/log5.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error_msg while listing storageclasses with driver=$DRIVER"
    estat=1
fi

# TEST AWS with name directive (using the cloud resource from config)

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log6.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: name=$CLOUD_NAME" parameter=buckets
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error":0' ${tmp}/log6.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error while listing buckets with name=$CLOUD_NAME"
    estat=1
fi
grep '"error_msg":""' ${tmp}/log6.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error_msg while listing buckets with name=$CLOUD_NAME"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log7.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: name=$CLOUD_NAME path=Vol1" parameter=contents
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error":0' ${tmp}/log7.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error while listing contents with name=$CLOUD_NAME"
    estat=1
fi
grep '"error_msg":""' ${tmp}/log7.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error_msg while listing contents with name=$CLOUD_NAME"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log8.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: name=$CLOUD_NAME bucket=cloud-query-test-bucket" parameter=createbucket
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error_msg":""' ${tmp}/log8.out 2>&1 >/dev/null
if test $? -ne 0; then
    # error 409 (already exists) is OK
    grep "409" ${tmp}/log8.out 2>&1 >/dev/null
    if test $? -ne 0; then
        echo "Error: got an error_msg while createbucket with driver=$DRIVER"
        estat=1
    fi
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log9.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: name=$CLOUD_NAME" parameter=regions
END_OF_DATA

run_bconsole


if [ "${CLOUD_NAME}" != "AzureCloud" ]; then
   # must report error=0 and error_msg= (empty)
   grep '"error":0' ${tmp}/log9.out 2>&1 >/dev/null
   if test $? -ne 0; then
      echo "Error: got an error while listing regions with driver=$DRIVER"
      estat=1
   fi
   grep '"error_msg":""' ${tmp}/log9.out 2>&1 >/dev/null
   if test $? -ne 0; then
      echo "Error: got an error_msg while listing regions with driver=$DRIVER"
      estat=1
   fi
else
   echo "AzureCloud detected: the region listing is not verified. FIXME."
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log10.out
setdebug tags=cloud level=50 trace=1 storage
.api 2 api_opts=j
.query storage=File1 plugin="cloud: name=$CLOUD_NAME" parameter=storageclasses
END_OF_DATA

run_bconsole

# must report error=0 and error_msg= (empty)
grep '"error":0' ${tmp}/log10.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error while listing storageclasses with name=$CLOUD_NAME"
    estat=1
fi
grep '"error_msg":""' ${tmp}/log10.out 2>&1 >/dev/null
if test $? -ne 0; then
    echo "Error: got an error_msg while listing storageclasses with name=$CLOUD_NAME"
    estat=1
fi
end_test
