#!/bin/bash -e

DISK="$1"

# This test assumes a kubernetes setup available with host as minikube

HOSTNAME="$2"

function install_cli_package() {
    # install kubectl kadalu
    KADALU_VERSION="0.0.1canary" make cli-build
    return $?
}

function test_install() {
    sed -i -e 's/imagePullPolicy: Always/imagePullPolicy: IfNotPresent/g' manifests/kadalu-operator.yaml

    echo "Installing Operator through CLI"
    cli/build/kubectl-kadalu install --local-yaml manifests/kadalu-operator.yaml || return 1
}

function test_storage_add() {
    sed -i -e "s/DISK/${DISK}/g" tests/get-minikube-pvc.yaml
    kubectl apply -f tests/get-minikube-pvc.yaml

    sleep 1
    cli/build/kubectl-kadalu storage-add storage-pool-3 --script-mode --type Replica3 --device ${HOSTNAME}:/mnt/${DISK}/file3.1 --path ${HOSTNAME}:/mnt/${DISK}/dir3.2 --pvc local-pvc || return 1

    # TODO: Enable this test after we resume testing Replica2 gluster cluster
    # Test Replica2 option
    # cli/build/kubectl-kadalu storage-add storage-pool-2 --script-mode --type Replica2 --device ${HOSTNAME}:/mnt/${DISK}/file2.1 --device ${HOSTNAME}:/mnt/${DISK}/file2.2 || return 1

    # # Test Replica2 with tie-breaker option
    # sudo truncate -s 2g /mnt/${DISK}/file2.{10,20}

    # cli/build/kubectl-kadalu storage-add storage-pool-2-1 --script-mode --type Replica2 --device ${HOSTNAME}:/mnt/${DISK}/file2.10 --device ${HOSTNAME}:/mnt/${DISK}/file2.20 --tiebreaker tie-breaker.kadalu.io:/mnt || return 1

    # Check if the type default is Replica1
    cli/build/kubectl-kadalu storage-add storage-pool-1 --script-mode --device ${HOSTNAME}:/mnt/${DISK}/file1.1 --device ${HOSTNAME}:/mnt/${DISK}/file1.2 --device ${HOSTNAME}:/mnt/${DISK}/file1.3 || return 1

    # Check for Disperse Volume
    sleep 1
    cli/build/kubectl-kadalu storage-add storage-pool-4 --script-mode --type Disperse --data 2 --redundancy 1 --device ${HOSTNAME}:/mnt/${DISK}/file4.1 --device ${HOSTNAME}:/mnt/${DISK}/file4.2 --device ${HOSTNAME}:/mnt/${DISK}/file4.3 || return 1

    # Check for external storage
    # TODO: For now, keep the name as 'ext-config' as PVC should use this to send request.
    # TODO: Enable this test after we resume testing external gluster cluster
    # cli/build/kubectl-kadalu storage-add ext-config --script-mode --external gluster1.kadalu.io:/kadalu || return 1
}

function main() {
    install_cli_package || (echo "install failed" && exit 1)
    test_install || (echo "CLI operator install failed" && exit 1)
    test_storage_add || (echo "Storage add commands failed" && exit 1)
}

main "$@"
