#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# AUTHORS: Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
#
##############################################################################

SCRIPTDIR="$(dirname "$(readlink -f ${BASH_SOURCE})")"
source "${SCRIPTDIR}/../../Macros"

NO_PROXY_NEEDED

error=0

POOLNAME=pool`date +%s`

STD_OUT=/tmp/stdout-`date +%s%N`
STD_ERR=/tmp/stderr-`date +%s%N`

testName="Test 1:Remove an existing pool without filesystems"

dpm-addpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-addpool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
fi

if [ $error -ne 1 ]; then
   dpm-rmpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-rmpool returned $ret:FAILURE"
      REPORT_OUTPUT dpm-rmpool $STD_OUT $STD_ERR
      error=1
   else
      dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
      if [ $? -ne 0 ]; then
         echo "$testName:dpm-rmpool returned 0 and removal confirmed:SUCCESS"
      else
         echo "$testName:dpm-rmpool returned 0 but removal not confirmed:FAILURE"
         REPORT_OUTPUT dpm-listspaces $STD_OUT $STD_ERR
         error=1
      fi
   fi
fi

if [ $error -eq 1 ]; then
   error=1
fi
dpm-rmpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR

testName="Test 2:Remove a non-existing pool"

dpm-rmpool --poolname nonexistingpool >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -eq 0 ]; then
   echo "$testName:dpm-rmpool returned 0:FAILURE"
   REPORT_OUTPUT dpm-rmpool $STD_OUT $STD_ERR
   error=1
else
   if grep -q "No such pool" $STD_ERR; then
      echo "$testName:dpm-rmpool exited with $ret and printed an appropriate error:SUCCESS"
   else
      echo "$testName:dpm-rmpool exited with $ret but did not print the expected error:FAILURE"
			echo $STD_ERR
      error=1
   fi
fi

testName="Test 3:Remove an existing pool with filesystems defined"

error=0
dpm-addpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-addpool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
fi

if [ $error -ne 1 ]; then
   dpm-addfs --poolname $POOLNAME --server $DPM_HOST --fs /tmp >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-addfs returned $ret:FAILURE"
      REPORT_OUTPUT dpm-addfs $STD_OUT $STD_ERR
      error=1
   fi
fi
if [ $error -ne 1 ]; then
   dpm-rmpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -eq 0 ]; then
      echo "$testName:dpm-rmpool returned 0:FAILURE"
      error=1
   else
      if grep -q "File exists" $STD_ERR; then
         echo "$testName:dpm-rmpool returned $ret and error message OK:SUCCESS"
      else
         echo "$testName:dpm-rmpool returned $ret but the error message is unexpected:FAILURE"
         echo $STD_ERR
      fi
   fi
fi

if [ $error -eq 1 ]; then
   error=1
fi
dpm-rmfs --server $DPM_HOST --fs /tmp >$STD_OUT 2>$STD_ERR
dpm-rmpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR


rm -f $STD_OUT
rm -f $STD_ERR

if [ $error -ne 0 ]; then
  TEST_FAILED
fi

TEST_PASSED

