summaryrefslogtreecommitdiff
path: root/spec/models/ci/processable_spec.rb
blob: 4490371bde58dfb45bdab5e89bdcebe60a9baf74 (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
# frozen_string_literal: true

require 'spec_helper'

describe Ci::Processable do
  let_it_be(:project) { create(:project) }
  let_it_be(:pipeline) { create(:ci_pipeline, project: project) }

  let_it_be(:detached_merge_request_pipeline) do
    create(:ci_pipeline, :detached_merge_request_pipeline, :with_job, project: project)
  end

  let_it_be(:legacy_detached_merge_request_pipeline) do
    create(:ci_pipeline, :legacy_detached_merge_request_pipeline, :with_job, project: project)
  end

  let_it_be(:merged_result_pipeline) do
    create(:ci_pipeline, :merged_result_pipeline, :with_job, project: project)
  end

  describe '#aggregated_needs_names' do
    let(:with_aggregated_needs) { pipeline.processables.select_with_aggregated_needs(project) }

    context 'with created status' do
      let!(:processable) { create(:ci_build, :created, project: project, pipeline: pipeline) }

      context 'with needs' do
        before do
          create(:ci_build_need, build: processable, name: 'test1')
          create(:ci_build_need, build: processable, name: 'test2')
        end

        it 'returns all processables' do
          expect(with_aggregated_needs).to contain_exactly(processable)
        end

        it 'returns all needs' do
          expect(with_aggregated_needs.first.aggregated_needs_names).to contain_exactly('test1', 'test2')
        end
      end

      context 'without needs' do
        it 'returns all processables' do
          expect(with_aggregated_needs).to contain_exactly(processable)
        end

        it 'returns empty needs' do
          expect(with_aggregated_needs.first.aggregated_needs_names).to be_nil
        end
      end
    end
  end

  describe 'validate presence of scheduling_type' do
    context 'on create' do
      let(:processable) do
        build(
          :ci_build, :created, project: project, pipeline: pipeline,
          importing: importing, scheduling_type: nil
        )
      end

      context 'when importing' do
        let(:importing) { true }

        context 'when validate_scheduling_type_of_processables is true' do
          before do
            stub_feature_flags(validate_scheduling_type_of_processables: true)
          end

          it 'does not validate' do
            expect(processable).to be_valid
          end
        end

        context 'when validate_scheduling_type_of_processables is false' do
          before do
            stub_feature_flags(validate_scheduling_type_of_processables: false)
          end

          it 'does not validate' do
            expect(processable).to be_valid
          end
        end
      end

      context 'when not importing' do
        let(:importing) { false }

        context 'when validate_scheduling_type_of_processables is true' do
          before do
            stub_feature_flags(validate_scheduling_type_of_processables: true)
          end

          it 'validates' do
            expect(processable).not_to be_valid
          end
        end

        context 'when validate_scheduling_type_of_processables is false' do
          before do
            stub_feature_flags(validate_scheduling_type_of_processables: false)
          end

          it 'does not validate' do
            expect(processable).to be_valid
          end
        end
      end
    end

    context 'on update' do
      let(:processable) { create(:ci_build, :created, project: project, pipeline: pipeline) }

      it 'does not validate' do
        processable.scheduling_type = nil
        expect(processable).to be_valid
      end
    end
  end

  describe '.populate_scheduling_type!' do
    let!(:build_without_needs) { create(:ci_build, project: project, pipeline: pipeline) }
    let!(:build_with_needs) { create(:ci_build, project: project, pipeline: pipeline) }
    let!(:needs_relation) { create(:ci_build_need, build: build_with_needs) }
    let!(:another_build) { create(:ci_build, project: project) }

    before do
      Ci::Processable.update_all(scheduling_type: nil)
    end

    it 'populates scheduling_type of processables' do
      expect do
        pipeline.processables.populate_scheduling_type!
      end.to change(pipeline.processables.where(scheduling_type: nil), :count).from(2).to(0)

      expect(build_without_needs.reload.scheduling_type).to eq('stage')
      expect(build_with_needs.reload.scheduling_type).to eq('dag')
    end

    it 'does not affect processables from other pipelines' do
      pipeline.processables.populate_scheduling_type!
      expect(another_build.reload.scheduling_type).to be_nil
    end
  end

  describe '#needs_attributes' do
    let(:build) { create(:ci_build, :created, project: project, pipeline: pipeline) }

    context 'with needs' do
      before do
        create(:ci_build_need, build: build, name: 'test1')
        create(:ci_build_need, build: build, name: 'test2')
      end

      it 'returns all needs attributes' do
        expect(build.needs_attributes).to contain_exactly(
          { 'artifacts' => true, 'name' => 'test1' },
          { 'artifacts' => true, 'name' => 'test2' }
        )
      end
    end

    context 'without needs' do
      it 'returns all needs attributes' do
        expect(build.needs_attributes).to be_empty
      end
    end
  end

  describe '#merge_request?' do
    subject { pipeline.processables.first.merge_request? }

    context 'in a detached merge request pipeline' do
      let(:pipeline) { detached_merge_request_pipeline }

      it { is_expected.to eq(pipeline.merge_request?) }
    end

    context 'in a legacy detached merge_request_pipeline' do
      let(:pipeline) { legacy_detached_merge_request_pipeline }

      it { is_expected.to eq(pipeline.merge_request?) }
    end

    context 'in a pipeline for merged results' do
      let(:pipeline) { merged_result_pipeline }

      it { is_expected.to eq(pipeline.merge_request?) }
    end
  end

  describe '#merge_request_ref?' do
    subject { pipeline.processables.first.merge_request_ref? }

    context 'in a detached merge request pipeline' do
      let(:pipeline) { detached_merge_request_pipeline }

      it { is_expected.to eq(pipeline.merge_request_ref?) }
    end

    context 'in a legacy detached merge_request_pipeline' do
      let(:pipeline) { legacy_detached_merge_request_pipeline }

      it { is_expected.to eq(pipeline.merge_request_ref?) }
    end

    context 'in a pipeline for merged results' do
      let(:pipeline) { merged_result_pipeline }

      it { is_expected.to eq(pipeline.merge_request_ref?) }
    end
  end

  describe '#legacy_detached_merge_request_pipeline?' do
    subject { pipeline.processables.first.legacy_detached_merge_request_pipeline? }

    context 'in a detached merge request pipeline' do
      let(:pipeline) { detached_merge_request_pipeline }

      it { is_expected.to eq(pipeline.legacy_detached_merge_request_pipeline?) }
    end

    context 'in a legacy detached merge_request_pipeline' do
      let(:pipeline) { legacy_detached_merge_request_pipeline }

      it { is_expected.to eq(pipeline.legacy_detached_merge_request_pipeline?) }
    end

    context 'in a pipeline for merged results' do
      let(:pipeline) { merged_result_pipeline }

      it { is_expected.to eq(pipeline.legacy_detached_merge_request_pipeline?) }
    end
  end
end