diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-19 09:10:05 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-19 09:10:05 +0300 |
commit | 7f12b2dc7406ceb8a9b1bbd730b38ad70fc88405 (patch) | |
tree | c858f594d572c4eafc741425ad04e88eae95269e /spec | |
parent | 7747c98443fd19821e89b3107fd784b417fe6216 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/db/docs_spec.rb | 5 | ||||
-rw-r--r-- | spec/lib/gitlab/database/each_database_spec.rb | 27 | ||||
-rw-r--r-- | spec/lib/gitlab/database/gitlab_schema_spec.rb | 19 | ||||
-rw-r--r-- | spec/lib/gitlab/database/migrations/reestablished_connection_stack_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/database/reindexing_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/database_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/diff/highlight_cache_spec.rb | 19 | ||||
-rw-r--r-- | spec/services/work_items/task_list_reference_removal_service_spec.rb | 6 | ||||
-rw-r--r-- | spec/tasks/dev_rake_spec.rb | 4 | ||||
-rw-r--r-- | spec/tasks/gitlab/db_rake_spec.rb | 13 | ||||
-rw-r--r-- | spec/workers/loose_foreign_keys/cleanup_worker_spec.rb | 2 |
11 files changed, 64 insertions, 37 deletions
diff --git a/spec/db/docs_spec.rb b/spec/db/docs_spec.rb index 20746e107fb..ad3705c3dbe 100644 --- a/spec/db/docs_spec.rb +++ b/spec/db/docs_spec.rb @@ -4,8 +4,11 @@ require 'spec_helper' RSpec.describe 'Database Documentation' do context 'for each table' do + # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/366834 + let(:database_base_models) { Gitlab::Database.database_base_models.select { |k, _| k != 'geo' } } + let(:all_tables) do - Gitlab::Database.database_base_models.flat_map { |_, m| m.connection.tables }.sort.uniq + database_base_models.flat_map { |_, m| m.connection.tables }.sort.uniq end let(:metadata_required_fields) do diff --git a/spec/lib/gitlab/database/each_database_spec.rb b/spec/lib/gitlab/database/each_database_spec.rb index 8345cdfb8fb..2a6eb8f779d 100644 --- a/spec/lib/gitlab/database/each_database_spec.rb +++ b/spec/lib/gitlab/database/each_database_spec.rb @@ -4,9 +4,10 @@ require 'spec_helper' RSpec.describe Gitlab::Database::EachDatabase do describe '.each_database_connection', :add_ci_connection do + let(:database_base_models) { { main: ActiveRecord::Base, ci: Ci::ApplicationRecord }.with_indifferent_access } + before do - allow(Gitlab::Database).to receive(:database_base_models) - .and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord }.with_indifferent_access) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(database_base_models) end it 'yields each connection after connecting SharedModel' do @@ -60,12 +61,20 @@ RSpec.describe Gitlab::Database::EachDatabase do end context 'when shared connections are not included' do + def clear_memoization(key) + Gitlab::Database.remove_instance_variable(key) if Gitlab::Database.instance_variable_defined?(key) + end + + before do + allow(Gitlab::Database).to receive(:database_base_models).and_return(database_base_models) + + # Clear the memoization because the return of Gitlab::Database#schemas_to_base_models depends stubbed value + clear_memoization(:@schemas_to_base_models) + clear_memoization(:@schemas_to_base_models_ee) + end + it 'only yields the unshared connections' do - if Gitlab::Database.has_config?(:ci) - expect(Gitlab::Database).to receive(:db_config_share_with).exactly(3).times.and_return(nil, 'main', 'main') - else - expect(Gitlab::Database).to receive(:db_config_share_with).twice.and_return(nil, 'main') - end + expect(Gitlab::Database).to receive(:db_config_share_with).exactly(3).times.and_return(nil, 'main', 'main') expect { |b| described_class.each_database_connection(include_shared: false, &b) } .to yield_successive_args([ActiveRecord::Base.connection, 'main']) @@ -79,7 +88,7 @@ RSpec.describe Gitlab::Database::EachDatabase do let(:model2) { Class.new(Gitlab::Database::SharedModel) } before do - allow(Gitlab::Database).to receive(:database_base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared) .and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord }.with_indifferent_access) end @@ -136,7 +145,7 @@ RSpec.describe Gitlab::Database::EachDatabase do let(:ci_model) { Class.new(Ci::ApplicationRecord) } before do - allow(Gitlab::Database).to receive(:database_base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared) .and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord }.with_indifferent_access) allow(main_model).to receive_message_chain('connection_db_config.name').and_return('main') diff --git a/spec/lib/gitlab/database/gitlab_schema_spec.rb b/spec/lib/gitlab/database/gitlab_schema_spec.rb index 611b2fbad72..72950895022 100644 --- a/spec/lib/gitlab/database/gitlab_schema_spec.rb +++ b/spec/lib/gitlab/database/gitlab_schema_spec.rb @@ -3,26 +3,27 @@ require 'spec_helper' RSpec.describe Gitlab::Database::GitlabSchema do describe '.tables_to_schema' do - subject { described_class.tables_to_schema } - it 'all tables have assigned a known gitlab_schema' do - is_expected.to all( - match([be_a(String), be_in([:gitlab_internal, :gitlab_shared, :gitlab_main, :gitlab_ci])]) + expect(described_class.tables_to_schema).to all( + match([be_a(String), be_in(Gitlab::Database.schemas_to_base_models.keys.map(&:to_sym))]) ) end # This being run across different databases indirectly also tests # a general consistency of structure across databases - Gitlab::Database.database_base_models.each do |db_config_name, db_class| - let(:db_data_sources) { db_class.connection.data_sources } - + Gitlab::Database.database_base_models.select { |k, _| k != 'geo' }.each do |db_config_name, db_class| context "for #{db_config_name} using #{db_class}" do + let(:db_data_sources) { db_class.connection.data_sources } + + # The Geo database does not share the same structure as all decomposed databases + subject { described_class.tables_to_schema.select { |_, v| v != :gitlab_geo } } + it 'new data sources are added' do missing_tables = db_data_sources.to_set - subject.keys expect(missing_tables).to be_empty, \ "Missing table(s) #{missing_tables.to_a} not found in #{described_class}.tables_to_schema. " \ - "Any new tables must be added to lib/gitlab/database/gitlab_schemas.yml." + "Any new tables must be added to #{described_class::GITLAB_SCHEMAS_FILE}." end it 'non-existing data sources are removed' do @@ -30,7 +31,7 @@ RSpec.describe Gitlab::Database::GitlabSchema do expect(extra_tables).to be_empty, \ "Extra table(s) #{extra_tables.to_a} found in #{described_class}.tables_to_schema. " \ - "Any removed or renamed tables must be removed from lib/gitlab/database/gitlab_schemas.yml." + "Any removed or renamed tables must be removed from #{described_class::GITLAB_SCHEMAS_FILE}." end end end diff --git a/spec/lib/gitlab/database/migrations/reestablished_connection_stack_spec.rb b/spec/lib/gitlab/database/migrations/reestablished_connection_stack_spec.rb index d197f39be40..c6327de98d1 100644 --- a/spec/lib/gitlab/database/migrations/reestablished_connection_stack_spec.rb +++ b/spec/lib/gitlab/database/migrations/reestablished_connection_stack_spec.rb @@ -11,7 +11,7 @@ RSpec.describe Gitlab::Database::Migrations::ReestablishedConnectionStack do end describe '#with_restored_connection_stack' do - Gitlab::Database.database_base_models.each do |db_config_name, _| + Gitlab::Database.database_base_models_with_gitlab_shared.each do |db_config_name, _| context db_config_name do it_behaves_like "reconfigures connection stack", db_config_name do it 'does restore connection hierarchy' do diff --git a/spec/lib/gitlab/database/reindexing_spec.rb b/spec/lib/gitlab/database/reindexing_spec.rb index 0c576505e07..976b9896dfa 100644 --- a/spec/lib/gitlab/database/reindexing_spec.rb +++ b/spec/lib/gitlab/database/reindexing_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Gitlab::Database::Reindexing do include Database::DatabaseHelpers describe '.invoke' do - let(:databases) { Gitlab::Database.database_base_models } + let(:databases) { Gitlab::Database.database_base_models_with_gitlab_shared } let(:databases_count) { databases.count } it 'cleans up any leftover indexes' do diff --git a/spec/lib/gitlab/database_spec.rb b/spec/lib/gitlab/database_spec.rb index 064613074cd..452a662bdcb 100644 --- a/spec/lib/gitlab/database_spec.rb +++ b/spec/lib/gitlab/database_spec.rb @@ -337,7 +337,7 @@ RSpec.describe Gitlab::Database do let(:model2) { Class.new(base_model) } before do - allow(described_class).to receive(:database_base_models) + allow(described_class).to receive(:database_base_models_using_load_balancing) .and_return({ model1: model1, model2: model2 }.with_indifferent_access) end diff --git a/spec/lib/gitlab/diff/highlight_cache_spec.rb b/spec/lib/gitlab/diff/highlight_cache_spec.rb index e643b58ee32..5350dda5fb2 100644 --- a/spec/lib/gitlab/diff/highlight_cache_spec.rb +++ b/spec/lib/gitlab/diff/highlight_cache_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' RSpec.describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do - let(:merge_request) { create(:merge_request_with_diffs) } + let_it_be(:merge_request) { create(:merge_request_with_diffs) } + let(:diff_hash) do { ".gitignore-false-false-false" => [{ line_code: nil, rich_text: nil, text: "@@ -17,3 +17,4 @@ rerun.txt", type: "match", index: 0, old_pos: 17, new_pos: 17 }, @@ -229,10 +230,10 @@ RSpec.describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do end describe 'metrics' do - let(:transaction) { Gitlab::Metrics::WebTransaction.new({} ) } + let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) } before do - allow(cache).to receive(:current_transaction).and_return(transaction) + allow(::Gitlab::Metrics::WebTransaction).to receive(:current).and_return(transaction) end it 'observes :gitlab_redis_diff_caching_memory_usage_bytes' do @@ -241,6 +242,18 @@ RSpec.describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do cache.write_if_empty end + + it 'records hit ratio metrics' do + expect(transaction) + .to receive(:increment).with(:gitlab_redis_diff_caching_requests_total).exactly(5).times + expect(transaction) + .to receive(:increment).with(:gitlab_redis_diff_caching_hits_total).exactly(4).times + + 5.times do + cache = described_class.new(merge_request.diffs) + cache.write_if_empty + end + end end describe '#key' do diff --git a/spec/services/work_items/task_list_reference_removal_service_spec.rb b/spec/services/work_items/task_list_reference_removal_service_spec.rb index 88fabe5fe40..91b7814ae92 100644 --- a/spec/services/work_items/task_list_reference_removal_service_spec.rb +++ b/spec/services/work_items/task_list_reference_removal_service_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' RSpec.describe WorkItems::TaskListReferenceRemovalService do let_it_be(:developer) { create(:user) } let_it_be(:project) { create(:project, :repository).tap { |project| project.add_developer(developer) } } - let_it_be(:task) { create(:work_item, project: project) } + let_it_be(:task) { create(:work_item, project: project, title: 'Task title') } let_it_be(:single_line_work_item, refind: true) do create(:work_item, project: project, description: "- [ ] #{task.to_reference}+ single line") end @@ -82,7 +82,7 @@ RSpec.describe WorkItems::TaskListReferenceRemovalService do let(:line_number_end) { 1 } let(:work_item) { single_line_work_item } - it_behaves_like 'successful work item task reference removal service', '- [ ] My title 1 single line' + it_behaves_like 'successful work item task reference removal service', '- [ ] Task title single line' context 'when description does not contain a task' do let_it_be(:no_matching_work_item) { create(:work_item, project: project, description: 'no matching task') } @@ -103,7 +103,7 @@ RSpec.describe WorkItems::TaskListReferenceRemovalService do context 'when task mardown spans multiple lines' do it_behaves_like 'successful work item task reference removal service', - "Any text\n\n* [ ] Item to be converted\n My title 1 second line\n third line\n* [x] task\n\nMore text" + "Any text\n\n* [ ] Item to be converted\n Task title second line\n third line\n* [x] task\n\nMore text" end context 'when updating the work item fails' do diff --git a/spec/tasks/dev_rake_spec.rb b/spec/tasks/dev_rake_spec.rb index 41728faa0e0..14a5ccfa323 100644 --- a/spec/tasks/dev_rake_spec.rb +++ b/spec/tasks/dev_rake_spec.rb @@ -23,7 +23,7 @@ RSpec.describe 'dev rake tasks' do subject(:setup_task) { run_rake_task('dev:setup') } - let(:connections) { Gitlab::Database.database_base_models.values.map(&:connection) } + let(:connections) { Gitlab::Database.database_base_models_with_gitlab_shared.values.map(&:connection) } it 'sets up the development environment', :aggregate_failures do expect(Rake::Task['gitlab:setup']).to receive(:invoke) @@ -55,7 +55,7 @@ RSpec.describe 'dev rake tasks' do end let(:connections) do - Gitlab::Database.database_base_models.values.filter_map do |model| + Gitlab::Database.database_base_models_with_gitlab_shared.values.filter_map do |model| model.connection if Gitlab::Database.db_config_share_with(model.connection_db_config).nil? end end diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb index d8199c09ca1..74bec406947 100644 --- a/spec/tasks/gitlab/db_rake_spec.rb +++ b/spec/tasks/gitlab/db_rake_spec.rb @@ -45,7 +45,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do before do skip_unless_ci_uses_database_tasks - allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(base_models) end it 'marks the migration complete on each database' do @@ -90,7 +90,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do let(:base_models) { { 'main' => main_model } } before do - allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(base_models) end it 'prints a warning message' do @@ -110,7 +110,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do let(:base_models) { { 'main' => main_model } } before do - allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(base_models) end it 'prints an error and exits' do @@ -136,6 +136,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do context 'when geo is not configured' do before do allow(ActiveRecord::Base).to receive_message_chain('configurations.configs_for').and_return([main_config]) + allow(Gitlab::Database).to receive(:has_config?).with(:geo).and_return(false) end context 'when the schema is already loaded' do @@ -260,7 +261,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do before do skip_unless_ci_uses_database_tasks - allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(base_models) end context 'when geo is not configured' do @@ -444,7 +445,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do before do skip_unless_ci_uses_database_tasks - allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(base_models) allow(main_model.connection).to receive(:table_exists?).with('schema_migrations').and_return(true) allow(ci_model.connection).to receive(:table_exists?).with('schema_migrations').and_return(true) @@ -574,7 +575,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do before do skip_if_multiple_databases_not_setup - allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(base_models) end it 'delegates to Gitlab::Database::Reindexing without a specific database' do diff --git a/spec/workers/loose_foreign_keys/cleanup_worker_spec.rb b/spec/workers/loose_foreign_keys/cleanup_worker_spec.rb index 632e4fb3071..77190dc49d9 100644 --- a/spec/workers/loose_foreign_keys/cleanup_worker_spec.rb +++ b/spec/workers/loose_foreign_keys/cleanup_worker_spec.rb @@ -169,7 +169,7 @@ RSpec.describe LooseForeignKeys::CleanupWorker do let(:expected_connection) { expected_connection_model.constantize.connection } before do - allow(Gitlab::Database).to receive(:database_base_models).and_return(database_base_models) + allow(Gitlab::Database).to receive(:database_base_models_with_gitlab_shared).and_return(database_base_models) if database_base_models.has_key?(:ci) Gitlab::Database::SharedModel.using_connection(database_base_models[:ci].connection) do |