Commit a12ca22a authored by Michael Kozono's avatar Michael Kozono

Refactor ProjectRegistryFinder spec

In order to consistently test it with and without FDW enabled.
parent e4c31f25
......@@ -24,7 +24,7 @@ module Geo
if use_legacy_queries?
legacy_find_synced_wikis
else
fdw_find_enabled_wikis
fdw_find_synced_wikis
end
relation.count
......@@ -158,7 +158,7 @@ module Geo
end
# @return [ActiveRecord::Relation<Geo::ProjectRegistry>]
def fdw_find_enabled_wikis
def fdw_find_synced_wikis
Geo::ProjectRegistry.synced_wikis.where(fdw_enabled_wikis)
end
......
......@@ -20,6 +20,7 @@ describe Geo::ProjectRegistryFinder, :geo do
stub_current_geo_node(secondary)
end
shared_examples 'counts all the things' do
describe '#count_synced_repositories' do
it 'delegates to #find_synced_repositories' do
expect(subject).to receive(:find_synced_repositories).and_call_original
......@@ -36,6 +37,14 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.count_synced_repositories).to eq 2
end
it 'counts synced wikis with nil wiki_access_level (which means enabled wiki)' do
project_synced.project_feature.update!(wiki_access_level: nil)
create(:geo_project_registry, :synced, project: project_synced)
expect(subject.count_synced_wikis).to eq 1
end
context 'with selective sync' do
before do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
......@@ -61,12 +70,8 @@ describe Geo::ProjectRegistryFinder, :geo do
end
describe '#count_synced_wikis' do
before do
allow(subject).to receive(:use_legacy_queries?).and_return(true)
end
it 'delegates to #legacy_find_synced_wikis' do
expect(subject).to receive(:legacy_find_synced_wikis).and_call_original
it 'delegates to the correct method' do
expect(subject).to receive("#{method_prefix}_find_synced_wikis".to_sym).and_call_original
subject.count_synced_wikis
end
......@@ -80,17 +85,17 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.count_synced_wikis).to eq 2
end
it 'does not count disabled wikis' do
it 'counts synced wikis with nil wiki_access_level (which means enabled wiki)' do
project_synced.project_feature.update!(wiki_access_level: nil)
create(:geo_project_registry, :synced, project: project_synced)
create(:geo_project_registry, :synced, project: create(:project, :wiki_disabled))
expect(subject.count_synced_wikis).to eq 1
end
it 'counts synced wikis with nil wiki_access_level (which means enabled wiki)' do
project_synced.project_feature.update!(wiki_access_level: nil)
it 'does not count disabled wikis' do
create(:geo_project_registry, :synced, project: project_synced)
create(:geo_project_registry, :synced, project: create(:project, :wiki_disabled))
expect(subject.count_synced_wikis).to eq 1
end
......@@ -208,20 +213,9 @@ describe Geo::ProjectRegistryFinder, :geo do
subject.count_verified_repositories
end
it 'counts projects that verified' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verified, project: build(:project))
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
it 'delegates to #legacy_find_verified_repositories when use_legacy_queries is true' do
expect(subject).to receive(:use_legacy_queries?).and_return(true)
expect(subject.count_verified_repositories).to eq 2
end
context 'with legacy queries' do
before do
allow(subject).to receive(:use_legacy_queries?).and_return(true)
end
it 'delegates to #legacy_find_verified_repositories' do
expect(subject).to receive(:legacy_find_verified_repositories).and_call_original
subject.count_verified_repositories
......@@ -235,15 +229,10 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.count_verified_repositories).to eq 2
end
end
end
describe '#count_verified_wikis' do
before do
allow(subject).to receive(:use_legacy_queries?).and_return(true)
end
it 'delegates to #legacy_find_synced_wikis' do
expect(subject).to receive(:legacy_find_verified_wikis).and_call_original
it 'delegates to the correct method' do
expect(subject).to receive("#{method_prefix}_find_verified_wikis".to_sym).and_call_original
subject.count_verified_wikis
end
......@@ -271,6 +260,22 @@ describe Geo::ProjectRegistryFinder, :geo do
subject.count_verification_failed_repositories
end
it 'delegates to #legacy_find_filtered_verification_failed_projects when use_legacy_queries is true' do
expect(subject).to receive(:use_legacy_queries?).and_return(true)
expect(subject).to receive(:legacy_find_filtered_verification_failed_projects).with('repository').and_call_original
subject.count_verification_failed_repositories
end
it 'delegates to #find_filtered_verification_failed_project_registries when use_legacy_queries is false' do
expect(subject).to receive(:use_legacy_queries?).and_return(false)
expect(subject).to receive(:find_filtered_verification_failed_project_registries).with('repository').and_call_original
subject.count_verification_failed_repositories
end
it 'counts projects that verification has failed' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
......@@ -280,17 +285,6 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.count_verification_failed_repositories).to eq 1
end
context 'with legacy queries' do
before do
allow(subject).to receive(:use_legacy_queries?).and_return(true)
end
it 'delegates to #legacy_find_filtered_verification_failed_projects' do
expect(subject).to receive(:legacy_find_filtered_verification_failed_projects).and_call_original
subject.find_verification_failed_project_registries('repository')
end
it 'counts projects that verification has failed' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
......@@ -300,7 +294,6 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.count_verification_failed_repositories).to eq 1
end
end
end
describe '#count_verification_failed_wikis' do
it 'delegates to #find_verification_failed_project_registries' do
......@@ -309,6 +302,22 @@ describe Geo::ProjectRegistryFinder, :geo do
subject.count_verification_failed_wikis
end
it 'delegates to #legacy_find_filtered_verification_failed_projects when use_legacy_queries is true' do
expect(subject).to receive(:use_legacy_queries?).and_return(true)
expect(subject).to receive(:legacy_find_filtered_verification_failed_projects).with('wiki').and_call_original
subject.count_verification_failed_wikis
end
it 'delegates to #find_filtered_verification_failed_project_registries when use_legacy_queries is false' do
expect(subject).to receive(:use_legacy_queries?).and_return(false)
expect(subject).to receive(:find_filtered_verification_failed_project_registries).with('wiki').and_call_original
subject.count_verification_failed_wikis
end
it 'counts projects that verification has failed' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
......@@ -317,25 +326,89 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.count_verification_failed_wikis).to eq 1
end
end
end
context 'with legacy queries' do
shared_examples 'finds all the things' do
describe '#find_unsynced_projects' do
it 'delegates to the correct method' do
expect(subject).to receive("#{method_prefix}_find_unsynced_projects".to_sym).and_call_original
subject.find_unsynced_projects(batch_size: 10)
end
it 'returns projects without an entry on the tracking database' do
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
projects = subject.find_unsynced_projects(batch_size: 10)
expect(projects).to match_ids(project_not_synced)
end
context 'with selective sync' do
before do
allow(subject).to receive(:use_legacy_queries?).and_return(true)
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
end
it 'delegates to #legacy_find_filtered_verification_failed_projects' do
expect(subject).to receive(:legacy_find_filtered_verification_failed_projects).and_call_original
it 'delegates to #legacy_find_unsynced_projects' do
expect(subject).to receive(:legacy_find_unsynced_projects).and_call_original
subject.find_verification_failed_project_registries('wiki')
subject.find_unsynced_projects(batch_size: 10)
end
it 'counts projects that verification has failed' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
create(:geo_project_registry, :wiki_verified, project: project_wiki_verified)
create(:geo_project_registry, :wiki_verification_failed, project: project_wiki_verification_failed)
it 'returns untracked projects in the synced group' do
project_1_in_synced_group = create(:project, group: synced_group)
project_2_in_synced_group = create(:project, group: synced_group)
expect(subject.count_verification_failed_wikis).to eq 1
create(:geo_project_registry, :sync_failed, project: project_1_in_synced_group)
projects = subject.find_unsynced_projects(batch_size: 10)
expect(projects).to match_ids(project_2_in_synced_group)
end
end
end
describe '#find_projects_updated_recently' do
it 'delegates to the correct method' do
expect(subject).to receive("#{method_prefix}_find_projects_updated_recently".to_sym).and_call_original
subject.find_projects_updated_recently(batch_size: 10)
end
it 'returns projects with a dirty entry on the tracking database' do
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty)
projects = subject.find_projects_updated_recently(batch_size: 10)
expect(projects).to match_ids([project_repository_dirty, project_wiki_dirty])
end
context 'with selective sync' do
before do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
end
it 'delegates to #legacy_find_projects_updated_recently' do
expect(subject).to receive(:legacy_find_projects_updated_recently).and_call_original
subject.find_projects_updated_recently(batch_size: 10)
end
it 'returns dirty projects in the synced group' do
project_1_in_synced_group = create(:project, group: synced_group)
project_2_in_synced_group = create(:project, group: synced_group)
project_3_in_synced_group = create(:project, group: synced_group)
create(:project, group: synced_group)
create(:geo_project_registry, :synced, :repository_dirty, project: project_1_in_synced_group)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_2_in_synced_group)
create(:geo_project_registry, :synced, project: project_3_in_synced_group)
projects = subject.find_projects_updated_recently(batch_size: 10)
expect(projects).to match_ids(project_1_in_synced_group, project_2_in_synced_group)
end
end
end
......@@ -392,7 +465,13 @@ describe Geo::ProjectRegistryFinder, :geo do
end
end
shared_examples 'find outdated registries for repositories/wikis' do
describe '#find_registries_to_verify' do
it 'delegates to the correct method' do
expect(subject).to receive("#{method_prefix}_find_registries_to_verify".to_sym).and_call_original
subject.find_registries_to_verify(batch_size: 10)
end
it 'does not return registries that are verified on primary and secondary' do
project_verified = create(:repository_state, :repository_verified, :wiki_verified).project
repository_verified = create(:repository_state, :repository_verified).project
......@@ -477,6 +556,7 @@ describe Geo::ProjectRegistryFinder, :geo do
expect(subject.find_registries_to_verify(batch_size: 100)).to be_empty
end
end
end
# Disable transactions via :delete method because a foreign table
# can't see changes inside a transaction of a different connection.
......@@ -485,91 +565,10 @@ describe Geo::ProjectRegistryFinder, :geo do
skip('FDW is not configured') if Gitlab::Database.postgresql? && !Gitlab::Geo::Fdw.enabled?
end
describe '#fdw_find_enabled_wikis' do
it 'does not count disabled wikis' do
expect(subject).to receive(:fdw_find_enabled_wikis).and_call_original
create(:geo_project_registry, :synced, project: project_synced)
create(:geo_project_registry, :synced, project: create(:project, :wiki_disabled))
expect(subject.count_synced_wikis).to eq 1
end
it 'counts synced wikis with nil wiki_access_level (which means enabled wiki)' do
project_synced.project_feature.update!(wiki_access_level: nil)
create(:geo_project_registry, :synced, project: project_synced)
expect(subject.count_synced_wikis).to eq 1
end
end
describe '#fdw_find_verified_wikis' do
it 'does not count disabled wikis' do
expect(subject).to receive(:fdw_find_verified_wikis).and_call_original
create(:geo_project_registry, :wiki_verified, project: project_wiki_verified)
create(:geo_project_registry, :wiki_verified, project: create(:project, :wiki_disabled))
expect(subject.count_verified_wikis).to eq 1
end
end
describe '#find_unsynced_projects' do
it 'delegates to #fdw_find_unsynced_projects' do
expect(subject).to receive(:fdw_find_unsynced_projects).and_call_original
subject.find_unsynced_projects(batch_size: 10)
end
it 'delegates to #legacy_find_unsynced_projects when node has selective sync' do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
expect(subject).to receive(:legacy_find_unsynced_projects).and_call_original
subject.find_unsynced_projects(batch_size: 10)
end
it 'returns projects without an entry on the tracking database' do
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
projects = subject.find_unsynced_projects(batch_size: 10)
expect(projects.count).to eq(1)
expect(projects.first.id).to eq(project_not_synced.id)
end
end
describe '#find_projects_updated_recently' do
it 'delegates to #fdw_find_projects_updated_recently' do
expect(subject).to receive(:fdw_find_projects_updated_recently).and_call_original
include_examples 'counts all the things'
subject.find_projects_updated_recently(batch_size: 10)
end
it 'delegates to #legacy_find_projects_updated_recently when node has selective sync' do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
expect(subject).to receive(:legacy_find_projects_updated_recently).and_call_original
subject.find_projects_updated_recently(batch_size: 10)
end
it 'returns projects with a dirty entry on the tracking database' do
project_repository_dirty = create(:project)
project_wiki_dirty = create(:project)
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty)
projects = subject.find_projects_updated_recently(batch_size: 10)
expect(projects.pluck(:id)).to match_array([project_repository_dirty.id, project_wiki_dirty.id])
end
end
describe '#find_registries_to_verify' do
include_examples 'find outdated registries for repositories/wikis'
include_examples 'finds all the things' do
let(:method_prefix) { 'fdw' }
end
end
......@@ -578,41 +577,10 @@ describe Geo::ProjectRegistryFinder, :geo do
allow(Gitlab::Geo::Fdw).to receive(:enabled?).and_return(false)
end
describe '#find_unsynced_projects' do
it 'delegates to #legacy_find_unsynced_projects' do
expect(subject).to receive(:legacy_find_unsynced_projects).and_call_original
subject.find_unsynced_projects(batch_size: 10)
end
it 'returns projects without an entry on the tracking database' do
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
include_examples 'counts all the things'
projects = subject.find_unsynced_projects(batch_size: 10)
expect(projects).to match_array([project_not_synced])
end
end
describe '#find_projects_updated_recently' do
it 'delegates to #legacy_find_projects_updated_recently' do
expect(subject).to receive(:legacy_find_projects_updated_recently).and_call_original
subject.find_projects_updated_recently(batch_size: 10)
end
it 'returns projects with a dirty entry on the tracking database' do
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty)
projects = subject.find_projects_updated_recently(batch_size: 10)
expect(projects.pluck(:id)).to match_array([project_repository_dirty.id, project_wiki_dirty.id])
end
end
describe '#find_registries_to_verify' do
include_examples 'find outdated registries for repositories/wikis'
include_examples 'finds all the things' do
let(:method_prefix) { 'legacy' }
end
end
end
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