BigW Consortium Gitlab

health_controller_spec.rb 2.95 KB
Newer Older
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
require 'spec_helper'

describe HealthController do
  include StubENV

  let(:token) { current_application_settings.health_check_access_token }
  let(:json_response) { JSON.parse(response.body) }

  before do
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
  end

  describe '#readiness' do
    context 'authorization token provided' do
      before do
        request.headers['TOKEN'] = token
      end

      it 'returns proper response' do
        get :readiness
        expect(json_response['db_check']['status']).to eq('ok')
        expect(json_response['redis_check']['status']).to eq('ok')
        expect(json_response['fs_shards_check']['status']).to eq('ok')
        expect(json_response['fs_shards_check']['labels']['shard']).to eq('default')
      end
    end

    context 'without authorization token' do
      it 'returns proper response' do
        get :readiness
        expect(response.status).to eq(404)
      end
    end
  end

  describe '#liveness' do
    context 'authorization token provided' do
      before do
        request.headers['TOKEN'] = token
      end

      it 'returns proper response' do
        get :liveness
        expect(json_response['db_check']['status']).to eq('ok')
        expect(json_response['redis_check']['status']).to eq('ok')
        expect(json_response['fs_shards_check']['status']).to eq('ok')
      end
    end

    context 'without authorization token' do
      it 'returns proper response' do
        get :liveness
        expect(response.status).to eq(404)
      end
    end
  end

  describe '#metrics' do
    context 'authorization token provided' do
      before do
        request.headers['TOKEN'] = token
      end

      it 'returns DB ping metrics' do
        get :metrics
        expect(response.body).to match(/^db_ping_timeout 0$/)
        expect(response.body).to match(/^db_ping_success 1$/)
        expect(response.body).to match(/^db_ping_latency [0-9\.]+$/)
      end

      it 'returns Redis ping metrics' do
        get :metrics
        expect(response.body).to match(/^redis_ping_timeout 0$/)
        expect(response.body).to match(/^redis_ping_success 1$/)
        expect(response.body).to match(/^redis_ping_latency [0-9\.]+$/)
      end

      it 'returns file system check metrics' do
        get :metrics
        expect(response.body).to match(/^filesystem_access_latency{shard="default"} [0-9\.]+$/)
        expect(response.body).to match(/^filesystem_accessible{shard="default"} 1$/)
        expect(response.body).to match(/^filesystem_write_latency{shard="default"} [0-9\.]+$/)
        expect(response.body).to match(/^filesystem_writable{shard="default"} 1$/)
        expect(response.body).to match(/^filesystem_read_latency{shard="default"} [0-9\.]+$/)
        expect(response.body).to match(/^filesystem_readable{shard="default"} 1$/)
      end
    end

    context 'without authorization token' do
      it 'returns proper response' do
        get :metrics
        expect(response.status).to eq(404)
      end
    end
  end
end