BigW Consortium Gitlab

deployment_spec.rb 4.79 KB
Newer Older
1 2
require 'spec_helper'

3
describe Deployment do
4 5 6 7 8 9 10 11 12 13
  subject { build(:deployment) }

  it { is_expected.to belong_to(:project) }
  it { is_expected.to belong_to(:environment) }
  it { is_expected.to belong_to(:user) }
  it { is_expected.to belong_to(:deployable) }

  it { is_expected.to delegate_method(:name).to(:environment).with_prefix }
  it { is_expected.to delegate_method(:commit).to(:project) }
  it { is_expected.to delegate_method(:commit_title).to(:commit).as(:try) }
14
  it { is_expected.to delegate_method(:manual_actions).to(:deployable).as(:try) }
15 16 17

  it { is_expected.to validate_presence_of(:ref) }
  it { is_expected.to validate_presence_of(:sha) }
Z.J. van de Weg committed
18

19
  describe 'after_create callbacks' do
20 21
    let(:environment) { create(:environment) }
    let(:store) { Gitlab::EtagCaching::Store.new }
22

23 24 25 26 27 28
    it 'invalidates the environment etag cache' do
      old_value = store.get(environment.etag_cache_key)

      create(:deployment, environment: environment)

      expect(store.get(environment.etag_cache_key)).not_to eq(old_value)
29 30 31
    end
  end

32
  describe '#includes_commit?' do
33
    let(:project) { create(:project, :repository) }
Z.J. van de Weg committed
34 35
    let(:environment) { create(:environment, project: project) }
    let(:deployment) do
36
      create(:deployment, environment: environment, sha: project.commit.id)
Z.J. van de Weg committed
37 38 39 40
    end

    context 'when there is no project commit' do
      it 'returns false' do
41 42 43
        commit = project.commit('feature')

        expect(deployment.includes_commit?(commit)).to be false
Z.J. van de Weg committed
44 45 46 47 48
      end
    end

    context 'when they share the same tree branch' do
      it 'returns true' do
49 50 51
        commit = project.commit

        expect(deployment.includes_commit?(commit)).to be true
Z.J. van de Weg committed
52 53
      end
    end
54 55 56 57 58 59 60 61 62

    context 'when the SHA for the deployment does not exist in the repo' do
      it 'returns false' do
        deployment.update(sha: Gitlab::Git::BLANK_SHA)
        commit = project.commit

        expect(deployment.includes_commit?(commit)).to be false
      end
    end
Z.J. van de Weg committed
63
  end
Kamil Trzcinski committed
64

65 66 67
  describe '#metrics' do
    let(:deployment) { create(:deployment) }

68
    subject { deployment.metrics }
69 70 71 72 73 74 75 76 77 78

    context 'metrics are disabled' do
      it { is_expected.to eq({}) }
    end

    context 'metrics are enabled' do
      let(:simple_metrics) do
        {
          success: true,
          metrics: {},
79 80
          last_update: 42,
          deployment_time: 1494408956
81 82 83 84
        }
      end

      before do
85
        allow(deployment.project).to receive_message_chain(:monitoring_service, :deployment_metrics)
86 87 88
                                       .with(any_args).and_return(simple_metrics)
      end

89
      it { is_expected.to eq(simple_metrics) }
90 91 92
    end
  end

93
  describe '#additional_metrics' do
94
    let(:project) { create(:project, :repository) }
95
    let(:deployment) { create(:deployment, project: project) }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    subject { deployment.additional_metrics }

    context 'metrics are disabled' do
      it { is_expected.to eq({}) }
    end

    context 'metrics are enabled' do
      let(:simple_metrics) do
        {
          success: true,
          metrics: {},
          last_update: 42
        }
      end

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

      before do
115
        allow(project).to receive(:prometheus_service).and_return(prometheus_service)
116 117 118 119 120 121 122
        allow(prometheus_service).to receive(:additional_deployment_metrics).and_return(simple_metrics)
      end

      it { is_expected.to eq(simple_metrics.merge({ deployment_time: deployment.created_at.to_i })) }
    end
  end

Kamil Trzcinski committed
123 124 125 126 127 128
  describe '#stop_action' do
    let(:build) { create(:ci_build) }

    subject { deployment.stop_action }

    context 'when no other actions' do
129
      let(:deployment) { FactoryBot.build(:deployment, deployable: build) }
Kamil Trzcinski committed
130 131 132 133 134

      it { is_expected.to be_nil }
    end

    context 'with other actions' do
135
      let!(:close_action) { create(:ci_build, :manual, pipeline: build.pipeline, name: 'close_app') }
Kamil Trzcinski committed
136 137

      context 'when matching action is defined' do
138
        let(:deployment) { FactoryBot.build(:deployment, deployable: build, on_stop: 'close_other_app') }
Kamil Trzcinski committed
139 140 141 142 143

        it { is_expected.to be_nil }
      end

      context 'when no matching action is defined' do
144
        let(:deployment) { FactoryBot.build(:deployment, deployable: build, on_stop: 'close_app') }
Kamil Trzcinski committed
145 146 147 148 149 150

        it { is_expected.to eq(close_action) }
      end
    end
  end

Kamil Trzcinski committed
151 152
  describe '#stop_action?' do
    subject { deployment.stop_action? }
Kamil Trzcinski committed
153 154 155 156 157 158 159 160 161

    context 'when no other actions' do
      let(:deployment) { build(:deployment) }

      it { is_expected.to be_falsey }
    end

    context 'when matching action is defined' do
      let(:build) { create(:ci_build) }
162
      let(:deployment) { FactoryBot.build(:deployment, deployable: build, on_stop: 'close_app') }
163
      let!(:close_action) { create(:ci_build, :manual, pipeline: build.pipeline, name: 'close_app') }
Kamil Trzcinski committed
164 165 166 167

      it { is_expected.to be_truthy }
    end
  end
168
end