Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_ci_ref_status_service_spec.rb « ci « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b069452a5558c3de0bf44fb524c1a6852dccd54 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

require 'spec_helper'

describe Ci::UpdateCiRefStatusService do
  describe '#call' do
    subject { described_class.new(pipeline) }

    shared_examples 'creates ci_ref' do
      it 'creates a ci_ref with the pipeline attributes' do
        expect do
          expect(subject.call).to eq(true)
        end.to change { Ci::Ref.count }.by(1)

        created_ref = pipeline.reload.ref_status
        %w[ref tag project status].each do |attr|
          expect(created_ref[attr]).to eq(pipeline[attr])
        end
      end

      it 'calls PipelineNotificationWorker pasing the ref_status' do
        expect(PipelineNotificationWorker).to receive(:perform_async).with(pipeline.id, ref_status: pipeline.status)

        subject.call
      end
    end

    shared_examples 'updates ci_ref' do
      where(:ref_status, :pipeline_status, :next_status) do
        [
          %w[failed success fixed],
          %w[failed failed failed],
          %w[success success success],
          %w[success failed failed]
        ]
      end

      with_them do
        let(:ci_ref) { create(:ci_ref, status: ref_status) }
        let(:pipeline) { create(:ci_pipeline, status: pipeline_status, project: ci_ref.project, ref: ci_ref.ref) }

        it 'sets ci_ref.status to next_status' do
          expect do
            expect(subject.call).to eq(true)
            expect(ci_ref.reload.status).to eq(next_status)
          end.not_to change { Ci::Ref.count }
        end

        it 'calls PipelineNotificationWorker pasing the ref_status' do
          expect(PipelineNotificationWorker).to receive(:perform_async).with(pipeline.id, ref_status: next_status)

          subject.call
        end
      end
    end

    shared_examples 'does a noop' do
      it "doesn't change ci_ref" do
        expect do
          expect do
            expect(subject.call).to eq(false)
          end.not_to change { ci_ref.reload.status }
        end.not_to change { Ci::Ref.count }
      end

      it "doesn't call PipelineNotificationWorker" do
        expect(PipelineNotificationWorker).not_to receive(:perform_async)

        subject.call
      end
    end

    context "ci_ref doesn't exists" do
      let(:pipeline) { create(:ci_pipeline, :success, ref: 'new-ref') }

      it_behaves_like 'creates ci_ref'

      context 'when an ActiveRecord::RecordNotUnique validation is raised' do
        let(:ci_ref) { create(:ci_ref, status: 'failed') }
        let(:pipeline) { create(:ci_pipeline, status: :success, project: ci_ref.project, ref: ci_ref.ref) }

        it 'reloads the ci_ref and retries once' do
          subject.instance_variable_set("@ref", subject.send(:build_ref))

          expect do
            expect(subject.call).to eq(true)
          end.not_to change { Ci::Ref.count }
          expect(ci_ref.reload.status).to eq('fixed')
        end

        it 'raises error on multiple retries' do
          allow_any_instance_of(Ci::Ref).to receive(:update)
            .and_raise(ActiveRecord::RecordNotUnique)

          expect { subject.call }.to raise_error(ActiveRecord::RecordNotUnique)
        end
      end
    end

    context 'ci_ref exists' do
      let!(:ci_ref) { create(:ci_ref, status: 'failed') }
      let(:pipeline) { ci_ref.pipelines.first }

      it_behaves_like 'updates ci_ref'

      context 'pipeline status is invalid' do
        let!(:pipeline) { create(:ci_pipeline, :running, project: ci_ref.project, ref: ci_ref.ref, tag: ci_ref.tag) }

        it_behaves_like 'does a noop'
      end

      context 'newer pipeline finished' do
        let(:newer_pipeline) { create(:ci_pipeline, :success, project: ci_ref.project, ref: ci_ref.ref, tag: ci_ref.tag) }

        before do
          ci_ref.update!(last_updated_by_pipeline: newer_pipeline)
        end

        it_behaves_like 'does a noop'
      end

      context 'ref is stale' do
        let(:pipeline1) { create(:ci_pipeline, :success, project: ci_ref.project, ref: ci_ref.ref, tag: ci_ref.tag) }
        let(:pipeline2) { create(:ci_pipeline, :success, project: ci_ref.project, ref: ci_ref.ref, tag: ci_ref.tag) }

        it 'reloads the ref and retry' do
          service1 = described_class.new(pipeline1)
          service2 = described_class.new(pipeline2)

          service2.send(:ref)
          service1.call
          expect(ci_ref.reload.status).to eq('fixed')
          expect do
            expect(service2.call).to eq(true)
            # We expect 'success' in this case rather than 'fixed' because
            # the ref is correctly reloaded on stale error.
            expect(ci_ref.reload.status).to eq('success')
          end.not_to change { Ci::Ref.count }
        end

        it 'aborts when a newer pipeline finished' do
          service1 = described_class.new(pipeline1)
          service2 = described_class.new(pipeline2)

          service2.call
          expect do
            expect(service1.call).to eq(false)
            expect(ci_ref.reload.status).to eq('fixed')
          end.not_to change { Ci::Ref.count }
        end
      end

      context 'ref exists as both tag/branch and tag' do
        let(:pipeline) { create(:ci_pipeline, :failed, project: ci_ref.project, ref: ci_ref.ref, tag: true) }
        let!(:branch_pipeline) { create(:ci_pipeline, :success, project: ci_ref.project, ref: ci_ref.ref, tag: false) }

        it_behaves_like 'creates ci_ref'
      end
    end
  end
end