#!/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:Create a pool for testing modifications"

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:creation confirmed:SUCCESS"
   fi
fi

testName="Test 2:Test setting default lifetime"

dpm-modifypool --poolname $POOLNAME --def_lifetime 93067 >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "Default Life Time: 1-01:51:07" $STD_OUT); then
         echo "$testName:Default Life Time does not match:FAILURE"
         error=1
      else
         echo "$testName:Default Life Time matches:SUCCESS"
      fi
   fi
fi

testName="Test 3:Test setting default pintime"

dpm-modifypool --poolname $POOLNAME --def_pintime 93127 >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "Default Pin Time: 1-01:52:07" $STD_OUT); then
         echo "$testName:Default Pin Time does not match:FAILURE"
         error=1
      else
         echo "$testName:Default Pin Time matches:SUCCESS"
      fi
   fi
fi

testName="Test 4:Test setting maximum pintime"

dpm-modifypool --poolname $POOLNAME --max_pintime 93187 >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "Max Pin Time: 1-01:53:07" $STD_OUT); then
         echo "$testName:Maximum Pin Time does not match:FAILURE"
         error=1
      else
         echo "$testName:Maximum Pin Time matches:SUCCESS"
      fi
   fi
fi

testName="Test 5:Test setting maximum lifetime"

dpm-modifypool --poolname $POOLNAME --max_lifetime 93187 >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "Max Life Time: 1-01:53:07" $STD_OUT); then
         echo "$testName:Maximum Life Time does not match:FAILURE"
         error=1
      else
         echo "$testName:Maximum Life Time matches:SUCCESS"
      fi
   fi
fi

testName="Test 6:Test setting GC start and stop thresholds"

dpm-modifypool --poolname $POOLNAME --gc_start_thresh 23 --gc_stop_thresh 25 >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      gcline=$(grep "Garbage Collector:" $STD_OUT)
      if (grep -q "Start < 23.0%" $STD_OUT) && (grep -q "Stop > 25.0%" $STD_OUT); then
         echo "$testName:Thresholds match:SUCCESS"
      else
         echo "$testName:Thresholds do not match:FAILURE"
         error=1
      fi
   fi
fi

testName="Test 7:Test default filesize"

dpm-modifypool --poolname $POOLNAME --def_filesize 302M >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
   error=1
else
   dpm-qryconf | grep $POOLNAME >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "DEFSIZE 302.00M" $STD_OUT); then
         echo "$testName:Default filesize does not match:FAILURE"
         error=1
      else
         echo "$testName:Default filesize matches:SUCCESS"
      fi
   fi
fi

testName="Test 8:Test setting space type"

dpm-modifypool --poolname $POOLNAME --s_type V >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-modifypool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "Space Type: Volatile" $STD_OUT); then
         echo "$testName:Default filesize does not match:FAILURE"
         error=1
      else
         echo "$testName:Default filesize matches:SUCCESS"
      fi
   fi
fi

testName="Test 9:Test setting retention policy"

dpm-modifypool --poolname $POOLNAME --ret_policy C >$STD_OUT 2>$STD_ERR
ret=$?
if [ $ret -ne 0 ]; then
   echo "$testName:dpm-modifypool returned $ret:FAILURE"
   REPORT_OUTPUT dpm-modifypool $STD_OUT $STD_ERR
   error=1
else
   dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
   if [ $? -ne 0 ]; then
      echo "$testName:dpm-listspaces exited with an error:FAILURE"
      error=1
   else
      if ! (grep -q "Retention Policy: Custodial" $STD_OUT); then
         echo "$testName:Retention policy does not match:FAILURE"
         error=1
      else
         echo "$testName:Retention policy matches:SUCCESS"
      fi
   fi
fi

testName="Test 10:Test setting group list by name"

if ( dpns-entergrpmap --group test_set_vogroup1 ) && \
   ( dpns-entergrpmap --group test_set_vogroup1/subgroup1 ) && \
   ( dpns-entergrpmap --group test_set_vogroup1/subgroup1/Role=role1 ); then

   id1=$[$(dpns-listgrpmap --group test_set_vogroup1 | cut -c1-8) + 0]
   id2=$[$(dpns-listgrpmap --group test_set_vogroup1/subgroup1 | cut -c1-8) + 0]
   id3=$[$(dpns-listgrpmap --group test_set_vogroup1/subgroup1/Role=role1 | cut -c1-8) + 0]

   dpm-modifypool --poolname $POOLNAME --group \
   "test_set_vogroup1,test_set_vogroup1/subgroup1/Role=role1" >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-modifypool returned $ret:FAILURE"
      REPORT_OUTPUT dpm-modifypool $STD_OUT $STD_ERR
      error=1
   else
      dpm-qryconf | grep $POOLNAME >$STD_OUT 2>$STD_ERR
      if [ $? -ne 0 ]; then
         echo "$testName:dpm-qryconf exited with an error:FAILURE"
         error=1
      else
         gidlist=$(sed -e 's/.*\(GIDS\)\s\([0-9,]*\)\s.*/\2/' $STD_OUT)
         if [ "x$gidlist" == "x$id1,$id3" ] || [ "x$gidlist" == "x$id3,$id1" ]; then
             dpm-modifypool --poolname $POOLNAME --group \
             "-test_set_vogroup1,+test_set_vogroup1/subgroup1" >$STD_OUT 2>$STD_ERR
             if [ $ret -ne 0 ]; then
                echo "$testName:dpm-modifypool returned $ret(+/-usage):FAILURE"
                REPORT_OUTPUT dpm-modifypool $STD_OUT $STD_ERR
                error=1
             else
                dpm-qryconf | grep $POOLNAME >$STD_OUT 2>$STD_ERR
                if [ $? -ne 0 ]; then
                   echo "$testName:dpm-qryconf exited with an error:FAILURE"
                   error=1
                else
                   gidlist=$(sed -e 's/.*\(GIDS\)\s\([0-9,]*\)\s.*/\2/' $STD_OUT)
                   if [ "x$gidlist" == "x$id2,$id3" ] || [ "x$gidlist" == "x$id3,$id2" ]; then
                      echo "$testName:dpm-modifypool OK, GID list verified:SUCCESS"
                   else
                      echo "$testName:GID list does not match(+-):FAILURE"
                      error=1
                   fi
                fi
             fi
         else
            echo "$testName:GID list does not match(non+-):FAILURE"
            error=1
         fi
      fi
   fi
else
   echo "$testName:dpm-entergrpmap call(s) failed:FAILURE"
   error=1
fi

dpns-rmgrpmap --group test_set_vogroup1
dpns-rmgrpmap --group test_set_vogroup1/subgroup1
dpns-rmgrpmap --group test_set_vogroup1/subgroup1/Role=role1

testName="Test 11:Test setting group list by GID"

if ( dpns-entergrpmap --group t_group_gid_1 ) && \
   ( dpns-entergrpmap --group t_group_gid_1/sub_gid_1 ) && \
   ( dpns-entergrpmap --group t_group_gid_1/sub_gid_1/Role=gid_role_1 ); then

   id1=$[$(dpns-listgrpmap --group t_group_gid_1 | cut -c1-8) + 0]
   id2=$[$(dpns-listgrpmap --group t_group_gid_1/sub_gid_1 | cut -c1-8) + 0]
   id3=$[$(dpns-listgrpmap --group t_group_gid_1/sub_gid_1/Role=gid_role_1 | cut -c1-8) + 0]

   dpm-modifypool --poolname $POOLNAME --gid "$id1,$id2" >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-modifypool returned $ret:FAILURE"
      REPORT_OUTPUT dpm-modifypool $STD_OUT $STD_ERR
      error=1
   else
      dpm-qryconf | grep $POOLNAME >$STD_OUT 2>$STD_ERR
      if [ $? -ne 0 ]; then
         echo "$testName:dpm-qryconf exited with an error:FAILURE"
         error=1
      else
         gidlist=$(sed -e 's/.*\(GIDS\)\s\([0-9,]*\)\s.*/\2/' $STD_OUT)
         if [ "x$gidlist" == "x$id1,$id2" ] || [ "x$gidlist" == "x$id2,$id1" ]; then
             dpm-modifypool --poolname $POOLNAME --gid "-${id1},+${id3}" >$STD_OUT 2>$STD_ERR
             if [ $ret -ne 0 ]; then
                echo "$testName:dpm-modifypool returned $ret(+/-usage):FAILURE"
                REPORT_OUTPUT dpm-modifypool $STD_OUT $STD_ERR
                error=1
             else
                dpm-qryconf | grep $POOLNAME >$STD_OUT 2>$STD_ERR
                if [ $? -ne 0 ]; then
                   echo "$testName:dpm-qryconf exited with an error:FAILURE"
                   error=1
                else
                   gidlist=$(sed -e 's/.*\(GIDS\)\s\([0-9,]*\)\s.*/\2/' $STD_OUT)
                   if [ "x$gidlist" == "x$id2,$id3" ] || [ "x$gidlist" == "x$id3,$id2" ]; then
                      echo "$testName:dpm-modifypool OK, GID list verified:SUCCESS"
                   else
                      echo "$testName:GID list does not match(+-):FAILURE"
                      error=1
                   fi
                fi
             fi
         else
            echo "$testName:GID list does not match(non+-):FAILURE"
            error=1
         fi
      fi
   fi
else
   echo "$testName:dpm-entergrpmap call(s) failed:FAILURE"
   error=1
fi

dpns-rmgrpmap --group t_group_gid_1
dpns-rmgrpmap --group t_group_gid_1/sub_gid_1
dpns-rmgrpmap --group t_group_gid_1/sub_gid_1/Role=gid_role_1

dpm-rmpool --poolname $POOLNAME

rm -f $STD_OUT
rm -f $STD_ERR

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

TEST_PASSED

