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

G1=g1`date +%s`
G2=g2`date +%s`

POOLNAME=pool`date +%s`

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

testName="Test 1:Create a pool without specifying any properties"

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
else
   dpm-listspaces --pool $POOLNAME >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-listspaces returned $ret:FAILURE"
      REPORT_OUTPUT dpm-listspaces $STD_OUT $STD_ERR
      error=1
   else
      echo "$testName:dpm-addpool returned 0 and creation confirmed:SUCCESS"
   fi
fi
dpm-rmpool --poolname $POOLNAME


testName="Test 2:Create a pool of type Volatile specifying pool parameters"

if ( dpns-entergrpmap --group $G1 >/dev/null 2>/dev/null ) && \
   ( dpns-entergrpmap --group $G2 >/dev/null 2>/dev/null ); then

   dpm-addpool --poolname $POOLNAME   \
               --def_filesize 128M    \
               --def_lifetime 3721    \
               --def_pintime 25321    \
               --max_lifetime 10921   \
               --max_pintime 10922    \
               --gc_start_thresh 20   \
               --gc_stop_thresh 25    \
               --s_type V             \
               --group "${G1},${G2}"  \
               >$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
   else
      dpm-qryconf >$STD_OUT 2>$STD_ERR
      if [ $ret -ne 0 ]; then
         echo "$testName:dpm-qryconf exited with error:FAILURE"
         error=1
      else
         cerror=0
         if [ $cerror -eq 0 ] && \
            ! (grep "^POOL $POOLNAME " $STD_OUT | grep -q " DEFSIZE 128.00M "); then
               echo "$testName:Default file size does not match:FAILURE"
               cerror=1
         fi
         if [ $cerror -eq 0 ] && \
            ! (grep "^POOL $POOLNAME " $STD_OUT | grep -q " GC_START_THRESH 20 "); then
               echo "$testName:GC_START_THRESH does not match:FAILURE"
               cerror=1
         fi
         if [ $cerror -eq 0 ] && \
            ! (grep "^POOL $POOLNAME " $STD_OUT | grep -q " GC_STOP_THRESH 25 "); then
               echo "$testName:GC_STOP_THRESH does not match:FAILURE"
               cerror=1
         fi
         if [ $cerror -eq 0 ] && \
            ! (grep "^POOL $POOLNAME " $STD_OUT | grep -q " S_TYPE V "); then
               echo "$testName:S_TYPE does not match:FAILURE"
               cerror=1
         fi
         if [ $cerror -eq 0 ]; then
            dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
            if [ $ret -ne 0 ]; then
               echo "$testName:dpm-listspaces exited with an error:FAILURE"
               error=1
            else
               if [ $cerror -eq 0 ] && \
                  ! (grep -q "Default Life Time: 0-01:02:01" $STD_OUT); then
                     echo "$testName:Default Life Time does not match:FAILURE"
                     cerror=1
               fi
               if [ $cerror -eq 0 ] && \
                  ! (grep -q "Default Pin Time: 0-07:02:01" $STD_OUT); then
                     echo "$testName:Default Pin Time does not match:FAILURE"
                     cerror=1
               fi
               if [ $cerror -eq 0 ] && \
                  ! (grep -q "Max Life Time: 0-03:02:01" $STD_OUT); then
                     echo "$testName:Max Life Time does not match:FAILURE"
                     cerror=1
               fi
               if [ $cerror -eq 0 ] && \
                  ! (grep -q "Max Pin Time: 0-03:02:02" $STD_OUT); then
                     echo "$testName:Max Pin Time does not match:FAILURE"
                     cerror=1
               fi
               if [ $cerror -eq 0 ] && \
                  ! (grep -q "Authorized FQANs: (${G1}), (${G2})$" $STD_OUT); then
                     echo "$testName:Authorized FQAN list does not match:FAILURE"
                     cerror=1
               fi
               if [ $cerror -eq 0 ]; then
                   echo "$testName:dpm-addpool OK and all properties match:SUCCESS"
               else
                   error=1
               fi
            fi
         else
            error=1
         fi
      fi
   fi

   dpm-rmpool --poolname $POOLNAME >$STD_OUT 2>$STD_ERR
   dpns-rmgrpmap --group $G1 >$STD_OUT 2>$STD_ERR
   dpns-rmgrpmap --group $G2 >$STD_OUT 2>$STD_ERR

else
   echo "$testName:Cannot register groups:FAILURE"
   error=1
fi

rm -f $STD_OUT
rm -f $STD_ERR

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

TEST_PASSED

