Skip to content

Commit

Permalink
Feature/3 check prom needs the opts (#4)
Browse files Browse the repository at this point in the history
* fix: check prom needs the opts

Signed-off-by: Yoan Blanc <[email protected]>

* Added tests to verify #3

* Ensure test-failure in CI, update changelog

Co-authored-by: Yoan Blanc <[email protected]>
  • Loading branch information
Skeen and yoan-adfinis authored Aug 3, 2020
1 parent b35eace commit 83347b2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.0.5
=====
Fixed trailing whitespace (#2)
Fixed a bug with not passing curl context on all calls (#3)

0.0.4
=====
Added CHANGELOG
Expand Down
2 changes: 1 addition & 1 deletion src/check_prometheus_metric.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function check_prometheus_server {
fi

# Check if the prometheus endpoint exists, and can be queried
curl -s ${PROMETHEUS_SERVER}/api/v1/query?query=cafebabe | jq -e '.status == "success"' 1>/dev/null 2>/dev/null
curl -s "${CURL_OPTS[@]}" ${PROMETHEUS_SERVER}/api/v1/query?query=cafebabe | jq -e '.status == "success"' 1>/dev/null 2>/dev/null
PROMETHEUS_OK=$?
if [ "${PROMETHEUS_OK}" -ne 0 ]; then
NAGIOS_STATUS=UNKNOWN
Expand Down
13 changes: 11 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/bin/bash

NETWORK_NAME=nagios_plugins_testnetwork
PROMETHEUS_NAME=nagios_plugins_prometheus
PROMETHEUS_PORT=9090
PUSHGATEWAY_NAME=nagios_plugins_pushgateway
PUSHGATEWAY_PORT=9091
ICINGA_NAME=icinga2
ICINGA_PORT=5665
NGINX_NAME=nagios_plugins_nginx
NGINX_PORT=8022

PLUGIN_SCRIPT=build/output.sh

export PLUGIN_SCRIPT=${PLUGIN_SCRIPT}
export PROMETHEUS_PORT=${PROMETHEUS_PORT}
export PUSHGATEWAY_PORT=${PUSHGATEWAY_PORT}
export ICINGA_PORT=5665
export ICINGA_PORT=${ICINGA_PORT}
export NGINX_PORT=${NGINX_PORT}

source tests/test_utils.bash

Expand All @@ -23,6 +25,7 @@ start_docker_network
start_prometheus
start_pushgateway
start_icinga
start_nginx_basic_auth

set_metric "pi" "3.14"

Expand All @@ -33,6 +36,12 @@ echo ""

echo "Ordinary tests"
bats tests/*.bats
ORDINARY_TESTS_STATUS=$?
echo "Ordinary tests status: ${ORDINARY_TESTS_STATUS}"

echo "Integration tests"
bats tests/icinga/*.bats
INTEGRATION_TESTS_STATUS=$?
echo "Integration tests status: ${INTEGRATION_TESTS_STATUS}"

! (( ${ORDINARY_TESTS_STATUS} || ${INTEGRATION_TESTS_STATUS} ))
49 changes: 49 additions & 0 deletions tests/basic_auth.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bats

PROMETHEUS_SERVER=http://localhost:${NGINX_PORT}/

UNABLE_TO_QUERY="UNKNOWN - unable to query prometheus endpoint!"
BASIC_AUTH_PARAMS="-C --user -C admin:admin"


load test_utils

@test "--- ${BATS_TEST_FILENAME} ---" {
true
}
# Failing due to basic auth
@test "Test without basic auth -q 1 -w 2 -c 3" {
OUTPUT="$(test_parameters '-q 1 -w 2 -c 3')"
echo $OUTPUT
[ "${OUTPUT}" == "${UNABLE_TO_QUERY}" ]
}
@test "Test without basic auth -q 2 -w 2 -c 3" {
OUTPUT="$(test_parameters '-q 2 -w 2 -c 3')"
[ "${OUTPUT}" == "${UNABLE_TO_QUERY}" ]
}
@test "Test without basic auth -q 3 -w 2 -c 3" {
OUTPUT="$(test_parameters '-q 3 -w 2 -c 3')"
[ "${OUTPUT}" == "${UNABLE_TO_QUERY}" ]
}
@test "Test without basic auth -q 4 -w 2 -c 3" {
OUTPUT="$(test_parameters '-q 4 -w 2 -c 3')"
[ "${OUTPUT}" == "${UNABLE_TO_QUERY}" ]
}

# Passing with basic auth
@test "Test with basic auth -q 1 -w 2 -c 3" {
OUTPUT="$(test_parameters "${BASIC_AUTH_PARAMS} -q 1 -w 2 -c 3")"
[ "${OUTPUT}" == "OK - tc is 1" ]
}
@test "Test with basic auth -q 2 -w 2 -c 3" {
OUTPUT="$(test_parameters "${BASIC_AUTH_PARAMS} -q 2 -w 2 -c 3")"
[ "${OUTPUT}" == "WARNING - tc is 2" ]
}
@test "Test with basic auth -q 3 -w 2 -c 3" {
OUTPUT="$(test_parameters "${BASIC_AUTH_PARAMS} -q 3 -w 2 -c 3")"
[ "${OUTPUT}" == "CRITICAL - tc is 3" ]
}
@test "Test with basic auth -q 4 -w 2 -c 3" {
OUTPUT="$(test_parameters "${BASIC_AUTH_PARAMS} -q 4 -w 2 -c 3")"
[ "${OUTPUT}" == "CRITICAL - tc is 4" ]
}
1 change: 1 addition & 0 deletions tests/configuration/htpasswd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
admin:$apr1$enb5u7Pi$yMZD6bvOaAUIRL0ki850L/
16 changes: 16 additions & 0 deletions tests/configuration/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
http {
server {
listen 80;

location / {
auth_basic "Prometheus";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://${PROMETHEUS_NAME}:9090/;
}

location /health {
return 200;
}
}
}
events {}
48 changes: 47 additions & 1 deletion tests/test_utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,52 @@ function start_docker_network() {
docker network create ${NETWORK_NAME}
}

function start_nginx_basic_auth() {
echo ""
echo "Creating configuration file"
CONFIG_FILE=$(mktemp --suffix=.conf)
chmod 666 ${CONFIG_FILE}
echo "${CONFIG_FILE}"

echo ""
echo "Templating configuration file"
cp tests/configuration/nginx.conf ${CONFIG_FILE}
sed -e "s/\${PROMETHEUS_NAME}/${PROMETHEUS_NAME}/" -i ${CONFIG_FILE}

echo ""
echo "Preparing htpasswd file"
HTPASSWD_FILE=$(mktemp)
chmod 666 ${HTPASSWD_FILE}
echo "${HTPASSWD_FILE}"
cp tests/configuration/htpasswd ${HTPASSWD_FILE}

echo ""
echo "Starting nginx container"
docker rm -f ${NGINX_NAME}
docker run --name ${NGINX_NAME} --net=${NETWORK_NAME} -d -p ${NGINX_PORT}:80 -v ${CONFIG_FILE}:/etc/nginx/nginx.conf:ro -v ${HTPASSWD_FILE}:/etc/nginx/.htpasswd:ro nginx

echo ""
echo "Waiting until nginx is up"
until $(curl --output /dev/null --silent --fail http://localhost:${NGINX_PORT}/health); do
printf '.'
sleep 1
done
}

function start_pushgateway() {
echo ""
echo "Starting pushgateway container"
docker rm -f ${PUSHGATEWAY_NAME}
docker run --name ${PUSHGATEWAY_NAME} --net=${NETWORK_NAME} -d -p ${PUSHGATEWAY_PORT}:9091 prom/pushgateway

echo ""
echo "Waiting until pushgateway is up"
until $(curl --output /dev/null --silent --fail http://localhost:${PUSHGATEWAY_PORT}); do
printf '.'
sleep 1
done
}

function start_prometheus() {
echo ""
echo "Creating configuration file"
Expand All @@ -64,7 +110,7 @@ function start_prometheus() {
echo ""
echo "Starting prometheus container"
docker rm -f ${PROMETHEUS_NAME}
docker run --name ${PROMETHEUS_NAME} --net=${NETWORK_NAME} -d -p ${PROMETHEUS_PORT}:9090 -v ${CONFIG_FILE}:/etc/prometheus/prometheus.yml prom/prometheus
docker run --name ${PROMETHEUS_NAME} --net=${NETWORK_NAME} -d -p ${PROMETHEUS_PORT}:9090 -v ${CONFIG_FILE}:/etc/prometheus/prometheus.yml:ro prom/prometheus

echo ""
echo "Waiting until prometheus is up"
Expand Down

0 comments on commit 83347b2

Please sign in to comment.