2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Feature::Gitaly do
|
2021-03-11 19:13:27 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:project_2) { create(:project) }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
let_it_be(:repository) { project.repository.raw }
|
|
|
|
let_it_be(:repository_2) { project_2.repository.raw }
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
before do
|
|
|
|
skip_feature_flags_yaml_validation
|
2023-01-13 00:05:48 +05:30
|
|
|
allow(Feature::Definition).to receive(:get).with(any_args).and_return(
|
|
|
|
Feature::Definition.new('flag.yml', name: :flag, type: :development)
|
|
|
|
)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
describe ".enabled_for_any?" do
|
2021-03-11 19:13:27 +05:30
|
|
|
context 'when the flag is set globally' do
|
|
|
|
context 'when the gate is closed' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_global_flag: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.enabled_for_any?(:gitaly_global_flag)).to be(false)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
context 'when the flag defaults to on' do
|
|
|
|
it 'returns true' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.enabled_for_any?(:gitaly_global_flag)).to be(true)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
context 'when the flag is enabled for a particular project' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_project_flag: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true for that project' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.enabled_for_any?(:gitaly_project_flag, project)).to be(true)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for any other project' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.enabled_for_any?(:gitaly_project_flag, project_2)).to be(false)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when no project is passed' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.enabled_for_any?(:gitaly_project_flag)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the flag is enabled for a particular repository' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_repository_flag: repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true for that repository' do
|
|
|
|
expect(described_class.enabled_for_any?(:gitaly_repository_flag, repository)).to be(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for any other repository' do
|
|
|
|
expect(described_class.enabled_for_any?(:gitaly_repository_flag, repository_2)).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when no repository is passed' do
|
|
|
|
expect(described_class.enabled_for_any?(:gitaly_repository_flag)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the flag is checked with multiple input actors' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_flag: repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if any of the flag is enabled for any of the input actors' do
|
|
|
|
expect(described_class.enabled_for_any?(:gitaly_flag, project, repository)).to be(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if any of the flag is not enabled for any of the input actors' do
|
|
|
|
expect(
|
|
|
|
described_class.enabled_for_any?(:gitaly_flag, project, project_2, repository_2)
|
|
|
|
).to be(false)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".server_feature_flags" do
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
before do
|
2023-01-13 00:05:48 +05:30
|
|
|
stub_feature_flags(
|
|
|
|
gitaly_global_flag: true,
|
|
|
|
gitaly_project_flag: project,
|
|
|
|
gitaly_repository_flag: repository,
|
|
|
|
gitaly_user_flag: user,
|
|
|
|
gitaly_group_flag: group,
|
|
|
|
non_gitaly_flag: false
|
|
|
|
)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
subject { described_class.server_feature_flags }
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'returns a hash of flags starting with the prefix, with dashes instead of underscores' do
|
|
|
|
expect(subject).to eq('gitaly-feature-global-flag' => 'true',
|
2023-01-13 00:05:48 +05:30
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project is passed' do
|
|
|
|
it 'returns the value for the flag on the given project' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.server_feature_flags(project: project))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'true',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
|
|
|
|
expect(described_class.server_feature_flags(project: project_2))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a repository is passed' do
|
|
|
|
it 'returns the value for the flag on the given repository' do
|
|
|
|
expect(described_class.server_feature_flags(repository: repository))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'true',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
|
|
|
|
expect(described_class.server_feature_flags(repository: repository_2))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is passed' do
|
|
|
|
it 'returns the value for the flag on the given user' do
|
|
|
|
expect(described_class.server_feature_flags(user: user))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'true',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
|
|
|
|
expect(described_class.server_feature_flags(user: create(:user)))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a group is passed' do
|
|
|
|
it 'returns the value for the flag on the given group' do
|
|
|
|
expect(described_class.server_feature_flags(group: group))
|
2021-03-11 19:13:27 +05:30
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
2023-01-13 00:05:48 +05:30
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'true')
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
expect(described_class.server_feature_flags(group: create(:group)))
|
2021-03-11 19:13:27 +05:30
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
2023-01-13 00:05:48 +05:30
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple actors are passed' do
|
|
|
|
it 'returns the corresponding enablement status for actors' do
|
|
|
|
expect(described_class.server_feature_flags(project: project_2, repository: repository))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'false',
|
|
|
|
'gitaly-feature-repository-flag' => 'true',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
|
|
|
|
expect(described_class.server_feature_flags(project: project, repository: repository_2))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'true',
|
|
|
|
'gitaly-feature-repository-flag' => 'false',
|
|
|
|
'gitaly-feature-user-flag' => 'false',
|
|
|
|
'gitaly-feature-group-flag' => 'false')
|
|
|
|
|
|
|
|
expect(described_class.server_feature_flags(user: user, project: project, repository: repository, group: group))
|
|
|
|
.to eq('gitaly-feature-global-flag' => 'true',
|
|
|
|
'gitaly-feature-project-flag' => 'true',
|
|
|
|
'gitaly-feature-repository-flag' => 'true',
|
|
|
|
'gitaly-feature-user-flag' => 'true',
|
|
|
|
'gitaly-feature-group-flag' => 'true')
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
context 'when table does not exist' do
|
|
|
|
before do
|
2021-12-11 22:18:48 +05:30
|
|
|
allow(Feature::FlipperFeature.database)
|
|
|
|
.to receive(:cached_table_exists?)
|
|
|
|
.and_return(false)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty Hash' do
|
|
|
|
expect(subject).to eq({})
|
|
|
|
end
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
describe ".user_actor" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
context 'when user is passed in' do
|
|
|
|
it 'returns a actor wrapper from user' do
|
|
|
|
expect(described_class.user_actor(user).flipper_id).to eql(user.flipper_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called without user and user_id is available in application context' do
|
|
|
|
it 'returns a actor wrapper from user_id' do
|
|
|
|
::Gitlab::ApplicationContext.with_context(user: user) do
|
|
|
|
expect(described_class.user_actor.flipper_id).to eql(user.flipper_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called without user and user_id is absent from application context' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.user_actor).to be(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when something else is passed' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.user_actor(1234)).to be(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".project_actor" do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
|
|
|
context 'when project is passed in' do
|
|
|
|
it 'returns a actor wrapper from project' do
|
|
|
|
expect(described_class.project_actor(project).flipper_id).to eql(project.flipper_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when something else is passed in' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.project_actor(1234)).to be(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".group_actor" do
|
|
|
|
let_it_be(:group) { create(:group ) }
|
|
|
|
let_it_be(:project) { create(:project, group: group) }
|
|
|
|
|
|
|
|
context 'when project is passed in' do
|
|
|
|
it "returns a actor wrapper from project's group" do
|
|
|
|
expect(described_class.group_actor(project).flipper_id).to eql(group.flipper_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when something else is passed in' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.group_actor(1234)).to be(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|