#!/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

TOKEN=t`date +%s`

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

testName="Test 1:Release space that has a token description using GUID"

dpm-reservespace --gspace 10M --token_desc $TOKEN >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-reservespace returned $ret:FAILURE"
   REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
   error=1
else
   the_guid=$(cat $STD_OUT)
fi

if [ $error -ne 1 ]; then
   dpm-releasespace --space_token $the_guid >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-releasespace returned $ret:FAILURE"
      REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
      error=1
   else
      dpm-getspacemd --space_token $the_guid >$STD_OUT 2>$STD_ERR
      ret=$?
      if [ $ret -eq 0 ] && grep -q "^$the_guid" $STD_OUT; then
         echo "$testName:dpm-getspacemd returned 0 and space still exists:FAILURE"
         REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
         error=1
      else
         echo "$testName:dpm-releasespace returned 0 and release confirmed:SUCCESS"
      fi
   fi
fi

if [ $error -eq 1 ]; then
   error=1
fi

testName="Test 2:Release space that has no token description using GUID"

error=0
dpm-reservespace --gspace 10M >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-reservespace returned $ret:FAILURE"
   REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
   error=1
else
   the_guid=$(cat $STD_OUT)
fi

if [ $error -ne 1 ]; then
   dpm-releasespace --space_token $the_guid >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-releasespace returned $ret:FAILURE"
      REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
      error=1
   else
      dpm-getspacemd --space_token $the_guid >$STD_OUT 2>$STD_ERR
      ret=$?
      if [ $ret -eq 0 ] && grep -q "^$the_guid" $STD_OUT; then
         echo "$testName:dpm-getspacemd returned 0 and space still exists:FAILURE"
         REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
         error=1
      else
         echo "$testName:dpm-releasespace returned 0 and release confirmed:SUCCESS"
      fi
   fi
fi

testName="Test 3:Release space that has token description using it"

error=0
dpm-reservespace --gspace 10M --token_desc $TOKEN >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-reservespace returned $ret:FAILURE"
   REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
   error=1
else
   the_guid=$(cat $STD_OUT)
fi
if [ $error -ne 1 ]; then
   dpm-releasespace --token_desc $TOKEN >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-releasespace returned $ret:FAILURE"
      REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
      error=1
   else
      dpm-getspacemd --space_token $the_guid >$STD_OUT 2>$STD_ERR
      ret=$?
      if [ $ret -eq 0 ] && grep -q "^$the_guid" $STD_OUT; then
         echo "$testName:dpm-getspacemd returned 0 and space still exists:FAILURE"
         REPORT_OUTPUT dpm-reservespace $STD_OUT $STD_ERR
         error=1
      else
         echo "$testName:dpm-releasespace returned 0 and release confirmed:SUCCESS"
      fi
   fi
fi

rm -f $STD_OUT
rm -f $STD_ERR

if [ $error -ne 0 ]; then
  error=1
fi

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

TEST_PASSED

