#!/bin/sh
# PCP QA Test No. 1705
# Exercise the bpftrace PMDA - error conditions
#

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

. ./common.bpftrace

case `admin/whatami`
in
    *openSUSE\ Leap\ 15.*)
	_notrun "this test does not work on openSUSE 15.x"
	;;
esac

_pmdabpftrace_check
echo "tracepoint:raw_syscalls:sys_enter" >$tmp.tmp
_pmdabpftrace_check_probes $tmp.tmp

case `hostname -s`
in
    bozo-vm)
	    rm -f $tmp.*
	    _notrun "cannot set bpftrace hz=999 on this VM"
	    ;;
esac

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

_prepare_pmda bpftrace
trap "_pmdabpftrace_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

_filter_exit_code()
{
    # some versions of bpftrace exit with different codes
    sed -e 's/ value -6$/ value 1/g'
}

# on vm04, the limit is 512 which is not enough; 600 seems enough
#
export BPFTRACE_MAX_PROBES=600

# real QA test starts here
cat <<EOF | _pmdabpftrace_install
# Installed by PCP QA test $seq on `date`
[dynamic_scripts]
enabled = true
auth_enabled = false

[bpftrace]
# ridiculously small for test5 running on (potentially) slow VMs
max_throughput = 4096
EOF

echo "=== bpftrace script without variables ==="
./src/store_and_fetch bpftrace.control.register "// name: test1
tracepoint:raw_syscalls:sys_enter { }" | grep -o '"error": "no bpftrace variables or printf statements found, please include at least one variable or printf statement in your script"'
echo

echo "=== invalid probe ==="
pmstore bpftrace.control.register "// name: test2
does_not_exist { @c = count(); }"
_pmdabpftrace_wait_for_value bpftrace.scripts.test2.status '"error"'
# error message changed between bpftrace versions
pminfo -f bpftrace.scripts.test2.error >>$seq.full
pminfo -f bpftrace.scripts.test2.error \
    | grep -E -q "(Invalid provider: 'does_not_exist')|(Unrecognized probe type: does_not_exist)|(Invalid probe type made it to attachpoint parser)|(Invalid probe type: does_not_exist)" \
    && echo "error message (invalid probe) found"
pminfo -f bpftrace.scripts.test2.exit_code | _filter_exit_code
echo

echo "=== syntax error ==="
pmstore bpftrace.control.register "// name: test3
tracepoint:raw_syscalls:sys_enter { @c = count(; }"
# 10sec (default) is not long enough on vm03
#
_pmdabpftrace_wait_for_value bpftrace.scripts.test3.status '"error"' 15
pminfo -f bpftrace.scripts.test3.error
pminfo -f bpftrace.scripts.test3.exit_code
echo
echo "=== too many attached tracepoints ==="
pmstore bpftrace.control.register "// name: test4
kprobe:* { @c = count(); }"
_pmdabpftrace_wait_for_value bpftrace.scripts.test4.status '"error"'
pminfo -f bpftrace.scripts.test4.error \
| sed \
    -e 's/ [1-9][0-9]* / PROBES /g' \
    -e 's/"ERROR: /"/' \
    -e '/value "Error attaching probe: '"'"'kprobe:\*'"'"'"/{
s/value ".*/value "Can'"'"'t attach to PROBES probes because it exceeds the current limit of PROBES probes.\
You can increase the limit through the BPFTRACE_MAX_PROBES environment variable, but BE CAREFUL since a high number of probes attached can cause your system to crash."/
}' \
    # end
pminfo -f bpftrace.scripts.test4.exit_code \
| sed -e '/value/s/ 255$/ 1/'
echo

echo "=== too much output ==="
pmstore bpftrace.control.register '// name: test5
profile:hz:999 { printf("test"); }'
_pmdabpftrace_wait_for_value bpftrace.scripts.test5.status '"error"' 20
pminfo -f bpftrace.scripts.test5.error | sed -e 's/ [1-9][0-9]* / LIMIT /g'
echo

echo "=== duplicate script name ==="
pmstore bpftrace.control.register "// name: test6
tracepoint:raw_syscalls:sys_enter { @x = count() }"
./src/store_and_fetch bpftrace.control.register "// name: test6
tracepoint:raw_syscalls:sys_enter { @x = count() }" | grep -o '"error": "Script name .* is already in use by another script."'
echo


_pmdabpftrace_remove
status=0
exit
