Add tests for Geo::RepositoryBackfillService

parent 6789e4fc
......@@ -15,11 +15,7 @@ module Geo
log('Started repository sync')
fetch_repositories do |started_at, finished_at|
log('Tracking sync information')
registry = Geo::ProjectRegistry.find_or_create_by(project_id: project.id)
registry.last_repository_synced_at = started_at
registry.last_repository_successful_sync_at = finished_at if finished_at
registry.save
update_tracking_database(started_at, finished_at)
end
log('Finished repository sync')
......@@ -41,25 +37,35 @@ module Geo
finished_at = nil
begin
fetch_project_repository
fetch_wiki_repository
expire_repository_caches
finished_at = DateTime.now
rescue Gitlab::Shell::Error => e
Rails.logger.error "Error syncing repository for project #{project.path_with_namespace}: #{e}"
end
yield started_at, finished_at
end
def fetch_project_repository
log('Fetching project repository')
project.create_repository unless project.repository_exists?
log('Fetching repository')
project.repository.fetch_geo_mirror(ssh_url_to_repo)
end
def fetch_wiki_repository
# Second .wiki call returns a Gollum::Wiki, and it will always create the physical repository when not found
if project.wiki.wiki.exist?
log('Fetching wiki repository')
project.wiki.repository.fetch_geo_mirror(ssh_url_to_wiki)
end
end
def expire_repository_caches
log('Expiring caches')
project.repository.after_sync
finished_at = DateTime.now
rescue Gitlab::Shell::Error => e
Rails.logger.error "Error syncing repository for project #{project.path_with_namespace}: #{e}"
end
yield started_at, finished_at
end
def try_obtain_lease
......@@ -78,6 +84,14 @@ module Geo
Gitlab::ExclusiveLease.cancel(lease_key, repository_lease)
end
def update_tracking_database(started_at, finished_at)
log('Tracking sync information')
registry = Geo::ProjectRegistry.find_or_create_by(project_id: project.id)
registry.last_repository_synced_at = started_at
registry.last_repository_successful_sync_at = finished_at if finished_at
registry.save
end
def lease_key
@key ||= "#{LEASE_KEY_PREFIX}:#{project.id}"
end
......
require 'spec_helper'
describe Geo::RepositoryBackfillService, services: true do
let(:project) { create(:project) }
let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') }
let(:project) { create(:empty_project) }
subject { Geo::RepositoryBackfillService.new(project) }
subject { described_class.new(project.id, '123456') }
describe '#execute' do
pending { raise 'must be implemented' }
it 'fetches project repositories' do
fetch_count = 0
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
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { true }
expect_any_instance_of(Repository).to receive(:after_sync).once
subject.execute
end
it 'releases leases' do
expect(Gitlab::ExclusiveLease).to receive(:cancel).exactly(2).and_call_original
subject.execute
end
context 'tracking database' do
it 'tracks repository sync' do
expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1)
end
it 'stores last_repository_successful_sync_at when succeed' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { true }
subject.execute
registry = Geo::ProjectRegistry.find_by(project_id: project.id)
expect(registry.last_repository_successful_sync_at).not_to be_nil
end
it 'reset last_repository_successful_sync_at when fail' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { 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
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