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