2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2018-05-03 08:55:14 -04:00
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe RemoteMirror, :mailer do
|
2018-10-01 23:21:46 -04:00
|
|
|
include GitHelpers
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
describe 'URL validation' do
|
|
|
|
context 'with a valid URL' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is valid' do
|
2018-05-03 08:55:14 -04:00
|
|
|
remote_mirror = build(:remote_mirror)
|
|
|
|
expect(remote_mirror).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid URL' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is not valid' do
|
2018-05-03 08:55:14 -04:00
|
|
|
remote_mirror = build(:remote_mirror, url: 'ftp://invalid.invalid')
|
2018-06-01 07:43:53 -04:00
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
expect(remote_mirror).not_to be_valid
|
|
|
|
end
|
2018-06-11 09:29:37 -04:00
|
|
|
|
|
|
|
it 'does not allow url with an invalid user' do
|
|
|
|
remote_mirror = build(:remote_mirror, url: 'http://$user:password@invalid.invalid')
|
|
|
|
|
|
|
|
expect(remote_mirror).to be_invalid
|
|
|
|
expect(remote_mirror.errors[:url].first).to include('Username needs to start with an alphanumeric character')
|
|
|
|
end
|
2018-12-10 11:23:52 -05:00
|
|
|
|
|
|
|
it 'does not allow url pointing to localhost' do
|
|
|
|
remote_mirror = build(:remote_mirror, url: 'http://127.0.0.2/t.git')
|
|
|
|
|
|
|
|
expect(remote_mirror).to be_invalid
|
|
|
|
expect(remote_mirror.errors[:url].first).to include('Requests to loopback addresses are not allowed')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow url pointing to the local network' do
|
|
|
|
remote_mirror = build(:remote_mirror, url: 'https://192.168.1.1')
|
|
|
|
|
|
|
|
expect(remote_mirror).to be_invalid
|
|
|
|
expect(remote_mirror.errors[:url].first).to include('Requests to the local network are not allowed')
|
|
|
|
end
|
2019-08-29 12:19:07 -04:00
|
|
|
|
|
|
|
it 'returns a nil safe_url' do
|
|
|
|
remote_mirror = build(:remote_mirror, url: 'http://[0:0:0:0:ffff:123.123.123.123]/foo.git')
|
|
|
|
|
|
|
|
expect(remote_mirror.url).to eq('http://[0:0:0:0:ffff:123.123.123.123]/foo.git')
|
|
|
|
expect(remote_mirror.safe_url).to be_nil
|
|
|
|
end
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'encrypting credentials' do
|
|
|
|
context 'when setting URL for a first time' do
|
|
|
|
it 'stores the URL without credentials' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
expect(mirror.read_attribute(:url)).to eq('http://test.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the credentials on a separate field' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
expect(mirror.credentials).to eq({ user: 'foo', password: 'bar' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles credentials with large content' do
|
|
|
|
mirror = create_mirror(url: 'http://bxnhm8dote33ct932r3xavslj81wxmr7o8yux8do10oozckkif:9ne7fuvjn40qjt35dgt8v86q9m9g9essryxj76sumg2ccl2fg26c0krtz2gzfpyq4hf22h328uhq6npuiq6h53tpagtsj7vsrz75@test.com')
|
|
|
|
|
|
|
|
expect(mirror.credentials).to eq({
|
|
|
|
user: 'bxnhm8dote33ct932r3xavslj81wxmr7o8yux8do10oozckkif',
|
|
|
|
password: '9ne7fuvjn40qjt35dgt8v86q9m9g9essryxj76sumg2ccl2fg26c0krtz2gzfpyq4hf22h328uhq6npuiq6h53tpagtsj7vsrz75'
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating the URL' do
|
|
|
|
it 'allows a new URL without credentials' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
mirror.update_attribute(:url, 'http://test.com')
|
|
|
|
|
|
|
|
expect(mirror.url).to eq('http://test.com')
|
|
|
|
expect(mirror.credentials).to eq({ user: nil, password: nil })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows a new URL with credentials' do
|
|
|
|
mirror = create_mirror(url: 'http://test.com')
|
|
|
|
|
|
|
|
mirror.update_attribute(:url, 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
expect(mirror.url).to eq('http://foo:bar@test.com')
|
|
|
|
expect(mirror.credentials).to eq({ user: 'foo', password: 'bar' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the remote config if credentials changed' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
repo = mirror.project.repository
|
|
|
|
|
|
|
|
mirror.update_attribute(:url, 'http://foo:baz@test.com')
|
|
|
|
|
2018-10-01 23:21:46 -04:00
|
|
|
config = rugged_repo(repo).config
|
2018-05-03 08:55:14 -04:00
|
|
|
expect(config["remote.#{mirror.remote_name}.url"]).to eq('http://foo:baz@test.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes previous remote' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
expect(RepositoryRemoveRemoteWorker).to receive(:perform_async).with(mirror.project.id, mirror.remote_name).and_call_original
|
|
|
|
|
2018-07-02 06:43:06 -04:00
|
|
|
mirror.update(url: 'http://test.com')
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#remote_name' do
|
|
|
|
context 'when remote name is persisted in the database' do
|
|
|
|
it 'returns remote name with random value' do
|
|
|
|
allow(SecureRandom).to receive(:hex).and_return('secret')
|
|
|
|
|
|
|
|
remote_mirror = create(:remote_mirror)
|
|
|
|
|
2019-07-19 11:28:23 -04:00
|
|
|
expect(remote_mirror.remote_name).to eq('remote_mirror_secret')
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when remote name is not persisted in the database' do
|
|
|
|
it 'returns remote name with remote mirror id' do
|
|
|
|
remote_mirror = create(:remote_mirror)
|
|
|
|
remote_mirror.remote_name = nil
|
|
|
|
|
|
|
|
expect(remote_mirror.remote_name).to eq("remote_mirror_#{remote_mirror.id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when remote is not persisted in the database' do
|
|
|
|
it 'returns nil' do
|
|
|
|
remote_mirror = build(:remote_mirror, remote_name: nil)
|
|
|
|
|
|
|
|
expect(remote_mirror.remote_name).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-17 14:10:12 -04:00
|
|
|
describe '#bare_url' do
|
|
|
|
it 'returns the URL without any credentials' do
|
|
|
|
remote_mirror = build(:remote_mirror, url: 'http://user:pass@example.com/foo')
|
|
|
|
|
|
|
|
expect(remote_mirror.bare_url).to eq('http://example.com/foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty string when the URL is nil' do
|
|
|
|
remote_mirror = build(:remote_mirror, url: nil)
|
|
|
|
|
|
|
|
expect(remote_mirror.bare_url).to eq('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-02 11:08:01 -04:00
|
|
|
describe '#update_repository' do
|
2020-04-28 17:09:35 -04:00
|
|
|
it 'performs update including options' do
|
|
|
|
git_remote_mirror = stub_const('Gitlab::Git::RemoteMirror', spy)
|
|
|
|
mirror = build(:remote_mirror)
|
2020-04-02 11:08:01 -04:00
|
|
|
|
2020-05-18 20:07:58 -04:00
|
|
|
expect(mirror).to receive(:options_for_update).and_return(keep_divergent_refs: true)
|
2020-04-28 17:09:35 -04:00
|
|
|
mirror.update_repository
|
|
|
|
|
|
|
|
expect(git_remote_mirror).to have_received(:new).with(
|
|
|
|
mirror.project.repository.raw,
|
|
|
|
mirror.remote_name,
|
2020-05-18 20:07:58 -04:00
|
|
|
keep_divergent_refs: true
|
2020-04-28 17:09:35 -04:00
|
|
|
)
|
|
|
|
expect(git_remote_mirror).to have_received(:update)
|
2020-04-02 11:08:01 -04:00
|
|
|
end
|
2020-04-28 17:09:35 -04:00
|
|
|
end
|
2020-04-02 11:08:01 -04:00
|
|
|
|
2020-04-28 17:09:35 -04:00
|
|
|
describe '#options_for_update' do
|
|
|
|
it 'includes the `keep_divergent_refs` option' do
|
2020-04-02 11:08:01 -04:00
|
|
|
mirror = build_stubbed(:remote_mirror, keep_divergent_refs: true)
|
|
|
|
|
2020-04-28 17:09:35 -04:00
|
|
|
options = mirror.options_for_update
|
2020-04-02 11:08:01 -04:00
|
|
|
|
2020-04-28 17:09:35 -04:00
|
|
|
expect(options).to include(keep_divergent_refs: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the `only_branches_matching` option' do
|
|
|
|
branch = create(:protected_branch)
|
|
|
|
mirror = build_stubbed(:remote_mirror, project: branch.project, only_protected_branches: true)
|
|
|
|
|
|
|
|
options = mirror.options_for_update
|
|
|
|
|
|
|
|
expect(options).to include(only_branches_matching: [branch.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the `ssh_key` option' do
|
|
|
|
mirror = build(:remote_mirror, :ssh, ssh_private_key: 'private-key')
|
|
|
|
|
|
|
|
options = mirror.options_for_update
|
|
|
|
|
|
|
|
expect(options).to include(ssh_key: 'private-key')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the `known_hosts` option' do
|
|
|
|
mirror = build(:remote_mirror, :ssh, ssh_known_hosts: 'known-hosts')
|
|
|
|
|
|
|
|
options = mirror.options_for_update
|
|
|
|
|
|
|
|
expect(options).to include(known_hosts: 'known-hosts')
|
2020-04-02 11:08:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
describe '#safe_url' do
|
|
|
|
context 'when URL contains credentials' do
|
|
|
|
it 'masks the credentials' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
expect(mirror.safe_url).to eq('http://*****:*****@test.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when URL does not contain credentials' do
|
|
|
|
it 'shows the full URL' do
|
|
|
|
mirror = create_mirror(url: 'http://test.com')
|
|
|
|
|
|
|
|
expect(mirror.safe_url).to eq('http://test.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
Rework retry strategy for remote mirrors
**Prevention of running 2 simultaneous updates**
Instead of using `RemoteMirror#update_status` and raise an error if
it's already running to prevent the same mirror being updated at the
same time we now use `Gitlab::ExclusiveLease` for that.
When we fail to obtain a lease in 3 tries, 30 seconds apart, we bail
and reschedule. We'll reschedule faster for the protected branches.
If the mirror already ran since it was scheduled, the job will be
skipped.
**Error handling: Remote side**
When an update fails because of a `Gitlab::Git::CommandError`, we
won't track this error in sentry, this could be on the remote side:
for example when branches have diverged.
In this case, we'll try 3 times scheduled 1 or 5 minutes apart.
In between, the mirror is marked as "to_retry", the error would be
visible to the user when they visit the settings page.
After 3 tries we'll mark the mirror as failed and notify the user.
We won't track this error in sentry, as it's not likely we can help
it.
The next event that would trigger a new refresh.
**Error handling: our side**
If an unexpected error occurs, we mark the mirror as failed, but we'd
still retry the job based on the regular sidekiq retries with
backoff. Same as we used to
The error would be reported in sentry, since its likely we need to do
something about it.
2019-08-13 16:52:01 -04:00
|
|
|
describe '#mark_as_failed!' do
|
2018-12-05 10:22:52 -05:00
|
|
|
let(:remote_mirror) { create(:remote_mirror) }
|
|
|
|
let(:error_message) { 'http://user:pass@test.com/root/repoC.git/' }
|
|
|
|
let(:sanitized_error_message) { 'http://*****:*****@test.com/root/repoC.git/' }
|
|
|
|
|
|
|
|
subject do
|
|
|
|
remote_mirror.update_start
|
Rework retry strategy for remote mirrors
**Prevention of running 2 simultaneous updates**
Instead of using `RemoteMirror#update_status` and raise an error if
it's already running to prevent the same mirror being updated at the
same time we now use `Gitlab::ExclusiveLease` for that.
When we fail to obtain a lease in 3 tries, 30 seconds apart, we bail
and reschedule. We'll reschedule faster for the protected branches.
If the mirror already ran since it was scheduled, the job will be
skipped.
**Error handling: Remote side**
When an update fails because of a `Gitlab::Git::CommandError`, we
won't track this error in sentry, this could be on the remote side:
for example when branches have diverged.
In this case, we'll try 3 times scheduled 1 or 5 minutes apart.
In between, the mirror is marked as "to_retry", the error would be
visible to the user when they visit the settings page.
After 3 tries we'll mark the mirror as failed and notify the user.
We won't track this error in sentry, as it's not likely we can help
it.
The next event that would trigger a new refresh.
**Error handling: our side**
If an unexpected error occurs, we mark the mirror as failed, but we'd
still retry the job based on the regular sidekiq retries with
backoff. Same as we used to
The error would be reported in sentry, since its likely we need to do
something about it.
2019-08-13 16:52:01 -04:00
|
|
|
remote_mirror.mark_as_failed!(error_message)
|
2018-12-05 10:22:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the update_status to failed' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(remote_mirror.reload.update_status).to eq('failed')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the sanitized error' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(remote_mirror.last_error).to eq(sanitized_error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
remote_mirror.project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2019-10-23 05:06:03 -04:00
|
|
|
it 'notifies the project maintainers', :sidekiq_might_not_need_inline do
|
2018-12-05 10:22:52 -05:00
|
|
|
perform_enqueued_jobs { subject }
|
|
|
|
|
|
|
|
should_email(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
context 'when remote mirror gets destroyed' do
|
|
|
|
it 'removes remote' do
|
|
|
|
mirror = create_mirror(url: 'http://foo:bar@test.com')
|
|
|
|
|
|
|
|
expect(RepositoryRemoveRemoteWorker).to receive(:perform_async).with(mirror.project.id, mirror.remote_name).and_call_original
|
|
|
|
|
|
|
|
mirror.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'stuck mirrors' do
|
2019-07-19 11:28:23 -04:00
|
|
|
it 'includes mirrors that were started over an hour ago' do
|
|
|
|
mirror = create_mirror(url: 'http://cantbeblank',
|
|
|
|
update_status: 'started',
|
Rework retry strategy for remote mirrors
**Prevention of running 2 simultaneous updates**
Instead of using `RemoteMirror#update_status` and raise an error if
it's already running to prevent the same mirror being updated at the
same time we now use `Gitlab::ExclusiveLease` for that.
When we fail to obtain a lease in 3 tries, 30 seconds apart, we bail
and reschedule. We'll reschedule faster for the protected branches.
If the mirror already ran since it was scheduled, the job will be
skipped.
**Error handling: Remote side**
When an update fails because of a `Gitlab::Git::CommandError`, we
won't track this error in sentry, this could be on the remote side:
for example when branches have diverged.
In this case, we'll try 3 times scheduled 1 or 5 minutes apart.
In between, the mirror is marked as "to_retry", the error would be
visible to the user when they visit the settings page.
After 3 tries we'll mark the mirror as failed and notify the user.
We won't track this error in sentry, as it's not likely we can help
it.
The next event that would trigger a new refresh.
**Error handling: our side**
If an unexpected error occurs, we mark the mirror as failed, but we'd
still retry the job based on the regular sidekiq retries with
backoff. Same as we used to
The error would be reported in sentry, since its likely we need to do
something about it.
2019-08-13 16:52:01 -04:00
|
|
|
last_update_started_at: 3.hours.ago,
|
|
|
|
last_update_at: 2.hours.ago)
|
2019-07-19 11:28:23 -04:00
|
|
|
|
|
|
|
expect(described_class.stuck.last).to eq(mirror)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes mirrors started over 3 hours ago for their first sync' do
|
2018-05-03 08:55:14 -04:00
|
|
|
mirror = create_mirror(url: 'http://cantbeblank',
|
|
|
|
update_status: 'started',
|
|
|
|
last_update_at: nil,
|
Rework retry strategy for remote mirrors
**Prevention of running 2 simultaneous updates**
Instead of using `RemoteMirror#update_status` and raise an error if
it's already running to prevent the same mirror being updated at the
same time we now use `Gitlab::ExclusiveLease` for that.
When we fail to obtain a lease in 3 tries, 30 seconds apart, we bail
and reschedule. We'll reschedule faster for the protected branches.
If the mirror already ran since it was scheduled, the job will be
skipped.
**Error handling: Remote side**
When an update fails because of a `Gitlab::Git::CommandError`, we
won't track this error in sentry, this could be on the remote side:
for example when branches have diverged.
In this case, we'll try 3 times scheduled 1 or 5 minutes apart.
In between, the mirror is marked as "to_retry", the error would be
visible to the user when they visit the settings page.
After 3 tries we'll mark the mirror as failed and notify the user.
We won't track this error in sentry, as it's not likely we can help
it.
The next event that would trigger a new refresh.
**Error handling: our side**
If an unexpected error occurs, we mark the mirror as failed, but we'd
still retry the job based on the regular sidekiq retries with
backoff. Same as we used to
The error would be reported in sentry, since its likely we need to do
something about it.
2019-08-13 16:52:01 -04:00
|
|
|
last_update_started_at: 4.hours.ago)
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
expect(described_class.stuck.last).to eq(mirror)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '#sync' do
|
2018-05-03 08:55:14 -04:00
|
|
|
let(:remote_mirror) { create(:project, :repository, :remote_mirror).remote_mirrors.first }
|
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 11:10:41 -04:00
|
|
|
freeze_time { example.run }
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with remote mirroring disabled' do
|
|
|
|
it 'returns nil' do
|
2018-07-02 06:43:06 -04:00
|
|
|
remote_mirror.update(enabled: false)
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
expect(remote_mirror.sync).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with remote mirroring enabled' do
|
2018-11-28 06:24:50 -05:00
|
|
|
it 'defaults to disabling only protected branches' do
|
|
|
|
expect(remote_mirror.only_protected_branches?).to be_falsey
|
|
|
|
end
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
context 'with only protected branches enabled' do
|
2018-11-28 06:24:50 -05:00
|
|
|
before do
|
|
|
|
remote_mirror.only_protected_branches = true
|
|
|
|
end
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
context 'when it did not update in the last minute' do
|
|
|
|
it 'schedules a RepositoryUpdateRemoteMirrorWorker to run now' do
|
2020-05-22 05:08:09 -04:00
|
|
|
expect(RepositoryUpdateRemoteMirrorWorker).to receive(:perform_async).with(remote_mirror.id, Time.current)
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
remote_mirror.sync
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it did update in the last minute' do
|
|
|
|
it 'schedules a RepositoryUpdateRemoteMirrorWorker to run in the next minute' do
|
2020-05-22 05:08:09 -04:00
|
|
|
remote_mirror.last_update_started_at = Time.current - 30.seconds
|
2018-05-03 08:55:14 -04:00
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
expect(RepositoryUpdateRemoteMirrorWorker).to receive(:perform_in).with(RemoteMirror::PROTECTED_BACKOFF_DELAY, remote_mirror.id, Time.current)
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
remote_mirror.sync
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with only protected branches disabled' do
|
|
|
|
before do
|
|
|
|
remote_mirror.only_protected_branches = false
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it did not update in the last 5 minutes' do
|
|
|
|
it 'schedules a RepositoryUpdateRemoteMirrorWorker to run now' do
|
2020-05-22 05:08:09 -04:00
|
|
|
expect(RepositoryUpdateRemoteMirrorWorker).to receive(:perform_async).with(remote_mirror.id, Time.current)
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
remote_mirror.sync
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it did update within the last 5 minutes' do
|
|
|
|
it 'schedules a RepositoryUpdateRemoteMirrorWorker to run in the next 5 minutes' do
|
2020-05-22 05:08:09 -04:00
|
|
|
remote_mirror.last_update_started_at = Time.current - 30.seconds
|
2018-05-03 08:55:14 -04:00
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
expect(RepositoryUpdateRemoteMirrorWorker).to receive(:perform_in).with(RemoteMirror::UNPROTECTED_BACKOFF_DELAY, remote_mirror.id, Time.current)
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
remote_mirror.sync
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '#ensure_remote!' do
|
2018-08-22 13:43:15 -04:00
|
|
|
let(:remote_mirror) { create(:project, :repository, :remote_mirror).remote_mirrors.first }
|
2018-11-12 05:52:48 -05:00
|
|
|
let(:project) { remote_mirror.project }
|
|
|
|
let(:repository) { project.repository }
|
2018-08-22 13:43:15 -04:00
|
|
|
|
|
|
|
it 'adds a remote multiple times with no errors' do
|
2018-11-12 05:52:48 -05:00
|
|
|
expect(repository).to receive(:add_remote).with(remote_mirror.remote_name, remote_mirror.url).twice.and_call_original
|
2018-08-22 13:43:15 -04:00
|
|
|
|
|
|
|
2.times do
|
|
|
|
remote_mirror.ensure_remote!
|
|
|
|
end
|
|
|
|
end
|
2018-11-12 05:52:48 -05:00
|
|
|
|
|
|
|
context 'SSH public-key authentication' do
|
|
|
|
it 'omits the password from the URL' do
|
|
|
|
remote_mirror.update!(auth_method: 'ssh_public_key', url: 'ssh://git:pass@example.com')
|
|
|
|
|
|
|
|
expect(repository).to receive(:add_remote).with(remote_mirror.remote_name, 'ssh://git@example.com')
|
|
|
|
|
|
|
|
remote_mirror.ensure_remote!
|
|
|
|
end
|
|
|
|
end
|
2018-08-22 13:43:15 -04:00
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '#url=' do
|
2019-01-15 01:33:48 -05:00
|
|
|
let(:remote_mirror) { create(:project, :repository, :remote_mirror).remote_mirrors.first }
|
|
|
|
|
|
|
|
it 'resets all the columns when URL changes' do
|
2020-05-22 05:08:09 -04:00
|
|
|
remote_mirror.update(last_error: Time.current,
|
|
|
|
last_update_at: Time.current,
|
|
|
|
last_successful_update_at: Time.current,
|
2019-01-15 01:33:48 -05:00
|
|
|
update_status: 'started',
|
|
|
|
error_notification_sent: true)
|
|
|
|
|
|
|
|
expect { remote_mirror.update_attribute(:url, 'http://new.example.com') }
|
|
|
|
.to change { remote_mirror.last_error }.to(nil)
|
|
|
|
.and change { remote_mirror.last_update_at }.to(nil)
|
|
|
|
.and change { remote_mirror.last_successful_update_at }.to(nil)
|
|
|
|
.and change { remote_mirror.update_status }.to('finished')
|
|
|
|
.and change { remote_mirror.error_notification_sent }.to(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '#updated_since?' do
|
2018-05-03 08:55:14 -04:00
|
|
|
let(:remote_mirror) { create(:project, :repository, :remote_mirror).remote_mirrors.first }
|
2020-05-22 05:08:09 -04:00
|
|
|
let(:timestamp) { Time.current - 5.minutes }
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 11:10:41 -04:00
|
|
|
freeze_time { example.run }
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-05-22 05:08:09 -04:00
|
|
|
remote_mirror.update(last_update_started_at: Time.current)
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when remote mirror does not have status failed' do
|
|
|
|
it 'returns true when last update started after the timestamp' do
|
|
|
|
expect(remote_mirror.updated_since?(timestamp)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when last update started before the timestamp' do
|
2020-05-22 05:08:09 -04:00
|
|
|
expect(remote_mirror.updated_since?(Time.current + 5.minutes)).to be false
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when remote mirror has status failed' do
|
|
|
|
it 'returns false when last update started after the timestamp' do
|
2018-07-02 06:43:06 -04:00
|
|
|
remote_mirror.update(update_status: 'failed')
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
expect(remote_mirror.updated_since?(timestamp)).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no project' do
|
|
|
|
it 'includes mirror with a project in pending_delete' do
|
|
|
|
mirror = create_mirror(url: 'http://cantbeblank',
|
|
|
|
update_status: 'finished',
|
|
|
|
enabled: true,
|
|
|
|
last_update_at: nil,
|
|
|
|
updated_at: 25.hours.ago)
|
|
|
|
project = mirror.project
|
|
|
|
project.pending_delete = true
|
|
|
|
project.save
|
|
|
|
mirror.reload
|
|
|
|
|
|
|
|
expect(mirror.sync).to be_nil
|
|
|
|
expect(mirror.valid?).to be_truthy
|
|
|
|
expect(mirror.update_status).to eq('finished')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-21 18:24:48 -04:00
|
|
|
describe '#disabled?' do
|
2020-09-03 14:08:29 -04:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
|
2019-03-21 18:24:48 -04:00
|
|
|
subject { remote_mirror.disabled? }
|
|
|
|
|
|
|
|
context 'when disabled' do
|
2020-09-03 14:08:29 -04:00
|
|
|
let(:remote_mirror) { build(:remote_mirror, project: project, enabled: false) }
|
2019-03-21 18:24:48 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when enabled' do
|
2020-09-03 14:08:29 -04:00
|
|
|
let(:remote_mirror) { build(:remote_mirror, project: project, enabled: true) }
|
2019-03-21 18:24:48 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
def create_mirror(params)
|
|
|
|
project = FactoryBot.create(:project, :repository)
|
|
|
|
project.remote_mirrors.create!(params)
|
|
|
|
end
|
|
|
|
end
|