File: process_package_file_service_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 (414 lines) | stat: -rw-r--r-- 16,367 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Debian::ProcessPackageFileService, feature_category: :package_registry do
  include ExclusiveLeaseHelpers

  let_it_be(:distribution) { create(:debian_project_distribution, :with_file, suite: 'unstable') }

  let(:debian_file_metadatum) { package_file.debian_file_metadatum }
  let(:service) { described_class.new(package_file, distribution_name, component_name) }

  describe '#execute' do
    using RSpec::Parameterized::TableSyntax

    subject { service.execute }

    shared_examples 'common validations' do
      context 'with package file without Debian metadata' do
        let!(:package_file) { create(:debian_package_file, without_loaded_metadatum: true) }

        let(:expected_error) { ArgumentError }
        let(:expected_message) { 'package file without Debian metadata' }

        it_behaves_like 'raises error'
      end

      context 'with already processed package file' do
        let!(:package_file) { create(:debian_package_file) }

        let(:expected_error) { ArgumentError }
        let(:expected_message) { 'already processed package file' }

        it_behaves_like 'raises error'
      end

      context 'without a distribution' do
        let(:expected_error) { ActiveRecord::RecordNotFound }
        let(:expected_message) { /^Couldn't find Packages::Debian::ProjectDistribution with / }

        before do
          # Workaround ActiveRecord cache
          Packages::Debian::ProjectDistribution.find(distribution.id).delete
        end

        it_behaves_like 'raises error'
      end

      context 'when there is a matching published package in another distribution' do
        let!(:matching_package) do
          create(
            :debian_package,
            project: distribution.project,
            name: 'sample',
            version: '1.2.3~alpha2'
          )
        end

        let(:expected_error) { ArgumentError }

        let(:expected_message) do
          "Debian package sample 1.2.3~alpha2 exists in distribution #{matching_package.distribution.codename}"
        end

        it_behaves_like 'raises error'
      end
    end

    shared_examples 'raises error' do
      it 'raises error', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change { Packages::Debian::Publication.count }
          .and not_change(package.package_files, :count)
          .and not_change { package.reload.name }
          .and not_change { package.version }
          .and not_change { package.status }
          .and not_change { debian_file_metadatum&.reload&.file_type }
          .and not_change { debian_file_metadatum&.component }
          .and raise_error(expected_error, expected_message)
      end
    end

    shared_examples 'does nothing' do
      it 'does nothing', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change { Packages::Debian::Publication.count }
          .and not_change(package.package_files, :count)
          .and not_change { package.reload.name }
          .and not_change { package.version }
          .and not_change { package.status }
          .and not_change { debian_file_metadatum&.reload&.file_type }
          .and not_change { debian_file_metadatum&.component }
      end
    end

    shared_examples 'updates package and changes file' do
      it 'updates package and changes file', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker)
          .to receive(:perform_async).with(:project, distribution.id)
        expect { subject }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and change { Packages::Debian::Publication.count }.by(1)
          .and change { package.package_files.count }.from(1).to(8)
          .and change { package.reload.name }.to('sample')
          .and change { package.version }.to('1.2.3~alpha2')
          .and change { package.status }.from('processing').to('default')
          .and change { package.publication }.from(nil)
          .and change { debian_file_metadatum.file_type }.from('unknown').to('changes')
          .and not_change { debian_file_metadatum.component }
      end
    end

    shared_examples 'updates package and package file' do
      it 'updates package and package file', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker)
          .to receive(:perform_async).with(:project, distribution.id)
        expect { subject }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and change { Packages::Debian::Publication.count }.by(1)
          .and not_change(package.package_files, :count)
          .and change { package.reload.name }.to('sample')
          .and change { package.version }.to('1.2.3~alpha2')
          .and change { package.status }.from('processing').to('default')
          .and change { package.publication }.from(nil)
          .and change { debian_file_metadatum.file_type }.from('unknown').to(expected_file_type)
          .and change { debian_file_metadatum.component }.from(nil).to(component_name)
      end
    end

    context 'with a changes file' do
      let!(:incoming) { create(:debian_incoming, project: distribution.project) }
      let!(:temporary_with_changes) { create(:debian_temporary_with_changes, project: distribution.project) }
      # Reload factory to reset associations cache for package files
      let(:package) { temporary_with_changes.reload }

      let(:package_file) { temporary_with_changes.package_files.first }
      let(:distribution_name) { nil }
      let(:component_name) { nil }

      it_behaves_like 'common validations'

      context 'with distribution_name' do
        let(:distribution_name) { distribution.codename }
        let(:expected_error) { ArgumentError }
        let(:expected_message) { 'unwanted distribution name' }

        it_behaves_like 'raises error'
      end

      context 'with component_name' do
        let(:component_name) { 'main' }
        let(:expected_error) { ArgumentError }
        let(:expected_message) { 'unwanted component name' }

        it_behaves_like 'raises error'
      end

      context 'with crafted file_metadata' do
        let(:complete_file_metadata) do
          {
            file_type: :changes,
            fields: {
              'Source' => 'abc',
              'Version' => '1.0',
              'Distribution' => 'sid'
            }
          }
        end

        let(:expected_error) { ArgumentError }

        before do
          allow_next_instance_of(::Packages::Debian::ExtractChangesMetadataService) do |extract_changes_metadata_svc|
            allow(extract_changes_metadata_svc).to receive(:execute).and_return(file_metadata)
          end
        end

        context 'with missing Source field' do
          let(:file_metadata) { complete_file_metadata.tap { |m| m[:fields].delete 'Source' } }
          let(:expected_message) { 'missing Source field' }

          it_behaves_like 'raises error'
        end

        context 'with missing Version field' do
          let(:file_metadata) { complete_file_metadata.tap { |m| m[:fields].delete 'Version' } }
          let(:expected_message) { 'missing Version field' }

          it_behaves_like 'raises error'
        end

        context 'with missing Distribution field' do
          let(:file_metadata) { complete_file_metadata.tap { |m| m[:fields].delete 'Distribution' } }
          let(:expected_message) { 'missing Distribution field' }

          it_behaves_like 'raises error'
        end
      end

      context 'when lease is already taken' do
        before do
          stub_exclusive_lease_taken(
            "packages:debian:process_package_file_service:#{distribution.project_id}_sample_1.2.3~alpha2",
            timeout: Packages::Debian::ProcessPackageFileService::DEFAULT_LEASE_TIMEOUT)
        end

        it_behaves_like 'does nothing'
      end

      context 'when there is no matching published package' do
        it_behaves_like 'updates package and changes file'
      end

      context 'when there is a matching published package' do
        let!(:matching_package) do
          create(
            :debian_package,
            project: distribution.project,
            published_in: distribution,
            name: 'sample',
            version: '1.2.3~alpha2'
          )
        end

        it 'reuses existing package and update package file', :aggregate_failures do
          expect(::Packages::Debian::GenerateDistributionWorker)
            .to receive(:perform_async).with(:project, distribution.id)
          expect { subject }
            .to change { Packages::Package.count }.from(3).to(2)
            .and not_change { Packages::PackageFile.count }
            .and not_change(Packages::Debian::Publication, :count)
            .and change { package.package_files.count }.from(1).to(0)
            .and change { incoming.package_files.count }.from(7).to(0)
            .and change { matching_package.package_files.count }.from(7).to(15)
            .and change { package_file.package }.from(package).to(matching_package)
            .and not_change(matching_package, :name)
            .and not_change(matching_package, :version)
            .and change { debian_file_metadatum.file_type }.from('unknown').to('changes')
            .and not_change { debian_file_metadatum.component }

          expect { package.reload }
            .to raise_error(ActiveRecord::RecordNotFound)
        end
      end

      context 'when there is a matching published package pending destruction' do
        let!(:matching_package) do
          create(
            :debian_package,
            :pending_destruction,
            project: distribution.project,
            published_in: distribution,
            name: 'sample',
            version: '1.2.3~alpha2'
          )
        end

        it_behaves_like 'updates package and changes file'
      end
    end

    context 'with a package file' do
      let!(:temporary_with_files) { create(:debian_temporary_with_files, project: distribution.project) }
      # Reload factory to reset associations cache for package files
      let(:package) { temporary_with_files.reload }

      let(:package_file) { package.package_files.with_file_name('libsample0_1.2.3~alpha2_amd64.deb').first }
      let(:distribution_name) { distribution.codename }
      let(:component_name) { 'main' }

      where(:case_name, :expected_file_type, :file_name, :component_name) do
        'with a deb'   | 'deb'  | 'libsample0_1.2.3~alpha2_amd64.deb'   | 'main'
        'with an udeb' | 'udeb' | 'sample-udeb_1.2.3~alpha2_amd64.udeb' | 'contrib'
        'with an ddeb' | 'ddeb' | 'sample-ddeb_1.2.3~alpha2_amd64.ddeb' | 'main'
      end

      with_them do
        context 'with Debian package file' do
          let(:package_file) { package.package_files.with_file_name(file_name).first }

          it_behaves_like 'common validations'

          context 'without distribution name' do
            let(:distribution_name) { '' }
            let(:expected_error) { ArgumentError }
            let(:expected_message) { 'missing distribution name' }

            it_behaves_like 'raises error'
          end

          context 'without component name' do
            let(:component_name) { '' }
            let(:expected_error) { ArgumentError }
            let(:expected_message) { 'missing component name' }

            it_behaves_like 'raises error'
          end

          context 'with invalid package file type' do
            let(:package_file) { package.package_files.with_file_name('sample_1.2.3~alpha2.tar.xz').first }
            let(:expected_error) { ArgumentError }
            let(:expected_message) { 'invalid package file type: source' }

            it_behaves_like 'raises error'
          end

          context 'when lease is already taken' do
            before do
              stub_exclusive_lease_taken(
                "packages:debian:process_package_file_service:#{distribution.project_id}_sample_1.2.3~alpha2",
                timeout: Packages::Debian::ProcessPackageFileService::DEFAULT_LEASE_TIMEOUT)
            end

            it_behaves_like 'does nothing'
          end

          context 'when there is no matching published package' do
            it_behaves_like 'updates package and package file'

            context 'with suite as distribution name' do
              let(:distribution_name) { distribution.suite }

              it_behaves_like 'updates package and package file'
            end
          end

          context 'when there is a matching published package' do
            let!(:matching_package) do
              create(
                :debian_package,
                project: distribution.project,
                published_in: distribution,
                name: 'sample',
                version: '1.2.3~alpha2'
              )
            end

            it 'reuses existing package and update package file', :aggregate_failures do
              expect(::Packages::Debian::GenerateDistributionWorker)
                .to receive(:perform_async).with(:project, distribution.id)
              expect { subject }
                .to change { Packages::Package.count }.from(2).to(1)
                .and change { Packages::PackageFile.count }.from(14).to(8)
                .and not_change(Packages::Debian::Publication, :count)
                .and change { package.package_files.count }.from(7).to(0)
                .and change { package_file.package }.from(package).to(matching_package)
                .and not_change(matching_package, :name)
                .and not_change(matching_package, :version)
                .and change { debian_file_metadatum.file_type }.from('unknown').to(expected_file_type)
                .and change { debian_file_metadatum.component }.from(nil).to(component_name)

              expect { package.reload }
                .to raise_error(ActiveRecord::RecordNotFound)
            end
          end

          context 'when there is a matching published package pending destruction' do
            let!(:matching_package) do
              create(
                :debian_package,
                :pending_destruction,
                project: distribution.project,
                published_in: distribution,
                name: 'sample',
                version: '1.2.3~alpha2'
              )
            end

            it_behaves_like 'updates package and package file'
          end
        end
      end
    end
  end

  describe '#lease_key' do
    let(:prefix) { 'packages:debian:process_package_file_service' }

    subject { service.send(:lease_key) }

    context 'with a changes file' do
      let!(:incoming) { create(:debian_incoming, project: distribution.project) }
      let!(:temporary_with_changes) { create(:debian_temporary_with_changes, project: distribution.project) }
      # Reload factory to reset associations cache for package files
      let(:package) { temporary_with_changes.reload }

      let(:package_file) { temporary_with_changes.package_files.first }
      let(:distribution_name) { nil }
      let(:component_name) { nil }

      it { is_expected.to eq("#{prefix}:#{distribution.project_id}_sample_1.2.3~alpha2") }
    end

    context 'with a package file' do
      let!(:temporary_with_files) { create(:debian_temporary_with_files, project: distribution.project) }
      # Reload factory to reset associations cache for package files
      let(:package) { temporary_with_files.reload }

      let(:package_file) { package.package_files.with_file_name('libsample0_1.2.3~alpha2_amd64.deb').first }
      let(:distribution_name) { distribution.codename }
      let(:component_name) { 'main' }

      it { is_expected.to eq("#{prefix}:#{distribution.project_id}_sample_1.2.3~alpha2") }
    end
  end
end