health_check_controller_spec.rb 4.41 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
describe HealthCheckController, :request_store do
6 7
  include StubENV

8
  let(:xml_response) { Hash.from_xml(response.body)['hash'] }
9
  let(:token) { Gitlab::CurrentSettings.health_check_access_token }
10 11
  let(:whitelisted_ip) { '127.0.0.1' }
  let(:not_whitelisted_ip) { '127.0.0.2' }
12

13
  before do
14
    allow(Settings.monitoring).to receive(:ip_whitelist).and_return([whitelisted_ip])
15 16 17
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
  end

18
  describe 'GET #index' do
19 20
    context 'when services are up but accessed from outside whitelisted ips' do
      before do
21
        allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(not_whitelisted_ip)
22 23
      end

24 25
      it 'returns a not found page' do
        get :index
26

27 28
        expect(response).to be_not_found
      end
29 30 31 32

      context 'when services are accessed with token' do
        it 'supports passing the token in the header' do
          request.headers['TOKEN'] = token
33

34
          get :index
35

36
          expect(response).to be_successful
37 38 39
          expect(response.content_type).to eq 'text/plain'
        end

40
        it 'supports passing the token in query params' do
blackst0ne's avatar
blackst0ne committed
41
          get :index, params: { token: token }
42

43
          expect(response).to be_successful
44 45 46
          expect(response.content_type).to eq 'text/plain'
        end
      end
47 48
    end

49 50
    context 'when services are up and accessed from whitelisted ips' do
      before do
51
        allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(whitelisted_ip)
52 53
      end

54
      it 'supports successful plaintext response' do
55
        get :index
56

57
        expect(response).to be_successful
58 59 60 61
        expect(response.content_type).to eq 'text/plain'
      end

      it 'supports successful json response' do
62
        get :index, format: :json
63

64
        expect(response).to be_successful
65 66 67 68 69
        expect(response.content_type).to eq 'application/json'
        expect(json_response['healthy']).to be true
      end

      it 'supports successful xml response' do
70
        get :index, format: :xml
71

72
        expect(response).to be_successful
73 74 75 76 77
        expect(response.content_type).to eq 'application/xml'
        expect(xml_response['healthy']).to be true
      end

      it 'supports successful responses for specific checks' do
blackst0ne's avatar
blackst0ne committed
78
        get :index, params: { checks: 'email' }, format: :json
79

80
        expect(response).to be_successful
81 82 83 84 85 86 87 88
        expect(response.content_type).to eq 'application/json'
        expect(json_response['healthy']).to be true
      end
    end

    context 'when a service is down but NO access token' do
      it 'returns a not found page' do
        get :index
89

90 91 92 93
        expect(response).to be_not_found
      end
    end

94
    context 'when a service is down and an endpoint is accessed from whitelisted ip' do
95
      before do
96 97
        allow(HealthCheck::Utils).to receive(:process_checks).with(['standard']).and_return('The server is on fire')
        allow(HealthCheck::Utils).to receive(:process_checks).with(['email']).and_return('Email is on fire')
98
        allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(whitelisted_ip)
99 100
      end

101
      it 'supports failure plaintext response' do
102
        get :index
103

104
        expect(response).to have_gitlab_http_status(500)
105 106 107 108 109
        expect(response.content_type).to eq 'text/plain'
        expect(response.body).to include('The server is on fire')
      end

      it 'supports failure json response' do
110
        get :index, format: :json
111

112
        expect(response).to have_gitlab_http_status(500)
113 114 115 116 117 118
        expect(response.content_type).to eq 'application/json'
        expect(json_response['healthy']).to be false
        expect(json_response['message']).to include('The server is on fire')
      end

      it 'supports failure xml response' do
119
        get :index, format: :xml
120

121
        expect(response).to have_gitlab_http_status(500)
122 123 124 125 126 127
        expect(response.content_type).to eq 'application/xml'
        expect(xml_response['healthy']).to be false
        expect(xml_response['message']).to include('The server is on fire')
      end

      it 'supports failure responses for specific checks' do
blackst0ne's avatar
blackst0ne committed
128
        get :index, params: { checks: 'email' }, format: :json
129

130
        expect(response).to have_gitlab_http_status(500)
131 132 133 134 135 136 137
        expect(response.content_type).to eq 'application/json'
        expect(json_response['healthy']).to be false
        expect(json_response['message']).to include('Email is on fire')
      end
    end
  end
end