tests_post-install_7_plugin.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 > /etc/crowdsec/profiles.yaml
  11. cat ./backup_http.yaml > /etc/crowdsec/notifications/http.yaml
  12. }
  13. function clear_backup() {
  14. rm ./backup_profiles.yaml
  15. rm ./backup_http.yaml
  16. }
  17. function modify_config() {
  18. cp ./config/http.yaml /etc/crowdsec/notifications/http.yaml
  19. cp ./config/profiles.yaml /etc/crowdsec/profiles.yaml
  20. systemctl restart crowdsec
  21. }
  22. function setup_tests() {
  23. backup
  24. cscli decisions delete --all
  25. modify_config
  26. python3 -u mock_http_server.py > mock_http_server_logs.log &
  27. MOCK_SERVER_PID=$!
  28. }
  29. function cleanup_tests() {
  30. restore_backup
  31. clear_backup
  32. kill -9 $MOCK_SERVER_PID
  33. rm mock_http_server_logs.log
  34. systemctl restart crowdsec
  35. }
  36. function run_tests() {
  37. log_line_count=$(cat mock_http_server_logs.log | wc -l)
  38. if [[ $log_line_count -ne "0" ]] ; then
  39. cleanup_tests
  40. fail "expected 0 log lines fom mock http server before adding decisions"
  41. fi
  42. cscli decisions add --ip 1.2.3.4 --duration 30s
  43. cscli decisions add --ip 1.2.3.5 --duration 30s
  44. sleep 5
  45. log_line_count=$(cat mock_http_server_logs.log | wc -l)
  46. if [[ $log_line_count -ne "1" ]] ; then
  47. cleanup_tests
  48. fail "expected 1 log line from http server"
  49. fi
  50. total_alerts=$(cat mock_http_server_logs.log | jq .request_body | jq length)
  51. if [[ $total_alerts -ne "2" ]] ; then
  52. cleanup_tests
  53. fail "expected to receive 2 alerts in the request body from plugin"
  54. fi
  55. first_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[0].decisions[0].value)
  56. if [[ $first_received_ip != "1.2.3.4" ]] ; then
  57. cleanup_tests
  58. fail "expected to receive IP 1.2.3.4 as value of first decision"
  59. fi
  60. second_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[1].decisions[0].value)
  61. if [[ $second_received_ip != "1.2.3.5" ]] ; then
  62. cleanup_tests
  63. fail "expected to receive IP 1.2.3.5 as value of second decision"
  64. fi
  65. }
  66. setup_tests
  67. run_tests
  68. cleanup_tests