#! /usr/bin/env bash set -euo pipefail set -m # Enable job control shopt -u huponexit RED="\e[31m" GREEN="\e[32m" MAGENTA="\e[35m" ENDCOLOR="\e[0m" open_connections() { METRICS=$(curl -s localhost:9100/metrics | prom2json) VALUE=$(echo $METRICS | jq -r '.[] | select(.name == "consul_open_tcp_connections") | .metrics[] | select(.labels.port = "8500") | .value') echo $VALUE } TMP_DIR=$(mktemp -d) PIDS=() # Start the port-opener, and save the PID python port-opener.py --verbose & OPENER_PID=$! PIDS+=($OPENER_PID) # Wait for the port opener to start sleep 1 # Start the node exporter node_exporter --collector.textfile.directory=$TMP_DIR & NODE_EXPORTER_PID=$! PIDS+=($NODE_EXPORTER_PID) # Run the port exporter port_exporter > $TMP_DIR/consul.prom sleep 1 echo -e "${MAGENTA}Testing with 100 connections${ENDCOLOR}" VALUE=$(open_connections) if [[ $VALUE -ne 100 ]]; then echo -e "${RED}Expected 100 connections, got $VALUE ${ENDCOLOR}" exit 1 else echo -e "${GREEN}Test passed ${ENDCOLOR}" fi kill -s SIGINT $OPENER_PID sleep 2 port_exporter > $TMP_DIR/consul.prom echo -e "${MAGENTA} Testing with 0 connections ${ENDCOLOR}" VALUE=$(open_connections) if [[ $VALUE -ne 0 ]]; then echo -e "${RED} Expected 0 connections, got $VALUE ${ENDCOLOR}" exit 1 else echo -e "${GREEN} Test passed ${ENDCOLOR}" fi kill $NODE_EXPORTER_PID