#!/bin/bash
# shellcheck disable=SC1091 # Runtime-resolved path; intentionally not followed.
. "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/../local-snap.sh" || return 1
set -eux

# Install LXD
install_lxd

if ! hasNeededAPIExtension storage_buckets; then
  echo "Skipping test as not supported"
  # shellcheck disable=SC2034
  FAIL=0
  exit 0
fi

poolDriverList="${1:-dir btrfs lvm lvm-thin zfs ceph}"

# Without storage_buckets_local, only ceph/cephobject is supported.
if ! hasNeededAPIExtension storage_buckets_local; then
  filteredList=""
  for driver in $poolDriverList; do
    [ "${driver}" = "ceph" ] && filteredList="${filteredList} ${driver}"
  done
  poolDriverList="${filteredList# }"
  if [ -z "${poolDriverList}" ]; then
    echo "Skipping test as no supported pool drivers found"
    # shellcheck disable=SC2034
    FAIL=0
    exit 0
  fi
fi

# Check if any local (non-ceph) pool drivers will be tested, which require MinIO.
needsMinio=false
for driver in $poolDriverList; do
  if [ "${driver}" != "ceph" ]; then
    needsMinio=true
    break
  fi
done

if [ "${needsMinio}" = "true" ]; then
  if [ "$(uname -m)" = "riscv64" ]; then
    echo "Skipping test as not supported on riscv64"
    # shellcheck disable=SC2034
    FAIL=0
    exit 0
  fi

  # Clean up the build dir in case it hung around from a failed test.
  rm -rf /opt/minio

  # Download the minio server and client binaries.
  download_minio /opt/minio

  # Set the snap config key for minio and restart LXD to have it take effect.
  snap set lxd minio.path=/opt/minio
  systemctl restart snap.lxd.daemon.service
  lxd waitready --timeout=300
fi

# Configure LXD
lxc project switch default
lxc project create test -c features.images=false
lxc project switch test

poolName="bucketpool$$"

for poolDriver in $poolDriverList; do
  echo "==> Create storage pool using driver ${poolDriver}"
  if [ "${poolDriver}" = "dir" ]; then
    lxc storage create "${poolName}" "${poolDriver}" volume.size=5GiB
  elif [ "${poolDriver}" = "ceph" ]; then
    lxc storage create "${poolName}" cephobject cephobject.radosgw.endpoint="${LXD_CEPH_CEPHOBJECT_RADOSGW:-http://127.0.0.1}"
  elif [ "${poolDriver}" = "lvm" ]; then
    lxc storage create "${poolName}" "${poolDriver}" size=40GiB lvm.use_thinpool=false volume.size=5GiB
  elif [ "${poolDriver}" = "lvm-thin" ]; then
    lxc storage create "${poolName}" lvm size=20GiB volume.size=5GiB
  else
    lxc storage create "${poolName}" "${poolDriver}" size=20GiB volume.size=5GiB
  fi

  if [ "${poolDriver}" != "ceph" ]; then
    lxc config set core.storage_buckets_address "127.0.0.1:9000"
  fi

  lxc storage show "${poolName}"

  echo "==> Creating buckets"
  lxc storage bucket create "${poolName}" bucket1

  echo "==> Deleting buckets and storage pool"
  lxc storage bucket delete "${poolName}" bucket1
  lxc storage rm "${poolName}"
done

lxc project switch default
lxc project delete test

if [ "${needsMinio}" = "true" ]; then
  # Remove MinIO now that the test is over.
  rm -rf /opt/minio
fi

# shellcheck disable=SC2034
FAIL=0
