health_controller_spec.rb 3.66 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe HealthController do
  include StubENV

  let(:json_response) { JSON.parse(response.body) }
7
  let(:token) { current_application_settings.health_check_access_token }
8 9
  let(:whitelisted_ip) { '127.0.0.1' }
  let(:not_whitelisted_ip) { '127.0.0.2' }
10 11

  before do
12
    allow(Settings.monitoring).to receive(:ip_whitelist).and_return([whitelisted_ip])
13
    stub_storage_settings({}) # Hide the broken storage
14 15 16 17
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
  end

  describe '#readiness' do
18
    shared_context 'endpoint responding with readiness data' do
19 20 21
      let(:request_params) { {} }

      subject { get :readiness, request_params }
22

23 24
      it 'responds with readiness checks data' do
        subject
25 26

        expect(json_response['db_check']['status']).to eq('ok')
27 28 29
        expect(json_response['cache_check']['status']).to eq('ok')
        expect(json_response['queues_check']['status']).to eq('ok')
        expect(json_response['shared_state_check']['status']).to eq('ok')
30 31 32 33 34
        expect(json_response['fs_shards_check']['status']).to eq('ok')
        expect(json_response['fs_shards_check']['labels']['shard']).to eq('default')
      end
    end

35 36 37 38 39 40 41 42
    context 'accessed from whitelisted ip' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(whitelisted_ip)
      end

      it_behaves_like 'endpoint responding with readiness data'
    end

43 44 45 46 47
    context 'accessed from not whitelisted ip' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(not_whitelisted_ip)
      end

48
      it 'responds with resource not found' do
49
        get :readiness
50

51 52
        expect(response.status).to eq(404)
      end
53 54 55 56 57 58 59 60 61 62 63 64 65

      context 'accessed with valid token' do
        context 'token passed in request header' do
          before do
            request.headers['TOKEN'] = token
          end

          it_behaves_like 'endpoint responding with readiness data'
        end
      end

      context 'token passed as URL param' do
        it_behaves_like 'endpoint responding with readiness data' do
66
          let(:request_params) { { token: token } }
67 68
        end
      end
69 70 71 72
    end
  end

  describe '#liveness' do
73 74
    shared_context 'endpoint responding with liveness data' do
      subject { get :liveness }
75

76 77
      it 'responds with liveness checks data' do
        subject
78 79

        expect(json_response['db_check']['status']).to eq('ok')
80 81 82
        expect(json_response['cache_check']['status']).to eq('ok')
        expect(json_response['queues_check']['status']).to eq('ok')
        expect(json_response['shared_state_check']['status']).to eq('ok')
83 84 85 86
        expect(json_response['fs_shards_check']['status']).to eq('ok')
      end
    end

87 88 89 90 91 92 93 94
    context 'accessed from whitelisted ip' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(whitelisted_ip)
      end

      it_behaves_like 'endpoint responding with liveness data'
    end

95 96 97 98 99
    context 'accessed from not whitelisted ip' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(not_whitelisted_ip)
      end

100
      it 'responds with resource not found' do
101
        get :liveness
102

103 104
        expect(response.status).to eq(404)
      end
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

      context 'accessed with valid token' do
        context 'token passed in request header' do
          before do
            request.headers['TOKEN'] = token
          end

          it_behaves_like 'endpoint responding with liveness data'
        end

        context 'token passed as URL param' do
          it_behaves_like 'endpoint responding with liveness data' do
            subject { get :liveness, token: token }
          end
        end
      end
121 122 123
    end
  end
end