searchindex_backend.rb 2.1 KB

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