#!/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: Gianni Pucciani, CERN
#          Alejandro Álvarez Ayllón, CERN
#          Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
# 
# BDII test
#
##############################################################################

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

NO_PROXY_NEEDED

myerrorlevel=0
echo
echo "Target node is $DPM_HOST. Date: `date`"
echo "Testing the Information System:"

# Check resource BDII port
portstate=`nmap -P0 -sT -p 2170 --max_rtt_timeout 1000ms ${DPM_HOST} | grep "2170/tcp" | awk '{print $2}'`
echo "  Resource BDII port is open:"
if [ ! "x${portstate}" = "xopen" ]; then
 myerrorlevel=1
 echo FAILURE
else
 echo SUCCESS; 
fi

# Check resource BDII bind string

content=`ldapsearch -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid"`
myret=$?

echo "  Resource BDII bind string:"
if [ ! "$myret" = "0" ]; then
 myerrorlevel=1
 echo FAILURE
else
 echo SUCCESS 
fi


# Check resource BDII content

for prot in rfio gsiftp; do
lines=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" GlueSEAccessProtocolLocalID=$prot | wc -l `

echo "  AccessProtocol $prot published: "
if [ $lines -lt 20 ]; then
 echo FAILURE
 myerrorlevel=1
else
 echo SUCCESS
fi
done

for prot in srmv2 ; do
lines=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" GlueSEControlProtocolLocalID=${prot} | wc -l` 

echo "  ControlProtocol $prot published: "
if [ $lines -lt 20 ]; then
 echo FAILURE
 myerrorlevel=1
else
 echo SUCCESS
fi
done

for service in "httpg://$DPM_HOST:8446/srm/managerv2"; do
lines=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" GlueServiceUniqueId=$service | wc -l`

echo "  Service $service published: "
if [ $lines -lt 20 ]; then
 echo FAILURE
 myerrorlevel=1
else
 echo SUCCESS
fi
done

# Checking for backward compatible AccessControlRule publishing

lines=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" GlueServiceAccessControlRule | grep "GlueServiceAccessControlRule: " | grep -v ": VO:" | wc -l`
lines2=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" GlueServiceAccessControlRule | grep "GlueServiceAccessControlRule: " | wc -l`
lines3=$((lines))


echo "  AcessControlRule published backward compatible way:"
if [ "$lines3" -ne "$lines2" ]; then
 echo FAILURE
 myerrorlevel=1
else
 echo SUCCESS
fi

# Check for default and unset values

lines=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" | grep 999999`
lines2=`ldapsearch  -x -h $DPM_HOST -p 2170 -b "mds-vo-name=resource,o=grid" | grep unset`


echo "  Unconfigured or default attributes present: "
if [ ! "x$lines" = "x" ]; then
 echo FAILURE
 myerrorlevel=1
else
 echo SUCCESS
fi
echo

# If TEST_LOG_LOCATION is available, the wrapper is being used, so dump the whole DB
if [ -n "$TEST_LOG_LOCATION" ]; then
  echo "TEST_LOG_LOCATION available. Dumping the published information into $TEST_LOG_LOCATION/bdii.dump"
  ldapsearch -x -h $DPM_HOST -p 2170 -b "o=grid" > $TEST_LOG_LOCATION/bdii.dump
fi

# End
echo "  Overall Information System test result: "
if [ $myerrorlevel -ne 0 ]; then
 TEST_FAILED
else
 TEST_PASSED
fi

