#! /bin/sh
# PCP QA Test No. 667
# checks basic pcp2graphite 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 pcp2graphite >$seq.full 2>&1 || _notrun "pcp2graphite not installed"

port_pickled=2004
port_text=2003
echo | $PCP_BINADM_DIR/telnet-probe localhost $port_pickled \
	&& _notrun "Someone already listening on graphite pickled port $port_pickled"
echo | $PCP_BINADM_DIR/telnet-probe localhost $port_text \
	&& _notrun "Someone already listening on graphite text port $port_text"

$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 "--- pcpgraphite stdout --" >>$seq.full
    cat $tmp.p2g.out >>$seq.full
    echo "--- pcpgraphite stderr --" >>$seq.full
    cat $tmp.p2g.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"}'`
pcp2graphite="$python `which pcp2graphite`"

echo | tee -a $seq.full
echo "=== 1. pcp2graphite one-shot pickle  ===" | tee -a $seq.full
socat tcp-listen:$port_pickled,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2graphite -t 1 hinv.ncpu hinv.ncpu >$tmp.p2g.out 2>$tmp.p2g.err &   # will error out after socket cat dies
p2gpid=$!
sleep 3
$signal $pid $p2gpid 2>/dev/null
wait
# python pickled format is binary, and python version dependent to boot.
# so, just look for the presence of hinv.ncpu in the output...
fgrep -q pcp.hinv.ncpu $tmp.socat.out
[ $? -eq 0 ] && echo "Found pcp.hinv.ncpu in pickled output"
_full_stash

echo "=== 2. pcp2graphite text, 2-second aligned  ===" | tee -a $seq.full
socat tcp-listen:$port_text,reuseaddr,fork - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2graphite -t 2 -P $port_text -u "/$scale" -m foobar. hinv.ncpu sample.datasize >$tmp.p2g.out 2>$tmp.p2g.err &
p2gpid=$!
sleep 5 # enough for at least 2 messages
$signal $pid $p2gpid 2>/dev/null
wait
head -2 $tmp.socat.out |
    sed -e "s,\.ncpu $ncpu ,.ncpu NCPUS ," |    # confirm ncpus value
    sed -r -e 's, [0-9]+[02468]$, TIMESTAMP2,'  # confirm time alignment
grep 'Excluding' $tmp.p2g.err
_full_stash

echo "=== 3. pcp2graphite bad-instance tolerance ===" | tee -a $seq.full
socat tcp-listen:$port_text,reuseaddr,fork - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2graphite -t 2 -P $port_text sample.bogus_bin sample.aggregate.null sample.string.hullo NoSuchMetric sample.bad >$tmp.p2g.out 2>$tmp.p2g.err &
p2gpid=$!
sleep 5 # enough for at least 2 messages
$signal $pid $p2gpid 2>/dev/null
wait
# confirm time alignment
head -10 $tmp.socat.out \
    | sed -r -e 's, [0-9]+[02468]$, TIMESTAMP2,'  \
    | LC_COLLATE=POSIX sort
grep 'Excluding' $tmp.p2g.err
_full_stash

status=0
exit
