#!/bin/bash
# meta: proxy=false
# meta: preconfig=../../DPM-config
##############################################################################
# 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>
#
# MODE: This test can be executed both local and remote
#
##############################################################################

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

NO_PROXY_NEEDED

error=0

POOLNAME=pool`date +%s`
POOLNAME2=po0l`date +%s`
FSNAME1=fs1`date +%s`1
FSNAME2=fs2`date +%s`1

mkdir -p /tmp/$FSNAME1
mkdir -p /tmp/$FSNAME2

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

testName="Test 1:Create a pool and add some filesystems ($POOLNAME)"

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-addfs --pool $POOLNAME --server $DPM_HOST --fs /tmp/$FSNAME1 >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-addfs returned $ret:FAILURE"
      REPORT_OUTPUT dpm-addfs $STD_OUT $STD_ERR
      error=1
   else
      dpm-addfs --pool $POOLNAME --server $DPM_HOST --fs /tmp/$FSNAME2 >$STD_OUT 2>$STD_ERR
      ret=$?
      if [ $ret -ne 0 ]; then
         echo "$testName:dpm-addfs returned $ret:FAILURE"
         REPORT_OUTPUT dpm-addfs $STD_OUT $STD_ERR
         error=1
      else
          dpm-listspaces --pool $POOLNAME --long >$STD_OUT 2>$STD_ERR
          ret=$?
          if [ $ret -ne 0 ]; then
             echo "$testName:dpm-listspaces return an error - cannot confirm:FAILURE"
             error=1
          else
             if (grep "$DPM_HOST:/tmp/$FSNAME1" $STD_OUT | grep -q Online) &&
                (grep "$DPM_HOST:/tmp/$FSNAME2" $STD_OUT | grep -q Online); then
                   echo "$testName:dpm-addfs returned 0 and creation confirmed:SUCCESS"
             else
                   echo "$testName:dpm-addfs returned 0 but creation not confirmed:FAILURE"
             fi
          fi
      fi
   fi
fi
dpm-rmfs --server $DPM_HOST --fs /tmp/$FSNAME1
dpm-rmfs --server $DPM_HOST --fs /tmp/$FSNAME2
dpm-rmpool --poolname $POOLNAME

testName="Test 2:Try to readd a filesystems for another pool"

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-addfs --pool $POOLNAME --server $DPM_HOST --fs /tmp >$STD_OUT 2>$STD_ERR
   ret=$?
   if [ $ret -ne 0 ]; then
      echo "$testName:dpm-addfs returned $ret:FAILURE"
      REPORT_OUTPUT dpm-addfs $STD_OUT $STD_ERR
      error=1
   else
      dpm-addpool --poolname $POOLNAME2 >$STD_OUT 2>$STD_ERR
      ret=$?
      if [ $ret -ne 0 ]; then
         echo "$testName:dpm-addpool(2) returned $ret:FAILURE"
         REPORT_OUTPUT dpm-addpool $STD_OUT $STD_ERR
         error=1
      else
         dpm-addfs --pool $POOLNAME2 --server $DPM_HOST --fs /tmp >$STD_OUT 2>$STD_ERR
         ret=$?
         if [ $ret -ne 0 ] && ( grep -q "Filesystem already part of a pool" $STD_ERR); then
            echo "$testName:dpm-addfs returns $ret,Filesystem already part of a pool:SUCCESS"
         else
            echo "$testName:dpm-addfs returns 0, unexpected error message:FAILURE"
            REPORT_OUTPUT dpm-addfs $STD_OUT $STD_ERR
            error=1
         fi
      fi
   fi
fi
dpm-rmfs --server $DPM_HOST --fs /tmp
dpm-rmpool --poolname $POOLNAME2
dpm-rmpool --poolname $POOLNAME


rm -f $STD_OUT
rm -f $STD_ERR

rm -rf /tmp/$FSNAME1
rm -rf /tmp/$FSNAME2

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

TEST_PASSED

