summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/exporter
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-10-04 06:06:05 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-10-04 06:06:05 +0000
commit535d167d40638105527bc6c9a86f33d2ffd62743 (patch)
tree27c73f33497378f6bb84840223ce7d3fda42cc0a /spec/lib/gitlab/metrics/exporter
parentbee11af56deccfd13243cf5f31d42bd992404066 (diff)
downloadgitlab-ce-535d167d40638105527bc6c9a86f33d2ffd62743.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/metrics/exporter')
-rw-r--r--spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb115
1 files changed, 115 insertions, 0 deletions
diff --git a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb
new file mode 100644
index 00000000000..5fd7438d28a
--- /dev/null
+++ b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb
@@ -0,0 +1,115 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::Metrics::Exporter::BaseExporter do
+ let(:exporter) { described_class.new }
+ let(:server) { double('server') }
+ let(:socket) { double('socket') }
+ let(:log_filename) { File.join(Rails.root, 'log', 'sidekiq_exporter.log') }
+ let(:settings) { double('settings') }
+
+ before do
+ allow(::WEBrick::HTTPServer).to receive(:new).and_return(server)
+ allow(server).to receive(:mount)
+ allow(server).to receive(:start)
+ allow(server).to receive(:shutdown)
+ allow(server).to receive(:listeners) { [socket] }
+ allow(socket).to receive(:close)
+ allow_any_instance_of(described_class).to receive(:log_filename).and_return(log_filename)
+ allow_any_instance_of(described_class).to receive(:settings).and_return(settings)
+ end
+
+ describe 'when exporter is enabled' do
+ before do
+ allow(settings).to receive(:enabled).and_return(true)
+ allow(settings).to receive(:port).and_return(3707)
+ allow(settings).to receive(:address).and_return('localhost')
+ end
+
+ describe 'when exporter is stopped' do
+ describe '#start' do
+ it 'starts the exporter' do
+ expect { exporter.start.join }.to change { exporter.thread? }.from(false).to(true)
+
+ expect(server).to have_received(:start)
+ end
+
+ describe 'with custom settings' do
+ let(:port) { 99999 }
+ let(:address) { 'sidekiq_exporter_address' }
+
+ before do
+ allow(settings).to receive(:port).and_return(port)
+ allow(settings).to receive(:address).and_return(address)
+ end
+
+ it 'starts server with port and address from settings' do
+ exporter.start.join
+
+ expect(::WEBrick::HTTPServer).to have_received(:new).with(
+ Port: port,
+ BindAddress: address,
+ Logger: anything,
+ AccessLog: anything
+ )
+ end
+ end
+ end
+
+ describe '#stop' do
+ it "doesn't shutdown stopped server" do
+ expect { exporter.stop }.not_to change { exporter.thread? }
+
+ expect(server).not_to have_received(:shutdown)
+ end
+ end
+ end
+
+ describe 'when exporter is running' do
+ before do
+ exporter.start.join
+ end
+
+ describe '#start' do
+ it "doesn't start running server" do
+ expect { exporter.start.join }.not_to change { exporter.thread? }
+
+ expect(server).to have_received(:start).once
+ end
+ end
+
+ describe '#stop' do
+ it 'shutdowns server' do
+ expect { exporter.stop }.to change { exporter.thread? }.from(true).to(false)
+
+ expect(socket).to have_received(:close)
+ expect(server).to have_received(:shutdown)
+ end
+ end
+ end
+ end
+
+ describe 'when exporter is disabled' do
+ before do
+ allow(settings).to receive(:enabled).and_return(false)
+ end
+
+ describe '#start' do
+ it "doesn't start" do
+ expect(exporter.start).to be_nil
+ expect { exporter.start }.not_to change { exporter.thread? }
+
+ expect(server).not_to have_received(:start)
+ end
+ end
+
+ describe '#stop' do
+ it "doesn't shutdown" do
+ expect { exporter.stop }.not_to change { exporter.thread? }
+
+ expect(server).not_to have_received(:shutdown)
+ end
+ end
+ end
+end