summaryrefslogtreecommitdiff
path: root/spec/services/deployments/older_deployments_drop_service_spec.rb
blob: 7e3074a16888ffc0d136defb3923aef769944cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Deployments::OlderDeploymentsDropService, feature_category: :continuous_delivery do
  let(:environment) { create(:environment) }
  let(:deployment) { create(:deployment, environment: environment) }
  let(:service) { described_class.new(deployment) }

  describe '#execute' do
    subject { service.execute }

    shared_examples 'it does not drop any build' do
      it do
        expect { subject }.to not_change(Ci::Build.failed, :count)
      end
    end

    context 'when deployment is nil' do
      let(:deployment) { nil }

      it_behaves_like 'it does not drop any build'
    end

    context 'when a deployment is passed in' do
      context 'and there is no active deployment for the related environment' do
        let(:deployment) { create(:deployment, :canceled, environment: environment) }
        let(:deployment2) { create(:deployment, :canceled, environment: environment) }

        before do
          deployment
          deployment2
        end

        it_behaves_like 'it does not drop any build'
      end

      context 'and there are active deployment for the related environment' do
        let(:deployment) { create(:deployment, :running, environment: environment) }
        let(:deployment2) { create(:deployment, :running, environment: environment) }

        context 'and there is no older deployment than "deployment"' do
          before do
            deployment
            deployment2
          end

          it_behaves_like 'it does not drop any build'
        end

        context 'and there is an older deployment than "deployment"' do
          let(:older_deployment) { create(:deployment, :running, environment: environment) }

          before do
            older_deployment
            deployment
            deployment2
          end

          it 'drops that older deployment' do
            deployable = older_deployment.deployable
            expect(deployable.failed?).to be_falsey

            subject

            expect(deployable.reload.failed?).to be_truthy
          end

          context 'when older deployable is a manual job' do
            let(:older_deployment) { create(:deployment, :created, environment: environment, deployable: build) }
            let(:build) { create(:ci_build, :manual) }

            # Manual jobs should not be accounted as outdated deployment jobs.
            # See https://gitlab.com/gitlab-org/gitlab/-/issues/255978 for more information.
            it 'does not drop any builds nor track the exception' do
              expect(Gitlab::ErrorTracking).not_to receive(:track_exception)

              expect { subject }.not_to change { Ci::Build.failed.count }
            end
          end

          context 'when deployable.drop raises RuntimeError' do
            before do
              allow_any_instance_of(Ci::Build).to receive(:drop).and_raise(RuntimeError)
            end

            it 'does not drop an older deployment and tracks the exception' do
              expect(Gitlab::ErrorTracking).to receive(:track_exception)
                .with(kind_of(RuntimeError), subject_id: deployment.id, build_id: older_deployment.deployable_id)

              expect { subject }.not_to change { Ci::Build.failed.count }
            end
          end

          context 'when ActiveRecord::StaleObjectError is raised' do
            before do
              allow_any_instance_of(Ci::Build)
                .to receive(:drop).and_raise(ActiveRecord::StaleObjectError)
            end

            it 'resets the object via Gitlab::OptimisticLocking' do
              allow_any_instance_of(Ci::Build).to receive(:reset).at_least(:once)

              subject
            end
          end

          context 'and there is no deployable for that older deployment' do
            let(:older_deployment) { create(:deployment, :running, environment: environment, deployable: nil) }

            it_behaves_like 'it does not drop any build'
          end
        end
      end
    end
  end
end