BigW Consortium Gitlab

deployments_controller_spec.rb 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
require 'spec_helper'

describe Projects::DeploymentsController do
  include ApiHelpers

  let(:user) { create(:user) }
  let(:project) { create(:empty_project) }
  let(:environment) { create(:environment, name: 'production', project: project) }

  before do
11
    project.team << [user, :master]
12 13 14 15 16

    sign_in(user)
  end

  describe 'GET #index' do
17
    it 'returns list of deployments from last 8 hours' do
18 19 20 21
      create(:deployment, environment: environment, created_at: 9.hours.ago)
      create(:deployment, environment: environment, created_at: 7.hours.ago)
      create(:deployment, environment: environment)

22
      get :index, deployment_params(after: 8.hours.ago)
23 24 25 26 27 28 29

      expect(response).to be_ok

      expect(json_response['deployments'].count).to eq(2)
    end

    it 'returns a list with deployments information' do
30
      create(:deployment, environment: environment)
31

32
      get :index, deployment_params
33

34 35
      expect(response).to be_ok
      expect(response).to match_response_schema('deployments')
36 37 38
    end
  end

39 40 41 42 43 44
  describe 'GET #metrics' do
    let(:deployment) { create(:deployment, project: project, environment: environment) }

    before do
      allow(controller).to receive(:deployment).and_return(deployment)
    end
45
    context 'when metrics are disabled' do
46
      before do
47
        allow(deployment).to receive(:has_metrics?).and_return false
48 49
      end

50
      it 'responds with not found' do
51
        get :metrics, deployment_params(id: deployment.id)
52 53

        expect(response).to be_not_found
54 55 56
      end
    end

57 58 59
    context 'when metrics are enabled' do
      before do
        allow(deployment).to receive(:has_metrics?).and_return true
60 61
      end

62 63 64 65 66 67 68 69 70 71
      context 'when environment has no metrics' do
        before do
          expect(deployment).to receive(:metrics).and_return(nil)
        end

        it 'returns a empty response 204 resposne' do
          get :metrics, deployment_params(id: deployment.id)
          expect(response).to have_http_status(204)
          expect(response.body).to eq('')
        end
72 73
      end

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      context 'when environment has some metrics' do
        let(:empty_metrics) do
          {
            success: true,
            metrics: {},
            last_update: 42
          }
        end

        before do
          expect(deployment).to receive(:metrics).and_return(empty_metrics)
        end

        it 'returns a metrics JSON document' do
          get :metrics, deployment_params(id: deployment.id)

          expect(response).to be_ok
          expect(json_response['success']).to be(true)
          expect(json_response['metrics']).to eq({})
          expect(json_response['last_update']).to eq(42)
        end
      end

      context 'when metrics service does not implement deployment metrics' do
        before do
          allow(deployment).to receive(:metrics).and_raise(NotImplementedError)
        end

        it 'responds with not found' do
          get :metrics, deployment_params(id: deployment.id)
104

105 106
          expect(response).to be_not_found
        end
107 108 109 110 111 112 113 114
      end
    end
  end

  def deployment_params(opts = {})
    opts.reverse_merge(namespace_id: project.namespace,
                       project_id: project,
                       environment_id: environment.id)
115 116
  end
end