summaryrefslogtreecommitdiff
path: root/spec/services/packages/debian/process_package_file_service_spec.rb
blob: caf29cfc4fa8464aab695634a0ae9e48220640a8 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Packages::Debian::ProcessPackageFileService, feature_category: :package_registry do
  describe '#execute' do
    let_it_be_with_reload(:distribution) { create(:debian_project_distribution, :with_suite, :with_file) }

    let!(:package) { create(:debian_package, :processing, project: distribution.project, published_in: nil) }
    let(:distribution_name) { distribution.codename }
    let(:component_name) { 'main' }
    let(:debian_file_metadatum) { package_file.debian_file_metadatum }

    subject { described_class.new(package_file, distribution_name, component_name) }

    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.execute }
          .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.reload.version }.to('1.2.3~alpha2')
          .and change { package.reload.status }.from('processing').to('default')
          .and change { package.reload.debian_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

    using RSpec::Parameterized::TableSyntax

    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'
    end

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

        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.execute }
              .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

    context 'without a distribution' do
      let(:package_file) { package.package_files.with_file_name('libsample0_1.2.3~alpha2_amd64.deb').first }
      let(:component_name) { 'main' }

      before do
        distribution.destroy!
      end

      it 'raise ActiveRecord::RecordNotFound', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject.execute }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change(package.package_files, :count)
          .and raise_error(ActiveRecord::RecordNotFound)
      end
    end

    context 'without distribution name' do
      let!(:package_file) { create(:debian_package_file, without_loaded_metadatum: true) }
      let(:distribution_name) { '' }

      it 'raise ArgumentError', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject.execute }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change(package.package_files, :count)
          .and raise_error(ArgumentError, 'missing distribution name')
      end
    end

    context 'without component name' do
      let!(:package_file) { create(:debian_package_file, without_loaded_metadatum: true) }
      let(:component_name) { '' }

      it 'raise ArgumentError', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject.execute }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change(package.package_files, :count)
          .and raise_error(ArgumentError, 'missing component name')
      end
    end

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

      it 'raise ArgumentError', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject.execute }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change(package.package_files, :count)
          .and raise_error(ArgumentError, 'package file without Debian metadata')
      end
    end

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

      let(:component_name) { 'main' }

      it 'raise ArgumentError', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject.execute }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change(package.package_files, :count)
          .and raise_error(ArgumentError, 'already processed package file')
      end
    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(:component_name) { 'main' }

      it 'raise ArgumentError', :aggregate_failures do
        expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
        expect { subject.execute }
          .to not_change(Packages::Package, :count)
          .and not_change(Packages::PackageFile, :count)
          .and not_change(package.package_files, :count)
          .and raise_error(ArgumentError, 'invalid package file type: source')
      end
    end
  end
end