BigW Consortium Gitlab

metrics_controller_spec.rb 3.91 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe MetricsController do
  include StubENV

  let(:json_response) { JSON.parse(response.body) }
7
  let(:metrics_multiproc_dir) { Dir.mktmpdir }
8
  let(:whitelisted_ip) { '127.0.0.1' }
9 10 11
  let(:whitelisted_ip_range) { '10.0.0.0/24' }
  let(:ip_in_whitelisted_range) { '10.0.0.1' }
  let(:not_whitelisted_ip) { '10.0.1.1' }
12

13 14
  before do
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
15
    allow(Prometheus::Client.configuration).to receive(:multiprocess_files_dir).and_return(metrics_multiproc_dir)
16
    allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(true)
17
    allow(Settings.monitoring).to receive(:ip_whitelist).and_return([whitelisted_ip, whitelisted_ip_range])
18 19
  end

20
  describe '#index' do
21
    shared_examples_for 'endpoint providing metrics' do
22
      it 'returns DB ping metrics' do
23
        get :index
24

25 26
        expect(response.body).to match(/^db_ping_timeout 0$/)
        expect(response.body).to match(/^db_ping_success 1$/)
27
        expect(response.body).to match(/^db_ping_latency_seconds [0-9\.]+$/)
28 29 30
      end

      it 'returns Redis ping metrics' do
31
        get :index
32

33 34
        expect(response.body).to match(/^redis_ping_timeout 0$/)
        expect(response.body).to match(/^redis_ping_success 1$/)
35
        expect(response.body).to match(/^redis_ping_latency_seconds [0-9\.]+$/)
36 37
      end

38 39 40 41 42
      it 'returns Caching ping metrics' do
        get :index

        expect(response.body).to match(/^redis_cache_ping_timeout 0$/)
        expect(response.body).to match(/^redis_cache_ping_success 1$/)
43
        expect(response.body).to match(/^redis_cache_ping_latency_seconds [0-9\.]+$/)
44 45 46 47 48 49 50
      end

      it 'returns Queues ping metrics' do
        get :index

        expect(response.body).to match(/^redis_queues_ping_timeout 0$/)
        expect(response.body).to match(/^redis_queues_ping_success 1$/)
51
        expect(response.body).to match(/^redis_queues_ping_latency_seconds [0-9\.]+$/)
52 53 54 55 56 57 58
      end

      it 'returns SharedState ping metrics' do
        get :index

        expect(response.body).to match(/^redis_shared_state_ping_timeout 0$/)
        expect(response.body).to match(/^redis_shared_state_ping_success 1$/)
59
        expect(response.body).to match(/^redis_shared_state_ping_latency_seconds [0-9\.]+$/)
60 61
      end

62
      it 'returns file system check metrics' do
63
        get :index
64

65
        expect(response.body).to match(/^filesystem_access_latency_seconds{shard="default"} [0-9\.]+$/)
66
        expect(response.body).to match(/^filesystem_accessible{shard="default"} 1$/)
67
        expect(response.body).to match(/^filesystem_write_latency_seconds{shard="default"} [0-9\.]+$/)
68
        expect(response.body).to match(/^filesystem_writable{shard="default"} 1$/)
69
        expect(response.body).to match(/^filesystem_read_latency_seconds{shard="default"} [0-9\.]+$/)
70 71
        expect(response.body).to match(/^filesystem_readable{shard="default"} 1$/)
      end
72 73

      context 'prometheus metrics are disabled' do
74 75 76
        before do
          allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(false)
        end
77 78

        it 'returns proper response' do
79
          get :index
80

81 82 83
          expect(response.status).to eq(404)
        end
      end
84 85
    end

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    context 'accessed from whitelisted ip' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(whitelisted_ip)
      end

      it_behaves_like 'endpoint providing metrics'
    end

    context 'accessed from ip in whitelisted range' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(ip_in_whitelisted_range)
      end

      it_behaves_like 'endpoint providing metrics'
    end

102 103 104 105 106
    context 'accessed from not whitelisted ip' do
      before do
        allow(Gitlab::RequestContext).to receive(:client_ip).and_return(not_whitelisted_ip)
      end

107
      it 'returns proper response' do
108
        get :index
109

110 111 112 113 114
        expect(response.status).to eq(404)
      end
    end
  end
end