#! /bin/sh
# PCP QA Test No. 1111
# checks basic pcp2influxdb functionality
#
# Copyright (c) 2015 Red Hat, Inc.
#
seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
which socat >/dev/null 2>&1 || _notrun "socat binary not installed"
which pcp2influxdb >$seq.full 2>&1 || _notrun "pcp2influxdb not installed"

$python -c "import requests" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python requests module not installed"
major=`$python -c 'import requests; print(requests.__version__.split(".")[0])'`
[ $major -ge 1 ] || _notrun "python requests module too old (v$major)"

port=8086
echo | $PCP_BINADM_DIR/telnet-probe localhost $port \
	&& _notrun "Someone already listening on typical InfluxDB $port"

$sudo rm -rf $seq.full $tmp.*
signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!

trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

_full_stash()
{
    echo "--- pcp2influxdb stdout --" >>$seq.full
    cat $tmp.p2i.out >>$seq.full
    echo "--- pcp2influxdb stderr --" >>$seq.full
    cat $tmp.p2i.err >>$seq.full
    echo "--- socket cat stdout --" >>$seq.full
    cat $tmp.socat.out >>$seq.full
    echo "--- socket cat stderr --" >>$seq.full
    cat $tmp.socat.err >>$seq.full
}

scale=10
ncpu=`pmprobe -v hinv.ncpu | awk '{print $3*'$scale'".0"}'`
pcp2influxdb="$python `which pcp2influxdb`"

echo | tee -a $seq.full
echo "=== 1. pcp2influxdb one-shot noauth ===" | tee -a $seq.full
socat tcp-listen:$port,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb --database roar -t 1 hinv.ncpu >$tmp.p2i.out 2>$tmp.p2i.err &   # will error out after socket cat dies
p2ipid=$!
sleep 3
$signal $pid $p2ipid 2>/dev/null
wait
egrep -q "^POST \/write\?db=roar" $tmp.socat.out
[ $? -eq 0 ] && echo "Found POST URL in noauth output"
egrep -q "hinv_ncpu value=[0-9]+(\.[0-9]+)? [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found proper metric body in noauth output"
_full_stash

echo "=== 2. pcp2influxdb noauth with tags ===" | tee -a $seq.full
socat tcp-listen:$port,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb --database roar --tag-string host=me.example.com -t 1 hinv.ncpu \
        >$tmp.p2i.out 2>$tmp.p2i.err &   # will error out after socket cat dies
p2ipid=$!
sleep 3
$signal $pid $p2ipid 2>/dev/null
wait
egrep -q "^POST \/write\?db=roar" $tmp.socat.out
[ $? -eq 0 ] && echo "Found POST URL in noauth output"
egrep -q "hinv_ncpu,host=me\.example\.com value=[0-9]+(\.[0-9]+)? [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found proper metric body in noauth output"
_full_stash

echo "=== 3. pcp2influxdb auth with tags ===" | tee -a $seq.full
socat tcp-listen:$port,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb --database roar --db-user pcp --db-password hey \
        --tag-string host=me.example.com -t 1 hinv.ncpu \
        >$tmp.p2i.out 2>$tmp.p2i.err &   # will error out after socket cat dies
p2ipid=$!
sleep 3
$signal $pid $p2ipid 2>/dev/null
wait
egrep -q "^POST \/write\?db=roar" $tmp.socat.out
[ $? -eq 0 ] && echo "Found POST URL in output"
egrep -q "Authorization: Basic cGNwOmhleQ==" $tmp.socat.out
[ $? -eq 0 ] && echo "Found auth data in HTTP request"
egrep -q "hinv_ncpu,host=me\.example\.com value=[0-9]+(\.[0-9]+)? [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found proper metric body in output"
_full_stash

echo "=== 4. pcp2influxdb bad-instance tolerance ===" | tee -a $seq.full
rm -f $tmp.socat.out $tmp.socat.err
socat tcp-listen:$port,reuseaddr - >>$tmp.socat.out 2>>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb -t 2 sample.bogus_bin sample.aggregate.null sample.string.hullo NoSuchMetric sample.bad >$tmp.p2i.out 2>$tmp.p2i.err &
p2ipid=$!
sleep 8 # enough for at least 2 messages
$signal $pid $p2ipid 2>/dev/null
wait
grep 'Excluding' $tmp.p2i.err
_full_stash

status=0
exit
