#!/bin/sh
# PCP QA Test No. 1193
# Exercise the prometheus PMDA with metrics injection
#
# Copyright (c) 2017 Ronak Jain.  All Rights Reserved.
#

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

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check
. ./common.python

[ -f $PCP_PMDAS_DIR/prometheus/pmdaprometheus.python ] || _notrun "prometheus pmda not installed"

$python -c "import requests"
[ $? -eq 0 ] || _notrun "python requests module not installed"

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

pmdaprometheus_remove()
{
    echo
    echo "=== remove prometheus agent ==="
    $sudo ./Remove >$tmp.out 2>&1
    _filter_pmda_remove <$tmp.out
}

pmdaprometheus_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/prometheus
    $sudo ./Remove >/dev/null 2>&1
    _service pmcd stop 2>&1 | _filter_pcp_stop

    echo
    echo "=== prometheus agent installation ==="
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out >>$here/$seq.full
}

generate_config()
{
    cd $PCP_PMDAS_DIR/prometheus
    $sudo ./pmdaprometheus.python -g -n "test" -f $tmp.json
}


_prepare_pmda prometheus
# note: _restore_auto_restart pmcd done in _cleanup_pmda()
trap "_cleanup_pmda prometheus; exit \$status" 0 1 2 3 15

_save_config $PCP_PMDAS_DIR/prometheus/metadata
_save_config $PCP_VAR_DIR/config/pmda
$sudo rm -f $PCP_PMDAS_DIR/prometheus/metadata/*
$sudo rm -f $PCP_VAR_DIR/config/pmda/*

cat > $tmp.json <<EOF
# HELP sample_gauge Sample gauge metric.
# TYPE sample_gauge gauge
sample_gauge 12.1
# HELP sample_counter Sample counter metric.
# TYPE sample_counter counter
sample_counter 1.70205394e+08
# HELP sample_summary Sample summary metric has instances.
# TYPE sample_summary summary
sample_summary{quantile="0"} 0.000159623
sample_summary{quantile="0.25"} 0.000291993
sample_summary_sum 1.3912067700000001
sample_summary_count 1818
# HELP sample_histogram Sample histogram metric has intances.
# TYPE sample_histogram histogram
sample_histogram{le="1"} 5945
sample_histogram{le="2"} 10862
sample_histogram{le="4"} 15813
sample_histogram_sum 45157
sample_histogram_count 18135
EOF

# real QA test starts here

generate_config

pmdaprometheus_install

if pminfo -v prometheus > $tmp.info 2> $tmp.err
then
    :
else
    echo "... failed! ... here is the Install log ..."
    cat $tmp.out
fi
cat $tmp.info $tmp.err

if pminfo -fd prometheus.test.sample_gauge > $tmp.info 2> $tmp.err
then
    echo "Fetch and desc prometheus.test.sample_gauge: success"
    cat $tmp.info
else
    echo "Fetch and desc prometheus.test.sample_gauge: failed"
    cat $tmp.err
fi

if pminfo -fd prometheus.test.sample_counter > $tmp.info 2> $tmp.err
then
    echo "Fetch and desc prometheus.test.sample_counter: success"
    cat $tmp.info
else
    echo "Fetch and desc prometheus.test.sample_counter: failed"
    cat $tmp.err
fi

if pminfo -f prometheus.test.sample_summary > $tmp.info 2> $tmp.err
then
    echo "Fetch prometheus.test.sample_summary: success"
    cat $tmp.info
else
    echo "Fetch prometheus.test.sample_summary: failed"
    cat $tmp.err
fi

if pminfo -f prometheus.test.sample_histogram > $tmp.info 2> $tmp.err
then
    echo "Fetch prometheus.test.sample_histogram: success"
    cat $tmp.info
else
    echo "Fetch prometheus.test.sample_histogram: failed"
    cat $tmp.err
fi

pmdaprometheus_remove

_restore_config $PCP_PMDAS_DIR/prometheus/metadata
_restore_config $PCP_VAR_DIR/config/pmda

status=0
exit
