2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Projects::UpdateRemoteMirrorService do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:remote_project) { create(:forked_project_with_submodules) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:remote_mirror) { create(:remote_mirror, project: project, enabled: true) }
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:remote_name) { remote_mirror.remote_name }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
subject(:service) { described_class.new(project, project.creator) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_ruby_remote_branches_ls_remote: false)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
describe '#execute' do
|
|
|
|
subject(:execute!) { service.execute(remote_mirror, 0) }
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
before do
|
2018-12-05 23:21:45 +05:30
|
|
|
project.repository.add_branch(project.owner, 'existing-branch', 'master')
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
allow(remote_mirror)
|
|
|
|
.to receive(:update_repository)
|
|
|
|
.and_return(double(divergent_refs: []))
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'ensures the remote exists' do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
expect(remote_mirror).to receive(:ensure_remote!)
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
execute!
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'fetches the remote repository' do
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(project.repository)
|
|
|
|
.to receive(:fetch_remote)
|
2019-10-12 21:52:04 +05:30
|
|
|
.with(remote_mirror.remote_name, no_tags: true, ssh_auth: remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
execute!
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'marks the mirror as started when beginning' do
|
|
|
|
expect(remote_mirror).to receive(:update_start!).and_call_original
|
|
|
|
|
|
|
|
execute!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the mirror as successfully finished' do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
result = execute!
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
expect(result[:status]).to eq(:success)
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(remote_mirror).to be_finished
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the mirror as failed and raises the error when an unexpected error occurs' do
|
|
|
|
allow(project.repository).to receive(:fetch_remote).and_raise('Badly broken')
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect { execute! }.to raise_error(/Badly broken/)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
expect(remote_mirror).to be_failed
|
|
|
|
expect(remote_mirror.last_error).to include('Badly broken')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the update fails because of a `Gitlab::Git::CommandError`' do
|
|
|
|
before do
|
|
|
|
allow(project.repository).to receive(:fetch_remote).and_raise(Gitlab::Git::CommandError.new('fetch failed'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'wraps `Gitlab::Git::CommandError`s in a service error' do
|
|
|
|
expect(execute!).to eq(status: :error, message: 'fetch failed')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the mirror as to be retried' do
|
|
|
|
execute!
|
|
|
|
|
|
|
|
expect(remote_mirror).to be_to_retry
|
|
|
|
expect(remote_mirror.last_error).to include('fetch failed')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "marks the mirror as failed after #{described_class::MAX_TRIES} tries" do
|
|
|
|
service.execute(remote_mirror, described_class::MAX_TRIES)
|
|
|
|
|
|
|
|
expect(remote_mirror).to be_failed
|
|
|
|
expect(remote_mirror.last_error).to include('fetch failed')
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when there are divergent refs' do
|
|
|
|
before do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'marks the mirror as failed and sets an error message' do
|
|
|
|
response = double(divergent_refs: %w[refs/heads/master refs/heads/develop])
|
|
|
|
expect(remote_mirror).to receive(:update_repository).and_return(response)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
execute!
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(remote_mirror).to be_failed
|
|
|
|
expect(remote_mirror.last_error).to include("Some refs have diverged")
|
|
|
|
expect(remote_mirror.last_error).to include("refs/heads/master\n")
|
|
|
|
expect(remote_mirror.last_error).to include("refs/heads/develop")
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
# https://gitlab.com/gitlab-org/gitaly/-/issues/2670
|
|
|
|
context 'when `gitaly_ruby_remote_branches_ls_remote` is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_ruby_remote_branches_ls_remote: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not perform a fetch' do
|
|
|
|
expect(project.repository).not_to receive(:fetch_remote)
|
|
|
|
|
|
|
|
execute!
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def stub_fetch_remote(project, remote_name:, ssh_auth:)
|
2018-12-05 23:21:45 +05:30
|
|
|
allow(project.repository)
|
|
|
|
.to receive(:fetch_remote)
|
2019-02-15 15:39:39 +05:30
|
|
|
.with(remote_name, no_tags: true, ssh_auth: ssh_auth) { fetch_remote(project.repository, remote_name) }
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def fetch_remote(repository, remote_name)
|
|
|
|
local_branch_names(repository).each do |branch|
|
|
|
|
commit = repository.commit(branch)
|
|
|
|
repository.write_ref("refs/remotes/#{remote_name}/#{branch}", commit.id) if commit
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def local_branch_names(repository)
|
2018-10-15 14:42:47 +05:30
|
|
|
branch_names = repository.branches.map(&:name)
|
|
|
|
# we want the protected branch to be pushed first
|
|
|
|
branch_names.unshift(branch_names.delete('master'))
|
|
|
|
end
|
|
|
|
end
|