2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-07-04 13:23:58 +00:00
|
|
|
require 'rake'
|
|
|
|
|
|
|
|
# if you make changes, then please also change this file 'test/support/searchindex_helper.rb'
|
|
|
|
# this is required as long as our test suite is made of RSpec and MiniTest
|
2018-09-19 13:54:49 +00:00
|
|
|
module SearchindexBackendHelper
|
|
|
|
|
2019-07-04 13:23:58 +00:00
|
|
|
def self.included(base)
|
|
|
|
# Execute in RSpec class context
|
|
|
|
base.class_exec do
|
|
|
|
|
2021-07-19 12:24:09 +00:00
|
|
|
after do
|
2019-07-04 13:23:58 +00:00
|
|
|
next if ENV['ES_URL'].blank?
|
|
|
|
|
|
|
|
Rake::Task['searchindex:drop'].execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-24 13:04:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
prepares elasticsearch
|
|
|
|
|
|
|
|
@param required [Boolean] raises error if ES is not configured. Recommended to avoid mysterious errors in CI.
|
|
|
|
@param rebuild [Boolean] rebuilds indexes and sleeps for 1 second after given yield block is executed
|
|
|
|
|
|
|
|
@yield given block run after ES is setup, but before index rebuilding
|
|
|
|
|
|
|
|
=end
|
|
|
|
def configure_elasticsearch(required: false, rebuild: false)
|
2018-09-19 13:54:49 +00:00
|
|
|
if ENV['ES_URL'].blank?
|
|
|
|
return if !required
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-03-12 08:23:19 +00:00
|
|
|
raise "Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Setting.set('es_url', ENV['ES_URL'])
|
|
|
|
|
|
|
|
# Setting.set('es_url', 'http://127.0.0.1:9200')
|
|
|
|
# Setting.set('es_index', 'estest.local_zammad')
|
|
|
|
# Setting.set('es_user', 'elasticsearch')
|
|
|
|
# Setting.set('es_password', 'zammad')
|
|
|
|
|
|
|
|
if ENV['ES_INDEX_RAND'].present?
|
2021-09-20 10:47:05 +00:00
|
|
|
rand_id = ENV.fetch('CI_JOB_ID', SecureRandom.uuid)
|
2021-05-12 11:37:44 +00:00
|
|
|
test_method_name = self.class.description.gsub(%r{[^\w]}, '_')
|
2021-09-20 10:47:05 +00:00
|
|
|
ENV['ES_INDEX'] = "es_index_#{test_method_name.downcase}_#{rand_id.downcase}"
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
if ENV['ES_INDEX'].blank?
|
2020-03-12 08:23:19 +00:00
|
|
|
raise "Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-09-19 13:54:49 +00:00
|
|
|
Setting.set('es_index', ENV['ES_INDEX'])
|
|
|
|
|
|
|
|
# set max attachment size in mb
|
|
|
|
Setting.set('es_attachment_max_size_in_mb', 1)
|
|
|
|
|
|
|
|
yield if block_given?
|
2019-09-24 13:04:42 +00:00
|
|
|
|
|
|
|
return if !rebuild
|
|
|
|
|
|
|
|
rebuild_searchindex
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rebuild_searchindex
|
2019-07-04 13:23:58 +00:00
|
|
|
Rake::Task.clear
|
|
|
|
Zammad::Application.load_tasks
|
2018-09-19 13:54:49 +00:00
|
|
|
Rake::Task['searchindex:rebuild'].execute
|
2019-09-24 13:04:42 +00:00
|
|
|
Rake::Task['searchindex:refresh'].execute
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-09-24 13:04:42 +00:00
|
|
|
# configure_elasticsearch has to be executed manually!!!
|
2018-09-19 13:54:49 +00:00
|
|
|
RSpec.configure do |config|
|
|
|
|
config.include SearchindexBackendHelper, searchindex: true
|
|
|
|
end
|