author | Alberto Bertogli
<albertito@blitiri.com.ar> 2024-04-19 17:25:20 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2024-04-19 17:25:20 UTC |
parent | d998ee1501d84b33db00073f43518a8a917b65ab |
test/.gitignore | +2 | -0 |
test/diff.sh | +16 | -0 |
test/httpd.go | +18 | -0 |
test/t-00-basic.expected | +9 | -0 |
test/t-00-basic.toml | +4 | -0 |
test/t-00-basic.vars | +9 | -0 |
test/t-01-config.expected | +9 | -0 |
test/t-01-config.toml | +7 | -0 |
test/t-01-config.vars | +9 | -0 |
test/test.sh | +63 | -0 |
diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..589ac45 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +httpd +*.got* diff --git a/test/diff.sh b/test/diff.sh new file mode 100755 index 0000000..e99228e --- /dev/null +++ b/test/diff.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Diff the metrics page, ignoring stuff that Prometheus libraries adds and +# we don't care about. + +cat "$2" \ + | grep -v -E '(^| )go_gc_' \ + | grep -v -E '(^| )go_goroutines' \ + | grep -v -E '(^| )go_info' \ + | grep -v -E '(^| )go_memstats' \ + | grep -v -E '(^| )go_threads' \ + | grep -v -E '(^| )process_' \ + | grep -v -E '(^| )promhttp_' \ + > "$2.filtered" + +exec diff -u "$1" "$2.filtered" > "$2.diff" diff --git a/test/httpd.go b/test/httpd.go new file mode 100644 index 0000000..4f7d97d --- /dev/null +++ b/test/httpd.go @@ -0,0 +1,18 @@ +package main + +import ( + "flag" + "net/http" +) + +var ( + addr = flag.String("addr", ":30081", "Address to listen on") + path = flag.String("path", ".", "Path to serve") +) + +func main() { + flag.Parse() + + http.Handle("/", http.FileServer(http.Dir(*path))) + http.ListenAndServe(*addr, nil) +} diff --git a/test/t-00-basic.expected b/test/t-00-basic.expected new file mode 100644 index 0000000..774e4ad --- /dev/null +++ b/test/t-00-basic.expected @@ -0,0 +1,9 @@ +# HELP a_float expvar "a-float" +# TYPE a_float untyped +a_float 1.2 +# HELP an_int expvar "an-int" +# TYPE an_int untyped +an_int 26 +# HELP with_unsupported_characters expvar "with/unsupported-characters" +# TYPE with_unsupported_characters untyped +with_unsupported_characters 55 diff --git a/test/t-00-basic.toml b/test/t-00-basic.toml new file mode 100644 index 0000000..a217368 --- /dev/null +++ b/test/t-00-basic.toml @@ -0,0 +1,4 @@ +listen_addr = ":30080" + +[target] +url = "http://localhost:30081/t-00-basic.vars" diff --git a/test/t-00-basic.vars b/test/t-00-basic.vars new file mode 100644 index 0000000..fdab564 --- /dev/null +++ b/test/t-00-basic.vars @@ -0,0 +1,9 @@ +{ +"an-int": 26, +"a-float": 1.2, +"a-map": {"good": 73.5, "bad": 38.4}, +"a-string": "I am a string", +"with/unsupported-characters": 55, +"cmdline": ["/usr/bin/test", "--flag"], +"memstats": {"Alloc":6702144,"TotalAlloc":36862810952,"Sys":161443112} +} diff --git a/test/t-01-config.expected b/test/t-01-config.expected new file mode 100644 index 0000000..0481397 --- /dev/null +++ b/test/t-01-config.expected @@ -0,0 +1,9 @@ +# HELP a_float expvar "a-float" +# TYPE a_float untyped +a_float 1.2 +# HELP an_int expvar "an-int" +# TYPE an_int untyped +an_int 26 +# HELP with_unsup This is very helpful +# TYPE with_unsup untyped +with_unsup 55 diff --git a/test/t-01-config.toml b/test/t-01-config.toml new file mode 100644 index 0000000..cf9b7b1 --- /dev/null +++ b/test/t-01-config.toml @@ -0,0 +1,7 @@ +listen_addr = ":30080" + +[target] +url = "http://localhost:30081/t-01-config.vars" + +m.with_unsup.expvar = "with/unsupported-characters" +m.with_unsup.help = "This is very helpful" diff --git a/test/t-01-config.vars b/test/t-01-config.vars new file mode 100644 index 0000000..fdab564 --- /dev/null +++ b/test/t-01-config.vars @@ -0,0 +1,9 @@ +{ +"an-int": 26, +"a-float": 1.2, +"a-map": {"good": 73.5, "bad": 38.4}, +"a-string": "I am a string", +"with/unsupported-characters": 55, +"cmdline": ["/usr/bin/test", "--flag"], +"memstats": {"Alloc":6702144,"TotalAlloc":36862810952,"Sys":161443112} +} diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..9aa3894 --- /dev/null +++ b/test/test.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +set -e +cd "$(dirname "${0}")" + +# Set traps to kill our subprocesses when we exit (for any reason). +trap ":" TERM # Avoid the EXIT handler from killing bash. +trap "exit 2" INT # Ctrl-C, make sure we fail in that case. +trap "kill 0" EXIT # Kill children on exit. + + +if [ "$V" == "1" ]; then + set -v +fi + + +echo "# Build" + +go build httpd.go + +( cd ..; go build $GOFLAGS ) + + +echo "# Test" + +function wait_until_ready() { + PORT=$1 + + while ! bash -c "true < /dev/tcp/localhost/$PORT" 2>/dev/null ; do + sleep 0.01 + done +} + +function test_one() { + echo "## $1" + + # Launch httpd. + ./httpd & + HTTPD_PID=$? + wait_until_ready 30081 + + # Launch prometheus-expvar-exporter + ../prometheus-expvar-exporter "-config=$1.toml" > .pee.log 2>&1 & + PEE_PID=$? + wait_until_ready 30080 + + # Get the exported metrics. + curl -s localhost:30080/metrics > "$1.got" + kill $PEE_PID $HTTPD_PID + + # Compare the results. + if ! ./diff.sh "$1.expected" "$1.got"; then + cat "$1.got.diff" + echo + echo + echo failed + exit 1 + fi +} + +for i in t-*.toml; do + test_one "$(basename "$i" .toml)" +done