tests_post-install_0base.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #! /usr/bin/env bash
  2. # -*- coding: utf-8 -*-
  3. source tests_base.sh
  4. echo $PATH
  5. sudo cp /etc/crowdsec/config.yaml ./config.yaml.backup
  6. ##########################
  7. ## TEST AGENT/LAPI/CAPI ##
  8. echo "CROWDSEC (AGENT+LAPI+CAPI)"
  9. ## status / start / stop
  10. # service should be up
  11. pidof crowdsec || fail "crowdsec process should be running"
  12. ${SYSTEMCTL} status crowdsec || fail "systemctl status crowdsec failed"
  13. #shut it down
  14. ${SYSTEMCTL} stop crowdsec || fail "failed to stop service"
  15. ${SYSTEMCTL} status crowdsec && fail "crowdsec should be down"
  16. pidof crowdsec && fail "crowdsec process shouldn't be running"
  17. #start it again
  18. ${SYSTEMCTL} start crowdsec || fail "failed to stop service"
  19. ${SYSTEMCTL} status crowdsec || fail "crowdsec should be up"
  20. sleep 5s
  21. pidof crowdsec || fail "crowdsec process should be running"
  22. #restart it
  23. ${SYSTEMCTL} restart crowdsec || fail "failed to stop service"
  24. ${SYSTEMCTL} status crowdsec || fail "crowdsec should be up"
  25. sleep 5s
  26. pidof crowdsec || fail "crowdsec process should be running"
  27. ## version
  28. ${CSCLI} version || fail "cannot run cscli version"
  29. ## alerts
  30. # alerts list at startup should just return one entry : community pull
  31. sleep 5
  32. ${CSCLI} alerts list -ojson | ${JQ} '. | length >= 1' || fail "expected at least one entry from cscli alerts list"
  33. ## capi
  34. ${CSCLI} capi status || fail "capi status should be ok"
  35. ## config
  36. ${CSCLI} config show || fail "failed to show config"
  37. ${CSCLI} config backup ./test || fail "failed to backup config"
  38. sudo rm -rf ./test
  39. ## lapi
  40. ${CSCLI} lapi status || fail "lapi status failed"
  41. ## metrics
  42. ${CSCLI} metrics || fail "failed to get metrics"
  43. ${SYSTEMCTL} stop crowdsec || fail "crowdsec should be down"
  44. #######################
  45. ## TEST WITHOUT LAPI ##
  46. echo "CROWDSEC (AGENT)"
  47. # test with -no-api flag
  48. cp ${SYSTEMD_SERVICE_FILE} /tmp/crowdsec.service-orig
  49. sed '/^ExecStart/ s/$/ -no-api/' ${SYSTEMD_SERVICE_FILE} > /tmp/crowdsec.service
  50. sudo mv /tmp/crowdsec.service /etc/systemd/system/crowdsec.service
  51. ${SYSTEMCTL} daemon-reload
  52. ${SYSTEMCTL} start crowdsec
  53. sleep 1
  54. pidof crowdsec && fail "crowdsec shouldn't run without LAPI (in flag)"
  55. ${SYSTEMCTL} stop crowdsec
  56. sudo cp /tmp/crowdsec.service-orig /etc/systemd/system/crowdsec.service
  57. ${SYSTEMCTL} daemon-reload
  58. # test with no api server in configuration file
  59. sudo cp ./config/config_no_lapi.yaml /etc/crowdsec/config.yaml
  60. ${SYSTEMCTL} start crowdsec
  61. sleep 1
  62. pidof crowdsec && fail "crowdsec agent should not run without lapi (in configuration file)"
  63. ##### cscli test ####
  64. ## capi
  65. ${CSCLI} -c ./config/config_no_lapi.yaml capi status && fail "capi status shouldn't be ok"
  66. ## config
  67. ${CSCLI_BIN} -c ./config/config_no_lapi.yaml config show || fail "failed to show config"
  68. ${CSCLI} -c ./config/config_no_lapi.yaml config backup ./test || fail "failed to backup config"
  69. sudo rm -rf ./test
  70. ## lapi
  71. ${CSCLI} -c ./config/config_no_lapi.yaml lapi status && fail "lapi status should not be ok" ## if lapi status success, it means that the test fail
  72. ## metrics
  73. ${CSCLI_BIN} -c ./config/config_no_lapi.yaml metrics
  74. ${SYSTEMCTL} stop crowdsec
  75. sudo cp ./config/config.yaml /etc/crowdsec/config.yaml
  76. ########################
  77. ## TEST WITHOUT AGENT ##
  78. echo "CROWDSEC (LAPI+CAPI)"
  79. # test with -no-cs flag
  80. sed '/^ExecStart/ s/$/ -no-cs/' /etc/systemd/system/crowdsec.service > /tmp/crowdsec.service
  81. sudo mv /tmp/crowdsec.service /etc/systemd/system/crowdsec.service
  82. ${SYSTEMCTL} daemon-reload
  83. ${SYSTEMCTL} start crowdsec
  84. pidof crowdsec || fail "crowdsec LAPI should run without agent (in flag)"
  85. ${SYSTEMCTL} stop crowdsec
  86. sed '/^ExecStart/s/-no-cs//g' ${SYSTEMD_SERVICE_FILE} > /tmp/crowdsec.service
  87. sudo mv /tmp/crowdsec.service /etc/systemd/system/crowdsec.service
  88. ${SYSTEMCTL} daemon-reload
  89. # test with no crowdsec agent in configuration file
  90. sudo cp ./config/config_no_agent.yaml /etc/crowdsec/config.yaml
  91. ${SYSTEMCTL} start crowdsec
  92. pidof crowdsec || fail "crowdsec LAPI should run without agent (in configuration file)"
  93. ## capi
  94. ${CSCLI} -c ./config/config_no_agent.yaml capi status || fail "capi status should be ok"
  95. ## config
  96. ${CSCLI_BIN} -c ./config/config_no_agent.yaml config show || fail "failed to show config"
  97. ${CSCLI} -c ./config/config_no_agent.yaml config backup ./test || fail "failed to backup config"
  98. sudo rm -rf ./test
  99. ## lapi
  100. ${CSCLI} -c ./config/config_no_agent.yaml lapi status || fail "lapi status failed"
  101. ## metrics
  102. ${CSCLI_BIN} -c ./config/config_no_agent.yaml metrics || fail "failed to get metrics"
  103. ${SYSTEMCTL} stop crowdsec
  104. sudo cp ./config/config.yaml /etc/crowdsec/config.yaml
  105. #######################
  106. ## TEST WITHOUT CAPI ##
  107. echo "CROWDSEC (AGENT+LAPI)"
  108. # test with no online client in configuration file
  109. sudo cp ./config/config_no_capi.yaml /etc/crowdsec/config.yaml
  110. ${SYSTEMCTL} start crowdsec
  111. pidof crowdsec || fail "crowdsec LAPI should run without CAPI (in configuration file)"
  112. ## capi
  113. ${CSCLI} -c ./config/config_no_capi.yaml capi status && fail "capi status should not be ok" ## if capi status success, it means that the test fail
  114. ## config
  115. ${CSCLI_BIN} -c ./config/config_no_capi.yaml config show || fail "failed to show config"
  116. ${CSCLI} -c ./config/config_no_capi.yaml config backup ./test || fail "failed to backup config"
  117. sudo rm -rf ./test
  118. ## lapi
  119. ${CSCLI} -c ./config/config_no_capi.yaml lapi status || fail "lapi status failed"
  120. ## metrics
  121. ${CSCLI_BIN} -c ./config/config_no_capi.yaml metrics || fail "failed to get metrics"
  122. sudo mv /tmp/crowdsec.service-orig /etc/systemd/system/crowdsec.service
  123. sudo cp ./config.yaml.backup /etc/crowdsec/config.yaml
  124. ${SYSTEMCTL} restart crowdsec