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

require 'spec_helper'

RSpec.describe Gitlab::HealthChecks::Probes::Collection do
  let(:readiness) { described_class.new(*checks) }

  describe '#execute' do
    subject { readiness.execute }

    context 'with all checks' do
      let(:checks) do
        [
          Gitlab::HealthChecks::DbCheck,
          *Gitlab::HealthChecks::Redis::ALL_INSTANCE_CHECKS,
          Gitlab::HealthChecks::GitalyCheck
        ]
      end

      it 'responds with readiness checks data' do
        expect_next_instance_of(Gitlab::GitalyClient::ServerService) do |service|
          expect(service).to receive(:readiness_check).and_return({ success: true })
        end

        expect(subject.http_status).to eq(200)

        expect(subject.json[:status]).to eq('ok')
        expect(subject.json['db_check']).to contain_exactly(status: 'ok')
        expect(subject.json['cache_check']).to contain_exactly(status: 'ok')
        expect(subject.json['queues_check']).to contain_exactly(status: 'ok')
        expect(subject.json['shared_state_check']).to contain_exactly(status: 'ok')
        expect(subject.json['gitaly_check']).to contain_exactly(
          status: 'ok', labels: { shard: 'default' })
      end

      context 'when Redis fails' do
        before do
          allow(Gitlab::HealthChecks::Redis::SharedStateCheck).to receive(:readiness).and_return(
            Gitlab::HealthChecks::Result.new('shared_state_check', false, "check error"))
        end

        it 'responds with failure' do
          expect(subject.http_status).to eq(503)

          expect(subject.json[:status]).to eq('failed')
          expect(subject.json['cache_check']).to contain_exactly(status: 'ok')
          expect(subject.json['shared_state_check']).to contain_exactly(
            status: 'failed', message: 'check error')
        end
      end

      context 'when check raises exception not handled inside the check' do
        before do
          expect(Gitlab::HealthChecks::Redis::CacheCheck).to receive(:readiness).and_raise(
            ::Redis::CannotConnectError, 'Redis down')
        end

        it 'responds with failure including the exception info' do
          expect(subject.http_status).to eq(500)

          expect(subject.json[:status]).to eq('failed')
          expect(subject.json[:message]).to eq('Redis::CannotConnectError : Redis down')
        end
      end

      context 'when some checks are not available' do
        before do
          allow(Gitlab::Runtime).to receive(:puma_in_clustered_mode?).and_return(false)
        end

        let(:checks) do
          [
            Gitlab::HealthChecks::MasterCheck
          ]
        end

        it 'asks for check availability' do
          expect(Gitlab::HealthChecks::MasterCheck).to receive(:available?)

          subject
        end

        it 'does not call `readiness` on checks that are not available' do
          expect(Gitlab::HealthChecks::MasterCheck).not_to receive(:readiness)

          subject
        end

        it 'does not fail collection check' do
          expect(subject.http_status).to eq(200)
          expect(subject.json[:status]).to eq('ok')
        end
      end
    end

    context 'without checks' do
      let(:checks) { [] }

      it 'responds with success' do
        expect(subject.http_status).to eq(200)

        expect(subject.json).to eq(status: 'ok')
      end
    end
  end
end