diff options
Diffstat (limited to 'spec/initializers')
-rw-r--r-- | spec/initializers/00_deprecations_spec.rb | 20 | ||||
-rw-r--r-- | spec/initializers/0_log_deprecations_spec.rb | 78 | ||||
-rw-r--r-- | spec/initializers/diagnostic_reports_spec.rb | 65 | ||||
-rw-r--r-- | spec/initializers/global_id_spec.rb | 2 | ||||
-rw-r--r-- | spec/initializers/memory_watchdog_spec.rb | 116 |
5 files changed, 269 insertions, 12 deletions
diff --git a/spec/initializers/00_deprecations_spec.rb b/spec/initializers/00_deprecations_spec.rb new file mode 100644 index 00000000000..e52e64415af --- /dev/null +++ b/spec/initializers/00_deprecations_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe '00_deprecations' do + where(:warning) do + [ + "ActiveModel::Errors#keys is deprecated and will be removed in Rails 6.2", + "Rendering actions with '.' in the name is deprecated:", + "default_hash is deprecated and will be removed from Rails 6.2" + ] + end + + with_them do + specify do + expect { ActiveSupport::Deprecation.warn(warning) } + .to raise_error(ActiveSupport::DeprecationException) + end + end +end diff --git a/spec/initializers/0_log_deprecations_spec.rb b/spec/initializers/0_log_deprecations_spec.rb index f5065126eaf..d34be32f7d0 100644 --- a/spec/initializers/0_log_deprecations_spec.rb +++ b/spec/initializers/0_log_deprecations_spec.rb @@ -11,6 +11,15 @@ RSpec.describe '0_log_deprecations' do load Rails.root.join('config/initializers/0_log_deprecations.rb') end + def with_deprecation_behavior + behavior = ActiveSupport::Deprecation.behavior + ActiveSupport::Deprecation.behavior = deprecation_behavior + yield + ensure + ActiveSupport::Deprecation.behavior = behavior + end + + let(:deprecation_behavior) { :stderr } let(:env_var) { '1' } before do @@ -24,19 +33,39 @@ RSpec.describe '0_log_deprecations' do end around do |example| - # reset state changed by initializer - Warning.clear(&example) + with_deprecation_behavior do + # reset state changed by initializer + Warning.clear(&example) + end end describe 'Ruby deprecations' do - context 'when catching deprecations through Kernel#warn' do - it 'also logs them to deprecation logger' do + shared_examples 'deprecation logger' do + it 'logs them to deprecation logger once and to stderr' do expect(Gitlab::DeprecationJsonLogger).to receive(:info).with( message: 'ABC gem is deprecated', source: 'ruby' ) - expect { warn('ABC gem is deprecated') }.to output.to_stderr + expect { subject }.to output.to_stderr + end + end + + context 'when catching deprecations through Kernel#warn' do + subject { warn('ABC gem is deprecated') } + + include_examples 'deprecation logger' + + context 'with non-notify deprecation behavior' do + let(:deprecation_behavior) { :silence } + + include_examples 'deprecation logger' + end + + context 'with notify deprecation behavior' do + let(:deprecation_behavior) { :notify } + + include_examples 'deprecation logger' end end @@ -60,13 +89,40 @@ RSpec.describe '0_log_deprecations' do end describe 'Rails deprecations' do - it 'logs them to deprecation logger' do - expect(Gitlab::DeprecationJsonLogger).to receive(:info).with( - message: match(/^DEPRECATION WARNING: ABC will be removed/), - source: 'rails' - ) + subject { ActiveSupport::Deprecation.warn('ABC will be removed') } + + shared_examples 'deprecation logger' do + it 'logs them to deprecation logger once' do + expect(Gitlab::DeprecationJsonLogger).to receive(:info).with( + message: match(/^DEPRECATION WARNING: ABC will be removed/), + source: 'rails' + ) + + subject + end + end + + context 'with non-notify deprecation behavior' do + let(:deprecation_behavior) { :silence } + + include_examples 'deprecation logger' + end + + context 'with notify deprecation behavior' do + let(:deprecation_behavior) { :notify } + + include_examples 'deprecation logger' + end + + context 'when deprecations were silenced' do + around do |example| + silenced = ActiveSupport::Deprecation.silenced + ActiveSupport::Deprecation.silenced = true + example.run + ActiveSupport::Deprecation.silenced = silenced + end - expect { ActiveSupport::Deprecation.warn('ABC will be removed') }.to output.to_stderr + include_examples 'deprecation logger' end context 'when disabled via environment' do diff --git a/spec/initializers/diagnostic_reports_spec.rb b/spec/initializers/diagnostic_reports_spec.rb new file mode 100644 index 00000000000..70574194916 --- /dev/null +++ b/spec/initializers/diagnostic_reports_spec.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'diagnostic reports' do + subject(:load_initializer) do + load Rails.root.join('config/initializers/diagnostic_reports.rb') + end + + shared_examples 'does not modify worker startup hooks' do + it do + expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_start) + expect(Gitlab::Memory::ReportsDaemon).not_to receive(:instance) + + load_initializer + end + end + + context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is set to true' do + before do + stub_env('GITLAB_DIAGNOSTIC_REPORTS_ENABLED', true) + end + + context 'when run in application context' do + before do + allow(::Gitlab::Runtime).to receive(:application?).and_return(true) + end + + it 'modifies worker startup hooks' do + report_daemon = instance_double(Gitlab::Memory::ReportsDaemon) + + expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_start).and_call_original + expect(Gitlab::Memory::ReportsDaemon).to receive(:instance).and_return(report_daemon) + expect(report_daemon).to receive(:start) + + load_initializer + end + end + + context 'when run in non-application context, such as rails console or tests' do + before do + allow(::Gitlab::Runtime).to receive(:application?).and_return(false) + end + + include_examples 'does not modify worker startup hooks' + end + end + + context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is not set' do + before do + allow(::Gitlab::Runtime).to receive(:application?).and_return(true) + end + + include_examples 'does not modify worker startup hooks' + end + + context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is set to false' do + before do + stub_env('GITLAB_DIAGNOSTIC_REPORTS_ENABLED', false) + allow(::Gitlab::Runtime).to receive(:application?).and_return(true) + end + + include_examples 'does not modify worker startup hooks' + end +end diff --git a/spec/initializers/global_id_spec.rb b/spec/initializers/global_id_spec.rb index 4deb1833999..edca4533b3a 100644 --- a/spec/initializers/global_id_spec.rb +++ b/spec/initializers/global_id_spec.rb @@ -9,7 +9,7 @@ RSpec.describe 'global_id' do it 'patches GlobalID to find aliased models when a deprecation exists' do allow(Gitlab::GlobalId::Deprecations).to receive(:deprecation_for).and_call_original - allow(Gitlab::GlobalId::Deprecations).to receive(:deprecation_for).with('Issue').and_return(double(new_model_name: 'Project')) + allow(Gitlab::GlobalId::Deprecations).to receive(:deprecation_for).with('Issue').and_return(double(new_name: 'Project')) project = create(:project) gid_string = Gitlab::GlobalId.build(model_name: Issue.name, id: project.id).to_s diff --git a/spec/initializers/memory_watchdog_spec.rb b/spec/initializers/memory_watchdog_spec.rb new file mode 100644 index 00000000000..56f995b5cd3 --- /dev/null +++ b/spec/initializers/memory_watchdog_spec.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe 'memory watchdog' do + subject(:run_initializer) do + load Rails.root.join('config/initializers/memory_watchdog.rb') + end + + context 'when GITLAB_MEMORY_WATCHDOG_ENABLED is truthy' do + let(:env_switch) { 'true' } + + before do + stub_env('GITLAB_MEMORY_WATCHDOG_ENABLED', env_switch) + end + + context 'when runtime is an application' do + let(:watchdog) { instance_double(Gitlab::Memory::Watchdog) } + let(:background_task) { instance_double(Gitlab::BackgroundTask) } + + before do + allow(Gitlab::Runtime).to receive(:application?).and_return(true) + end + + it 'registers a life-cycle hook' do + expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_start) + + run_initializer + end + + shared_examples 'starts watchdog with handler' do |handler_class| + it "uses the #{handler_class} and starts the watchdog" do + expect(Gitlab::Memory::Watchdog).to receive(:new).with( + handler: an_instance_of(handler_class), + logger: Gitlab::AppLogger).and_return(watchdog) + expect(Gitlab::BackgroundTask).to receive(:new).with(watchdog).and_return(background_task) + expect(background_task).to receive(:start) + expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_start).and_yield + + run_initializer + end + end + + # In tests, the Puma constant does not exist so we cannot use a verified double. + # rubocop: disable RSpec/VerifiedDoubles + context 'when puma' do + let(:puma) do + Class.new do + def self.cli_config + Struct.new(:options).new + end + end + end + + before do + stub_const('Puma', puma) + stub_const('Puma::Cluster::WorkerHandle', double.as_null_object) + + allow(Gitlab::Runtime).to receive(:puma?).and_return(true) + end + + it_behaves_like 'starts watchdog with handler', Gitlab::Memory::Watchdog::PumaHandler + end + # rubocop: enable RSpec/VerifiedDoubles + + context 'when sidekiq' do + before do + allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true) + end + + it_behaves_like 'starts watchdog with handler', Gitlab::Memory::Watchdog::TermProcessHandler + end + + context 'when other runtime' do + it_behaves_like 'starts watchdog with handler', Gitlab::Memory::Watchdog::NullHandler + end + end + + context 'when runtime is unsupported' do + it 'does not register life-cycle hook' do + expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_start) + + run_initializer + end + end + end + + context 'when GITLAB_MEMORY_WATCHDOG_ENABLED is false' do + let(:env_switch) { 'false' } + + before do + stub_env('GITLAB_MEMORY_WATCHDOG_ENABLED', env_switch) + # To rule out we return early due to this being false. + allow(Gitlab::Runtime).to receive(:application?).and_return(true) + end + + it 'does not register life-cycle hook' do + expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_start) + + run_initializer + end + end + + context 'when GITLAB_MEMORY_WATCHDOG_ENABLED is not set' do + before do + # To rule out we return early due to this being false. + allow(Gitlab::Runtime).to receive(:application?).and_return(true) + end + + it 'does not register life-cycle hook' do + expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_start) + + run_initializer + end + end +end |