#!/bin/sh
# PCP QA Test No. 1243
# Exercise shell mechanism in pmDiscoverServices(3).
#
# Copyright (c) 2018 Red Hat.
#

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

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

_check_valgrind
_check_helgrind
_get_libpcp_config
$service_discovery || _notrun "No support for service discovery"

if sed -e 's/$/ /' /etc/hosts | grep '::1[ 	].*localhost ' >/dev/null
then
    :
else
    # need something like this in /etc/hosts
    # ::1 localhost.localdomain localhost localhost6.localdomain6 localhost6
    #
    echo "Warning: no IPv6 localhost (not localhost6) entry in /etc/hosts, so only"
    echo "IPv4 localhost will be discovered below"
fi

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
hostname=`hostname`
hostip=`_host_to_ipaddr $hostname`
localip=`_host_to_ipaddr localhost`
$sudo rm -rf $tmp $tmp.* $seq.full 
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    tee -a $here/$seq.full \
    | sed \
	-e "s,$tmp.discover,DISCOVER_DIR,g" \
	-e "s,\[::1\],IP_ADDRESS,g" \
	-e "s,$localip,IP_ADDRESS,g" \
	-e "s,$hostip,IP_ADDRESS,g" \
    #end
}

# expect at least 2 pmcd servers on <realip_addr> and 127.0.0.1
# but there may be others ...
# === std out ===
# Discovered pmcd servers:
#   pcp://IP_ADDRESS:44321
#   pcp://IP_ADDRESS:44321
#   pcp://IP_ADDRESS:44321
# === std err ===
#
_fix_pmcds_found()
{
    $PCP_AWK_PROG '
/^Discovered pmcd servers:/	{ print; n = 0; inblock = 1; next }
inblock == 1 && /pcp:/		{ n++; next }
inblock == 1 && $1 == "==="	{ if (n >= 2)
				    print "  >= 2 pcp://IP_ADDRESS:44321 lines"
				  else
				    print "  " n " pcp://IP_ADDRESS:44321 lines"
				  inblock = 0
				}
				{ print }'
}

# real QA test starts here
mkdir -p $tmp.discover

# test script with local addresses
cat >$tmp.discover/localhost.sh <<EOF
#!/bin/sh
echo localhost
echo 127.0.0.1
EOF
chmod 755 $tmp.discover/localhost.sh

# test script with a mix of hostnames
cat >$tmp.discover/hostnames.sh <<EOF
#!/bin/sh
echo $hostname
echo no.such.host
EOF
chmod 755 $tmp.discover/hostnames.sh

# drop-in kubectl so we can see args
cat >$tmp.kubectl.sh <<EOF
#!/bin/sh
echo "\$@" > $tmp.kubectl.args
EOF
chmod 755 $tmp.kubectl.sh

export PCP_KUBECTL_PROG=$tmp.kubectl.sh

echo "== discovery directory" >> $seq.full
find $tmp.discover >> $seq.full
echo "== localhost.sh" >> $seq.full
$tmp.discover/localhost.sh >> $seq.full
echo "== hostnames.sh" >> $seq.full
$tmp.discover/hostnames.sh >> $seq.full

echo "== checking pcp-kube-pods argument handling" | tee -a $seq.full
$PCP_BINADM_DIR/discover/pcp-kube-pods
cat $tmp.kubectl.args | tee -a $seq.full

# handy pmfind debugging option: -Ddiscovery
echo "== checking pmfind results and memory access" | tee -a $seq.full
_run_valgrind pmfind -m shell,path=$tmp.discover,maxThreads=1 -s pmcd \
	| _filter \
	| _fix_pmcds_found

echo "== checking pmfind results and thread locks" | tee -a $seq.full
_run_helgrind pmfind -m shell,path=$tmp.discover,maxThreads=4 -s pmcd \
	| _filter

# success, all done
status=0
exit
