debian-mirror-gitlab/spec/services/packages/debian/process_changes_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
5.9 KiB
Ruby
Raw Normal View History

2021-04-29 21:17:54 +05:30
# frozen_string_literal: true
2023-06-20 00:43:36 +05:30
2021-04-29 21:17:54 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Packages::Debian::ProcessChangesService, feature_category: :package_registry do
2021-04-29 21:17:54 +05:30
describe '#execute' do
let_it_be(:user) { create(:user) }
2023-04-23 21:23:45 +05:30
let_it_be_with_reload(:distribution) { create(:debian_project_distribution, :with_file, suite: 'unstable') }
2022-10-11 01:57:18 +05:30
let!(:incoming) { create(:debian_incoming, project: distribution.project) }
2021-04-29 21:17:54 +05:30
2023-04-23 21:23:45 +05:30
let(:package_file) { incoming.package_files.with_file_name('sample_1.2.3~alpha2_amd64.changes').first }
2021-04-29 21:17:54 +05:30
subject { described_class.new(package_file, user) }
context 'with valid package file' do
it 'updates package and package file', :aggregate_failures do
2021-09-04 01:27:46 +05:30
expect(::Packages::Debian::GenerateDistributionWorker).to receive(:perform_async).with(:project, distribution.id)
2021-04-29 21:17:54 +05:30
expect { subject.execute }
.to change { Packages::Package.count }.from(1).to(2)
.and not_change { Packages::PackageFile.count }
2023-05-27 22:25:52 +05:30
.and change { incoming.package_files.count }.from(8).to(0)
2021-06-08 01:23:25 +05:30
.and change { package_file.debian_file_metadatum&.reload&.file_type }.from('unknown').to('changes')
2021-04-29 21:17:54 +05:30
created_package = Packages::Package.last
expect(created_package.name).to eq 'sample'
expect(created_package.version).to eq '1.2.3~alpha2'
expect(created_package.creator).to eq user
end
2022-03-02 08:16:31 +05:30
2023-04-23 21:23:45 +05:30
context 'with non-matching distribution' do
2022-03-02 08:16:31 +05:30
before do
2023-04-23 21:23:45 +05:30
distribution.update! suite: FFaker::Lorem.word
end
it { expect { subject.execute }.to raise_error(ActiveRecord::RecordNotFound) }
end
context 'with missing field in .changes file' do
shared_examples 'raises error with missing field' do |missing_field|
before do
allow_next_instance_of(::Packages::Debian::ExtractChangesMetadataService) do |extract_changes_metadata_service|
expect(extract_changes_metadata_service).to receive(:execute).once.and_wrap_original do |m, *args|
metadata = m.call(*args)
metadata[:fields].delete(missing_field)
metadata
end
end
end
it { expect { subject.execute }.to raise_error(ArgumentError, "missing #{missing_field} field") }
end
it_behaves_like 'raises error with missing field', 'Source'
it_behaves_like 'raises error with missing field', 'Version'
it_behaves_like 'raises error with missing field', 'Distribution'
end
2023-06-20 00:43:36 +05:30
context 'with existing package in the same distribution' do
2023-04-23 21:23:45 +05:30
let_it_be_with_reload(:existing_package) do
create(:debian_package, name: 'sample', version: '1.2.3~alpha2', project: distribution.project, published_in: distribution)
2022-03-02 08:16:31 +05:30
end
2022-10-11 01:57:18 +05:30
it 'does not create a package and assigns the package_file to the existing package' do
2022-03-02 08:16:31 +05:30
expect { subject.execute }
.to not_change { Packages::Package.count }
.and not_change { Packages::PackageFile.count }
2023-06-20 00:43:36 +05:30
.and change { package_file.package }.to(existing_package)
end
context 'and marked as pending_destruction' do
it 'does not re-use the existing package' do
existing_package.pending_destruction!
expect { subject.execute }
.to change { Packages::Package.count }.by(1)
.and not_change { Packages::PackageFile.count }
end
end
end
context 'with existing package in another distribution' do
let_it_be_with_reload(:existing_package) do
create(:debian_package, name: 'sample', version: '1.2.3~alpha2', project: distribution.project)
end
it 'raise ExtractionError' 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 { incoming.package_files.count }
.and raise_error(ArgumentError,
"Debian package #{existing_package.name} #{existing_package.version} exists " \
"in distribution #{existing_package.debian_distribution.codename}")
2022-03-02 08:16:31 +05:30
end
2023-06-20 00:43:36 +05:30
context 'and marked as pending_destruction' do
2023-03-04 22:38:38 +05:30
it 'does not re-use the existing package' do
2022-03-02 08:16:31 +05:30
existing_package.pending_destruction!
expect { subject.execute }
.to change { Packages::Package.count }.by(1)
.and not_change { Packages::PackageFile.count }
end
end
end
2021-04-29 21:17:54 +05:30
end
context 'with invalid package file' do
let(:package_file) { incoming.package_files.first }
it 'raise ExtractionError', :aggregate_failures do
2021-09-04 01:27:46 +05:30
expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
2021-04-29 21:17:54 +05:30
expect { subject.execute }
.to not_change { Packages::Package.count }
.and not_change { Packages::PackageFile.count }
.and not_change { incoming.package_files.count }
.and raise_error(Packages::Debian::ExtractChangesMetadataService::ExtractionError, 'is not a changes file')
end
end
context 'when creating package fails' do
before do
allow_next_instance_of(::Packages::Debian::FindOrCreatePackageService) do |find_or_create_package_service|
expect(find_or_create_package_service).to receive(:execute).and_raise(ActiveRecord::ConnectionTimeoutError, 'connect timeout')
end
end
2023-03-04 22:38:38 +05:30
it 're-raise error', :aggregate_failures do
2021-09-04 01:27:46 +05:30
expect(::Packages::Debian::GenerateDistributionWorker).not_to receive(:perform_async)
2021-04-29 21:17:54 +05:30
expect { subject.execute }
.to not_change { Packages::Package.count }
.and not_change { Packages::PackageFile.count }
.and not_change { incoming.package_files.count }
.and raise_error(ActiveRecord::ConnectionTimeoutError, 'connect timeout')
end
end
end
end