diff options
Diffstat (limited to 'spec/metrics_server/metrics_server_spec.rb')
-rw-r--r-- | spec/metrics_server/metrics_server_spec.rb | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/spec/metrics_server/metrics_server_spec.rb b/spec/metrics_server/metrics_server_spec.rb index 4e3c6900875..fc18df9b5cd 100644 --- a/spec/metrics_server/metrics_server_spec.rb +++ b/spec/metrics_server/metrics_server_spec.rb @@ -8,18 +8,32 @@ require_relative '../support/helpers/next_instance_of' RSpec.describe MetricsServer do # rubocop:disable RSpec/FilePath include NextInstanceOf + let(:prometheus_config) { ::Prometheus::Client.configuration } + let(:metrics_dir) { Dir.mktmpdir } + + # Prometheus::Client is a singleton, i.e. shared global state, so + # we need to reset it after testing. + let!(:old_multiprocess_files_dir) { prometheus_config.multiprocess_files_dir } + before do # We do not want this to have knock-on effects on the test process. allow(Gitlab::ProcessManagement).to receive(:modify_signals) end + after do + Gitlab::Metrics.reset_registry! + prometheus_config.multiprocess_files_dir = old_multiprocess_files_dir + + FileUtils.rm_rf(metrics_dir, secure: true) + end + describe '.spawn' do context 'when in parent process' do it 'forks into a new process and detaches it' do expect(Process).to receive(:fork).and_return(99) expect(Process).to receive(:detach).with(99) - described_class.spawn('sidekiq', metrics_dir: 'path/to/metrics') + described_class.spawn('sidekiq', metrics_dir: metrics_dir) end end @@ -35,13 +49,13 @@ RSpec.describe MetricsServer do # rubocop:disable RSpec/FilePath expect(server).to receive(:start) end - described_class.spawn('sidekiq', metrics_dir: 'path/to/metrics') + described_class.spawn('sidekiq', metrics_dir: metrics_dir) end it 'resets signal handlers from parent process' do expect(Gitlab::ProcessManagement).to receive(:modify_signals).with(%i[A B], 'DEFAULT') - described_class.spawn('sidekiq', metrics_dir: 'path/to/metrics', trapped_signals: %i[A B]) + described_class.spawn('sidekiq', metrics_dir: metrics_dir, trapped_signals: %i[A B]) end end end @@ -49,29 +63,27 @@ RSpec.describe MetricsServer do # rubocop:disable RSpec/FilePath describe '#start' do let(:exporter_class) { Class.new(Gitlab::Metrics::Exporter::BaseExporter) } let(:exporter_double) { double('fake_exporter', start: true) } - let(:prometheus_config) { ::Prometheus::Client.configuration } - let(:metrics_dir) { Dir.mktmpdir } let(:settings) { { "fake_exporter" => { "enabled" => true } } } - let!(:old_metrics_dir) { prometheus_config.multiprocess_files_dir } + let(:ruby_sampler_double) { double(Gitlab::Metrics::Samplers::RubySampler) } subject(:metrics_server) { described_class.new('fake', metrics_dir, true)} before do stub_const('Gitlab::Metrics::Exporter::FakeExporter', exporter_class) - expect(exporter_class).to receive(:instance).with(settings['fake_exporter'], synchronous: true).and_return(exporter_double) + expect(exporter_class).to receive(:instance).with( + settings['fake_exporter'], gc_requests: true, synchronous: true + ).and_return(exporter_double) expect(Settings).to receive(:monitoring).and_return(settings) - end - after do - Gitlab::Metrics.reset_registry! - FileUtils.rm_rf(metrics_dir, secure: true) - prometheus_config.multiprocess_files_dir = old_metrics_dir + allow(Gitlab::Metrics::Samplers::RubySampler).to receive(:initialize_instance).and_return(ruby_sampler_double) + allow(ruby_sampler_double).to receive(:start) end it 'configures ::Prometheus::Client' do metrics_server.start expect(prometheus_config.multiprocess_files_dir).to eq metrics_dir + expect(::Prometheus::Client.configuration.pid_provider.call).to eq 'fake_exporter' end it 'ensures that metrics directory exists in correct mode (0700)' do @@ -105,5 +117,11 @@ RSpec.describe MetricsServer do # rubocop:disable RSpec/FilePath metrics_server.start end + + it 'starts a RubySampler instance' do + expect(ruby_sampler_double).to receive(:start) + + subject.start + end end end |