diff options
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/geo/repository_sync_service_spec.rb | 297 | ||||
-rw-r--r-- | spec/services/geo/wiki_sync_service_spec.rb | 102 |
2 files changed, 156 insertions, 243 deletions
diff --git a/spec/services/geo/repository_sync_service_spec.rb b/spec/services/geo/repository_sync_service_spec.rb index 0d40cdc5d80..f1c9e7b8469 100644 --- a/spec/services/geo/repository_sync_service_spec.rb +++ b/spec/services/geo/repository_sync_service_spec.rb @@ -1,14 +1,14 @@ require 'spec_helper' -describe Geo::RepositorySyncService, services: true do +RSpec.describe Geo::RepositorySyncService, services: true do let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') } let(:lease) { double(try_obtain: true) } - subject { described_class.new(project.id) } + subject { described_class.new(project) } before do allow(Gitlab::ExclusiveLease).to receive(:new) - .with(subject.__send__(:lease_key), anything) + .with(subject.lease_key, anything) .and_return(lease) allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) @@ -16,285 +16,96 @@ describe Geo::RepositorySyncService, services: true do end describe '#execute' do - context 'when project has never been synced' do - let(:project) { create(:project_empty_repo) } + let(:project) { create(:project_empty_repo) } + let(:repository) { project.repository } + let(:url_to_repo) { "#{primary.clone_url_prefix}#{project.path_with_namespace}.git" } - it 'fetches project repositories' do - fetch_count = 0 + it 'fetches project repository' do + expect(repository).to receive(:fetch_geo_mirror).with(url_to_repo).once - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) do - fetch_count += 1 - end - - subject.execute - - expect(fetch_count).to eq 2 - end - - it 'expires repository caches' do - expect_any_instance_of(Repository).to receive(:expire_all_method_caches).once - expect_any_instance_of(Repository).to receive(:expire_branch_cache).once - expect_any_instance_of(Repository).to receive(:expire_content_cache).once - - subject.execute - end - - it 'releases lease' do - expect(Gitlab::ExclusiveLease).to receive(:cancel).once.with( - subject.__send__(:lease_key), anything).and_call_original - - subject.execute - end - - it 'does not fetch project repositories if cannot obtain a lease' do - allow(lease).to receive(:try_obtain) { false } - - expect_any_instance_of(Repository).not_to receive(:fetch_geo_mirror) - - subject.execute - end - - context 'tracking database' do - it 'creates a new registry' do - expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1) - end - - it 'sets last_repository_successful_sync_at when repository sync succeed' do - subject.execute - - registry = Geo::ProjectRegistry.find_by(project_id: project.id) - - expect(registry.last_repository_successful_sync_at).not_to be_nil - end - - it 'resets last_repository_successful_sync_at when repository sync fail' do - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.git/) { raise Gitlab::Shell::Error } - - subject.execute - - registry = Geo::ProjectRegistry.find_by(project_id: project.id) - - expect(registry.last_repository_successful_sync_at).to be_nil - end - - it 'sets last_wiki_successful_sync_at when wiki sync succeed' do - subject.execute - - registry = Geo::ProjectRegistry.find_by(project_id: project.id) - - expect(registry.last_wiki_successful_sync_at).not_to be_nil - end - - it 'resets last_wiki_successful_sync_at when wiki sync fail' do - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.wiki.git/) { raise Gitlab::Shell::Error } - - subject.execute - - registry = Geo::ProjectRegistry.find_by(project_id: project.id) - - expect(registry.last_wiki_successful_sync_at).to be_nil - end - end + subject.execute end - context 'when project has been synced' do - let(:project) { create(:project) } - let(:last_repository_synced_at) { 5.days.ago } - let(:last_wiki_synced_at) { 4.days.ago } - - let!(:registry) do - create(:geo_project_registry, :synced, - project: project, - last_repository_synced_at: last_repository_synced_at, - last_repository_successful_sync_at: last_repository_synced_at, - last_wiki_synced_at: last_wiki_synced_at, - last_wiki_successful_sync_at: last_wiki_synced_at) - end - - it 'does not fetch project repositories' do - expect_any_instance_of(Repository).not_to receive(:fetch_geo_mirror) - - subject.execute - end - - context 'tracking database' do - it 'does not create a new registry' do - expect { subject.execute }.not_to change(Geo::ProjectRegistry, :count) - end - - it 'does not update last_repository_successful_sync_at' do - subject.execute - - registry.reload - - expect(registry.last_repository_synced_at).to be_within(1.minute).of(last_repository_synced_at) - expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at) - end - - it 'does not update last_wiki_successful_sync_at' do - subject.execute - - registry.reload + it 'expires repository caches' do + expect_any_instance_of(Repository).to receive(:expire_all_method_caches).once + expect_any_instance_of(Repository).to receive(:expire_branch_cache).once + expect_any_instance_of(Repository).to receive(:expire_content_cache).once - expect(registry.last_wiki_synced_at).to be_within(1.minute).of(last_wiki_synced_at) - expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(last_wiki_synced_at) - end - end + subject.execute end - context 'when last attempt to sync project repositories failed' do - let(:project) { create(:project) } - let!(:registry) { create(:geo_project_registry, :sync_failed, project: project) } - - it 'fetches project repositories' do - fetch_count = 0 + it 'releases lease' do + expect(Gitlab::ExclusiveLease).to receive(:cancel).once.with( + subject.__send__(:lease_key), anything).and_call_original - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) do - fetch_count += 1 - end + subject.execute + end - subject.execute + it 'does not fetch project repository if cannot obtain a lease' do + allow(lease).to receive(:try_obtain) { false } - expect(fetch_count).to eq 2 - end + expect(repository).not_to receive(:fetch_geo_mirror) - context 'tracking database' do - it 'sets last_repository_successful_sync_at' do - subject.execute + subject.execute + end - registry.reload + it 'rescues when Gitlab::Shell::Error is raised' do + allow(repository).to receive(:fetch_geo_mirror).with(url_to_repo) { raise Gitlab::Shell::Error } - expect(registry.last_repository_successful_sync_at).not_to be_nil - end + expect { subject.execute }.not_to raise_error + end - it 'sets last_wiki_successful_sync_at' do - subject.execute + it 'rescues exception and fires after_create hook when Gitlab::Git::Repository::NoRepository is raised' do + allow(repository).to receive(:fetch_geo_mirror).with(url_to_repo) { raise Gitlab::Git::Repository::NoRepository } - registry.reload + expect(repository).to receive(:after_create) - expect(registry.last_wiki_successful_sync_at).not_to be_nil - end - end + expect { subject.execute }.not_to raise_error end - context 'when project repository is dirty' do - let(:project) { create(:project) } - let(:last_wiki_synced_at) { 4.days.ago } - - let!(:registry) do - create(:geo_project_registry, :synced, :repository_dirty, - project: project, - last_wiki_synced_at: last_wiki_synced_at, - last_wiki_successful_sync_at: last_wiki_synced_at) + context 'tracking database' do + it 'creates a new registry if does not exists' do + expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1) end - it 'fetches project repository' do - expect_any_instance_of(Repository).to receive(:fetch_geo_mirror).once + it 'does not create a new registry if one exist' do + create(:geo_project_registry, project: project) - subject.execute + expect { subject.execute }.not_to change(Geo::ProjectRegistry, :count) end - context 'exceptions' do - it 'rescues when Gitlab::Shell::Error is raised' do - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.git/) { raise Gitlab::Shell::Error } - - expect { subject.execute }.not_to raise_error - end - - it 'rescues exception and fires after_create hook when Gitlab::Git::Repository::NoRepository is raised' do - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.git/) { raise Gitlab::Git::Repository::NoRepository } - - expect_any_instance_of(Repository).to receive(:after_create) + context 'when repository sync succeed' do + let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) } - expect { subject.execute }.not_to raise_error - end - end - - context 'tracking database' do - it 'updates last_repository_successful_sync_at' do + before do subject.execute - - registry.reload - - expect(registry.last_repository_synced_at).to be_within(1.minute).of(DateTime.now) - expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(DateTime.now) end - it 'does not update last_wiki_successful_sync_at' do - subject.execute - - registry.reload - - expect(registry.last_wiki_synced_at).to be_within(1.minute).of(last_wiki_synced_at) - expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(last_wiki_synced_at) + it 'sets last_repository_synced_at' do + expect(registry.last_repository_synced_at).not_to be_nil end - it 'resets resync_repository' do - subject.execute - - registry.reload - - expect(registry.resync_repository).to be false + it 'sets last_repository_successful_sync_at' do + expect(registry.last_repository_successful_sync_at).not_to be_nil end end - end - - context 'when project wiki is dirty' do - let(:project) { create(:project) } - let(:last_repository_synced_at) { 5.days.ago } - - let!(:registry) do - create(:geo_project_registry, :synced, :wiki_dirty, - project: project, - last_repository_synced_at: last_repository_synced_at, - last_repository_successful_sync_at: last_repository_synced_at) - end - it 'fetches wiki repository' do - expect_any_instance_of(Repository).to receive(:fetch_geo_mirror).once + context 'when repository sync fail' do + let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) } + let(:url_to_repo) { "#{primary.clone_url_prefix}#{project.path_with_namespace}.git" } - subject.execute - end - - context 'exceptions' do - it 'rescues exception when Gitlab::Shell::Error is raised' do - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.wiki\.git/) { raise Gitlab::Shell::Error } - - expect { subject.execute }.not_to raise_error - end + before do + allow(repository).to receive(:fetch_geo_mirror).with(url_to_repo) { raise Gitlab::Shell::Error } - it 'rescues exception when Gitlab::Git::Repository::NoRepository is raised' do - allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.wiki\.git/) { raise Gitlab::Git::Repository::NoRepository } - - expect { subject.execute }.not_to raise_error - end - end - - context 'tracking database' do - it 'updates last_wiki_successful_sync_at' do subject.execute - - registry.reload - - expect(registry.last_wiki_synced_at).to be_within(1.minute).of(DateTime.now) - expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(DateTime.now) end - it 'does not update last_repository_successful_sync_at' do - subject.execute - - registry.reload - - expect(registry.last_repository_synced_at).to be_within(1.minute).of(last_repository_synced_at) - expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at) + it 'sets last_repository_synced_at' do + expect(registry.last_repository_synced_at).not_to be_nil end - it 'resets resync_wiki' do - subject.execute - - registry.reload - - expect(registry.resync_wiki).to be false + it 'resets last_repository_successful_sync_at' do + expect(registry.last_repository_successful_sync_at).to be_nil end end end diff --git a/spec/services/geo/wiki_sync_service_spec.rb b/spec/services/geo/wiki_sync_service_spec.rb new file mode 100644 index 00000000000..f01cbb2393d --- /dev/null +++ b/spec/services/geo/wiki_sync_service_spec.rb @@ -0,0 +1,102 @@ +require 'spec_helper' + +RSpec.describe Geo::WikiSyncService, services: true do + let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') } + let(:lease) { double(try_obtain: true) } + + subject { described_class.new(project) } + + before do + allow(Gitlab::ExclusiveLease).to receive(:new) + .with(subject.lease_key, anything) + .and_return(lease) + + allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) + .and_return(true) + end + + describe '#execute' do + let(:project) { create(:project_empty_repo) } + let(:repository) { project.wiki.repository } + let(:url_to_repo) { "#{primary.clone_url_prefix}#{project.path_with_namespace}.wiki.git" } + + it 'fetches wiki repository' do + expect(repository).to receive(:fetch_geo_mirror).with(url_to_repo).once + + subject.execute + end + + it 'releases lease' do + expect(Gitlab::ExclusiveLease).to receive(:cancel).once.with( + subject.__send__(:lease_key), anything).and_call_original + + subject.execute + end + + it 'does not fetch wiki repository if cannot obtain a lease' do + allow(lease).to receive(:try_obtain) { false } + + expect(repository).not_to receive(:fetch_geo_mirror) + + subject.execute + end + + it 'rescues exception when Gitlab::Shell::Error is raised' do + allow(repository).to receive(:fetch_geo_mirror).with(url_to_repo) { raise Gitlab::Shell::Error } + + expect { subject.execute }.not_to raise_error + end + + it 'rescues exception when Gitlab::Git::Repository::NoRepository is raised' do + allow(repository).to receive(:fetch_geo_mirror).with(url_to_repo) { raise Gitlab::Git::Repository::NoRepository } + + expect { subject.execute }.not_to raise_error + end + + context 'tracking database' do + it 'creates a new registry if does not exists' do + expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1) + end + + it 'does not create a new registry if one exists' do + create(:geo_project_registry, project: project) + + expect { subject.execute }.not_to change(Geo::ProjectRegistry, :count) + end + + context 'when repository sync succeed' do + let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) } + + before do + subject.execute + end + + it 'sets last_wiki_synced_at' do + expect(registry.last_wiki_synced_at).not_to be_nil + end + + it 'sets last_wiki_successful_sync_at' do + expect(registry.last_wiki_successful_sync_at).not_to be_nil + end + end + + context 'when wiki sync fail' do + let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) } + + before do + allow(repository).to receive(:fetch_geo_mirror).with(url_to_repo) { raise Gitlab::Shell::Error } + + subject.execute + end + + it 'sets last_wiki_synced_at' do + expect(registry.last_wiki_synced_at).not_to be_nil + end + + it 'resets last_wiki_successful_sync_at' do + expect(registry.last_wiki_successful_sync_at).to be_nil + end + end + end + end +end |