Centralized Elasticsearch and searchindex logic into single support helper module. This fixes an issue on the CI env that is caused by not removed ES indexes.

This commit is contained in:
Thorsten Eckel 2018-06-03 17:24:35 +02:00
parent 7f4c540fa0
commit c50d6a49ed
10 changed files with 82 additions and 235 deletions

View file

@ -1,43 +1,16 @@
require 'test_helper' require 'test_helper'
require 'rake'
class FormControllerTest < ActionDispatch::IntegrationTest class FormControllerTest < ActionDispatch::IntegrationTest
include SearchindexHelper
setup do setup do
@headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.4' } @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.4' }
if ENV['ES_URL'].present? configure_elasticsearch
#fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
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?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
end
Ticket.destroy_all Ticket.destroy_all
# drop/create indexes rebuild_searchindex
Setting.reload
Rake::Task.clear
Zammad::Application.load_tasks
Rake::Task['searchindex:rebuild'].execute
end
teardown do
if ENV['ES_URL'].present?
Rake::Task['searchindex:drop'].execute
end
end end
test '01 - get config call' do test '01 - get config call' do

View file

@ -1,8 +1,8 @@
require 'test_helper' require 'test_helper'
require 'rake'
class OrganizationControllerTest < ActionDispatch::IntegrationTest class OrganizationControllerTest < ActionDispatch::IntegrationTest
include SearchindexHelper
setup do setup do
# set accept header # set accept header
@ -73,32 +73,11 @@ class OrganizationControllerTest < ActionDispatch::IntegrationTest
organization_id: @organization.id, organization_id: @organization.id,
) )
# configure es configure_elasticsearch do
if ENV['ES_URL'].present?
#fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
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?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
travel 1.minute travel 1.minute
# drop/create indexes rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
# execute background jobs # execute background jobs
Scheduler.worker(true) Scheduler.worker(true)

View file

@ -1,7 +1,8 @@
require 'test_helper' require 'test_helper'
require 'rake'
class ReportsControllerTest < ActionDispatch::IntegrationTest class ReportsControllerTest < ActionDispatch::IntegrationTest
include SearchindexHelper
setup do setup do
# set accept header # set accept header
@ -69,31 +70,12 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1, created_by_id: 1,
) )
if ENV['ES_URL'].present?
#fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
Setting.set('es_url', ENV['ES_URL'])
# Setting.set('es_url', 'http://127.0.0.1:9200') configure_elasticsearch do
# Setting.set('es_index', 'estest.local_zammad')
# Setting.set('es_user', 'elasticsearch')
# Setting.set('es_password', 'zammad')
if ENV['ES_INDEX_RAND'].present?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
travel 1.minute travel 1.minute
# drop/create indexes rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
# execute background jobs # execute background jobs
Scheduler.worker(true) Scheduler.worker(true)
@ -101,11 +83,6 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
sleep 6 sleep 6
end end
end end
teardown do
if ENV['ES_URL'].present?
Rake::Task['searchindex:drop'].execute
end
end
test '01.01 report example - admin access' do test '01.01 report example - admin access' do

View file

@ -1,8 +1,8 @@
require 'test_helper' require 'test_helper'
require 'rake'
class SearchControllerTest < ActionDispatch::IntegrationTest class SearchControllerTest < ActionDispatch::IntegrationTest
include SearchindexHelper
setup do setup do
# set current user # set current user
@ -142,35 +142,11 @@ class SearchControllerTest < ActionDispatch::IntegrationTest
type: Ticket::Article::Type.where(name: 'email').first, type: Ticket::Article::Type.where(name: 'email').first,
) )
# configure es configure_elasticsearch do
if ENV['ES_URL'].present?
#fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
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?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
# set max attachment size in mb
Setting.set('es_attachment_max_size_in_mb', 1)
travel 1.minute travel 1.minute
# drop/create indexes rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
# execute background jobs # execute background jobs
Scheduler.worker(true) Scheduler.worker(true)
@ -179,12 +155,6 @@ class SearchControllerTest < ActionDispatch::IntegrationTest
end end
end end
teardown do
if ENV['ES_URL'].present?
Rake::Task['searchindex:drop'].execute
end
end
test 'settings index with nobody' do test 'settings index with nobody' do
params = { params = {
query: 'test 1234', query: 'test 1234',

View file

@ -1,8 +1,8 @@
require 'test_helper' require 'test_helper'
require 'rake'
class UserControllerTest < ActionDispatch::IntegrationTest class UserControllerTest < ActionDispatch::IntegrationTest
include SearchindexHelper
setup do setup do
# set accept header # set accept header
@ -84,40 +84,19 @@ class UserControllerTest < ActionDispatch::IntegrationTest
organization_id: @organization.id, organization_id: @organization.id,
) )
# configure es configure_elasticsearch do
if ENV['ES_URL'].present?
#fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
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?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
travel 1.minute travel 1.minute
# drop/create indexes rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
# execute background jobs # execute background jobs
Scheduler.worker(true) Scheduler.worker(true)
sleep 6 sleep 6
end end
UserInfo.current_user_id = nil
UserInfo.current_user_id = nil
end end
test 'user create tests - no user' do test 'user create tests - no user' do

View file

@ -1,8 +1,8 @@
require 'test_helper' require 'test_helper'
require 'rake'
class UserOrganizationControllerTest < ActionDispatch::IntegrationTest class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
include SearchindexHelper
setup do setup do
# set accept header # set accept header
@ -84,39 +84,17 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
organization_id: @organization.id, organization_id: @organization.id,
) )
# configure es configure_elasticsearch do
if ENV['ES_URL'].present?
#fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
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?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
travel 1.minute travel 1.minute
# drop/create indexes rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
# execute background jobs # execute background jobs
Scheduler.worker(true) Scheduler.worker(true)
sleep 6 sleep 6
end end
end end
test 'user create tests - no user' do test 'user create tests - no user' do

View file

@ -1,38 +1,13 @@
require 'integration_test_helper' require 'integration_test_helper'
require 'rake'
class ElasticsearchTest < ActiveSupport::TestCase class ElasticsearchTest < ActiveSupport::TestCase
include SearchindexHelper
setup do setup do
# set config configure_elasticsearch(required: true)
if ENV['ES_URL'].blank?
raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
end
Setting.set('es_url', ENV['ES_URL'])
if ENV['ES_INDEX_RAND'].present?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
# Setting.set('es_url', 'http://127.0.0.1:9200') rebuild_searchindex
# Setting.set('es_index', 'estest.local_zammad')
# Setting.set('es_user', 'elasticsearch')
# Setting.set('es_password', 'zammad')
# set max attachment size in mb
Setting.set('es_attachment_max_size_in_mb', 1)
# drop/create indexes
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
groups = Group.where(name: 'Users') groups = Group.where(name: 'Users')
roles = Role.where(name: 'Agent') roles = Role.where(name: 'Agent')
@ -100,12 +75,6 @@ class ElasticsearchTest < ActiveSupport::TestCase
) )
end end
teardown do
if ENV['ES_URL'].present?
Rake::Task['searchindex:drop'].execute
end
end
# check search attributes # check search attributes
test 'a - objects' do test 'a - objects' do

View file

@ -1,37 +1,15 @@
require 'integration_test_helper' require 'integration_test_helper'
require 'rake'
class ReportTest < ActiveSupport::TestCase class ReportTest < ActiveSupport::TestCase
include SearchindexHelper
setup do setup do
# set config configure_elasticsearch(required: true)
if ENV['ES_URL'].blank?
raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
end
Setting.set('es_url', ENV['ES_URL'])
if ENV['ES_INDEX_RAND'].present?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
# 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')
# Setting.set('es_attachment_max_size_in_mb', 1)
Ticket.destroy_all Ticket.destroy_all
# drop/create indexes rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
group1 = Group.lookup(name: 'Users') group1 = Group.lookup(name: 'Users')
group2 = Group.create_if_not_exists( group2 = Group.create_if_not_exists(
@ -275,13 +253,6 @@ class ReportTest < ActiveSupport::TestCase
# execute background jobs # execute background jobs
Scheduler.worker(true) Scheduler.worker(true)
end
teardown do
if ENV['ES_URL'].present?
Rake::Task['searchindex:drop'].execute
end
end end
test 'compare' do test 'compare' do

View file

@ -3,6 +3,8 @@ require File.expand_path('../config/environment', __dir__)
require 'rails/test_help' require 'rails/test_help'
require 'cache' require 'cache'
require 'test_support_helper'
class ActiveSupport::TestCase class ActiveSupport::TestCase
# disable transactions / to work with own database connections for each thread # disable transactions / to work with own database connections for each thread

View file

@ -0,0 +1,49 @@
require 'rake'
module SearchindexHelper
def self.included(base)
base.teardown do
next if ENV['ES_URL'].blank?
Rake::Task['searchindex:drop'].execute
end
end
def configure_elasticsearch(required: false)
if ENV['ES_URL'].blank?
return if !required
raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
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?
ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
end
if ENV['ES_INDEX'].blank?
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
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?
end
def rebuild_searchindex
# drop/create indexes
Rake::Task.clear
Zammad::Application.load_tasks
#Rake::Task["searchindex:drop"].execute
#Rake::Task["searchindex:create"].execute
Rake::Task['searchindex:rebuild'].execute
end
end