tests_post-install_0base.sh 5.3 KB

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