Welcome to mirror list, hosted at ThFree Co, Russian Federation.

base_exporter_spec.rb « exporter « metrics « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5fd7438d28ab07ca111f1353ddb88007cf0134b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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