searchindex_helper.rb 1.8 KB

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