2022-01-26 12:08:38 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Ci::NamespaceMirror do
|
|
|
|
let!(:group1) { create(:group) }
|
|
|
|
let!(:group2) { create(:group, parent: group1) }
|
|
|
|
let!(:group3) { create(:group, parent: group2) }
|
|
|
|
let!(:group4) { create(:group, parent: group3) }
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
before do
|
|
|
|
# refreshing ci mirrors according to the parent tree above
|
|
|
|
Namespaces::SyncEvent.find_each { |event| Ci::NamespaceMirror.sync!(event) }
|
|
|
|
|
|
|
|
# checking initial situation. we need to reload to reflect the changes of event sync
|
|
|
|
expect(group1.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id])
|
|
|
|
expect(group2.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id, group2.id])
|
|
|
|
expect(group3.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id, group2.id, group3.id])
|
|
|
|
expect(group4.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id, group2.id, group3.id, group4.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'scopes' do
|
2022-04-04 11:22:00 +05:30
|
|
|
describe '.by_group_and_descendants' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let_it_be(:another_group) { create(:group) }
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
subject(:result) { described_class.by_group_and_descendants(group2.id) }
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
it 'returns groups having group2.id in traversal_ids' do
|
|
|
|
expect(result.pluck(:namespace_id)).to contain_exactly(group2.id, group3.id, group4.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.contains_any_of_namespaces' do
|
|
|
|
let!(:other_group1) { create(:group) }
|
|
|
|
let!(:other_group2) { create(:group, parent: other_group1) }
|
|
|
|
let!(:other_group3) { create(:group, parent: other_group2) }
|
|
|
|
|
|
|
|
subject(:result) { described_class.contains_any_of_namespaces([group2.id, other_group2.id]) }
|
|
|
|
|
|
|
|
it 'returns groups having group2.id in traversal_ids' do
|
|
|
|
expect(result.pluck(:namespace_id)).to contain_exactly(
|
|
|
|
group2.id, group3.id, group4.id, other_group2.id, other_group3.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
describe '.contains_traversal_ids' do
|
|
|
|
let!(:other_group1) { create(:group) }
|
|
|
|
let!(:other_group2) { create(:group, parent: other_group1) }
|
|
|
|
let!(:other_group3) { create(:group, parent: other_group2) }
|
|
|
|
let!(:other_group4) { create(:group) }
|
|
|
|
|
|
|
|
subject(:result) { described_class.contains_traversal_ids(all_traversal_ids) }
|
|
|
|
|
|
|
|
context 'when passing a top-level group' do
|
|
|
|
let(:all_traversal_ids) do
|
|
|
|
[
|
|
|
|
[other_group1.id]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only itself and children of that group' do
|
|
|
|
expect(result.map(&:namespace)).to contain_exactly(other_group1, other_group2, other_group3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when passing many levels of groups' do
|
|
|
|
let(:all_traversal_ids) do
|
|
|
|
[
|
|
|
|
[other_group2.parent_id, other_group2.id],
|
|
|
|
[other_group3.parent_id, other_group3.id],
|
|
|
|
[other_group4.id]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only the asked group' do
|
|
|
|
expect(result.map(&:namespace)).to contain_exactly(other_group2, other_group3, other_group4)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when passing invalid data ' do
|
|
|
|
let(:all_traversal_ids) do
|
|
|
|
[
|
|
|
|
["; UPDATE"]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'data is properly sanitised' do
|
|
|
|
expect(result.to_sql).to include "((traversal_ids[1])) IN (('; UPDATE'))"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
describe '.by_namespace_id' do
|
|
|
|
subject(:result) { described_class.by_namespace_id(group2.id) }
|
|
|
|
|
|
|
|
it 'returns namesapce mirrors of namespace id' do
|
|
|
|
expect(result).to contain_exactly(group2.ci_namespace_mirror)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
describe '.sync!' do
|
|
|
|
subject(:sync) { described_class.sync!(Namespaces::SyncEvent.last) }
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
context 'when namespace mirror does not exist in the first place' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:namespace) { group3 }
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
before do
|
|
|
|
namespace.ci_namespace_mirror.destroy!
|
|
|
|
namespace.sync_events.create!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the mirror' do
|
|
|
|
expect { sync }.to change { described_class.count }.from(3).to(4)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(namespace.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id, group2.id, group3.id])
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
context 'when namespace mirror does already exist' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:namespace) { group3 }
|
|
|
|
|
|
|
|
before do
|
2022-03-02 08:16:31 +05:30
|
|
|
namespace.sync_events.create!
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'updates the mirror' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect { sync }.not_to change { described_class.count }
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(namespace.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id, group2.id, group3.id])
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
shared_context 'changing the middle namespace' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:namespace) { group2 }
|
|
|
|
|
|
|
|
before do
|
2022-03-02 08:16:31 +05:30
|
|
|
group2.update!(parent: nil) # creates a sync event
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'updates traversal_ids for the base and descendants' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect { sync }.not_to change { described_class.count }
|
|
|
|
|
|
|
|
expect(group1.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group1.id])
|
|
|
|
expect(group2.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group2.id])
|
|
|
|
expect(group3.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group2.id, group3.id])
|
|
|
|
expect(group4.reload.ci_namespace_mirror).to have_attributes(traversal_ids: [group2.id, group3.id, group4.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it_behaves_like 'changing the middle namespace'
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
context 'when the FFs use_traversal_ids and use_traversal_ids_for_ancestors are disabled' do
|
2022-01-26 12:08:38 +05:30
|
|
|
before do
|
2022-07-23 23:45:48 +05:30
|
|
|
stub_feature_flags(use_traversal_ids: false,
|
2022-01-26 12:08:38 +05:30
|
|
|
use_traversal_ids_for_ancestors: false)
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it_behaves_like 'changing the middle namespace'
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|