prometheus_controller_spec.rb 1.64 KB
Newer Older
1 2 3 4
require('spec_helper')

describe Projects::PrometheusController do
  let(:user) { create(:user) }
5
  let!(:project) { create(:project) }
6 7 8 9 10

  let(:prometheus_service) { double('prometheus_service') }

  before do
    allow(controller).to receive(:project).and_return(project)
11
    allow(project).to receive(:prometheus_service).and_return(prometheus_service)
12 13 14 15 16 17 18 19

    project.add_master(user)
    sign_in(user)
  end

  describe 'GET #active_metrics' do
    context 'when prometheus metrics are enabled' do
      context 'when data is not present' do
20 21 22 23
        before do
          allow(prometheus_service).to receive(:matched_metrics).and_return({})
        end

24 25 26
        it 'returns no content response' do
          get :active_metrics, project_params(format: :json)

27
          expect(response).to have_gitlab_http_status(204)
28 29 30 31 32 33 34
        end
      end

      context 'when data is available' do
        let(:sample_response) { { some_data: 1 } }

        before do
35
          allow(prometheus_service).to receive(:matched_metrics).and_return(sample_response)
36 37 38 39 40
        end

        it 'returns no content response' do
          get :active_metrics, project_params(format: :json)

41
          expect(response).to have_gitlab_http_status(200)
42 43 44 45 46 47 48 49
          expect(json_response).to eq(sample_response.deep_stringify_keys)
        end
      end

      context 'when requesting non json response' do
        it 'returns not found response' do
          get :active_metrics, project_params

50
          expect(response).to have_gitlab_http_status(404)
51 52 53 54 55 56 57 58
        end
      end
    end
  end

  def project_params(opts = {})
    opts.reverse_merge(namespace_id: project.namespace, project_id: project)
  end
59
end