tests_post-install_7_plugin.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #! /usr/bin/env bash
  2. # -*- coding: utf-8 -*-
  3. source tests_base.sh
  4. MOCK_SERVER_PID=""
  5. function backup () {
  6. cat /etc/crowdsec/profiles.yaml > ./backup_profiles.yaml
  7. cat /etc/crowdsec/notifications/http.yaml > ./backup_http.yaml
  8. }
  9. function restore_backup () {
  10. cat ./backup_profiles.yaml | sudo tee /etc/crowdsec/profiles.yaml > /dev/null
  11. cat ./backup_http.yaml | sudo tee /etc/crowdsec/notifications/http.yaml > /dev/null
  12. }
  13. function clear_backup() {
  14. rm ./backup_profiles.yaml
  15. rm ./backup_http.yaml
  16. }
  17. function modify_config() {
  18. PLUGINS_DIR=$(find /usr -type d -wholename "*"crowdsec/plugins)
  19. sed -i "s#/usr/local/lib/crowdsec/plugins#${PLUGINS_DIR}#g" ./config/config.yaml
  20. cat ./config/config.yaml | sudo tee /etc/crowdsec/config.yaml > /dev/null
  21. cat ./config/http.yaml | sudo tee /etc/crowdsec/notifications/http.yaml > /dev/null
  22. cat ./config/profiles.yaml | sudo tee /etc/crowdsec/profiles.yaml > /dev/null
  23. ${SYSTEMCTL} restart crowdsec
  24. sleep 5s
  25. }
  26. function setup_tests() {
  27. backup
  28. cscli decisions delete --all
  29. modify_config
  30. python3 -u mock_http_server.py > mock_http_server_logs.log &
  31. MOCK_SERVER_PID=$!
  32. }
  33. function cleanup_tests() {
  34. restore_backup
  35. clear_backup
  36. kill -9 $MOCK_SERVER_PID
  37. rm mock_http_server_logs.log
  38. ${SYSTEMCTL} restart crowdsec
  39. sleep 5s
  40. }
  41. function run_tests() {
  42. log_line_count=$(cat mock_http_server_logs.log | wc -l)
  43. if [[ $log_line_count -ne "0" ]] ; then
  44. cleanup_tests
  45. fail "expected 0 log lines fom mock http server before adding decisions"
  46. fi
  47. ${CSCLI} decisions add --ip 1.2.3.4 --duration 30s
  48. ${CSCLI} decisions add --ip 1.2.3.5 --duration 30s
  49. sleep 5
  50. cat mock_http_server_logs.log
  51. log_line_count=$(cat mock_http_server_logs.log | wc -l)
  52. if [[ $log_line_count -ne "1" ]] ; then
  53. cleanup_tests
  54. fail "expected 1 log line from http server"
  55. fi
  56. total_alerts=$(cat mock_http_server_logs.log | jq .request_body | jq length)
  57. if [[ $total_alerts -ne "2" ]] ; then
  58. cleanup_tests
  59. fail "expected to receive 2 alerts in the request body from plugin"
  60. fi
  61. first_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[0].decisions[0].value)
  62. if [[ $first_received_ip != "1.2.3.4" ]] ; then
  63. cleanup_tests
  64. fail "expected to receive IP 1.2.3.4 as value of first decision"
  65. fi
  66. second_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[1].decisions[0].value)
  67. if [[ $second_received_ip != "1.2.3.5" ]] ; then
  68. cleanup_tests
  69. fail "expected to receive IP 1.2.3.5 as value of second decision"
  70. fi
  71. }
  72. setup_tests
  73. run_tests
  74. cleanup_tests