Commit a14d0934 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '213740-flaky-test-ee-spec-workers-elastic_indexer_worker_spec-rb' into 'master'

Resolve "Flaky test `elastic_indexer_worker_spec.rb`"

Closes #213740

See merge request gitlab-org/gitlab!29211
parents 272a6faa 50c40a09
# frozen_string_literal: true
require 'spec_helper'
# This module is monkey-patched in config/initializers/elastic_client_setup.rb
describe "Monkey-patches to ::Elasticsearch::Model::Client" do
before do
stub_ee_application_setting(elasticsearch_url: ['http://localhost:9200'])
end
it 'uses the same client instance for all subclasses' do
a = Class.new { include ::Elasticsearch::Model }
b = Class.new { include ::Elasticsearch::Model }
c = Class.new(b)
expect(::Gitlab::Elastic::Client).to receive(:build).with(anything) { :fake_client }.once
# Ensure that the same client instance is used between classes and between
# requests
[a, b, c, b, c, b, a].each do |klass|
expect(klass.__elasticsearch__.client).to eq(:fake_client)
end
end
end
...@@ -5,6 +5,11 @@ require 'spec_helper' ...@@ -5,6 +5,11 @@ require 'spec_helper'
describe Elastic::IndexRecordService, :elastic do describe Elastic::IndexRecordService, :elastic do
subject { described_class.new } subject { described_class.new }
# Create admin user and search globally to avoid dealing with permissions in
# these tests
let(:user) { create(:admin) }
let(:search_options) { { options: { current_user: user, project_ids: :any } } }
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
...@@ -27,13 +32,21 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -27,13 +32,21 @@ describe Elastic::IndexRecordService, :elastic do
it 'indexes new records' do it 'indexes new records' do
object = create(type) object = create(type)
Sidekiq::Testing.disable! do
if type != :project
# You cannot find anything in the index if it's parent project is
# not first indexed.
subject.execute(object.project, true)
end
end
# Prevent records from being added via bulk indexing updates # Prevent records from being added via bulk indexing updates
::Elastic::ProcessBookkeepingService.clear_tracking! ::Elastic::ProcessBookkeepingService.clear_tracking!
expect do expect do
expect(subject.execute(object, true)).to eq(true) expect(subject.execute(object, true)).to eq(true)
ensure_elasticsearch_index! ensure_elasticsearch_index!
end.to change { Elasticsearch::Model.search('*').records.size }.by(1) end.to change { object.class.elastic_search('*', search_options).total_count }.by(1)
end end
it 'updates the index when object is changed' do it 'updates the index when object is changed' do
...@@ -41,6 +54,13 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -41,6 +54,13 @@ describe Elastic::IndexRecordService, :elastic do
Sidekiq::Testing.disable! do Sidekiq::Testing.disable! do
object = create(type) object = create(type)
if type != :project
# You cannot find anything in the index if it's parent project is
# not first indexed.
subject.execute(object.project, true)
end
expect(subject.execute(object, true)).to eq(true) expect(subject.execute(object, true)).to eq(true)
object.update(attribute => "new") object.update(attribute => "new")
end end
...@@ -48,7 +68,7 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -48,7 +68,7 @@ describe Elastic::IndexRecordService, :elastic do
expect do expect do
expect(subject.execute(object, false)).to eq(true) expect(subject.execute(object, false)).to eq(true)
ensure_elasticsearch_index! ensure_elasticsearch_index!
end.to change { Elasticsearch::Model.search('new').records.size }.by(1) end.to change { object.class.elastic_search('new', search_options).total_count }.by(1)
end end
it 'ignores Elasticsearch::Transport::Transport::Errors::NotFound errors' do it 'ignores Elasticsearch::Transport::Transport::Errors::NotFound errors' do
...@@ -74,7 +94,11 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -74,7 +94,11 @@ describe Elastic::IndexRecordService, :elastic do
end end
# Nothing should be in the index at this point # Nothing should be in the index at this point
expect(Elasticsearch::Model.search('*').total_count).to be(0) expect(Project.elastic_search('*', search_options).total_count).to be(0)
expect(Issue.elastic_search('*', search_options).total_count).to be(0)
expect(Milestone.elastic_search('*', search_options).total_count).to be(0)
expect(MergeRequest.elastic_search('*', search_options).total_count).to be(0)
expect(ProjectSnippet.elastic_search('*', search_options).total_count).to be(0)
end end
it 'indexes records associated with the project' do it 'indexes records associated with the project' do
......
...@@ -5,6 +5,11 @@ require 'spec_helper' ...@@ -5,6 +5,11 @@ require 'spec_helper'
describe ElasticIndexerWorker, :elastic do describe ElasticIndexerWorker, :elastic do
subject { described_class.new } subject { described_class.new }
# Create admin user and search globally to avoid dealing with permissions in
# these tests
let(:user) { create(:admin) }
let(:search_options) { { options: { current_user: user, project_ids: :any } } }
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
...@@ -23,12 +28,12 @@ describe ElasticIndexerWorker, :elastic do ...@@ -23,12 +28,12 @@ describe ElasticIndexerWorker, :elastic do
describe 'Indexing, updating, and deleting records' do describe 'Indexing, updating, and deleting records' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:type, :name, :attribute) do where(:type, :name) do
:project | "Project" | :name :project | "Project"
:issue | "Issue" | :title :issue | "Issue"
:note | "Note" | :note :note | "Note"
:milestone | "Milestone" | :title :milestone | "Milestone"
:merge_request | "MergeRequest" | :title :merge_request | "MergeRequest"
end end
with_them do with_them do
...@@ -47,6 +52,13 @@ describe ElasticIndexerWorker, :elastic do ...@@ -47,6 +52,13 @@ describe ElasticIndexerWorker, :elastic do
Sidekiq::Testing.disable! do Sidekiq::Testing.disable! do
object = create(type) object = create(type)
if type != :project
# You cannot find anything in the index if it's parent project is
# not first indexed.
subject.perform("index", "Project", object.project.id, object.project.es_id)
end
subject.perform("index", name, object.id, object.es_id) subject.perform("index", name, object.id, object.es_id)
ensure_elasticsearch_index! ensure_elasticsearch_index!
object.destroy object.destroy
...@@ -55,7 +67,7 @@ describe ElasticIndexerWorker, :elastic do ...@@ -55,7 +67,7 @@ describe ElasticIndexerWorker, :elastic do
expect do expect do
subject.perform("delete", name, object.id, object.es_id, { 'es_parent' => object.es_parent }) subject.perform("delete", name, object.id, object.es_id, { 'es_parent' => object.es_parent })
ensure_elasticsearch_index! ensure_elasticsearch_index!
end.to change { Elasticsearch::Model.search('*').total_count }.by(-1) end.to change { object.class.elastic_search('*', search_options).total_count }.by(-1)
end end
end end
end end
...@@ -84,12 +96,20 @@ describe ElasticIndexerWorker, :elastic do ...@@ -84,12 +96,20 @@ describe ElasticIndexerWorker, :elastic do
ensure_elasticsearch_index! ensure_elasticsearch_index!
## All database objects + data from repository. The absolute value does not matter ## All database objects + data from repository. The absolute value does not matter
expect(Elasticsearch::Model.search('*').total_count).to be > 40 expect(Project.elastic_search('*', search_options).records).to include(project)
expect(Issue.elastic_search('*', search_options).records).to include(issue)
expect(Milestone.elastic_search('*', search_options).records).to include(milestone)
expect(Note.elastic_search('*', search_options).records).to include(note)
expect(MergeRequest.elastic_search('*', search_options).records).to include(merge_request)
subject.perform("delete", "Project", project.id, project.es_id) subject.perform("delete", "Project", project.id, project.es_id)
ensure_elasticsearch_index! ensure_elasticsearch_index!
expect(Elasticsearch::Model.search('*').total_count).to be(0) expect(Project.elastic_search('*', search_options).total_count).to be(0)
expect(Issue.elastic_search('*', search_options).total_count).to be(0)
expect(Milestone.elastic_search('*', search_options).total_count).to be(0)
expect(Note.elastic_search('*', search_options).total_count).to be(0)
expect(MergeRequest.elastic_search('*', search_options).total_count).to be(0)
end end
it 'retries if index raises error' do it 'retries if index raises error' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment