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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Debian::ProcessPackageFileWorker, type: :worker, feature_category: :package_registry do
shared_examples 'returns early without error' do
it 'returns early without error' do
expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
expect(::Packages::Debian::ProcessPackageFileService).not_to receive(:new)
subject
end
end
let_it_be_with_reload(:distribution) { create(:debian_project_distribution, :with_file) }
let_it_be_with_reload(:incoming) { create(:debian_incoming, project: distribution.project) }
let_it_be_with_reload(:temp_with_changes) { create(:debian_temporary_with_changes, project: distribution.project) }
let_it_be_with_reload(:temp_with_files) { create(:debian_temporary_with_files, project: distribution.project) }
describe '#perform' do
let(:package) { temp_with_files }
let(:package_file) { package.package_files.with_file_name('libsample0_1.2.3~alpha2_amd64.deb').first }
let(:debian_file_metadatum) { package_file.debian_file_metadatum }
let(:worker) { described_class.new }
let(:package_file_id) { package_file.id }
let(:distribution_name) { distribution.codename }
let(:component_name) { 'main' }
subject { worker.perform(package_file_id, distribution_name, component_name) }
shared_context 'with changes file' do
let(:package) { temp_with_changes }
let(:package_file) { package.package_files.first }
let(:distribution_name) { nil }
let(:component_name) { nil }
before do
distribution.update! suite: 'unstable'
end
end
context 'with non existing package file' do
let(:package_file_id) { non_existing_record_id }
it_behaves_like 'returns early without error'
end
context 'with nil package file id' do
let(:package_file_id) { nil }
it_behaves_like 'returns early without error'
end
context 'with already processed package file' do
let_it_be(:package_file) { create(:debian_package_file) }
it_behaves_like 'returns early without error'
end
context 'with mocked service' do
it 'calls ProcessPackageFileService' do
expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
expect_next_instance_of(::Packages::Debian::ProcessPackageFileService) do |service|
expect(service).to receive(:execute)
.with(no_args)
end
subject
end
end
shared_examples 'handling error' do |error_message:, error_class:|
it 'marks the package as errored', :aggregate_failures do
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
instance_of(error_class),
package_file_id: package_file_id,
project_id: package.project_id,
distribution_name: distribution_name,
component_name: component_name
)
expect { subject }
.to not_change(Packages::Package, :count)
.and not_change { Packages::PackageFile.count }
.and not_change { package.package_files.count }
.and change { package_file.reload.status }.to('error')
.and change { package.reload.status }.from('processing').to('error')
.and change { package.status_message }.to(error_message)
end
end
context 'with controlled errors' do
context 'with a package file' do
context 'when component name is blank' do
let(:component_name) { '' }
it_behaves_like 'handling error',
error_message: 'missing component name',
error_class: ArgumentError
end
context 'when distribution name is blank' do
let(:distribution_name) { '' }
it_behaves_like 'handling error',
error_message: 'missing distribution name',
error_class: ArgumentError
end
context 'when package file is not deb, ddeb or udeb' do
let(:package_file) { package.package_files.with_file_name('sample_1.2.3~alpha2.tar.xz').first }
it_behaves_like 'handling error',
error_message: 'invalid package file type: source',
error_class: ArgumentError
end
end
context 'with changes file' do
include_context 'with changes file'
let(:file_metadata_source) { 'src' }
let(:file_metadata_version) { '0.1' }
let(:file_metadata_distribution) { 'Breezy Badger' }
let(:file_metadata) do
{
fields: {
'Source' => file_metadata_source,
'Version' => file_metadata_version,
'Distribution' => file_metadata_distribution
}
}
end
context 'when component name is blank' do
let(:component_name) { '' }
it_behaves_like 'handling error',
error_message: 'unwanted component name',
error_class: ArgumentError
end
context 'with distribution name is blank' do
let(:distribution_name) { '' }
it_behaves_like 'handling error',
error_message: 'unwanted distribution name',
error_class: ArgumentError
end
context 'with missing file metadata fields' do
before do
allow_next_instance_of(::Packages::Debian::ExtractChangesMetadataService) do |instance|
allow(instance).to receive(:execute).and_return(file_metadata)
end
end
context 'when source field is missing' do
before do
file_metadata[:fields].delete('Source')
end
it_behaves_like 'handling error',
error_message: 'missing Source field',
error_class: ArgumentError
end
context 'when Version field is missing' do
before do
file_metadata[:fields].delete('Version')
end
it_behaves_like 'handling error',
error_message: 'missing Version field',
error_class: ArgumentError
end
context 'when Distribution field is missing' do
before do
file_metadata[:fields].delete('Distribution')
end
it_behaves_like 'handling error',
error_message: 'missing Distribution field',
error_class: ArgumentError
end
end
end
end
context 'with uncontrolled errors' do
before do
allow_next_instance_of(::Packages::Debian::ProcessPackageFileService) do |instance|
allow(instance).to receive(:execute).and_raise(StandardError.new('Boom'))
end
end
it_behaves_like 'handling error',
error_message: 'Unexpected error: StandardError',
error_class: StandardError
end
context 'with correct job arguments' do
let(:job_args) { [package_file.id, distribution_name, component_name] }
it_behaves_like 'an idempotent worker'
context 'with a Debian changes file' do
include_context 'with changes file'
it 'sets the Debian file type to changes', :aggregate_failures do
expect(::Packages::Debian::GenerateDistributionWorker)
.to receive(:perform_async).with(:project, distribution.id)
expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
# Using subject inside this block will process the job multiple times
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.reload.file_type }.from('unknown').to('changes')
.and not_change { debian_file_metadatum.component }
end
end
context 'with Debian files' do
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'
'with a ddeb' | 'ddeb' | 'sample-ddeb_1.2.3~alpha2_amd64.ddeb' | 'main'
end
with_them do
let(:package_file) { package.package_files.with_file_name(file_name).first }
let(:job_args) { [package_file.id, distribution_name, component_name] }
it 'sets the correct Debian file type', :aggregate_failures do
expect(::Packages::Debian::GenerateDistributionWorker)
.to receive(:perform_async).with(:project, distribution.id)
expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
# Using subject inside this block will process the job multiple times
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.reload.file_type }.from('unknown').to(expected_file_type)
.and change { debian_file_metadatum.component }.from(nil).to(component_name)
end
end
end
end
end
end
|