File: generate_rspec_pipeline_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (352 lines) | stat: -rw-r--r-- 11,743 bytes parent folder | download
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# frozen_string_literal: true

require 'fast_spec_helper'
require 'tempfile'

require_relative '../../scripts/generate_rspec_pipeline'

RSpec.describe GenerateRspecPipeline, :silence_stdout, feature_category: :tooling do
  describe '#generate!' do
    let!(:rspec_files) { Tempfile.new(['rspec_files_path', '.txt']) }
    let(:rspec_files_content) do
      "spec/migrations/a_spec.rb spec/migrations/b_spec.rb spec/migrations/c_spec.rb spec/migrations/d_spec.rb " \
        "spec/lib/gitlab/background_migration/a_spec.rb spec/lib/gitlab/background_migration/b_spec.rb " \
        "spec/models/a_spec.rb spec/models/b_spec.rb " \
        "spec/controllers/a_spec.rb spec/controllers/b_spec.rb " \
        "spec/features/a_spec.rb spec/features/b_spec.rb " \
        "ee/spec/features/a_spec.rb"
    end

    let(:pipeline_template) { Tempfile.new(['pipeline_template', '.yml.erb']) }
    let(:pipeline_template_content) do
      <<~YAML
      <% if test_suite_prefix.nil? && rspec_files_per_test_level[:migration][:files].size > 0 %>
      rspec migration:
      <% if rspec_files_per_test_level[:migration][:parallelization] > 1 %>
        parallel: <%= rspec_files_per_test_level[:migration][:parallelization] %>
      <% end %>
      <% end %>
      <% if test_suite_prefix.nil? && rspec_files_per_test_level[:background_migration][:files].size > 0 %>
      rspec background_migration:
      <% if rspec_files_per_test_level[:background_migration][:parallelization] > 1 %>
        parallel: <%= rspec_files_per_test_level[:background_migration][:parallelization] %>
      <% end %>
      <% end %>
      <% if test_suite_prefix.nil? && rspec_files_per_test_level[:unit][:files].size > 0 %>
      rspec unit:
      <% if rspec_files_per_test_level[:unit][:parallelization] > 1 %>
        parallel: <%= rspec_files_per_test_level[:unit][:parallelization] %>
      <% end %>
      <% end %>
      <% if test_suite_prefix.nil? && rspec_files_per_test_level[:integration][:files].size > 0 %>
      rspec integration:
      <% if rspec_files_per_test_level[:integration][:parallelization] > 1 %>
        parallel: <%= rspec_files_per_test_level[:integration][:parallelization] %>
      <% end %>
      <% end %>
      <% if test_suite_prefix.nil? && rspec_files_per_test_level[:system][:files].size > 0 %>
      rspec system:
      <% if rspec_files_per_test_level[:system][:parallelization] > 1 %>
        parallel: <%= rspec_files_per_test_level[:system][:parallelization] %>
      <% end %>
      <% end %>
      <% if test_suite_prefix == 'ee/' && rspec_files_per_test_level[:system][:files].size > 0 %>
      rspec-ee system:
      <% if rspec_files_per_test_level[:system][:parallelization] > 1 %>
        parallel: <%= rspec_files_per_test_level[:system][:parallelization] %>
      <% end %>
      <% end %>
      YAML
    end

    let(:knapsack_report) { Tempfile.new(['knapsack_report', '.json']) }
    let(:knapsack_report_content) do
      <<~JSON
      {
        "spec/migrations/a_spec.rb": 620.3,
        "spec/migrations/b_spec.rb": 610.1,
        "spec/migrations/c_spec.rb": 20.1,
        "spec/migrations/d_spec.rb": 20.1,
        "spec/migrations/e_spec.rb": 20.1,
        "spec/migrations/f_spec.rb": 20.1,
        "spec/migrations/g_spec.rb": 20.1,
        "spec/lib/gitlab/background_migration/a_spec.rb": 60.5,
        "spec/lib/gitlab/background_migration/b_spec.rb": 180.3,
        "spec/models/a_spec.rb": 360.2,
        "spec/models/b_spec.rb": 180.6,
        "spec/controllers/a_spec.rb": 60.2,
        "spec/controllers/ab_spec.rb": 180.4,
        "spec/features/a_spec.rb": 360.1,
        "spec/features/b_spec.rb": 180.5,
        "ee/spec/features/a_spec.rb": 180.5
      }
      JSON
    end

    around do |example|
      rspec_files.write(rspec_files_content)
      rspec_files.rewind
      pipeline_template.write(pipeline_template_content)
      pipeline_template.rewind
      knapsack_report.write(knapsack_report_content)
      knapsack_report.rewind
      example.run
    ensure
      rspec_files.close
      rspec_files.unlink
      pipeline_template.close
      pipeline_template.unlink
      knapsack_report.close
      knapsack_report.unlink
    end

    context 'when rspec_files and pipeline_template_path exists' do
      subject do
        described_class.new(
          rspec_files_path: rspec_files.path,
          pipeline_template_path: pipeline_template.path
        )
      end

      it 'generates the pipeline config with default parallelization' do
        subject.generate!

        expect(File.read("#{pipeline_template.path}.yml"))
          .to eq(
            "rspec migration:\nrspec background_migration:\nrspec unit:\n" \
            "rspec integration:\nrspec system:"
          )
      end

      context 'when parallelization > 0' do
        before do
          stub_const("#{described_class}::DEFAULT_AVERAGE_TEST_FILE_DURATION_IN_SECONDS", 360)
        end

        it 'generates the pipeline config' do
          subject.generate!

          expect(File.read("#{pipeline_template.path}.yml"))
            .to eq(
              "rspec migration:\n  parallel: 4\nrspec background_migration:\n  parallel: 2\n" \
              "rspec unit:\n  parallel: 2\nrspec integration:\n  parallel: 2\n" \
              "rspec system:\n  parallel: 2"
            )
        end
      end

      context 'when parallelization > MAX_NODES_COUNT' do
        let(:rspec_files_content) do
          Array.new(51) { |i| "spec/migrations/#{i}_spec.rb" }.join(' ')
        end

        before do
          stub_const(
            "#{described_class}::DEFAULT_AVERAGE_TEST_FILE_DURATION_IN_SECONDS",
            described_class::OPTIMAL_TEST_JOB_DURATION_IN_SECONDS
          )
        end

        it 'generates the pipeline config with max parallelization of 50' do
          subject.generate!

          expect(File.read("#{pipeline_template.path}.yml")).to eq("rspec migration:\n  parallel: 50")
        end
      end
    end

    context 'when knapsack_report_path is given' do
      subject do
        described_class.new(
          rspec_files_path: rspec_files.path,
          pipeline_template_path: pipeline_template.path,
          knapsack_report_path: knapsack_report.path
        )
      end

      it 'generates the pipeline config with parallelization based on Knapsack' do
        subject.generate!

        expect(File.read("#{pipeline_template.path}.yml"))
          .to eq(
            "rspec migration:\n  parallel: 4\nrspec background_migration:\n" \
            "rspec unit:\n  parallel: 2\nrspec integration:\n" \
            "rspec system:\n  parallel: 2"
          )
      end

      context 'and RSpec files have a high duration' do
        let(:rspec_files_content) do
          "spec/migrations/a_spec.rb spec/migrations/b_spec.rb"
        end

        it 'generates the pipeline config with parallelization based on Knapsack' do
          subject.generate!

          expect(File.read("#{pipeline_template.path}.yml"))
            .to eq(
              "rspec migration:\n  parallel: 2"
            )
        end
      end

      context 'and Knapsack report does not contain valid JSON' do
        let(:knapsack_report_content) { "#{super()}," }

        it 'generates the pipeline config with default parallelization' do
          subject.generate!

          expect(File.read("#{pipeline_template.path}.yml"))
            .to eq(
              "rspec migration:\nrspec background_migration:\nrspec unit:\n" \
              "rspec integration:\nrspec system:"
            )
        end
      end
    end

    context 'when test_suite_prefix is given' do
      subject do
        described_class.new(
          rspec_files_path: rspec_files.path,
          pipeline_template_path: pipeline_template.path,
          knapsack_report_path: knapsack_report.path,
          test_suite_prefix: 'ee/'
        )
      end

      it 'generates the pipeline config based on the test_suite_prefix' do
        subject.generate!

        expect(File.read("#{pipeline_template.path}.yml"))
          .to eq("rspec-ee system:")
      end
    end

    describe 'job_tags option' do
      let(:pipeline_template_content) do
        <<~YAML
        default:
          image: $DEFAULT_CI_IMAGE
          <%- if job_tags.any? -%>
          tags:
            <%- job_tags.each do |job_tag| -%>
            - <%= job_tag %>
            <%- end -%>
          <%- end -%>
        YAML
      end

      before do
        subject.generate!
      end

      context 'when job_tags is not given' do
        subject do
          described_class.new(
            rspec_files_path: rspec_files.path,
            pipeline_template_path: pipeline_template.path
          )
        end

        it 'generates the pipeline config with no tags' do
          expect(File.read("#{pipeline_template.path}.yml"))
            .to eq(
              <<~YAML.chomp
                    default:
                      image: $DEFAULT_CI_IMAGE
              YAML
            )
        end
      end

      context 'when job_tags is given' do
        subject do
          described_class.new(
            rspec_files_path: rspec_files.path,
            pipeline_template_path: pipeline_template.path,
            job_tags: job_tags
          )
        end

        context 'with two tags' do
          let(:job_tags) { %w[foo bar] }

          it 'generates the pipeline config with the expected tags' do
            expect(File.read("#{pipeline_template.path}.yml"))
              .to eq(
                <<~YAML.chomp
                      default:
                        image: $DEFAULT_CI_IMAGE
                        tags:
                          - foo
                          - bar
                YAML
              )
          end
        end

        context 'with empty tags array' do
          let(:job_tags) { [] }

          it 'generates the pipeline without any tags defined' do
            expect(File.read("#{pipeline_template.path}.yml"))
              .to eq(
                <<~YAML.chomp
                      default:
                        image: $DEFAULT_CI_IMAGE
                YAML
              )
          end
        end
      end
    end

    context 'when generated_pipeline_path is given' do
      let(:custom_pipeline_filename) { Tempfile.new(['custom_pipeline_filename', '.yml']) }

      around do |example|
        example.run
      ensure
        custom_pipeline_filename.close
        custom_pipeline_filename.unlink
      end

      subject do
        described_class.new(
          rspec_files_path: rspec_files.path,
          pipeline_template_path: pipeline_template.path,
          generated_pipeline_path: custom_pipeline_filename.path
        )
      end

      it 'writes the pipeline config in the given generated_pipeline_path' do
        subject.generate!

        expect(File.read(custom_pipeline_filename.path))
          .to eq(
            "rspec migration:\nrspec background_migration:\nrspec unit:\n" \
            "rspec integration:\nrspec system:"
          )
      end
    end

    context 'when rspec_files does not exist' do
      subject { described_class.new(rspec_files_path: nil, pipeline_template_path: pipeline_template.path) }

      it 'generates the pipeline config using the no-op template' do
        subject.generate!

        expect(File.read("#{pipeline_template.path}.yml")).to include("no-op:")
      end
    end

    context 'when pipeline_template_path does not exist' do
      subject { described_class.new(rspec_files_path: rspec_files.path, pipeline_template_path: nil) }

      it 'generates the pipeline config using the no-op template' do
        expect { subject }.to raise_error(ArgumentError)
      end
    end
  end
end