searchindex_backend.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'rake'
  2. # if you make changes, then please also change this file 'test/support/searchindex_helper.rb'
  3. # this is required as long as our test suite is made of RSpec and MiniTest
  4. module SearchindexBackendHelper
  5. def self.included(base)
  6. # Execute in RSpec class context
  7. base.class_exec do
  8. after(:each) do
  9. next if ENV['ES_URL'].blank?
  10. Rake::Task['searchindex:drop'].execute
  11. end
  12. end
  13. end
  14. =begin
  15. prepares elasticsearch
  16. @param required [Boolean] raises error if ES is not configured. Recommended to avoid mysterious errors in CI.
  17. @param rebuild [Boolean] rebuilds indexes and sleeps for 1 second after given yield block is executed
  18. @yield given block run after ES is setup, but before index rebuilding
  19. =end
  20. def configure_elasticsearch(required: false, rebuild: false)
  21. if ENV['ES_URL'].blank?
  22. return if !required
  23. raise "Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
  24. end
  25. Setting.set('es_url', ENV['ES_URL'])
  26. # Setting.set('es_url', 'http://127.0.0.1:9200')
  27. # Setting.set('es_index', 'estest.local_zammad')
  28. # Setting.set('es_user', 'elasticsearch')
  29. # Setting.set('es_password', 'zammad')
  30. if ENV['ES_INDEX_RAND'].present?
  31. rand_id = ENV.fetch('CI_JOB_ID', "r#{rand(999)}")
  32. test_method_name = self.class.description.gsub(/[^\w]/, '_')
  33. ENV['ES_INDEX'] = "es_index_#{test_method_name.downcase}_#{rand_id}_#{rand(999_999_999)}"
  34. end
  35. if ENV['ES_INDEX'].blank?
  36. raise "Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
  37. end
  38. Setting.set('es_index', ENV['ES_INDEX'])
  39. # set max attachment size in mb
  40. Setting.set('es_attachment_max_size_in_mb', 1)
  41. yield if block_given?
  42. return if !rebuild
  43. rebuild_searchindex
  44. end
  45. def rebuild_searchindex
  46. Rake::Task.clear
  47. Zammad::Application.load_tasks
  48. Rake::Task['searchindex:rebuild'].execute
  49. Rake::Task['searchindex:refresh'].execute
  50. end
  51. end
  52. # configure_elasticsearch has to be executed manually!!!
  53. RSpec.configure do |config|
  54. config.include SearchindexBackendHelper, searchindex: true
  55. end