diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-04 21:08:35 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-04 21:08:35 +0000 |
commit | a653ddb57e30fc5e40a5b2dbd93eed00d8395533 (patch) | |
tree | a805b2e799e896558556392680761e065fe0a1d6 /spec | |
parent | e15501a5e1f54249434167c0198dab775bdc4a1f (diff) | |
download | gitlab-ce-a653ddb57e30fc5e40a5b2dbd93eed00d8395533.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/feature/definition_spec.rb | 36 | ||||
-rw-r--r-- | spec/lib/feature_spec.rb | 79 | ||||
-rw-r--r-- | spec/lib/gitlab/doctor/secrets_spec.rb | 34 | ||||
-rw-r--r-- | spec/lib/gitlab/gon_helper_spec.rb | 1 | ||||
-rw-r--r-- | spec/lib/gitlab/lograge/custom_options_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/project_statistics_spec.rb | 19 | ||||
-rw-r--r-- | spec/requests/api/features_spec.rb | 1 | ||||
-rw-r--r-- | spec/support/helpers/stub_feature_flags.rb | 14 |
8 files changed, 119 insertions, 77 deletions
diff --git a/spec/lib/feature/definition_spec.rb b/spec/lib/feature/definition_spec.rb index 2f95f8eeab7..e5ada68effa 100644 --- a/spec/lib/feature/definition_spec.rb +++ b/spec/lib/feature/definition_spec.rb @@ -165,18 +165,14 @@ RSpec.describe Feature::Definition do using RSpec::Parameterized::TableSyntax let(:definition) do - Feature::Definition.new("development/enabled_feature_flag.yml", - name: :enabled_feature_flag, - type: 'development', - milestone: milestone, - default_enabled: false) + described_class.new("development/enabled_feature_flag.yml", + name: :enabled_feature_flag, + type: 'development', + milestone: milestone, + default_enabled: false) end before do - allow(Feature::Definition).to receive(:definitions) do - { definition.key => definition } - end - allow(Gitlab).to receive(:version_info).and_return(Gitlab::VersionInfo.parse(current_milestone)) end @@ -192,7 +188,7 @@ RSpec.describe Feature::Definition do end with_them do - it {is_expected.to be(expected)} + it { is_expected.to be(expected) } end end @@ -254,23 +250,23 @@ RSpec.describe Feature::Definition do using RSpec::Parameterized::TableSyntax let(:definition) do - Feature::Definition.new("development/enabled_feature_flag.yml", - name: :enabled_feature_flag, - type: 'development', - milestone: milestone, - log_state_changes: log_state_change, - default_enabled: false) + described_class.new("development/enabled_feature_flag.yml", + name: :enabled_feature_flag, + type: 'development', + milestone: milestone, + log_state_changes: log_state_change, + default_enabled: false) end before do - allow(Feature::Definition).to receive(:definitions) do - { definition.key => definition } - end + stub_feature_flag_definition(:enabled_feature_flag, + milestone: milestone, + log_state_changes: log_state_change) allow(Gitlab).to receive(:version_info).and_return(Gitlab::VersionInfo.new(10, 0, 0)) end - subject { Feature::Definition.log_states?(key) } + subject { described_class.log_states?(key) } where(:ctx, :key, :milestone, :log_state_change, :expected) do 'When flag does not exist' | :no_flag | "0.0" | true | false diff --git a/spec/lib/feature_spec.rb b/spec/lib/feature_spec.rb index 54d95ff9e05..92ad899fc5e 100644 --- a/spec/lib/feature_spec.rb +++ b/spec/lib/feature_spec.rb @@ -157,6 +157,9 @@ RSpec.describe Feature, stub_feature_flags: false do describe '.enabled?' do before do allow(Feature).to receive(:log_feature_flag_states?).and_return(false) + + stub_feature_flag_definition(:disabled_feature_flag) + stub_feature_flag_definition(:enabled_feature_flag) end context 'when self-recursive' do @@ -170,15 +173,15 @@ RSpec.describe Feature, stub_feature_flags: false do end it 'returns the default value' do - expect(described_class.enabled?(:ff, default_enabled: true)).to eq true + expect(described_class.enabled?(:enabled_feature_flag, default_enabled: true)).to eq true end it 'detects self recursion' do expect(Gitlab::ErrorTracking) .to receive(:track_exception) - .with(have_attributes(message: 'self recursion'), { stack: [:ff] }) + .with(have_attributes(message: 'self recursion'), { stack: [:enabled_feature_flag] }) - described_class.enabled?(:ff) + described_class.enabled?(:enabled_feature_flag) end end @@ -186,7 +189,7 @@ RSpec.describe Feature, stub_feature_flags: false do before do allow(Feature).to receive(:with_feature).and_wrap_original do |original, name, &block| original.call(name) do |ff| - Feature.enabled?(:"deeper_#{name}") + Feature.enabled?(:"deeper_#{name}", type: :undefined, default_enabled: true) block.call(ff) end end @@ -197,15 +200,21 @@ RSpec.describe Feature, stub_feature_flags: false do .to receive(:track_exception) .with(have_attributes(message: 'deep recursion'), stack: have_attributes(size: be > 10)) - described_class.enabled?(:ff) + described_class.enabled?(:enabled_feature_flag) end end - it 'returns false for undefined feature' do + it 'returns false (and tracks / raises exception for dev) for undefined feature' do + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) + expect(described_class.enabled?(:some_random_feature_flag)).to be_falsey end - it 'returns true for undefined feature with default_enabled' do + it 'returns false for undefined feature with default_enabled: false' do + expect(described_class.enabled?(:some_random_feature_flag, default_enabled: false)).to be_falsey + end + + it 'returns true for undefined feature with default_enabled: true' do expect(described_class.enabled?(:some_random_feature_flag, default_enabled: true)).to be_truthy end @@ -257,15 +266,7 @@ RSpec.describe Feature, stub_feature_flags: false do before do allow(Feature).to receive(:log_feature_flag_states?).and_call_original - definition = Feature::Definition.new("development/enabled_feature_flag.yml", - name: :enabled_feature_flag, - type: 'development', - log_state_changes: true, - default_enabled: false) - - allow(Feature::Definition).to receive(:definitions) do - { definition.key => definition } - end + stub_feature_flag_definition(:enabled_feature_flag, log_state_changes: true) described_class.enable(:feature_flag_state_logs) described_class.enable(:enabled_feature_flag) @@ -283,7 +284,7 @@ RSpec.describe Feature, stub_feature_flags: false do end context 'cached feature flag', :request_store do - let(:flag) { :some_feature_flag } + let(:flag) { :enabled_feature_flag } before do described_class.send(:flipper).memoize = false @@ -467,21 +468,29 @@ RSpec.describe Feature, stub_feature_flags: false do end describe '.disable?' do - it 'returns true for undefined feature' do + it 'returns true (and tracks / raises exception for dev) for undefined feature' do + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) + expect(described_class.disabled?(:some_random_feature_flag)).to be_truthy end - it 'returns false for undefined feature with default_enabled' do + it 'returns true for undefined feature with default_enabled: false' do + expect(described_class.disabled?(:some_random_feature_flag, default_enabled: false)).to be_truthy + end + + it 'returns false for undefined feature with default_enabled: true' do expect(described_class.disabled?(:some_random_feature_flag, default_enabled: true)).to be_falsey end it 'returns true for existing disabled feature in the database' do + stub_feature_flag_definition(:disabled_feature_flag) described_class.disable(:disabled_feature_flag) expect(described_class.disabled?(:disabled_feature_flag)).to be_truthy end it 'returns false for existing enabled feature in the database' do + stub_feature_flag_definition(:enabled_feature_flag) described_class.enable(:enabled_feature_flag) expect(described_class.disabled?(:enabled_feature_flag)).to be_falsey @@ -620,14 +629,7 @@ RSpec.describe Feature, stub_feature_flags: false do let(:log_state_changes) { false } let(:milestone) { "0.0" } let(:flag_name) { :some_flag } - let(:definition) do - Feature::Definition.new("development/#{flag_name}.yml", - name: flag_name, - type: 'development', - milestone: milestone, - log_state_changes: log_state_changes, - default_enabled: false) - end + let(:flag_type) { 'development' } before do Feature.enable(:feature_flag_state_logs) @@ -637,9 +639,10 @@ RSpec.describe Feature, stub_feature_flags: false do allow(Feature).to receive(:log_feature_flag_states?).with(:feature_flag_state_logs).and_call_original allow(Feature).to receive(:log_feature_flag_states?).with(:some_flag).and_call_original - allow(Feature::Definition).to receive(:definitions) do - { definition.key => definition } - end + stub_feature_flag_definition(flag_name, + type: flag_type, + milestone: milestone, + log_state_changes: log_state_changes) end subject { described_class.log_feature_flag_states?(flag_name) } @@ -647,6 +650,7 @@ RSpec.describe Feature, stub_feature_flags: false do context 'when flag is feature_flag_state_logs' do let(:milestone) { "14.6" } let(:flag_name) { :feature_flag_state_logs } + let(:flag_type) { 'ops' } let(:log_state_changes) { true } it { is_expected.to be_falsey } @@ -657,13 +661,7 @@ RSpec.describe Feature, stub_feature_flags: false do end context 'when flag is old while log_state_changes is not present ' do - let(:definition) do - Feature::Definition.new("development/#{flag_name}.yml", - name: flag_name, - type: 'development', - milestone: milestone, - default_enabled: false) - end + let(:log_state_changes) { nil } it { is_expected.to be_falsey } end @@ -685,12 +683,7 @@ RSpec.describe Feature, stub_feature_flags: false do end context 'when milestone is nil' do - let(:definition) do - Feature::Definition.new("development/#{flag_name}.yml", - name: flag_name, - type: 'development', - default_enabled: false) - end + let(:milestone) { nil } it { is_expected.to be_falsey } end diff --git a/spec/lib/gitlab/doctor/secrets_spec.rb b/spec/lib/gitlab/doctor/secrets_spec.rb index f95a7eb1492..efdd6cc1199 100644 --- a/spec/lib/gitlab/doctor/secrets_spec.rb +++ b/spec/lib/gitlab/doctor/secrets_spec.rb @@ -7,10 +7,25 @@ RSpec.describe Gitlab::Doctor::Secrets do let!(:group) { create(:group, runners_token: "test") } let!(:project) { create(:project) } let!(:grafana_integration) { create(:grafana_integration, project: project, token: "test") } + let!(:integration) { create(:integration, project: project, properties: { test_key: "test_value" }) } let(:logger) { double(:logger).as_null_object } subject { described_class.new(logger).run! } + before do + allow(Gitlab::Runtime).to receive(:rake?).and_return(true) + end + + context 'when not ran in a Rake runtime' do + before do + allow(Gitlab::Runtime).to receive(:rake?).and_return(false) + end + + it 'raises an error' do + expect { subject }.to raise_error(StandardError, 'can only be used in a Rake environment') + end + end + context 'when encrypted attributes are properly set' do it 'detects decryptable secrets' do expect(logger).to receive(:info).with(/User failures: 0/) @@ -42,6 +57,25 @@ RSpec.describe Gitlab::Doctor::Secrets do end end + context 'when initializers attempt to use encrypted data' do + it 'skips the initializers and detects bad data' do + integration.encrypted_properties = "invalid" + integration.save! + + expect(logger).to receive(:info).with(/Integration failures: 1/) + + subject + end + + it 'resets the initializers after the task runs' do + subject + + expect(integration).to receive(:initialize_properties) + + integration.run_callbacks(:initialize) + end + end + context 'when GrafanaIntegration token is set via private method' do it 'can access GrafanaIntegration token value' do expect(logger).to receive(:info).with(/GrafanaIntegration failures: 0/) diff --git a/spec/lib/gitlab/gon_helper_spec.rb b/spec/lib/gitlab/gon_helper_spec.rb index 28cb9125af1..dd4dcca809b 100644 --- a/spec/lib/gitlab/gon_helper_spec.rb +++ b/spec/lib/gitlab/gon_helper_spec.rb @@ -44,6 +44,7 @@ RSpec.describe Gitlab::GonHelper do describe '#push_frontend_feature_flag' do before do skip_feature_flags_yaml_validation + skip_default_enabled_yaml_check end it 'pushes a feature flag to the frontend' do diff --git a/spec/lib/gitlab/lograge/custom_options_spec.rb b/spec/lib/gitlab/lograge/custom_options_spec.rb index d8f351bb8a3..58b05be6ff9 100644 --- a/spec/lib/gitlab/lograge/custom_options_spec.rb +++ b/spec/lib/gitlab/lograge/custom_options_spec.rb @@ -96,23 +96,15 @@ RSpec.describe Gitlab::Lograge::CustomOptions do end end - context 'when feature flags are present', :request_store do + context 'when feature flags are present', :request_store do before do allow(Feature).to receive(:log_feature_flag_states?).and_return(false) - definitions = {} [:enabled_feature, :disabled_feature].each do |flag_name| - definitions[flag_name] = Feature::Definition.new("development/enabled_feature.yml", - name: flag_name, - type: 'development', - log_state_changes: true, - default_enabled: false) - + stub_feature_flag_definition(flag_name, log_state_changes: true) allow(Feature).to receive(:log_feature_flag_states?).with(flag_name).and_call_original end - allow(Feature::Definition).to receive(:definitions).and_return(definitions) - Feature.enable(:enabled_feature) Feature.disable(:disabled_feature) end diff --git a/spec/models/project_statistics_spec.rb b/spec/models/project_statistics_spec.rb index 8244e0d052d..2c29d4c42f4 100644 --- a/spec/models/project_statistics_spec.rb +++ b/spec/models/project_statistics_spec.rb @@ -35,7 +35,7 @@ RSpec.describe ProjectStatistics do build_artifacts_size: 1.exabyte, snippets_size: 1.exabyte, pipeline_artifacts_size: 512.petabytes - 1, - uploads_size: 500.petabytes, + uploads_size: 512.petabytes, container_registry_size: 12.petabytes ) @@ -49,7 +49,7 @@ RSpec.describe ProjectStatistics do expect(statistics.storage_size).to eq(8.exabytes - 1) expect(statistics.snippets_size).to eq(1.exabyte) expect(statistics.pipeline_artifacts_size).to eq(512.petabytes - 1) - expect(statistics.uploads_size).to eq(500.petabytes) + expect(statistics.uploads_size).to eq(512.petabytes) expect(statistics.container_registry_size).to eq(12.petabytes) end end @@ -363,7 +363,7 @@ RSpec.describe ProjectStatistics do end describe '#update_storage_size' do - it "sums all storage counters" do + it "sums the relevant storage counters" do statistics.update!( repository_size: 2, wiki_size: 4, @@ -372,13 +372,24 @@ RSpec.describe ProjectStatistics do pipeline_artifacts_size: 3, build_artifacts_size: 3, packages_size: 6, + uploads_size: 5 + ) + + statistics.reload + + expect(statistics.storage_size).to eq 28 + end + + it 'excludes the container_registry_size' do + statistics.update!( + repository_size: 2, uploads_size: 5, container_registry_size: 10 ) statistics.reload - expect(statistics.storage_size).to eq 38 + expect(statistics.storage_size).to eq 7 end it 'works during wiki_size backfill' do diff --git a/spec/requests/api/features_spec.rb b/spec/requests/api/features_spec.rb index a265f67115a..6c88874dd5d 100644 --- a/spec/requests/api/features_spec.rb +++ b/spec/requests/api/features_spec.rb @@ -26,6 +26,7 @@ RSpec.describe API::Features, stub_feature_flags: false do end skip_feature_flags_yaml_validation + skip_default_enabled_yaml_check end describe 'GET /features' do diff --git a/spec/support/helpers/stub_feature_flags.rb b/spec/support/helpers/stub_feature_flags.rb index 77f31169ecb..f1654e55b7e 100644 --- a/spec/support/helpers/stub_feature_flags.rb +++ b/spec/support/helpers/stub_feature_flags.rb @@ -70,4 +70,18 @@ module StubFeatureFlags def skip_default_enabled_yaml_check allow(Feature::Definition).to receive(:default_enabled?).and_return(false) end + + def stub_feature_flag_definition(name, opts = {}) + opts = opts.with_defaults( + name: name, + type: 'development', + default_enabled: false + ) + + Feature::Definition.new("#{opts[:type]}/#{name}.yml", opts).tap do |definition| + all_definitions = Feature::Definition.definitions + all_definitions[definition.key] = definition + allow(Feature::Definition).to receive(:definitions).and_return(all_definitions) + end + end end |