diff options
Diffstat (limited to 'spec/lib/gitlab')
5 files changed, 166 insertions, 5 deletions
diff --git a/spec/lib/gitlab/background_migration/backfill_imported_issue_search_data_spec.rb b/spec/lib/gitlab/background_migration/backfill_imported_issue_search_data_spec.rb new file mode 100644 index 00000000000..7138578f308 --- /dev/null +++ b/spec/lib/gitlab/background_migration/backfill_imported_issue_search_data_spec.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe Gitlab::BackgroundMigration::BackfillImportedIssueSearchData, :migration, schema: 20220621040800 do + let!(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') } + let!(:issue_search_data_table) { table(:issue_search_data) } + + let!(:user) { table(:users).create!(email: 'author@example.com', username: 'author', projects_limit: 10) } + let!(:project) do + table(:projects) + .create!( + namespace_id: namespace.id, + creator_id: user.id, + name: 'projecty', + path: 'path', + project_namespace_id: namespace.id) + end + + let!(:issue) do + table(:issues).create!( + project_id: project.id, + title: 'Patterson', + description: FFaker::HipsterIpsum.paragraph + ) + end + + let(:migration) do + described_class.new(start_id: 1, + end_id: 30, + batch_table: :issues, + batch_column: :id, + sub_batch_size: 2, + pause_ms: 0, + connection: ApplicationRecord.connection) + end + + let(:perform_migration) { migration.perform } + + context 'when issue has search data record' do + let!(:issue_search_data) { issue_search_data_table.create!(project_id: project.id, issue_id: issue.id) } + + it 'does not create or update any search data records' do + expect { perform_migration } + .to not_change { issue_search_data_table.count } + .and not_change { issue_search_data } + + expect(issue_search_data_table.count).to eq(1) + end + end + + context 'when issue has no search data record' do + let(:title_node) { "'#{issue.title.downcase}':1A" } + + it 'creates search data records' do + expect { perform_migration } + .to change { issue_search_data_table.count }.from(0).to(1) + + expect(issue_search_data_table.find_by(project_id: project.id).issue_id) + .to eq(issue.id) + + expect(issue_search_data_table.find_by(project_id: project.id).search_vector) + .to include(title_node) + end + end + + context 'error handling' do + let!(:issue2) do + table(:issues).create!( + project_id: project.id, + title: 'Chatterton', + description: FFaker::HipsterIpsum.paragraph + ) + end + + before do + issue.update!(description: Array.new(30_000) { SecureRandom.hex }.join(' ')) + end + + let(:title_node2) { "'#{issue2.title.downcase}':1A" } + + it 'skips insertion for that issue but continues with migration' do + expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |logger| + expect(logger) + .to receive(:error) + .with(a_hash_including(message: /string is too long for tsvector/, model_id: issue.id)) + end + + expect { perform_migration }.to change { issue_search_data_table.count }.from(0).to(1) + expect(issue_search_data_table.find_by(issue_id: issue.id)).to eq(nil) + expect(issue_search_data_table.find_by(issue_id: issue2.id).search_vector) + .to include(title_node2) + end + + it 're-raises exceptions' do + allow(migration) + .to receive(:update_search_data_individually) + .and_raise(ActiveRecord::StatementTimeout) + + expect { perform_migration }.to raise_error(ActiveRecord::StatementTimeout) + end + end +end diff --git a/spec/lib/gitlab/background_migration/batching_strategies/primary_key_batching_strategy_spec.rb b/spec/lib/gitlab/background_migration/batching_strategies/primary_key_batching_strategy_spec.rb index 521e2067744..943b5744b64 100644 --- a/spec/lib/gitlab/background_migration/batching_strategies/primary_key_batching_strategy_spec.rb +++ b/spec/lib/gitlab/background_migration/batching_strategies/primary_key_batching_strategy_spec.rb @@ -45,10 +45,30 @@ RSpec.describe Gitlab::BackgroundMigration::BatchingStrategies::PrimaryKeyBatchi end end + context 'when job_class is provided with a batching_scope' do + let(:job_class) do + Class.new(described_class) do + def self.batching_scope(relation, job_arguments:) + min_id = job_arguments.first + + relation.where.not(type: 'Project').where('id >= ?', min_id) + end + end + end + + it 'applies the batching scope' do + expect(job_class).to receive(:batching_scope).and_call_original + + batch_bounds = batching_strategy.next_batch(:namespaces, :id, batch_min_value: namespace4.id, batch_size: 3, job_arguments: [1], job_class: job_class) + + expect(batch_bounds).to eq([namespace4.id, namespace4.id]) + end + end + context 'additional filters' do let(:strategy_with_filters) do Class.new(described_class) do - def apply_additional_filters(relation, job_arguments:) + def apply_additional_filters(relation, job_arguments:, job_class: nil) min_id = job_arguments.first relation.where.not(type: 'Project').where('id >= ?', min_id) diff --git a/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb index 9057c4e99df..7b97dde3808 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb @@ -77,7 +77,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do context 'without tags' do it 'extracts an empty tag list' do - expect(CommitStatus) + expect(Gitlab::Ci::Tags::BulkInsert) .to receive(:bulk_insert_tags!) .with([job]) .and_call_original @@ -95,7 +95,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do end it 'bulk inserts tags' do - expect(CommitStatus) + expect(Gitlab::Ci::Tags::BulkInsert) .to receive(:bulk_insert_tags!) .with([job]) .and_call_original diff --git a/spec/lib/gitlab/ci/tags/bulk_insert_spec.rb b/spec/lib/gitlab/ci/tags/bulk_insert_spec.rb index 063376499e2..72574d50176 100644 --- a/spec/lib/gitlab/ci/tags/bulk_insert_spec.rb +++ b/spec/lib/gitlab/ci/tags/bulk_insert_spec.rb @@ -18,7 +18,7 @@ RSpec.describe Gitlab::Ci::Tags::BulkInsert do let(:error_message) do <<~MESSAGE A mechanism depending on internals of 'act-as-taggable-on` has been designed - to bulk insert tags for Ci::Build records. + to bulk insert tags for Ci::Build/Ci::Runner records. Please review the code carefully before updating the gem version https://gitlab.com/gitlab-org/gitlab/-/issues/350053 MESSAGE @@ -27,6 +27,21 @@ RSpec.describe Gitlab::Ci::Tags::BulkInsert do it { expect(ActsAsTaggableOn::VERSION).to eq(acceptable_version), error_message } end + describe '.bulk_insert_tags!' do + let(:inserter) { instance_double(described_class) } + + it 'delegates to bulk insert class' do + expect(Gitlab::Ci::Tags::BulkInsert) + .to receive(:new) + .with(statuses) + .and_return(inserter) + + expect(inserter).to receive(:insert!) + + described_class.bulk_insert_tags!(statuses) + end + end + describe '#insert!' do context 'without tags' do it { expect(service.insert!).to be_falsey } @@ -45,6 +60,17 @@ RSpec.describe Gitlab::Ci::Tags::BulkInsert do expect(other_job.reload.tag_list).to match_array(%w[tag2 tag3 tag4]) end + it 'persists taggings' do + service.insert! + + expect(job.taggings.size).to eq(2) + expect(other_job.taggings.size).to eq(3) + + expect(Ci::Build.tagged_with('tag1')).to include(job) + expect(Ci::Build.tagged_with('tag2')).to include(job, other_job) + expect(Ci::Build.tagged_with('tag3')).to include(other_job) + end + context 'when batching inserts for tags' do before do stub_const("#{described_class}::TAGS_BATCH_SIZE", 2) @@ -83,6 +109,15 @@ RSpec.describe Gitlab::Ci::Tags::BulkInsert do expect(job.reload.tag_list).to match_array(%w[tag1 tag2]) expect(other_job.reload.tag_list).to be_empty end + + it 'persists taggings' do + service.insert! + + expect(job.taggings.size).to eq(2) + + expect(Ci::Build.tagged_with('tag1')).to include(job) + expect(Ci::Build.tagged_with('tag2')).to include(job) + end end end end diff --git a/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb b/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb index 8819171cfd0..6fe3b22d8bc 100644 --- a/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb +++ b/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb @@ -322,6 +322,7 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m describe '#retry_failed_jobs!' do let(:batched_migration) { create(:batched_background_migration, status: 'failed') } + let(:job_class) { Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJob } subject(:retry_failed_jobs) { batched_migration.retry_failed_jobs! } @@ -335,7 +336,8 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m anything, batch_min_value: 6, batch_size: 5, - job_arguments: batched_migration.job_arguments + job_arguments: batched_migration.job_arguments, + job_class: job_class ).and_return([6, 10]) end end |