diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 18:40:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 18:40:28 +0300 |
commit | b595cb0c1dec83de5bdee18284abe86614bed33b (patch) | |
tree | 8c3d4540f193c5ff98019352f554e921b3a41a72 /spec/lib/gitlab/metrics | |
parent | 2f9104a328fc8a4bddeaa4627b595166d24671d0 (diff) |
Add latest changes from gitlab-org/gitlab@15-2-stable-eev15.2.0-rc42
Diffstat (limited to 'spec/lib/gitlab/metrics')
-rw-r--r-- | spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb | 47 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/memory_spec.rb | 40 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/sli_spec.rb | 8 |
4 files changed, 97 insertions, 4 deletions
diff --git a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb index 66fba7ab683..dc5c7eb2e55 100644 --- a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb +++ b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb @@ -19,6 +19,7 @@ RSpec.describe Gitlab::Metrics::Exporter::BaseExporter do allow(settings).to receive(:enabled).and_return(true) allow(settings).to receive(:port).and_return(0) allow(settings).to receive(:address).and_return('127.0.0.1') + allow(settings).to receive(:[]).with('tls_enabled').and_return(false) end after do @@ -88,6 +89,51 @@ RSpec.describe Gitlab::Metrics::Exporter::BaseExporter do exporter end end + + context 'with TLS enabled' do + let(:test_cert) { Rails.root.join('spec/fixtures/x509_certificate.crt').to_s } + let(:test_key) { Rails.root.join('spec/fixtures/x509_certificate_pk.key').to_s } + + before do + allow(settings).to receive(:[]).with('tls_enabled').and_return(true) + allow(settings).to receive(:[]).with('tls_cert_path').and_return(test_cert) + allow(settings).to receive(:[]).with('tls_key_path').and_return(test_key) + end + + it 'injects the necessary OpenSSL config for WEBrick' do + expect(::WEBrick::HTTPServer).to receive(:new).with( + a_hash_including( + SSLEnable: true, + SSLCertificate: an_instance_of(OpenSSL::X509::Certificate), + SSLPrivateKey: an_instance_of(OpenSSL::PKey::RSA), + SSLStartImmediately: true, + SSLExtraChainCert: [] + )) + + exporter.start + end + + context 'with intermediate certificates' do + let(:test_cert) { Rails.root.join('spec/fixtures/clusters/chain_certificates.pem').to_s } + let(:test_key) { Rails.root.join('spec/fixtures/clusters/sample_key.key').to_s } + + it 'injects them in the extra chain' do + expect(::WEBrick::HTTPServer).to receive(:new).with( + a_hash_including( + SSLEnable: true, + SSLCertificate: an_instance_of(OpenSSL::X509::Certificate), + SSLPrivateKey: an_instance_of(OpenSSL::PKey::RSA), + SSLStartImmediately: true, + SSLExtraChainCert: [ + an_instance_of(OpenSSL::X509::Certificate), + an_instance_of(OpenSSL::X509::Certificate) + ] + )) + + exporter.start + end + end + end end describe 'when thread is not alive' do @@ -159,6 +205,7 @@ RSpec.describe Gitlab::Metrics::Exporter::BaseExporter do allow(settings).to receive(:enabled).and_return(true) allow(settings).to receive(:port).and_return(0) allow(settings).to receive(:address).and_return('127.0.0.1') + allow(settings).to receive(:[]).with('tls_enabled').and_return(false) stub_const('Gitlab::Metrics::Exporter::MetricsMiddleware', fake_collector) diff --git a/spec/lib/gitlab/metrics/memory_spec.rb b/spec/lib/gitlab/metrics/memory_spec.rb new file mode 100644 index 00000000000..fd8ca3b37c6 --- /dev/null +++ b/spec/lib/gitlab/metrics/memory_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe Gitlab::Metrics::Memory do + describe '.gc_heap_fragmentation' do + subject(:call) do + described_class.gc_heap_fragmentation( + heap_live_slots: gc_stat_heap_live_slots, + heap_eden_pages: gc_stat_heap_eden_pages + ) + end + + context 'when the Ruby heap is perfectly utilized' do + # All objects are located in a single heap page. + let(:gc_stat_heap_live_slots) { described_class::HEAP_SLOTS_PER_PAGE } + let(:gc_stat_heap_eden_pages) { 1 } + + it { is_expected.to eq(0) } + end + + context 'when the Ruby heap is greatly fragmented' do + # There is one object per heap page. + let(:gc_stat_heap_live_slots) { described_class::HEAP_SLOTS_PER_PAGE } + let(:gc_stat_heap_eden_pages) { described_class::HEAP_SLOTS_PER_PAGE } + + # The heap can never be "perfectly fragmented" because that would require + # zero objects per page. + it { is_expected.to be > 0.99 } + end + + context 'when the Ruby heap is semi-fragmented' do + # All objects are spread over two pages i.e. each page is 50% utilized. + let(:gc_stat_heap_live_slots) { described_class::HEAP_SLOTS_PER_PAGE } + let(:gc_stat_heap_eden_pages) { 2 } + + it { is_expected.to eq(0.5) } + end + end +end diff --git a/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb b/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb index dfae5aa6784..b1566ffa7b4 100644 --- a/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb +++ b/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb @@ -125,5 +125,11 @@ RSpec.describe Gitlab::Metrics::Samplers::RubySampler do sampler.sample end + + it 'adds a heap fragmentation metric' do + expect(sampler.metrics[:heap_fragmentation]).to receive(:set).with({}, anything) + + sampler.sample + end end end diff --git a/spec/lib/gitlab/metrics/sli_spec.rb b/spec/lib/gitlab/metrics/sli_spec.rb index 102ea442b3a..d100f66be19 100644 --- a/spec/lib/gitlab/metrics/sli_spec.rb +++ b/spec/lib/gitlab/metrics/sli_spec.rb @@ -172,11 +172,11 @@ RSpec.describe Gitlab::Metrics::Sli do fake_counter end - def fake_total_counter(name) - fake_prometheus_counter("gitlab_sli:#{name}:total") + def fake_total_counter(name, separator = '_') + fake_prometheus_counter(['gitlab_sli', name, 'total'].join(separator)) end - def fake_numerator_counter(name, numerator_name) - fake_prometheus_counter("gitlab_sli:#{name}:#{numerator_name}_total") + def fake_numerator_counter(name, numerator_name, separator = '_') + fake_prometheus_counter(["gitlab_sli", name, "#{numerator_name}_total"].join(separator)) end end |