Commit 50f00309 authored by Nick Thomas's avatar Nick Thomas

Introduce a stub_current_geo_node helper into the Geo specs

parent 16c148ba
module EE
module GeoHelpers
def stub_current_geo_node(node)
allow(::Gitlab::Geo).to receive(:current_node).and_return(node)
end
end
end
require 'spec_helper' require 'spec_helper'
describe Gitlab::Geo::LogCursor::Daemon, :postgresql do describe Gitlab::Geo::LogCursor::Daemon, :postgresql do
include ::EE::GeoHelpers
describe '#run!' do describe '#run!' do
set(:geo_node) { create(:geo_node) } set(:geo_node) { create(:geo_node) }
before do before do
allow(Gitlab::Geo).to receive(:current_node).and_return(geo_node) stub_current_geo_node(geo_node)
end end
it 'traps signals' do it 'traps signals' do
...@@ -135,7 +137,7 @@ describe Gitlab::Geo::LogCursor::Daemon, :postgresql do ...@@ -135,7 +137,7 @@ describe Gitlab::Geo::LogCursor::Daemon, :postgresql do
end end
it 'does not schedule a GeoRepositoryDestroyWorker when event node is not the current node' do it 'does not schedule a GeoRepositoryDestroyWorker when event node is not the current node' do
allow(Gitlab::Geo).to receive(:current_node).and_return(build(:geo_node)) stub_current_geo_node(build(:geo_node))
expect(Geo::RepositoriesCleanUpWorker).not_to receive(:perform_in) expect(Geo::RepositoriesCleanUpWorker).not_to receive(:perform_in)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Geo::Transfer do describe Gitlab::Geo::Transfer do
let!(:primary_node) { FactoryGirl.create(:geo_node, :primary) } include ::EE::GeoHelpers
let!(:secondary_node) { FactoryGirl.create(:geo_node) }
let(:lfs_object) { create(:lfs_object, :with_file) } set(:primary_node) { create(:geo_node, :primary) }
set(:secondary_node) { create(:geo_node) }
set(:lfs_object) { create(:lfs_object, :with_file) }
let(:url) { primary_node.geo_transfers_url(:lfs, lfs_object.id.to_s) } let(:url) { primary_node.geo_transfers_url(:lfs, lfs_object.id.to_s) }
let(:content) { StringIO.new("1\n2\n3") } let(:content) { StringIO.new("1\n2\n3") }
let(:size) { File.stat(lfs_object.file.path).size } let(:size) { File.stat(lfs_object.file.path).size }
...@@ -20,7 +22,8 @@ describe Gitlab::Geo::Transfer do ...@@ -20,7 +22,8 @@ describe Gitlab::Geo::Transfer do
end end
it '#download_from_primary' do it '#download_from_primary' do
allow(Gitlab::Geo).to receive(:current_node) { secondary_node } stub_current_geo_node(secondary_node)
response = double(success?: true) response = double(success?: true)
expect(HTTParty).to receive(:get).and_return(response) expect(HTTParty).to receive(:get).and_return(response)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Geo do describe Gitlab::Geo do
let(:primary_node) { FactoryGirl.create(:geo_node, :primary) } include ::EE::GeoHelpers
let(:secondary_node) { FactoryGirl.create(:geo_node) }
describe 'current_node' do set(:primary_node) { create(:geo_node, :primary) }
before do set(:secondary_node) { create(:geo_node) }
primary_node
end
describe 'current_node' do
it 'returns a GeoNode instance' do it 'returns a GeoNode instance' do
expect(described_class.current_node).to eq(primary_node) expect(described_class.current_node).to eq(primary_node)
end end
end end
describe 'primary_node' do describe 'primary_node' do
before do
primary_node
secondary_node
end
it 'returns a GeoNode primary instance' do it 'returns a GeoNode primary instance' do
expect(described_class.current_node).to eq(primary_node) expect(described_class.primary_node).to eq(primary_node)
end end
end end
describe 'primary?' do describe 'primary?' do
context 'when current node is a primary node' do context 'when current node is a primary node' do
before do
primary_node
end
it 'returns true' do it 'returns true' do
expect(described_class.primary?).to be_truthy expect(described_class.primary?).to be_truthy
end end
...@@ -46,12 +35,12 @@ describe Gitlab::Geo do ...@@ -46,12 +35,12 @@ describe Gitlab::Geo do
describe 'primary_node_configured?' do describe 'primary_node_configured?' do
context 'when current node is a primary node' do context 'when current node is a primary node' do
it 'returns true' do it 'returns true' do
primary_node
expect(described_class.primary_node_configured?).to be_truthy expect(described_class.primary_node_configured?).to be_truthy
end end
it 'returns false when primary does not exist' do it 'returns false when primary does not exist' do
primary_node.destroy
expect(described_class.primary_node_configured?).to be_falsey expect(described_class.primary_node_configured?).to be_falsey
end end
end end
...@@ -60,8 +49,7 @@ describe Gitlab::Geo do ...@@ -60,8 +49,7 @@ describe Gitlab::Geo do
describe 'secondary?' do describe 'secondary?' do
context 'when current node is a secondary node' do context 'when current node is a secondary node' do
before do before do
secondary_node stub_current_geo_node(secondary_node)
allow(described_class).to receive(:current_node) { secondary_node }
end end
it 'returns true' do it 'returns true' do
...@@ -78,16 +66,16 @@ describe Gitlab::Geo do ...@@ -78,16 +66,16 @@ describe Gitlab::Geo do
describe 'enabled?' do describe 'enabled?' do
context 'when any GeoNode exists' do context 'when any GeoNode exists' do
before do
secondary_node
end
it 'returns true' do it 'returns true' do
expect(described_class.enabled?).to be_truthy expect(described_class.enabled?).to be_truthy
end end
end end
context 'when no GeoNode exists' do context 'when no GeoNode exists' do
before do
GeoNode.delete_all
end
it 'returns false' do it 'returns false' do
expect(described_class.enabled?).to be_falsey expect(described_class.enabled?).to be_falsey
end end
...@@ -111,17 +99,10 @@ describe Gitlab::Geo do ...@@ -111,17 +99,10 @@ describe Gitlab::Geo do
end end
end end
context 'with RequestStore enabled' do context 'with RequestStore enabled', :request_store do
before do
RequestStore.begin!
end
after do
RequestStore.end!
RequestStore.clear!
end
it 'return false when no GeoNode exists' do it 'return false when no GeoNode exists' do
GeoNode.delete_all
expect(GeoNode).to receive(:exists?).once.and_call_original expect(GeoNode).to receive(:exists?).once.and_call_original
2.times { expect(described_class.enabled?).to be_falsey } 2.times { expect(described_class.enabled?).to be_falsey }
...@@ -131,23 +112,14 @@ describe Gitlab::Geo do ...@@ -131,23 +112,14 @@ describe Gitlab::Geo do
describe 'readonly?' do describe 'readonly?' do
context 'when current node is secondary' do context 'when current node is secondary' do
before do
secondary_node
end
it 'returns true' do it 'returns true' do
allow(described_class).to receive(:current_node) { secondary_node } stub_current_geo_node(secondary_node)
expect(described_class.secondary?).to be_truthy expect(described_class.secondary?).to be_truthy
end end
end end
context 'current node is primary' do context 'current node is primary' do
before do it 'returns false ' do
primary_node
end
it 'returns false when ' do
allow(described_class).to receive(:current_node) { primary_node }
expect(described_class.secondary?).to be_falsey expect(described_class.secondary?).to be_falsey
end end
end end
...@@ -211,9 +183,6 @@ describe Gitlab::Geo do ...@@ -211,9 +183,6 @@ describe Gitlab::Geo do
end end
it 'activates cron jobs for primary' do it 'activates cron jobs for primary' do
allow(described_class).to receive(:primary?).and_return(true)
allow(described_class).to receive(:secondary?).and_return(false)
described_class.configure_cron_jobs! described_class.configure_cron_jobs!
expect(described_class.repository_sync_job).not_to be_enabled expect(described_class.repository_sync_job).not_to be_enabled
...@@ -221,9 +190,8 @@ describe Gitlab::Geo do ...@@ -221,9 +190,8 @@ describe Gitlab::Geo do
expect(Sidekiq::Cron::Job.find('ldap_test')).to be_enabled expect(Sidekiq::Cron::Job.find('ldap_test')).to be_enabled
end end
it 'activates cron jobs for secondary' do it 'does not activate cron jobs for secondary' do
allow(described_class).to receive(:primary?).and_return(false) stub_current_geo_node(secondary_node)
allow(described_class).to receive(:secondary?).and_return(true)
described_class.configure_cron_jobs! described_class.configure_cron_jobs!
...@@ -233,8 +201,7 @@ describe Gitlab::Geo do ...@@ -233,8 +201,7 @@ describe Gitlab::Geo do
end end
it 'deactivates all jobs when Geo is not active' do it 'deactivates all jobs when Geo is not active' do
allow(described_class).to receive(:primary?).and_return(false) GeoNode.update_all(enabled: false)
allow(described_class).to receive(:secondary?).and_return(false)
described_class.configure_cron_jobs! described_class.configure_cron_jobs!
...@@ -244,13 +211,11 @@ describe Gitlab::Geo do ...@@ -244,13 +211,11 @@ describe Gitlab::Geo do
end end
it 'reactivates cron jobs when node turns off Geo' do it 'reactivates cron jobs when node turns off Geo' do
allow(described_class).to receive(:primary?).and_return(false) stub_current_geo_node(secondary_node)
allow(described_class).to receive(:secondary?).and_return(true)
described_class.configure_cron_jobs! described_class.configure_cron_jobs!
expect(Sidekiq::Cron::Job.find('ldap_test')).not_to be_enabled expect(Sidekiq::Cron::Job.find('ldap_test')).not_to be_enabled
allow(described_class).to receive(:primary?).and_return(false)
allow(described_class).to receive(:secondary?).and_return(false) allow(described_class).to receive(:secondary?).and_return(false)
described_class.configure_cron_jobs! described_class.configure_cron_jobs!
......
require 'spec_helper' require 'spec_helper'
describe GeoNode, type: :model do describe GeoNode, type: :model do
include ::EE::GeoHelpers
let(:new_node) { create(:geo_node, schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') } let(:new_node) { create(:geo_node, schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
let(:new_primary_node) { create(:geo_node, :primary, schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') } let(:new_primary_node) { create(:geo_node, :primary, schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
let(:empty_node) { described_class.new } let(:empty_node) { described_class.new }
...@@ -122,13 +124,13 @@ describe GeoNode, type: :model do ...@@ -122,13 +124,13 @@ describe GeoNode, type: :model do
subject { described_class.new } subject { described_class.new }
it 'returns true when node is the current node' do it 'returns true when node is the current node' do
allow(Gitlab::Geo).to receive(:current_node) { subject } stub_current_geo_node(subject)
expect(subject.current?).to eq true expect(subject.current?).to eq true
end end
it 'returns false when node is not the current node' do it 'returns false when node is not the current node' do
allow(Gitlab::Geo).to receive(:current_node) { double } stub_current_geo_node(double)
expect(subject.current?).to eq false expect(subject.current?).to eq false
end end
......
...@@ -2,21 +2,21 @@ require 'spec_helper' ...@@ -2,21 +2,21 @@ require 'spec_helper'
describe API::Geo do describe API::Geo do
include ApiHelpers include ApiHelpers
include ::EE::GeoHelpers
let(:admin) { create(:admin) } set(:admin) { create(:admin) }
let(:user) { create(:user) } set(:user) { create(:user) }
let!(:primary_node) { create(:geo_node, :primary) } set(:primary_node) { create(:geo_node, :primary) }
let!(:secondary_node) { create(:geo_node) } set(:secondary_node) { create(:geo_node) }
let(:geo_token_header) do let(:geo_token_header) do
{ 'X-Gitlab-Token' => secondary_node.system_hook.token } { 'X-Gitlab-Token' => secondary_node.system_hook.token }
end end
before do before do
allow(Gitlab::Geo).to receive(:current_node) { secondary_node } stub_current_geo_node(secondary_node)
end end
describe 'GET /geo/transfers/attachment/1' do describe 'GET /geo/transfers/attachment/1' do
let!(:secondary_node) { create(:geo_node) }
let(:note) { create(:note, :with_attachment) } let(:note) { create(:note, :with_attachment) }
let(:upload) { Upload.find_by(model: note, uploader: 'AttachmentUploader') } let(:upload) { Upload.find_by(model: note, uploader: 'AttachmentUploader') }
let(:transfer) { Gitlab::Geo::FileTransfer.new(:attachment, upload) } let(:transfer) { Gitlab::Geo::FileTransfer.new(:attachment, upload) }
...@@ -52,7 +52,6 @@ describe API::Geo do ...@@ -52,7 +52,6 @@ describe API::Geo do
end end
describe 'GET /geo/transfers/avatar/1' do describe 'GET /geo/transfers/avatar/1' do
let!(:secondary_node) { create(:geo_node) }
let(:user) { create(:user, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) } let(:user) { create(:user, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
let(:upload) { Upload.find_by(model: user, uploader: 'AvatarUploader') } let(:upload) { Upload.find_by(model: user, uploader: 'AvatarUploader') }
let(:transfer) { Gitlab::Geo::FileTransfer.new(:avatar, upload) } let(:transfer) { Gitlab::Geo::FileTransfer.new(:avatar, upload) }
...@@ -88,7 +87,6 @@ describe API::Geo do ...@@ -88,7 +87,6 @@ describe API::Geo do
end end
describe 'GET /geo/transfers/file/1' do describe 'GET /geo/transfers/file/1' do
let!(:secondary_node) { create(:geo_node) }
let(:project) { create(:project) } let(:project) { create(:project) }
let(:upload) { Upload.find_by(model: project, uploader: 'FileUploader') } let(:upload) { Upload.find_by(model: project, uploader: 'FileUploader') }
let(:transfer) { Gitlab::Geo::FileTransfer.new(:file, upload) } let(:transfer) { Gitlab::Geo::FileTransfer.new(:file, upload) }
...@@ -125,7 +123,6 @@ describe API::Geo do ...@@ -125,7 +123,6 @@ describe API::Geo do
end end
describe 'GET /geo/transfers/lfs/1' do describe 'GET /geo/transfers/lfs/1' do
let!(:secondary_node) { create(:geo_node) }
let(:lfs_object) { create(:lfs_object, :with_file) } let(:lfs_object) { create(:lfs_object, :with_file) }
let(:req_header) do let(:req_header) do
transfer = Gitlab::Geo::LfsTransfer.new(lfs_object) transfer = Gitlab::Geo::LfsTransfer.new(lfs_object)
...@@ -161,14 +158,9 @@ describe API::Geo do ...@@ -161,14 +158,9 @@ describe API::Geo do
end end
end end
describe 'GET /geo/status' do describe 'GET /geo/status', :postgresql do
let!(:secondary_node) { create(:geo_node) }
let(:request) { Gitlab::Geo::BaseRequest.new } let(:request) { Gitlab::Geo::BaseRequest.new }
before do
skip("Not using PostgreSQL") unless Gitlab::Database.postgresql?
end
it 'responds with 401 with invalid auth header' do it 'responds with 401 with invalid auth header' do
get api('/geo/status'), nil, Authorization: 'Test' get api('/geo/status'), nil, Authorization: 'Test'
...@@ -185,7 +177,7 @@ describe API::Geo do ...@@ -185,7 +177,7 @@ describe API::Geo do
context 'when requesting secondary node with valid auth header' do context 'when requesting secondary node with valid auth header' do
before do before do
allow(Gitlab::Geo).to receive(:current_node) { secondary_node } stub_current_geo_node(secondary_node)
allow(request).to receive(:requesting_node) { primary_node } allow(request).to receive(:requesting_node) { primary_node }
end end
...@@ -199,7 +191,7 @@ describe API::Geo do ...@@ -199,7 +191,7 @@ describe API::Geo do
context 'when requesting primary node with valid auth header' do context 'when requesting primary node with valid auth header' do
before do before do
allow(Gitlab::Geo).to receive(:current_node) { primary_node } stub_current_geo_node(primary_node)
allow(request).to receive(:requesting_node) { secondary_node } allow(request).to receive(:requesting_node) { secondary_node }
end end
......
require 'spec_helper' require 'spec_helper'
describe Geo::FileDownloadService do describe Geo::FileDownloadService do
let!(:primary) { create(:geo_node, :primary) } include ::EE::GeoHelpers
let(:secondary) { create(:geo_node) }
set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) }
before do before do
allow(described_class).to receive(:current_node) { secondary } stub_current_geo_node(secondary)
end end
describe '#execute' do describe '#execute' do
......
require 'spec_helper' require 'spec_helper'
describe Geo::NodeStatusService do describe Geo::NodeStatusService do
let!(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
let(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
subject { described_class.new } subject { described_class.new }
before do
allow(described_class).to receive(:current_node) { primary }
end
describe '#call' do describe '#call' do
it 'parses a 401 response' do it 'parses a 401 response' do
request = double(success?: false, request = double(success?: false,
...@@ -75,8 +71,6 @@ describe Geo::NodeStatusService do ...@@ -75,8 +71,6 @@ describe Geo::NodeStatusService do
end end
it 'returns meaningful error message when primary uses incorrect db key' do it 'returns meaningful error message when primary uses incorrect db key' do
secondary # create it before mocking GeoNode#secret_access_key
allow_any_instance_of(GeoNode).to receive(:secret_access_key).and_raise(OpenSSL::Cipher::CipherError) allow_any_instance_of(GeoNode).to receive(:secret_access_key).and_raise(OpenSSL::Cipher::CipherError)
status = subject.call(secondary) status = subject.call(secondary)
...@@ -85,7 +79,7 @@ describe Geo::NodeStatusService do ...@@ -85,7 +79,7 @@ describe Geo::NodeStatusService do
end end
it 'gracefully handles case when primary is deleted' do it 'gracefully handles case when primary is deleted' do
primary.destroy primary.destroy!
status = subject.call(secondary) status = subject.call(secondary)
......
require 'spec_helper' require 'spec_helper'
describe Geo::FileDownloadDispatchWorker do describe Geo::FileDownloadDispatchWorker do
include ::EE::GeoHelpers
set(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') } set(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
before do before do
allow(Gitlab::Geo).to receive(:current_node).and_return(secondary) stub_current_geo_node(secondary)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew).and_return(true) allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew).and_return(true)
allow_any_instance_of(described_class).to receive(:over_time?).and_return(false) allow_any_instance_of(described_class).to receive(:over_time?).and_return(false)
......
require 'spec_helper' require 'spec_helper'
describe Geo::RepositorySyncWorker, :postgresql do describe Geo::RepositorySyncWorker, :postgresql do
include ::EE::GeoHelpers
set(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') } set(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
set(:synced_group) { create(:group) } set(:synced_group) { create(:group) }
...@@ -10,7 +12,7 @@ describe Geo::RepositorySyncWorker, :postgresql do ...@@ -10,7 +12,7 @@ describe Geo::RepositorySyncWorker, :postgresql do
subject { described_class.new } subject { described_class.new }
before do before do
allow(Gitlab::Geo).to receive(:current_node).and_return(secondary) stub_current_geo_node(secondary)
end end
describe '#perform' do describe '#perform' do
......
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