Remove duplicated code in Geo event store specs

This introduces a shared example that encapsulates the common
behavior present in the parent class removing the duplicated
code in the specs.
parent 611ef60f
# frozen_string_literal: true
require 'spec_helper'
describe Geo::HashedStorageAttachmentsEventStore do
include EE::GeoHelpers
set(:secondary_node) { create(:geo_node) }
let(:project) { create(:project, path: 'bar') }
let(:attachments_event) { build(:geo_hashed_storage_attachments_event, project: project) }
set(:secondary_node) { create(:geo_node) }
let(:old_attachments_path) { attachments_event.old_attachments_path }
let(:new_attachments_path) {attachments_event.new_attachments_path }
subject(:event_store) { described_class.new(project, old_storage_version: 1, new_storage_version: 2, old_attachments_path: old_attachments_path, new_attachments_path: new_attachments_path) }
subject { described_class.new(project, old_storage_version: 1, new_storage_version: 2, old_attachments_path: old_attachments_path, new_attachments_path: new_attachments_path) }
before do
TestEnv.clean_test_path
end
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { event_store.create }.not_to change(Geo::HashedStorageAttachmentsEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::HashedStorageAttachmentsEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { event_store.create }.not_to change(Geo::HashedStorageAttachmentsEvent, :count)
end
it 'creates a attachment migration event' do
expect { event_store.create }.to change(Geo::HashedStorageAttachmentsEvent, :count).by(1)
stub_primary_node
end
it 'tracks project attributes' do
event_store.create
event = Geo::HashedStorageAttachmentsEvent.last
subject.create
expect(event).to have_attributes(
expect(Geo::HashedStorageAttachmentsEvent.last).to have_attributes(
old_attachments_path: old_attachments_path,
new_attachments_path: new_attachments_path
)
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::HashedStorageMigratedEventStore do
let(:project) { create(:project, path: 'bar') }
include EE::GeoHelpers
set(:secondary_node) { create(:geo_node) }
let(:project) { create(:project, path: 'bar') }
let(:old_disk_path) { "#{project.namespace.full_path}/foo" }
let(:old_wiki_disk_path) { "#{old_disk_path}.wiki" }
subject(:event_store) { described_class.new(project, old_storage_version: nil, old_disk_path: old_disk_path, old_wiki_disk_path: old_wiki_disk_path) }
subject { described_class.new(project, old_storage_version: nil, old_disk_path: old_disk_path, old_wiki_disk_path: old_wiki_disk_path) }
describe '#create' do
before do
TestEnv.clean_test_path
end
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
before do
TestEnv.clean_test_path
end
expect { event_store.create }.not_to change(Geo::HashedStorageMigratedEvent, :count)
end
describe '#create' do
it_behaves_like 'a Geo event store', Geo::HashedStorageMigratedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { subject.create }.not_to change(Geo::HashedStorageMigratedEvent, :count)
end
it 'creates a hashed migration event' do
expect { event_store.create }.to change(Geo::HashedStorageMigratedEvent, :count).by(1)
stub_primary_node
end
it 'tracks project attributes' do
event_store.create
event = Geo::HashedStorageMigratedEvent.last
subject.create
expect(event).to have_attributes(
expect(Geo::HashedStorageMigratedEvent.last).to have_attributes(
repository_storage_name: project.repository_storage,
old_storage_version: nil,
new_storage_version: project.storage_version,
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::JobArtifactDeletedEventStore do
include EE::GeoHelpers
set(:secondary_node) { create(:geo_node) }
let(:job_artifact) { create(:ci_job_artifact, :archive) }
subject(:event_store) { described_class.new(job_artifact) }
subject { described_class.new(job_artifact) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { event_store.create }.not_to change(Geo::JobArtifactDeletedEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::JobArtifactDeletedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
stub_primary_node
end
it 'does not create an event when LFS object is not on a local store' do
allow(job_artifact).to receive(:local_store?).and_return(false)
expect { event_store.create }.not_to change(Geo::JobArtifactDeletedEvent, :count)
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { event_store.create }.not_to change(Geo::JobArtifactDeletedEvent, :count)
end
it 'creates a LFS object deleted event' do
expect { event_store.create }.to change(Geo::JobArtifactDeletedEvent, :count).by(1)
expect { subject.create }.not_to change(Geo::JobArtifactDeletedEvent, :count)
end
it 'tracks LFS object attributes' do
event_store.create
event = Geo::JobArtifactDeletedEvent.last
subject.create
expect(event.job_artifact_id).to eq(job_artifact.id)
expect(event.file_path).to match(%r{\A\h+/\h+/\h+/[\d_]+/\d+/\d+/ci_build_artifacts.zip\z})
expect(Geo::JobArtifactDeletedEvent.last).to have_attributes(
job_artifact_id: job_artifact.id,
file_path: match(%r{\A\h+/\h+/\h+/[\d_]+/\d+/\d+/ci_build_artifacts.zip\z})
)
end
it 'logs an error message when event creation fail' do
invalid_job_artifact = create(:ci_job_artifact)
event_store = described_class.new(invalid_job_artifact)
subject = described_class.new(invalid_job_artifact)
expected_message = {
class: "Geo::JobArtifactDeletedEventStore",
......@@ -58,7 +49,7 @@ describe Geo::JobArtifactDeletedEventStore do
expect(Gitlab::Geo::Logger).to receive(:error)
.with(expected_message).and_call_original
event_store.create
subject.create
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::LfsObjectDeletedEventStore do
include EE::GeoHelpers
set(:secondary_node) { create(:geo_node) }
let(:lfs_object) { create(:lfs_object, :with_file, oid: 'b68143e6463773b1b6c6fd009a76c32aeec041faff32ba2ed42fd7f708a00004') }
subject(:event_store) { described_class.new(lfs_object) }
subject { described_class.new(lfs_object) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { event_store.create }.not_to change(Geo::LfsObjectDeletedEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::LfsObjectDeletedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
stub_primary_node
end
it 'does not create an event when LFS object is not on a local store' do
allow(lfs_object).to receive(:local_store?).and_return(false)
expect { event_store.create }.not_to change(Geo::LfsObjectDeletedEvent, :count)
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { event_store.create }.not_to change(Geo::LfsObjectDeletedEvent, :count)
end
it 'creates a LFS object deleted event' do
expect { event_store.create }.to change(Geo::LfsObjectDeletedEvent, :count).by(1)
expect { subject.create }.not_to change(Geo::LfsObjectDeletedEvent, :count)
end
it 'tracks LFS object attributes' do
event_store.create
event = Geo::LfsObjectDeletedEvent.last
subject.create
expect(event).to have_attributes(
expect(Geo::LfsObjectDeletedEvent.last).to have_attributes(
lfs_object_id: lfs_object.id,
oid: lfs_object.oid,
file_path: 'b6/81/43e6463773b1b6c6fd009a76c32aeec041faff32ba2ed42fd7f708a00004'
......@@ -48,7 +37,7 @@ describe Geo::LfsObjectDeletedEventStore do
it 'logs an error message when event creation fail' do
invalid_lfs_object = create(:lfs_object)
event_store = described_class.new(invalid_lfs_object)
subject = described_class.new(invalid_lfs_object)
expected_message = {
class: "Geo::LfsObjectDeletedEventStore",
......@@ -61,7 +50,7 @@ describe Geo::LfsObjectDeletedEventStore do
expect(Gitlab::Geo::Logger).to receive(:error)
.with(expected_message).and_call_original
event_store.create
subject.create
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::RepositoriesChangedEventStore do
include EE::GeoHelpers
let(:geo_node) { create(:geo_node) }
subject { described_class.new(geo_node) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { subject.create }.not_to change(Geo::RepositoriesChangedEvent, :count)
end
it 'creates a repositories changed event when running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { true }
expect { subject.create }.to change(Geo::RepositoriesChangedEvent, :count).by(1)
end
it_behaves_like 'a Geo event store', Geo::RepositoriesChangedEvent
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Geo::RepositoryCreatedEventStore do
include EE::GeoHelpers
set(:project) { create(:project) }
set(:secondary_node) { create(:geo_node) }
subject(:create!) { described_class.new(project).create }
subject { described_class.new(project) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { create! }.not_to change(Geo::RepositoryCreatedEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::RepositoryCreatedEvent
context 'running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { create! }.not_to change(Geo::RepositoryCreatedEvent, :count)
end
it 'creates a created event' do
expect { create! }.to change(Geo::RepositoryCreatedEvent, :count).by(1)
stub_primary_node
end
it 'tracks information for the created project' do
create!
event = Geo::RepositoryCreatedEvent.last
subject.create
expect(event).to have_attributes(
expect(Geo::RepositoryCreatedEvent.last).to have_attributes(
project_id: project.id,
repo_path: project.disk_path,
wiki_path: project.wiki.disk_path,
......@@ -45,10 +33,9 @@ describe Geo::RepositoryCreatedEventStore do
it 'does not set a wiki path if the wiki is disabled' do
project.update!(wiki_enabled: false)
create!
subject.create
event = Geo::RepositoryCreatedEvent.last
expect(event.wiki_path).to be_nil
expect(Geo::RepositoryCreatedEvent.last.wiki_path).to be_nil
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::RepositoryDeletedEventStore do
include EE::GeoHelpers
set(:project) { create(:project, path: 'bar') }
set(:secondary_node) { create(:geo_node) }
let(:project_id) { project.id }
let(:project_name) { project.name }
let(:repo_path) { project.full_path }
......@@ -12,37 +17,23 @@ describe Geo::RepositoryDeletedEventStore do
subject { described_class.new(project, repo_path: repo_path, wiki_path: wiki_path) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { subject.create }.not_to change(Geo::RepositoryDeletedEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::RepositoryDeletedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { subject.create }.not_to change(Geo::RepositoryDeletedEvent, :count)
end
it 'creates a deleted event' do
expect { subject.create }.to change(Geo::RepositoryDeletedEvent, :count).by(1)
stub_primary_node
end
it 'tracks information for the deleted project' do
subject.create
event = Geo::RepositoryDeletedEvent.last
expect(event.project_id).to eq(project_id)
expect(event.deleted_path).to eq(repo_path)
expect(event.deleted_wiki_path).to eq(wiki_path)
expect(event.deleted_project_name).to eq(project_name)
expect(event.repository_storage_name).to eq(storage_name)
expect(Geo::RepositoryDeletedEvent.last).to have_attributes(
project_id: project_id,
deleted_path: repo_path,
deleted_wiki_path: wiki_path,
deleted_project_name: project_name,
repository_storage_name: storage_name
)
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::RepositoryRenamedEventStore do
include EE::GeoHelpers
set(:project) { create(:project, path: 'bar') }
set(:secondary_node) { create(:geo_node) }
let(:old_path) { 'foo' }
let(:old_path_with_namespace) { "#{project.namespace.full_path}/foo" }
subject(:event_store) { described_class.new(project, old_path: old_path, old_path_with_namespace: old_path_with_namespace) }
subject { described_class.new(project, old_path: old_path, old_path_with_namespace: old_path_with_namespace) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { event_store.create }.not_to change(Geo::RepositoryRenamedEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::RepositoryRenamedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { subject.create }.not_to change(Geo::RepositoryRenamedEvent, :count)
end
it 'creates a renamed event' do
expect { event_store.create }.to change(Geo::RepositoryRenamedEvent, :count).by(1)
stub_primary_node
end
it 'tracks old and new paths for project repositories' do
event_store.create
event = Geo::RepositoryRenamedEvent.last
expect(event.repository_storage_name).to eq(project.repository_storage)
expect(event.old_path_with_namespace).to eq(old_path_with_namespace)
expect(event.new_path_with_namespace).to eq(project.disk_path)
expect(event.old_wiki_path_with_namespace).to eq("#{old_path_with_namespace}.wiki")
expect(event.new_wiki_path_with_namespace).to eq(project.wiki.disk_path)
expect(event.old_path).to eq(old_path)
expect(event.new_path).to eq(project.path)
subject.create
expect(Geo::RepositoryRenamedEvent.last).to have_attributes(
repository_storage_name: project.repository_storage,
old_path_with_namespace: old_path_with_namespace,
new_path_with_namespace: project.disk_path,
old_wiki_path_with_namespace: "#{old_path_with_namespace}.wiki",
new_wiki_path_with_namespace: project.wiki.disk_path,
old_path: old_path,
new_path: project.path
)
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::RepositoryUpdatedEventStore do
include EE::GeoHelpers
set(:project) { create(:project, :repository) }
set(:secondary_node) { create(:geo_node) }
let(:blankrev) { Gitlab::Git::BLANK_SHA }
let(:refs) { ['refs/heads/tést', 'refs/tags/tag'] }
......@@ -13,38 +18,18 @@ describe Geo::RepositoryUpdatedEventStore do
]
end
describe '#create' do
it 'does not create a push event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
subject = described_class.new(project, refs: refs, changes: changes)
subject { described_class.new(project, refs: refs, changes: changes) }
expect { subject.create }.not_to change(Geo::RepositoryUpdatedEvent, :count)
end
describe '#create' do
it_behaves_like 'a Geo event store', Geo::RepositoryUpdatedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
subject = described_class.new(project, refs: refs, changes: changes)
expect { subject.create }.not_to change(Geo::RepositoryUpdatedEvent, :count)
end
it 'creates a push event' do
subject = described_class.new(project, refs: refs, changes: changes)
expect { subject.create }.to change(Geo::RepositoryUpdatedEvent, :count).by(1)
stub_primary_node
end
context 'when repository is being updated' do
it 'does not track ref name when post-receive event affect multiple refs' do
subject = described_class.new(project, refs: refs, changes: changes)
subject.create
expect(Geo::RepositoryUpdatedEvent.last.ref).to be_nil
......@@ -61,16 +46,12 @@ describe Geo::RepositoryUpdatedEventStore do
end
it 'tracks number of branches post-receive event affects' do
subject = described_class.new(project, refs: refs, changes: changes)
subject.create
expect(Geo::RepositoryUpdatedEvent.last.branches_affected).to eq 1
end
it 'tracks number of tags post-receive event affects' do
subject = described_class.new(project, refs: refs, changes: changes)
subject.create
expect(Geo::RepositoryUpdatedEvent.last.tags_affected).to eq 1
......@@ -82,7 +63,6 @@ describe Geo::RepositoryUpdatedEventStore do
{ before: '123456', after: '789012', ref: 'refs/heads/tést' },
{ before: blankrev, after: '210987', ref: 'refs/heads/feature' }
]
subject = described_class.new(project, refs: refs, changes: changes)
subject.create
......@@ -110,13 +90,13 @@ describe Geo::RepositoryUpdatedEventStore do
subject.create
push_event = Geo::RepositoryUpdatedEvent.last
expect(push_event.ref).to be_nil
expect(push_event.branches_affected).to be_zero
expect(push_event.tags_affected).to be_zero
expect(push_event.new_branch).to eq false
expect(push_event.remove_branch).to eq false
expect(Geo::RepositoryUpdatedEvent.last).to have_attributes(
ref: be_nil,
branches_affected: be_zero,
tags_affected: be_zero,
new_branch: false,
remove_branch: false
)
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::UploadDeletedEventStore do
include EE::GeoHelpers
set(:secondary_node) { create(:geo_node) }
let(:upload) { create(:upload) }
subject(:event_store) { described_class.new(upload) }
subject { described_class.new(upload) }
describe '#create' do
it 'does not create an event when not running on a primary node' do
allow(Gitlab::Geo).to receive(:primary?) { false }
expect { event_store.create }.not_to change(Geo::UploadDeletedEvent, :count)
end
it_behaves_like 'a Geo event store', Geo::UploadDeletedEvent
context 'when running on a primary node' do
before do
allow(Gitlab::Geo).to receive(:primary?) { true }
end
it 'does not create an event when there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { event_store.create }.not_to change(Geo::UploadDeletedEvent, :count)
end
it 'creates an upload deleted event' do
expect { event_store.create }.to change(Geo::UploadDeletedEvent, :count).by(1)
stub_primary_node
end
it 'does not create an event when the upload does not use local storage' do
allow(upload).to receive(:local?).and_return(false)
expect { event_store.create }.not_to change(Geo::UploadDeletedEvent, :count)
expect { subject.create }.not_to change(Geo::UploadDeletedEvent, :count)
end
it 'tracks upload attributes' do
event_store.create
event = Geo::UploadDeletedEvent.last
subject.create
expect(event).to have_attributes(
expect(Geo::UploadDeletedEvent.last).to have_attributes(
upload_id: upload.id,
file_path: upload.path,
model_id: upload.model_id,
......
# frozen_string_literal: true
shared_examples_for 'a Geo event store' do |event_class|
context 'running on a secondary node' do
before do
stub_secondary_node
end
it 'does not create an event ' do
expect { subject.create }.not_to change(event_class, :count)
end
end
context 'when running on a primary node' do
before do
stub_primary_node
end
it 'does not create an event if there are no secondary nodes' do
allow(Gitlab::Geo).to receive(:secondary_nodes) { [] }
expect { subject.create }.not_to change(event_class, :count)
end
it 'creates an event' do
expect { subject.create }.to change(event_class, :count).by(1)
end
end
end
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