summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/health_checks/puma_check_spec.rb
blob: 0bd0965518f46b0abf0e3a7aa56ee17431cfc73f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::HealthChecks::PumaCheck do
  let(:result_class) { Gitlab::HealthChecks::Result }
  let(:readiness) { described_class.readiness }
  let(:metrics) { described_class.metrics }

  shared_examples 'with state' do |(state, message)|
    it "does provide readiness" do
      expect(readiness).to eq(result_class.new('puma_check', state, message))
    end

    it "does provide metrics" do
      expect(metrics).to include(
        an_object_having_attributes(name: 'puma_check_success', value: state ? 1 : 0))
      expect(metrics).to include(
        an_object_having_attributes(name: 'puma_check_latency_seconds', value: be >= 0))
    end
  end

  context 'when Puma is not loaded' do
    before do
      allow(Gitlab::Runtime).to receive(:puma?).and_return(false)
      hide_const('Puma')
    end

    it "does not provide readiness and metrics" do
      expect(readiness).to be_nil
      expect(metrics).to be_nil
    end
  end

  context 'when Puma is loaded' do
    before do
      allow(Gitlab::Runtime).to receive(:puma?).and_return(true)
      stub_const('Puma', Module.new)
    end

    context 'when stats are missing' do
      before do
        expect(Puma).to receive(:stats).and_raise(NoMethodError)
      end

      it_behaves_like 'with state', [false, 'unexpected Puma check result: 0']
    end

    context 'for Single mode' do
      before do
        expect(Puma).to receive(:stats) do
          '{}'
        end
      end

      it_behaves_like 'with state', true
    end

    context 'for Cluster mode' do
      before do
        expect(Puma).to receive(:stats) do
          '{"workers":2}'
        end
      end

      it_behaves_like 'with state', true
    end
  end
end