#!/bin/sh
# PCP QA Test No. 766
# Test using the pmfind app to find PCP servers using the active probing
# discovery mechanism. Probe inet only.
#
# Copyright (c) 2014 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.discovery

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_sought_filter()
{
    # Only pmcd is guaranteed to be running, but other services may also be.
    # Transform two cases - no servers vs found servers - into deterministic
    # output for the --all invocations
    sed \
	-e 's/No \(pmproxy servers\) discovered/Sought \1/g' \
	-e 's/No \(pmwebapi servers\) discovered/Sought \1/g' \
	-e 's/Discovered \(pmproxy servers\):/Sought \1/g' \
	-e 's/Discovered \(pmwebapi servers\):/Sought \1/g' \
    # end
}

_unresolved_filter()
{
    sed -e '/  pcp:/d;
	    /  proxy:/d;
	    /  http:/d' \
    | _sought_filter
}

_resolved_filter()
{
    # Pass unresolved urls, filter the resolved ones.
    sed -e '/  pcp:\/\/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/{
p
n
}' \
	-e '/  proxy:\/\/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/{
p
n
}' \
	-e '/  http:\/\/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/{
p
n
}' \
	-e '/  pcp:/d' \
	-e '/  proxy:/d' \
	-e '/  http:/d' \
    | _sought_filter
}

_error_tolerance_filter()
{
    grep Discovered.pmcd.servers:
    # ignore everything else, incl. errors
}

# real QA test starts here
_control_service_discovery

# Obtain the inet address of the primary network interface ...
# cull any loopback aliases
addr=`host $(hostname) \
      | sed -n \
	  -e '/^127\.0\.1/d' \
	  -e '/has address/s/.* address //p'`
if [ -z "$addr" ]
then
    host $(hostname) >>$seq_full 2>&1
    _notrun "no primary network interface?"
    #NOTREACHED
fi
numaddr=`echo "$addr" | wc -w | sed -e 's/  *//g'`
if [ $numaddr -gt 1 ]
then
    echo "Warning: $(hostname) has multiple network interfaces!" >>$seq_full
    echo "Culling: extras from addr=$addr" >>$seq_full
    addr=`echo "$addr" | sed -e 's/ .*//' -e 1q`
fi
echo "addr=$addr" >>$seq_full

# Probe the obtained network.
# Test various combinations of service queries and subnet sizes. Keep the
# subnet size small -- say max 4 bits.
echo "-m probe=$addr/32" >> $seq_full
echo "-m probe=INET_ADDR/32"
pmfind -m probe=$addr/32 | _unresolved_filter
echo "Exit status: $?" | tee -a $seq_full

# for this one, we should not be surprised to find peer VMs
# in the QA Farm ... there is no DNS to support the reverse
# lookup, so they will be IP addr on the same Class C subnet
#
subnet=`echo $addr | sed -e 's/\.[^.]*$/./' -e 's/\./\\\\./g'`
echo "subnet=$subnet" >>$seq_full
echo "-s pmcd -m probe=$addr/31" -r >> $seq_full
echo "-s pmcd -m probe=INET_ADDR/31 -r"
pmfind -s pmcd -m probe=$addr/31 -r \
| tee -a $seq_full \
| _resolved_filter \
| grep -v "pcp://$subnet"
echo "Exit status: $?" | tee -a $seq_full

echo "-q -m probe=$addr/30" >> $seq_full
echo "-q -m probe=INET_ADDR/30"
pmfind -q -m probe=$addr/30 | _unresolved_filter
echo "Exit status: $?" | tee -a $seq_full

echo "-q -s pmcd -m probe=$addr/29 --resolve" >> $seq_full
echo "-q -s pmcd -m probe=INET_ADDR/29 --resolve"
pmfind -q -s pmcd -m probe=$addr/29 --resolve | _resolved_filter
echo "Exit status: $?" | tee -a $seq_full

echo "-q -s pmcd -m probe=$addr/28,maxThreads=8" >> $seq_full
echo "-q -s pmcd -m probe=INET_ADDR/28,maxThreads=8"
pmfind -q -s pmcd -m probe=$addr/28,maxThreads=8 | _unresolved_filter
echo "Exit status: $?" | tee -a $seq_full

# Try to cause errors; check pmprintf path doesn't blow stack
# pmfind just needs to survive to its result message
echo "ulimit -n 10" >> $seq_full
echo "ulimit -n 10"
bash -c "ulimit -n 10; pmfind -s pmcd -m probe=127.0.0.1/24,maxThreads=64" 2>&1 | tee -a $seq_full | _error_tolerance_filter
echo "Exit status: $?" | tee -a $seq_full

# success, all done
status=0

exit
