Commit 08489450 authored by Michael Kozono's avatar Michael Kozono

Setup specs with Geo nodes properly

Previously, many tests depended on the coupling between the
GeoNode factory and the config. With that decoupled, these
tests have GeoNodes in the database but *this* machine is
not associated with any GeoNode record.
parent b5c7b4e6
...@@ -82,7 +82,7 @@ describe Oauth::GeoAuthController, :geo do ...@@ -82,7 +82,7 @@ describe Oauth::GeoAuthController, :geo do
it "redirects to primary node's oauth endpoint" do it "redirects to primary node's oauth endpoint" do
get :callback, params: { state: login_state } get :callback, params: { state: login_state }
expect(response).to redirect_to('/') expect(response).to redirect_to(secondary_node.uri.path)
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Geo::JwtRequestDecoder do describe Gitlab::Geo::JwtRequestDecoder do
include EE::GeoHelpers
let!(:primary_node) { FactoryBot.create(:geo_node, :primary) } let!(:primary_node) { FactoryBot.create(:geo_node, :primary) }
let(:data) { { input: 123 } } let(:data) { { input: 123 } }
let(:request) { Gitlab::Geo::TransferRequest.new(data) } let(:request) { Gitlab::Geo::TransferRequest.new(data) }
subject { described_class.new(request.headers['Authorization']) } subject { described_class.new(request.headers['Authorization']) }
before do
stub_current_geo_node(primary_node)
end
describe '#decode' do describe '#decode' do
it 'decodes correct data' do it 'decodes correct data' do
expect(subject.decode).to eq(data) expect(subject.decode).to eq(data)
......
...@@ -18,6 +18,8 @@ describe Gitlab::Geo, :geo, :request_store do ...@@ -18,6 +18,8 @@ describe Gitlab::Geo, :geo, :request_store do
describe '.current_node' do describe '.current_node' do
it 'returns a GeoNode instance' do it 'returns a GeoNode instance' do
expect(GeoNode).to receive(:current_node).and_return(primary_node)
expect(described_class.current_node).to eq(primary_node) expect(described_class.current_node).to eq(primary_node)
end end
...@@ -42,6 +44,10 @@ describe Gitlab::Geo, :geo, :request_store do ...@@ -42,6 +44,10 @@ describe Gitlab::Geo, :geo, :request_store do
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
stub_current_geo_node(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
......
...@@ -3,11 +3,15 @@ ...@@ -3,11 +3,15 @@
require 'spec_helper' require 'spec_helper'
describe Ci::JobArtifact do describe Ci::JobArtifact do
include EE::GeoHelpers
describe '#destroy' do describe '#destroy' do
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
it 'creates a JobArtifactDeletedEvent' do it 'creates a JobArtifactDeletedEvent' do
stub_current_geo_node(primary)
job_artifact = create(:ci_job_artifact, :archive) job_artifact = create(:ci_job_artifact, :archive)
expect do expect do
......
...@@ -102,7 +102,8 @@ describe GeoNode, :geo, type: :model do ...@@ -102,7 +102,8 @@ describe GeoNode, :geo, type: :model do
context 'prevent locking yourself out' do context 'prevent locking yourself out' do
it 'does not accept adding a non primary node with same details as current_node' do it 'does not accept adding a non primary node with same details as current_node' do
node = build(:geo_node, :primary, primary: false) stub_geo_setting(node_name: 'foo')
node = build(:geo_node, :primary, primary: false, name: 'foo')
expect(node).not_to be_valid expect(node).not_to be_valid
expect(node.errors.full_messages.count).to eq(1) expect(node.errors.full_messages.count).to eq(1)
......
require 'spec_helper' require 'spec_helper'
describe LfsObject do describe LfsObject do
include EE::GeoHelpers
describe '#destroy' do describe '#destroy' do
subject { create(:lfs_object, :with_file) } subject { create(:lfs_object, :with_file) }
...@@ -9,6 +11,8 @@ describe LfsObject do ...@@ -9,6 +11,8 @@ describe LfsObject do
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
it 'logs an event to the Geo event log' do it 'logs an event to the Geo event log' do
stub_current_geo_node(primary)
expect { subject.destroy }.to change(Geo::LfsObjectDeletedEvent, :count).by(1) expect { subject.destroy }.to change(Geo::LfsObjectDeletedEvent, :count).by(1)
end end
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Namespace do describe Namespace do
include EE::GeoHelpers
let!(:namespace) { create(:namespace) } let!(:namespace) { create(:namespace) }
let!(:free_plan) { create(:free_plan) } let!(:free_plan) { create(:free_plan) }
let!(:bronze_plan) { create(:bronze_plan) } let!(:bronze_plan) { create(:bronze_plan) }
...@@ -148,6 +150,8 @@ describe Namespace do ...@@ -148,6 +150,8 @@ describe Namespace do
allow(gitlab_shell).to receive(:mv_namespace) allow(gitlab_shell).to receive(:mv_namespace)
.with(project_legacy.repository_storage, full_path_was, new_path) .with(project_legacy.repository_storage, full_path_was, new_path)
.and_return(true) .and_return(true)
stub_current_geo_node(primary)
end end
it 'logs the Geo::RepositoryRenamedEvent for each project inside namespace' do it 'logs the Geo::RepositoryRenamedEvent for each project inside namespace' do
......
require 'spec_helper' require 'spec_helper'
describe Upload do describe Upload do
include EE::GeoHelpers
describe '#destroy' do describe '#destroy' do
subject { create(:upload, checksum: '8710d2c16809c79fee211a9693b64038a8aae99561bc86ce98a9b46b45677fe4') } subject { create(:upload, checksum: '8710d2c16809c79fee211a9693b64038a8aae99561bc86ce98a9b46b45677fe4') }
...@@ -9,6 +11,8 @@ describe Upload do ...@@ -9,6 +11,8 @@ describe Upload do
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
it 'logs an event to the Geo event log' do it 'logs an event to the Geo event log' do
stub_current_geo_node(primary)
expect { subject.destroy }.to change(Geo::UploadDeletedEvent, :count).by(1) expect { subject.destroy }.to change(Geo::UploadDeletedEvent, :count).by(1)
end end
end end
......
...@@ -170,7 +170,8 @@ describe API::GeoNodes, :geo, :prometheus, api: true do ...@@ -170,7 +170,8 @@ describe API::GeoNodes, :geo, :prometheus, api: true do
end end
it 'returns 200 for the primary node' do it 'returns 200 for the primary node' do
expect(GeoNodeStatus).to receive(:fast_current_node_status).and_return(secondary_status) set_current_geo_node!(primary)
create(:geo_node_status, :healthy, geo_node: primary)
post api("/geo_nodes/#{primary.id}/repair", admin) post api("/geo_nodes/#{primary.id}/repair", admin)
......
...@@ -17,6 +17,10 @@ describe API::Geo do ...@@ -17,6 +17,10 @@ describe API::Geo do
Gitlab::Geo::TransferRequest.new(transfer.request_data.merge(file_id: 100000)).headers Gitlab::Geo::TransferRequest.new(transfer.request_data.merge(file_id: 100000)).headers
end end
before do
stub_current_geo_node(primary_node)
end
shared_examples 'with terms enforced' do shared_examples 'with terms enforced' do
before do before do
enforce_terms enforce_terms
......
...@@ -12,7 +12,7 @@ describe "Git HTTP requests (Geo)", :geo do ...@@ -12,7 +12,7 @@ describe "Git HTTP requests (Geo)", :geo do
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
# Ensure the token always comes from the real time of the request # Ensure the token always comes from the real time of the request
let!(:auth_token) { Gitlab::Geo::BaseRequest.new(scope: project.full_path).authorization } let(:auth_token) { Gitlab::Geo::BaseRequest.new(scope: project.full_path).authorization }
let!(:user) { create(:user) } let!(:user) { create(:user) }
let!(:user_without_any_access) { create(:user) } let!(:user_without_any_access) { create(:user) }
let!(:user_without_push_access) { create(:user) } let!(:user_without_push_access) { create(:user) }
...@@ -29,6 +29,9 @@ describe "Git HTTP requests (Geo)", :geo do ...@@ -29,6 +29,9 @@ describe "Git HTTP requests (Geo)", :geo do
stub_licensed_features(geo: true) stub_licensed_features(geo: true)
stub_current_geo_node(current_node) stub_current_geo_node(current_node)
# Current Geo node must be stubbed before this is instantiated
auth_token
end end
shared_examples_for 'Geo request' do shared_examples_for 'Geo request' do
......
require 'spec_helper' require 'spec_helper'
describe Geo::FileUploadService do describe Geo::FileUploadService do
include EE::GeoHelpers
set(:node) { create(:geo_node, :primary) } set(:node) { create(:geo_node, :primary) }
let(:transfer_request) { Gitlab::Geo::TransferRequest.new(request_data) } let(:transfer_request) { Gitlab::Geo::TransferRequest.new(request_data) }
let(:req_header) { transfer_request.headers['Authorization'] } let(:req_header) { transfer_request.headers['Authorization'] }
before do
stub_current_geo_node(node)
end
shared_examples 'no authorization header' do shared_examples 'no authorization header' do
it 'returns nil' do it 'returns nil' do
service = described_class.new(params, nil) service = described_class.new(params, nil)
......
...@@ -3,32 +3,32 @@ require 'spec_helper' ...@@ -3,32 +3,32 @@ require 'spec_helper'
describe Geo::NodeCreateService do describe Geo::NodeCreateService do
describe '#execute' do describe '#execute' do
it 'creates a new node with valid params' do it 'creates a new node with valid params' do
service = described_class.new(url: 'http://example.com') service = described_class.new(name: 'foo', url: 'http://example.com')
expect { service.execute }.to change(GeoNode, :count).by(1) expect { service.execute }.to change(GeoNode, :count).by(1)
end end
it 'does not create a node with invalid params' do it 'does not create a node with invalid params' do
service = described_class.new(url: 'ftp://example.com') service = described_class.new(name: 'foo', url: 'ftp://example.com')
expect { service.execute }.not_to change(GeoNode, :count) expect { service.execute }.not_to change(GeoNode, :count)
end end
it 'returns true when creation succeeds' do it 'returns true when creation succeeds' do
service = described_class.new(url: 'http://example.com') service = described_class.new(name: 'foo', url: 'http://example.com')
expect(service.execute.persisted?).to eq true expect(service.execute.persisted?).to eq true
end end
it 'returns false when creation fails' do it 'returns false when creation fails' do
service = described_class.new(url: 'ftp://example.com') service = described_class.new(name: 'foo', url: 'ftp://example.com')
expect(service.execute.persisted?).to eq false expect(service.execute.persisted?).to eq false
end end
it 'parses the namespace_ids when node have namespace restrictions' do it 'parses the namespace_ids when node have namespace restrictions' do
groups = create_list(:group, 2) groups = create_list(:group, 2)
params = { url: 'http://example.com', namespace_ids: groups.map(&:id).join(',') } params = { name: 'foo', url: 'http://example.com', namespace_ids: groups.map(&:id).join(',') }
service = described_class.new(params) service = described_class.new(params)
service.execute service.execute
......
...@@ -10,6 +10,10 @@ describe Geo::NodeStatusPostService, :geo do ...@@ -10,6 +10,10 @@ describe Geo::NodeStatusPostService, :geo do
subject { described_class.new } subject { described_class.new }
describe '#execute' do describe '#execute' do
before do
stub_current_geo_node(primary)
end
it 'parses a 401 response' do it 'parses a 401 response' do
response = double(success?: false, response = double(success?: false,
code: 401, code: 401,
...@@ -59,8 +63,6 @@ describe Geo::NodeStatusPostService, :geo do ...@@ -59,8 +63,6 @@ describe Geo::NodeStatusPostService, :geo do
end end
it 'does not include id in the payload' do it 'does not include id in the payload' do
stub_current_geo_node(primary)
expect(Gitlab::HTTP).to receive(:post) expect(Gitlab::HTTP).to receive(:post)
.with( .with(
primary.status_url, primary.status_url,
...@@ -76,8 +78,6 @@ describe Geo::NodeStatusPostService, :geo do ...@@ -76,8 +78,6 @@ describe Geo::NodeStatusPostService, :geo do
end end
it 'sends geo_node_id in the request' do it 'sends geo_node_id in the request' do
stub_current_geo_node(primary)
expect(Gitlab::HTTP).to receive(:post) expect(Gitlab::HTTP).to receive(:post)
.with( .with(
primary.status_url, primary.status_url,
......
require 'spec_helper' require 'spec_helper'
describe Geo::NodeUpdateService do describe Geo::NodeUpdateService do
include EE::GeoHelpers
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
let(:geo_node) { create(:geo_node) } let(:geo_node) { create(:geo_node) }
let(:groups) { create_list(:group, 2) } let(:groups) { create_list(:group, 2) }
let(:namespace_ids) { groups.map(&:id).join(',') } let(:namespace_ids) { groups.map(&:id).join(',') }
before do
stub_current_geo_node(primary)
end
describe '#execute' do describe '#execute' do
it 'updates the node' do it 'updates the node' do
params = { url: 'http://example.com' } params = { url: 'http://example.com' }
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::AfterRenameService do describe Projects::AfterRenameService do
include EE::GeoHelpers
describe '#execute' do describe '#execute' do
context 'when running on a primary node' do context 'when running on a primary node' do
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
...@@ -12,6 +14,10 @@ describe Projects::AfterRenameService do ...@@ -12,6 +14,10 @@ describe Projects::AfterRenameService do
let!(:full_path_before_rename) { project.full_path } let!(:full_path_before_rename) { project.full_path }
let(:path_after_rename) { "#{project.path}-renamed" } let(:path_after_rename) { "#{project.path}-renamed" }
before do
stub_current_geo_node(primary)
end
it 'logs the Geo::RepositoryRenamedEvent for project backed by hashed storage' do it 'logs the Geo::RepositoryRenamedEvent for project backed by hashed storage' do
expect { service_execute }.to change(Geo::RepositoryRenamedEvent, :count) expect { service_execute }.to change(Geo::RepositoryRenamedEvent, :count)
end end
......
require 'spec_helper' require 'spec_helper'
describe Projects::CreateService, '#execute' do describe Projects::CreateService, '#execute' do
include EE::GeoHelpers
let(:user) { create :user } let(:user) { create :user }
let(:opts) do let(:opts) do
{ {
...@@ -197,6 +199,10 @@ describe Projects::CreateService, '#execute' do ...@@ -197,6 +199,10 @@ describe Projects::CreateService, '#execute' do
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
before do
stub_current_geo_node(primary)
end
it 'logs an event to the Geo event log' do it 'logs an event to the Geo event log' do
expect { create_project(user, opts) }.to change(Geo::RepositoryCreatedEvent, :count).by(1) expect { create_project(user, opts) }.to change(Geo::RepositoryCreatedEvent, :count).by(1)
end end
......
require 'spec_helper' require 'spec_helper'
describe Projects::DestroyService do describe Projects::DestroyService do
include EE::GeoHelpers
let!(:user) { create(:user) } let!(:user) { create(:user) }
let!(:project) { create(:project, :repository, namespace: user.namespace) } let!(:project) { create(:project, :repository, namespace: user.namespace) }
let!(:project_id) { project.id } let!(:project_id) { project.id }
...@@ -33,6 +35,10 @@ describe Projects::DestroyService do ...@@ -33,6 +35,10 @@ describe Projects::DestroyService do
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
before do
stub_current_geo_node(primary)
end
it 'logs an event to the Geo event log' do it 'logs an event to the Geo event log' do
# Run Sidekiq immediately to check that renamed repository will be removed # Run Sidekiq immediately to check that renamed repository will be removed
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
......
require 'spec_helper' require 'spec_helper'
describe Projects::HashedStorage::MigrateAttachmentsService do describe Projects::HashedStorage::MigrateAttachmentsService do
include EE::GeoHelpers
let(:project) { create(:project, storage_version: 1) } let(:project) { create(:project, storage_version: 1) }
let(:legacy_storage) { Storage::LegacyProject.new(project) } let(:legacy_storage) { Storage::LegacyProject.new(project) }
let(:hashed_storage) { Storage::HashedProject.new(project) } let(:hashed_storage) { Storage::HashedProject.new(project) }
let(:old_attachments_path) { legacy_storage.disk_path } let(:old_attachments_path) { legacy_storage.disk_path }
let(:new_attachments_path) { hashed_storage.disk_path } let(:new_attachments_path) { hashed_storage.disk_path }
let(:service) { described_class.new(project, old_attachments_path) } let(:service) { described_class.new(project, old_attachments_path) }
set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) }
describe '#execute' do before do
set(:primary) { create(:geo_node, :primary) } stub_current_geo_node(primary)
set(:secondary) { create(:geo_node) } end
describe '#execute' do
context 'on success' do context 'on success' do
before do before do
TestEnv.clean_test_path TestEnv.clean_test_path
......
require 'spec_helper' require 'spec_helper'
describe Projects::HashedStorage::MigrateRepositoryService do describe Projects::HashedStorage::MigrateRepositoryService do
include EE::GeoHelpers
let(:project) { create(:project, :empty_repo, :wiki_repo, :legacy_storage) } let(:project) { create(:project, :empty_repo, :wiki_repo, :legacy_storage) }
let(:legacy_storage) { Storage::LegacyProject.new(project) } let(:legacy_storage) { Storage::LegacyProject.new(project) }
let(:hashed_storage) { Storage::HashedProject.new(project) } let(:hashed_storage) { Storage::HashedProject.new(project) }
...@@ -12,6 +14,7 @@ describe Projects::HashedStorage::MigrateRepositoryService do ...@@ -12,6 +14,7 @@ describe Projects::HashedStorage::MigrateRepositoryService do
before do before do
TestEnv.clean_test_path TestEnv.clean_test_path
stub_current_geo_node(primary)
end end
it 'creates a Geo::HashedStorageMigratedEvent on success' do it 'creates a Geo::HashedStorageMigratedEvent on success' do
......
require 'spec_helper' require 'spec_helper'
describe Projects::TransferService do describe Projects::TransferService do
include EE::GeoHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group) } let(:group) { create(:group) }
let(:project) { create(:project, :repository, namespace: user.namespace) } let(:project) { create(:project, :repository, namespace: user.namespace) }
...@@ -16,6 +18,8 @@ describe Projects::TransferService do ...@@ -16,6 +18,8 @@ describe Projects::TransferService do
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
it 'logs an event to the Geo event log' do it 'logs an event to the Geo event log' do
stub_current_geo_node(primary)
expect { subject.execute(group) }.to change(Geo::RepositoryRenamedEvent, :count).by(1) expect { subject.execute(group) }.to change(Geo::RepositoryRenamedEvent, :count).by(1)
end end
end end
......
module EE module EE
module GeoHelpers module GeoHelpers
# Actually sets the specified node to be the current one, so it works on new
# instances of GeoNode, unlike stub_current_geo_node. But this is slower.
def set_current_geo_node!(node)
node.name = GeoNode.current_node_name
node.save!(validate: false)
end
def stub_current_geo_node(node) def stub_current_geo_node(node)
allow(::Gitlab::Geo).to receive(:current_node).and_return(node) allow(::Gitlab::Geo).to receive(:current_node).and_return(node)
allow(node).to receive(:current?).and_return(true) unless node.nil? allow(node).to receive(:current?).and_return(true) unless node.nil?
......
...@@ -25,5 +25,9 @@ module EE ...@@ -25,5 +25,9 @@ module EE
allow(object).to receive_message_chain(:current_application_settings, setting) { value } allow(object).to receive_message_chain(:current_application_settings, setting) { value }
end end
end end
def stub_geo_setting(messages)
allow(::Gitlab.config.geo).to receive_messages(to_settings(messages))
end
end end
end end
...@@ -11,6 +11,7 @@ describe 'geo rake tasks', :geo do ...@@ -11,6 +11,7 @@ describe 'geo rake tasks', :geo do
describe 'set_primary_node task' do describe 'set_primary_node task' do
before do before do
stub_config_setting(url: 'https://example.com:1234/relative_part') stub_config_setting(url: 'https://example.com:1234/relative_part')
stub_geo_setting(node_name: 'Region 1 node')
end end
it 'creates a GeoNode' do it 'creates a GeoNode' do
...@@ -22,6 +23,7 @@ describe 'geo rake tasks', :geo do ...@@ -22,6 +23,7 @@ describe 'geo rake tasks', :geo do
node = GeoNode.first node = GeoNode.first
expect(node.name).to eq('Region 1 node')
expect(node.uri.scheme).to eq('https') expect(node.uri.scheme).to eq('https')
expect(node.url).to eq('https://example.com:1234/relative_part/') expect(node.url).to eq('https://example.com:1234/relative_part/')
expect(node.primary).to be_truthy expect(node.primary).to be_truthy
......
require 'spec_helper' require 'spec_helper'
describe RemoveUnreferencedLfsObjectsWorker do describe RemoveUnreferencedLfsObjectsWorker do
include EE::GeoHelpers
describe '#perform' do describe '#perform' do
context 'when running in a Geo primary node' do context 'when running in a Geo primary node' do
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
it 'logs an event to the Geo event log for every unreferenced LFS objects' do it 'logs an event to the Geo event log for every unreferenced LFS objects' do
stub_current_geo_node(primary)
unreferenced_lfs_object_1 = create(:lfs_object, :with_file) unreferenced_lfs_object_1 = create(:lfs_object, :with_file)
unreferenced_lfs_object_2 = create(:lfs_object, :with_file) unreferenced_lfs_object_2 = create(:lfs_object, :with_file)
referenced_lfs_object = create(:lfs_object) referenced_lfs_object = create(:lfs_object)
......
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