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

ref_spec.rb « ci « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2727c7701b84dba226f801d19016818ac344253d (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::Ref, feature_category: :continuous_integration do
  using RSpec::Parameterized::TableSyntax

  it { is_expected.to belong_to(:project) }

  describe '.ensure_for' do
    let_it_be(:project) { create(:project, :repository) }

    subject { described_class.ensure_for(pipeline) }

    shared_examples_for 'ensures ci_ref' do
      context 'when ci_ref already exists' do
        let(:options) { {} }

        it 'returns an existing ci_ref' do
          expect { subject }.not_to change { described_class.count }

          expect(subject).to eq(described_class.find_by(project_id: project.id, ref_path: expected_ref_path))
        end
      end

      context 'when ci_ref does not exist yet' do
        let(:options) { { ci_ref_presence: false } }

        it 'creates a new ci_ref' do
          expect { subject }.to change { described_class.count }.by(1)

          expect(subject).to eq(described_class.find_by(project_id: project.id, ref_path: expected_ref_path))
        end
      end
    end

    context 'when pipeline is a branch pipeline' do
      let!(:pipeline) { create(:ci_pipeline, ref: 'master', project: project, **options) }
      let(:expected_ref_path) { 'refs/heads/master' }

      it_behaves_like 'ensures ci_ref'
    end

    context 'when pipeline is a tag pipeline' do
      let!(:pipeline) { create(:ci_pipeline, ref: 'v1.1.0', tag: true, project: project, **options) }
      let(:expected_ref_path) { 'refs/tags/v1.1.0' }

      it_behaves_like 'ensures ci_ref'
    end

    context 'when pipeline is a detached merge request pipeline' do
      let(:merge_request) do
        create(
          :merge_request,
          target_project: project, target_branch: 'master',
          source_project: project, source_branch: 'feature'
        )
      end

      let!(:pipeline) do
        create(:ci_pipeline, :detached_merge_request_pipeline, merge_request: merge_request, project: project, **options)
      end

      let(:expected_ref_path) { 'refs/heads/feature' }

      it_behaves_like 'ensures ci_ref'
    end
  end

  describe '#last_finished_pipeline_id' do
    let(:pipeline_status) { :running }
    let(:pipeline_source) { Enums::Ci::Pipeline.sources[:push] }
    let(:pipeline) { create(:ci_pipeline, pipeline_status, source: pipeline_source) }
    let(:ci_ref) { pipeline.ci_ref }

    context 'when there are no finished pipelines' do
      it 'returns nil' do
        expect(ci_ref.last_finished_pipeline_id).to be_nil
      end
    end

    context 'when there are finished pipelines' do
      let(:pipeline_status) { :success }

      it 'returns the pipeline id' do
        expect(ci_ref.last_finished_pipeline_id).to eq(pipeline.id)
      end

      context 'when the pipeline a dangling pipeline' do
        let(:pipeline_source) { Enums::Ci::Pipeline.sources[:ondemand_dast_scan] }

        it 'returns nil' do
          expect(ci_ref.last_finished_pipeline_id).to be_nil
        end
      end
    end
  end

  describe '#update_status_by!' do
    subject { ci_ref.update_status_by!(pipeline) }

    let!(:ci_ref) { create(:ci_ref) }

    shared_examples_for 'no-op' do
      it 'does nothing and returns nil' do
        expect { subject }.not_to change { ci_ref.status_name }

        is_expected.to be_nil
      end
    end

    context 'when pipeline status is success or failed' do
      using RSpec::Parameterized::TableSyntax

      where(:pipeline_status, :current_ref_status, :expected_ref_status) do
        :success   | :unknown       | :success
        :success   | :success       | :success
        :success   | :failed        | :fixed
        :success   | :fixed         | :success
        :success   | :broken        | :fixed
        :success   | :still_failing | :fixed
        :failed    | :unknown       | :failed
        :failed    | :success       | :broken
        :failed    | :failed        | :still_failing
        :failed    | :fixed         | :broken
        :failed    | :broken        | :still_failing
        :failed    | :still_failing | :still_failing
      end

      with_them do
        let(:ci_ref) { create(:ci_ref, status: described_class.state_machines[:status].states[current_ref_status].value) }
        let(:pipeline) { create(:ci_pipeline, status: pipeline_status, ci_ref: ci_ref) }

        it 'transitions the status via state machine' do
          expect(subject).to eq(expected_ref_status)
          expect(ci_ref.status_name).to eq(expected_ref_status)
        end
      end
    end

    context 'when pipeline status is success' do
      let(:pipeline) { create(:ci_pipeline, :success, ci_ref: ci_ref) }

      it 'updates the status' do
        expect { subject }.to change { ci_ref.status_name }.from(:unknown).to(:success)

        is_expected.to eq(:success)
      end
    end

    context 'when pipeline status is canceled' do
      let(:pipeline) { create(:ci_pipeline, status: :canceled, ci_ref: ci_ref) }

      it { is_expected.to eq(:unknown) }
    end

    context 'when pipeline status is skipped' do
      let(:pipeline) { create(:ci_pipeline, status: :skipped, ci_ref: ci_ref) }

      it_behaves_like 'no-op'
    end

    context 'when pipeline status is not complete' do
      let(:pipeline) { create(:ci_pipeline, :running, ci_ref: ci_ref) }

      it_behaves_like 'no-op'
    end

    context 'when pipeline is not the latest pipeline' do
      let!(:pipeline) { create(:ci_pipeline, :success, ci_ref: ci_ref) }
      let!(:latest_pipeline) { create(:ci_pipeline, :success, ci_ref: ci_ref) }

      it_behaves_like 'no-op'
    end

    context 'when pipeline does not belong to the ci_ref' do
      let(:pipeline) { create(:ci_pipeline, :success, ci_ref: create(:ci_ref)) }

      it_behaves_like 'no-op'
    end
  end

  context 'loose foreign key on ci_refs.project_id' do
    it_behaves_like 'cleanup by a loose foreign key' do
      let!(:parent) { create(:project) }
      let!(:model) { create(:ci_ref, project: parent) }
    end
  end

  describe '#last_successful_ci_source_pipeline' do
    let_it_be(:ci_ref) { create(:ci_ref) }

    let(:ci_source) { Enums::Ci::Pipeline.sources[:push] }
    let(:dangling_source) { Enums::Ci::Pipeline.sources[:parent_pipeline] }

    subject(:result) { ci_ref.last_successful_ci_source_pipeline }

    context 'when there are no successful CI source pipelines' do
      let!(:running_ci_source) { create(:ci_pipeline, :running, ci_ref: ci_ref, source: ci_source) }
      let!(:successful_dangling_source) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: dangling_source) }

      it { is_expected.to be_nil }
    end

    context 'when there are successful CI source pipelines' do
      context 'and the latest pipeline is a successful CI source pipeline' do
        let!(:failed_ci_source) { create(:ci_pipeline, :failed, ci_ref: ci_ref, source: ci_source) }
        let!(:successful_dangling_source) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: dangling_source, child_of: failed_ci_source) }
        let!(:successful_ci_source) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: ci_source) }

        it 'returns the last successful CI source pipeline' do
          expect(result).to eq(successful_ci_source)
        end
      end

      context 'and there is a newer successful dangling source pipeline than the successful CI source pipelines' do
        let!(:successful_ci_source_1) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: ci_source) }
        let!(:successful_ci_source_2) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: ci_source) }
        let!(:failed_ci_source) { create(:ci_pipeline, :failed, ci_ref: ci_ref, source: ci_source) }
        let!(:successful_dangling_source) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: dangling_source, child_of: failed_ci_source) }

        it 'returns the last successful CI source pipeline' do
          expect(result).to eq(successful_ci_source_2)
        end

        context 'and the newer successful dangling source is a child of a successful CI source pipeline' do
          let!(:parent_ci_source) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: ci_source) }
          let!(:successful_child_source) { create(:ci_pipeline, :success, ci_ref: ci_ref, source: dangling_source, child_of: parent_ci_source) }

          it 'returns the parent pipeline instead' do
            expect(result).to eq(parent_ci_source)
          end
        end
      end
    end
  end

  describe '#last_unlockable_ci_source_pipeline' do
    let(:ci_source) { Enums::Ci::Pipeline.sources[:push] }
    let(:dangling_source) { Enums::Ci::Pipeline.sources[:parent_pipeline] }

    let_it_be(:project) { create(:project) }
    let_it_be(:ci_ref) { create(:ci_ref, project: project) }

    subject(:result) { ci_ref.last_unlockable_ci_source_pipeline }

    context 'when there are unlockable pipelines in the ref' do
      context 'and the last CI source pipeline in the ref is unlockable' do
        let!(:unlockable_ci_source_1) { create(:ci_pipeline, :success, project: project, ci_ref: ci_ref, source: ci_source) }
        let!(:unlockable_ci_source_2) { create(:ci_pipeline, :blocked, project: project, ci_ref: ci_ref, source: ci_source) }

        it 'returns the CI source pipeline' do
          expect(result).to eq(unlockable_ci_source_2)
        end

        context 'and it has unlockable child pipelines' do
          let!(:child) { create(:ci_pipeline, :success, project: project, ci_ref: ci_ref, source: dangling_source, child_of: unlockable_ci_source_2) }
          let!(:child_2) { create(:ci_pipeline, :success, project: project, ci_ref: ci_ref, source: dangling_source, child_of: unlockable_ci_source_2) }

          it 'returns the parent CI source pipeline' do
            expect(result).to eq(unlockable_ci_source_2)
          end
        end

        context 'and it has a non-unlockable child pipeline' do
          let!(:child) { create(:ci_pipeline, :running, project: project, ci_ref: ci_ref, source: dangling_source, child_of: unlockable_ci_source_2) }

          it 'returns the parent CI source pipeline' do
            expect(result).to eq(unlockable_ci_source_2)
          end
        end
      end

      context 'and the last CI source pipeline in the ref is not unlockable' do
        let!(:unlockable_ci_source) { create(:ci_pipeline, :skipped, project: project, ci_ref: ci_ref, source: ci_source) }
        let!(:unlockable_dangling_source) { create(:ci_pipeline, :success, project: project, ci_ref: ci_ref, source: dangling_source, child_of: unlockable_ci_source) }
        let!(:non_unlockable_ci_source) { create(:ci_pipeline, :running, project: project, ci_ref: ci_ref, source: ci_source) }
        let!(:non_unlockable_ci_source_2) { create(:ci_pipeline, :running, project: project, ci_ref: ci_ref, source: ci_source) }

        it 'returns the last unlockable CI source pipeline before it' do
          expect(result).to eq(unlockable_ci_source)
        end

        context 'and it has unlockable child pipelines' do
          let!(:child) { create(:ci_pipeline, :success, project: project, ci_ref: ci_ref, source: dangling_source, child_of: non_unlockable_ci_source) }
          let!(:child_2) { create(:ci_pipeline, :success, project: project, ci_ref: ci_ref, source: dangling_source, child_of: non_unlockable_ci_source) }

          it 'returns the last unlockable CI source pipeline before it' do
            expect(result).to eq(unlockable_ci_source)
          end
        end
      end
    end

    context 'when there are no unlockable pipelines in the ref' do
      let!(:non_unlockable_pipeline) { create(:ci_pipeline, :running, project: project, ci_ref: ci_ref, source: ci_source) }
      let!(:pipeline_from_another_ref) { create(:ci_pipeline, :success, source: ci_source) }

      it { is_expected.to be_nil }
    end
  end
end