metrics_controller_spec.rb 2.88 KB
Newer Older
1
2
# frozen_string_literal: true

3
require 'spec_helper'
4

5
describe Projects::Prometheus::MetricsController do
6
  let(:user) { create(:user) }
7
  let(:project) { create(:project) }
8

9
  let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
10
11

  before do
12
    project.add_maintainer(user)
13
14
15
    sign_in(user)
  end

16
  describe 'GET #active_common' do
17
18
19
20
    context 'when prometheus_adapter can query' do
      before do
        allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
      end
21

22
23
24
25
26
      context 'when prometheus metrics are enabled' do
        context 'when data is not present' do
          before do
            allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return({})
          end
27

28
          it 'returns no content response' do
blackst0ne's avatar
blackst0ne committed
29
            get :active_common, params: project_params(format: :json)
30

31
32
            expect(response).to have_gitlab_http_status(204)
          end
33
34
        end

35
36
37
38
39
40
        context 'when data is available' do
          let(:sample_response) { { some_data: 1 } }

          before do
            allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return(sample_response)
          end
41

42
          it 'returns no content response' do
blackst0ne's avatar
blackst0ne committed
43
            get :active_common, params: project_params(format: :json)
44
45
46
47

            expect(response).to have_gitlab_http_status(200)
            expect(json_response).to eq(sample_response.deep_stringify_keys)
          end
48
49
        end

50
51
        context 'when requesting non json response' do
          it 'returns not found response' do
blackst0ne's avatar
blackst0ne committed
52
            get :active_common, params: project_params
53

54
55
            expect(response).to have_gitlab_http_status(404)
          end
56
57
        end
      end
58
    end
59

60
61
62
    context 'when prometheus_adapter cannot query' do
      it 'renders 404' do
        prometheus_adapter = double('prometheus_adapter', can_query?: false)
63

64
65
66
        allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
        allow(prometheus_adapter).to receive(:query).with(:matched_metrics).and_return({})

blackst0ne's avatar
blackst0ne committed
67
        get :active_common, params: project_params(format: :json)
68
69
70
71
72
73
74

        expect(response).to have_gitlab_http_status(404)
      end
    end

    context 'when prometheus_adapter is disabled' do
      it 'renders 404' do
blackst0ne's avatar
blackst0ne committed
75
        get :active_common, params: project_params(format: :json)
76
77

        expect(response).to have_gitlab_http_status(404)
78
79
80
81
      end
    end
  end

82
83
84
85
86
87
  describe '#prometheus_adapter' do
    before do
      allow(controller).to receive(:project).and_return(project)
    end

    it 'calls prometheus adapter service' do
88
89
90
      expect_next_instance_of(::Prometheus::AdapterService) do |instance|
        expect(instance).to receive(:prometheus_adapter)
      end
91
92
93
94
95

      subject.__send__(:prometheus_adapter)
    end
  end

96
97
98
  def project_params(opts = {})
    opts.reverse_merge(namespace_id: project.namespace, project_id: project)
  end
99
end