crowdsec/scripts/func_tests/tests_post-install_7_plugin.sh
Shivam Sandbhor e89543f725
Add functional tests for plugins (#941)
* Add functional tests for plugins

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
(cherry picked from commit 1a11c87296454aac1ebbc95c06b813ae67c91819)

* Update plugin dir in functional tests

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>

* Update plugin process config in func tests

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>

* Robust config replacement

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
2021-09-08 15:22:34 +02:00

81 lines
No EOL
2.3 KiB
Bash
Executable file

#! /usr/bin/env bash
# -*- coding: utf-8 -*-
source tests_base.sh
MOCK_SERVER_PID=""
function backup () {
cat /etc/crowdsec/profiles.yaml > ./backup_profiles.yaml
cat /etc/crowdsec/notifications/http.yaml > ./backup_http.yaml
}
function restore_backup () {
cat ./backup_profiles.yaml > /etc/crowdsec/profiles.yaml
cat ./backup_http.yaml > /etc/crowdsec/notifications/http.yaml
}
function clear_backup() {
rm ./backup_profiles.yaml
rm ./backup_http.yaml
}
function modify_config() {
cp ./config/http.yaml /etc/crowdsec/notifications/http.yaml
cp ./config/profiles.yaml /etc/crowdsec/profiles.yaml
systemctl restart crowdsec
}
function setup_tests() {
backup
cscli decisions delete --all
modify_config
python3 -u mock_http_server.py > mock_http_server_logs.log &
MOCK_SERVER_PID=$!
}
function cleanup_tests() {
restore_backup
clear_backup
kill -9 $MOCK_SERVER_PID
rm mock_http_server_logs.log
systemctl restart crowdsec
}
function run_tests() {
log_line_count=$(cat mock_http_server_logs.log | wc -l)
if [[ $log_line_count -ne "0" ]] ; then
cleanup_tests
fail "expected 0 log lines fom mock http server before adding decisions"
fi
cscli decisions add --ip 1.2.3.4 --duration 30s
cscli decisions add --ip 1.2.3.5 --duration 30s
sleep 5
log_line_count=$(cat mock_http_server_logs.log | wc -l)
if [[ $log_line_count -ne "1" ]] ; then
cleanup_tests
fail "expected 1 log line from http server"
fi
total_alerts=$(cat mock_http_server_logs.log | jq .request_body | jq length)
if [[ $total_alerts -ne "2" ]] ; then
cleanup_tests
fail "expected to receive 2 alerts in the request body from plugin"
fi
first_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[0].decisions[0].value)
if [[ $first_received_ip != "1.2.3.4" ]] ; then
cleanup_tests
fail "expected to receive IP 1.2.3.4 as value of first decision"
fi
second_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[1].decisions[0].value)
if [[ $second_received_ip != "1.2.3.5" ]] ; then
cleanup_tests
fail "expected to receive IP 1.2.3.5 as value of second decision"
fi
}
setup_tests
run_tests
cleanup_tests