#!/bin/sh
# PCP QA Test No. 1191
# Exercise the prometheus PMDA
#
# 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
}

_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
$sudo rm -f $PCP_PMDAS_DIR/prometheus/metadata/*

# real QA test starts here
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

pmdaprometheus_remove

_restore_config $PCP_PMDAS_DIR/prometheus/metadata

status=0
exit
