From 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 18 Feb 2021 10:34:06 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-9-stable-ee --- spec/initializers/lograge_spec.rb | 24 ++++++++- spec/initializers/net_http_patch_spec.rb | 86 ++++++++++++++++++++++++++++++++ spec/initializers/validate_puma_spec.rb | 67 +++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 spec/initializers/net_http_patch_spec.rb create mode 100644 spec/initializers/validate_puma_spec.rb (limited to 'spec/initializers') diff --git a/spec/initializers/lograge_spec.rb b/spec/initializers/lograge_spec.rb index d5f9ef569c7..abb1673bb88 100644 --- a/spec/initializers/lograge_spec.rb +++ b/spec/initializers/lograge_spec.rb @@ -64,11 +64,11 @@ RSpec.describe 'lograge', type: :request do ) expect(Lograge.formatter).to receive(:call) - .with(a_hash_including(cpu_s: 0.11)) + .with(a_hash_including(cpu_s: 0.111112)) .and_call_original expect(Lograge.logger).to receive(:send) - .with(anything, include('"cpu_s":0.11')) + .with(anything, include('"cpu_s":0.111112')) .and_call_original subject @@ -89,6 +89,26 @@ RSpec.describe 'lograge', type: :request do subject end + context 'when logging memory allocations' do + include MemoryInstrumentationHelper + + before do + skip_memory_instrumentation! + end + + it 'logs memory usage metrics' do + expect(Lograge.formatter).to receive(:call) + .with(a_hash_including(:mem_objects)) + .and_call_original + + expect(Lograge.logger).to receive(:send) + .with(anything, include('"mem_objects":')) + .and_call_original + + subject + end + end + it 'limits param size' do expect(Lograge.formatter).to receive(:call) .with(a_hash_including(params: limited_params)) diff --git a/spec/initializers/net_http_patch_spec.rb b/spec/initializers/net_http_patch_spec.rb new file mode 100644 index 00000000000..e5205abbed2 --- /dev/null +++ b/spec/initializers/net_http_patch_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true +require 'fast_spec_helper' + +RSpec.describe 'Net::HTTP patch proxy user and password encoding' do + let(:net_http) { Net::HTTP.new('hostname.example') } + + describe '#proxy_user' do + subject { net_http.proxy_user } + + it { is_expected.to eq(nil) } + + context 'with http_proxy env' do + let(:http_proxy) { 'http://proxy.example:8000' } + + before do + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:[]).with('http_proxy').and_return(http_proxy) + end + + it { is_expected.to eq(nil) } + + context 'and user:password authentication' do + let(:http_proxy) { 'http://Y%5CX:R%25S%5D%20%3FX@proxy.example:8000' } + + context 'when on multiuser safe platform' do + # linux, freebsd, darwin are considered multi user safe platforms + # See https://github.com/ruby/net-http/blob/v0.1.1/lib/net/http.rb#L1174-L1178 + + before do + allow(net_http).to receive(:environment_variable_is_multiuser_safe?).and_return(true) + end + + it { is_expected.to eq 'Y\\X' } + end + + context 'when not on multiuser safe platform' do + before do + allow(net_http).to receive(:environment_variable_is_multiuser_safe?).and_return(false) + end + + it { is_expected.to be_nil } + end + end + end + end + + describe '#proxy_pass' do + subject { net_http.proxy_pass } + + it { is_expected.to eq(nil) } + + context 'with http_proxy env' do + let(:http_proxy) { 'http://proxy.example:8000' } + + before do + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:[]).with('http_proxy').and_return(http_proxy) + end + + it { is_expected.to eq(nil) } + + context 'and user:password authentication' do + let(:http_proxy) { 'http://Y%5CX:R%25S%5D%20%3FX@proxy.example:8000' } + + context 'when on multiuser safe platform' do + # linux, freebsd, darwin are considered multi user safe platforms + # See https://github.com/ruby/net-http/blob/v0.1.1/lib/net/http.rb#L1174-L1178 + + before do + allow(net_http).to receive(:environment_variable_is_multiuser_safe?).and_return(true) + end + + it { is_expected.to eq 'R%S] ?X' } + end + + context 'when not on multiuser safe platform' do + before do + allow(net_http).to receive(:environment_variable_is_multiuser_safe?).and_return(false) + end + + it { is_expected.to be_nil } + end + end + end + end +end diff --git a/spec/initializers/validate_puma_spec.rb b/spec/initializers/validate_puma_spec.rb new file mode 100644 index 00000000000..9ff0ef2c319 --- /dev/null +++ b/spec/initializers/validate_puma_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'validate puma' do + include RakeHelpers + + subject do + load Rails.root.join('config/initializers/validate_puma.rb') + end + + before do + stub_const('Puma', double) + allow(Gitlab::Runtime).to receive(:puma?).and_return(true) + allow(Puma).to receive_message_chain(:cli_config, :options).and_return(workers: workers) + end + + context 'for .com' do + before do + allow(Gitlab).to receive(:com?).and_return(true) + end + + context 'when worker count is 0' do + let(:workers) { 0 } + + specify { expect { subject }.to raise_error(String) } + end + + context 'when worker count is > 0' do + let(:workers) { 2 } + + specify { expect { subject }.not_to raise_error } + end + end + + context 'for other environments' do + before do + allow(Gitlab).to receive(:com?).and_return(false) + + allow(main_object).to receive(:warn) + end + + context 'when worker count is 0' do + let(:workers) { 0 } + + specify { expect { subject }.not_to raise_error } + + it 'warns about running Puma in a Single mode' do + expect(main_object).to receive(:warn) do |warning| + expect(warning).to include('https://gitlab.com/groups/gitlab-org/-/epics/5303') + end + + subject + end + end + + context 'when worker count is > 0' do + let(:workers) { 2 } + + specify { expect { subject }.not_to raise_error } + + it 'does not issue a warning' do + expect(main_object).not_to receive(:warn) + end + end + end +end -- cgit v1.2.3