summaryrefslogtreecommitdiff
path: root/spec/services/packages
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2021-01-20 13:34:23 -0600
committerRobert Speicher <rspeicher@gmail.com>2021-01-20 13:34:23 -0600
commit6438df3a1e0fb944485cebf07976160184697d72 (patch)
tree00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /spec/services/packages
parent42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff)
downloadgitlab-ce-6438df3a1e0fb944485cebf07976160184697d72.tar.gz
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'spec/services/packages')
-rw-r--r--spec/services/packages/create_event_service_spec.rb25
-rw-r--r--spec/services/packages/debian/create_package_file_service_spec.rb106
-rw-r--r--spec/services/packages/debian/extract_metadata_service_spec.rb59
-rw-r--r--spec/services/packages/debian/get_or_create_incoming_service_spec.rb36
-rw-r--r--spec/services/packages/maven/find_or_create_package_service_spec.rb54
-rw-r--r--spec/services/packages/nuget/search_service_spec.rb150
6 files changed, 353 insertions, 77 deletions
diff --git a/spec/services/packages/create_event_service_spec.rb b/spec/services/packages/create_event_service_spec.rb
index f581d704087..f7bab0e5a9f 100644
--- a/spec/services/packages/create_event_service_spec.rb
+++ b/spec/services/packages/create_event_service_spec.rb
@@ -56,7 +56,7 @@ RSpec.describe Packages::CreateEventService do
end
end
- shared_examples 'redis package event creation' do |originator_type, expected_scope|
+ shared_examples 'redis package unique event creation' do |originator_type, expected_scope|
context 'with feature flag disable' do
before do
stub_feature_flags(collect_package_events_redis: false)
@@ -70,29 +70,27 @@ RSpec.describe Packages::CreateEventService do
end
it 'tracks the event' do
- expect(::Gitlab::UsageDataCounters::GuestPackageEventCounter).not_to receive(:count)
- expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(user.id, Packages::Event.allowed_event_name(expected_scope, event_name, originator_type))
+ expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(/package/, values: user.id)
subject
end
end
- shared_examples 'redis package guest event creation' do |originator_type, expected_scope|
+ shared_examples 'redis package count event creation' do |originator_type, expected_scope|
context 'with feature flag disabled' do
before do
stub_feature_flags(collect_package_events_redis: false)
end
it 'does not track the event' do
- expect(::Gitlab::UsageDataCounters::GuestPackageEventCounter).not_to receive(:count)
+ expect(::Gitlab::UsageDataCounters::PackageEventCounter).not_to receive(:count)
subject
end
end
it 'tracks the event' do
- expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
- expect(::Gitlab::UsageDataCounters::GuestPackageEventCounter).to receive(:count).with(Packages::Event.allowed_event_name(expected_scope, event_name, originator_type))
+ expect(::Gitlab::UsageDataCounters::PackageEventCounter).to receive(:count).at_least(:once)
subject
end
@@ -102,21 +100,23 @@ RSpec.describe Packages::CreateEventService do
let(:user) { create(:user) }
it_behaves_like 'db package event creation', 'user', 'container'
- it_behaves_like 'redis package event creation', 'user', 'container'
+ it_behaves_like 'redis package unique event creation', 'user', 'container'
+ it_behaves_like 'redis package count event creation', 'user', 'container'
end
context 'with a deploy token' do
let(:user) { create(:deploy_token) }
it_behaves_like 'db package event creation', 'deploy_token', 'container'
- it_behaves_like 'redis package event creation', 'deploy_token', 'container'
+ it_behaves_like 'redis package unique event creation', 'deploy_token', 'container'
+ it_behaves_like 'redis package count event creation', 'deploy_token', 'container'
end
context 'with no user' do
let(:user) { nil }
it_behaves_like 'db package event creation', 'guest', 'container'
- it_behaves_like 'redis package guest event creation', 'guest', 'container'
+ it_behaves_like 'redis package count event creation', 'guest', 'container'
end
context 'with a package as scope' do
@@ -126,14 +126,15 @@ RSpec.describe Packages::CreateEventService do
let(:user) { nil }
it_behaves_like 'db package event creation', 'guest', 'npm'
- it_behaves_like 'redis package guest event creation', 'guest', 'npm'
+ it_behaves_like 'redis package count event creation', 'guest', 'npm'
end
context 'with user' do
let(:user) { create(:user) }
it_behaves_like 'db package event creation', 'user', 'npm'
- it_behaves_like 'redis package event creation', 'user', 'npm'
+ it_behaves_like 'redis package unique event creation', 'user', 'npm'
+ it_behaves_like 'redis package count event creation', 'user', 'npm'
end
end
end
diff --git a/spec/services/packages/debian/create_package_file_service_spec.rb b/spec/services/packages/debian/create_package_file_service_spec.rb
new file mode 100644
index 00000000000..74b97a4f941
--- /dev/null
+++ b/spec/services/packages/debian/create_package_file_service_spec.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Packages::Debian::CreatePackageFileService do
+ include WorkhorseHelpers
+
+ let_it_be(:package) { create(:debian_incoming, without_package_files: true) }
+
+ describe '#execute' do
+ let(:file_name) { 'libsample0_1.2.3~alpha2_amd64.deb' }
+ let(:fixture_path) { "spec/fixtures/packages/debian/#{file_name}" }
+
+ let(:params) do
+ {
+ file: file,
+ file_name: file_name,
+ file_sha1: '54321',
+ file_md5: '12345'
+ }.with_indifferent_access
+ end
+
+ let(:service) { described_class.new(package, params) }
+
+ subject(:package_file) { service.execute }
+
+ shared_examples 'a valid deb' do
+ it 'creates a new package file', :aggregate_failures do
+ expect(package_file).to be_valid
+ expect(package_file.file.read).to start_with('!<arch>')
+ expect(package_file.size).to eq(1124)
+ expect(package_file.file_name).to eq(file_name)
+ expect(package_file.file_sha1).to eq('54321')
+ expect(package_file.file_sha256).to eq('543212345')
+ expect(package_file.file_md5).to eq('12345')
+ expect(package_file.debian_file_metadatum).to be_valid
+ expect(package_file.debian_file_metadatum.file_type).to eq('unknown')
+ expect(package_file.debian_file_metadatum.architecture).to be_nil
+ expect(package_file.debian_file_metadatum.fields).to be_nil
+ end
+ end
+
+ context 'with temp file' do
+ let!(:file) do
+ upload_path = ::Packages::PackageFileUploader.workhorse_local_upload_path
+ file_path = upload_path + '/' + file_name
+
+ FileUtils.mkdir_p(upload_path)
+ File.write(file_path, File.read(fixture_path))
+
+ UploadedFile.new(file_path, filename: File.basename(file_path), sha256: '543212345')
+ end
+
+ it_behaves_like 'a valid deb'
+ end
+
+ context 'with remote file' do
+ let!(:fog_connection) do
+ stub_package_file_object_storage(direct_upload: true)
+ end
+
+ before do
+ allow_next_instance_of(UploadedFile) do |uploaded_file|
+ allow(uploaded_file).to receive(:sha256).and_return('543212345')
+ end
+ end
+
+ let(:tmp_object) do
+ fog_connection.directories.new(key: 'packages').files.create( # rubocop:disable Rails/SaveBang
+ key: "tmp/uploads/#{file_name}",
+ body: File.read(fixture_path)
+ )
+ end
+
+ let!(:file) { fog_to_uploaded_file(tmp_object) }
+
+ it_behaves_like 'a valid deb'
+ end
+
+ context 'package is missing' do
+ let(:package) { nil }
+ let(:params) { {} }
+
+ it 'raises an error' do
+ expect { subject.execute }.to raise_error(ArgumentError, 'Invalid package')
+ end
+ end
+
+ context 'params is empty' do
+ let(:params) { {} }
+
+ it 'raises an error' do
+ expect { subject.execute }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+ end
+
+ context 'file is missing' do
+ let(:file_name) { 'libsample0_1.2.3~alpha2_amd64.deb' }
+ let(:file) { nil }
+
+ it 'raises an error' do
+ expect { subject.execute }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+ end
+ end
+end
diff --git a/spec/services/packages/debian/extract_metadata_service_spec.rb b/spec/services/packages/debian/extract_metadata_service_spec.rb
new file mode 100644
index 00000000000..0aa9a67b263
--- /dev/null
+++ b/spec/services/packages/debian/extract_metadata_service_spec.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+require 'spec_helper'
+
+RSpec.describe Packages::Debian::ExtractMetadataService do
+ let(:service) { described_class.new(package_file) }
+
+ subject { service.execute }
+
+ RSpec.shared_context 'Debian ExtractMetadata Service' do |trait|
+ let(:package_file) { create(:debian_package_file, trait) }
+ end
+
+ RSpec.shared_examples 'Test Debian ExtractMetadata Service' do |expected_file_type, expected_architecture, expected_fields|
+ it "returns file_type #{expected_file_type.inspect}" do
+ expect(subject[:file_type]).to eq(expected_file_type)
+ end
+
+ it "returns architecture #{expected_architecture.inspect}" do
+ expect(subject[:architecture]).to eq(expected_architecture)
+ end
+
+ it "returns fields #{expected_fields.nil? ? '' : 'including '}#{expected_fields.inspect}" do
+ if expected_fields.nil?
+ expect(subject[:fields]).to be_nil
+ else
+ expect(subject[:fields]).to include(**expected_fields)
+ end
+ end
+ end
+
+ using RSpec::Parameterized::TableSyntax
+
+ where(:case_name, :trait, :expected_file_type, :expected_architecture, :expected_fields) do
+ 'with invalid' | :invalid | :unknown | nil | nil
+ 'with source' | :source | :source | nil | nil
+ 'with dsc' | :dsc | :dsc | nil | { 'Binary': 'sample-dev, libsample0, sample-udeb' }
+ 'with deb' | :deb | :deb | 'amd64' | { 'Multi-Arch': 'same' }
+ 'with udeb' | :udeb | :udeb | 'amd64' | { 'Package': 'sample-udeb' }
+ 'with buildinfo' | :buildinfo | :buildinfo | nil | { 'Architecture': 'amd64 source', 'Build-Architecture': 'amd64' }
+ 'with changes' | :changes | :changes | nil | { 'Architecture': 'source amd64', 'Binary': 'libsample0 sample-dev sample-udeb' }
+ end
+
+ with_them do
+ include_context 'Debian ExtractMetadata Service', params[:trait] do
+ it_behaves_like 'Test Debian ExtractMetadata Service',
+ params[:expected_file_type],
+ params[:expected_architecture],
+ params[:expected_fields]
+ end
+ end
+
+ context 'with invalid package file' do
+ let(:package_file) { create(:conan_package_file) }
+
+ it 'raise error' do
+ expect { subject }.to raise_error(described_class::ExtractionError, 'invalid package file')
+ end
+ end
+end
diff --git a/spec/services/packages/debian/get_or_create_incoming_service_spec.rb b/spec/services/packages/debian/get_or_create_incoming_service_spec.rb
new file mode 100644
index 00000000000..ab99b091246
--- /dev/null
+++ b/spec/services/packages/debian/get_or_create_incoming_service_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Packages::Debian::GetOrCreateIncomingService do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:user) { create(:user) }
+
+ subject(:service) { described_class.new(project, user) }
+
+ describe '#execute' do
+ subject(:package) { service.execute }
+
+ context 'run once' do
+ it 'creates a new package', :aggregate_failures do
+ expect(package).to be_valid
+ expect(package.project_id).to eq(project.id)
+ expect(package.creator_id).to eq(user.id)
+ expect(package.name).to eq('incoming')
+ expect(package.version).to be_nil
+ expect(package.package_type).to eq('debian')
+ expect(package.debian_incoming?).to be_truthy
+ end
+
+ it_behaves_like 'assigns the package creator'
+ end
+
+ context 'run twice' do
+ let!(:package2) { service.execute }
+
+ it 'returns the same object' do
+ expect(package2.id).to eq(package.id)
+ end
+ end
+ end
+end
diff --git a/spec/services/packages/maven/find_or_create_package_service_spec.rb b/spec/services/packages/maven/find_or_create_package_service_spec.rb
index 2eaad7db445..82dffeefcde 100644
--- a/spec/services/packages/maven/find_or_create_package_service_spec.rb
+++ b/spec/services/packages/maven/find_or_create_package_service_spec.rb
@@ -11,29 +11,36 @@ RSpec.describe Packages::Maven::FindOrCreatePackageService do
let(:file_name) { 'test.jar' }
let(:param_path) { "#{path}/#{version}" }
let(:params) { { path: param_path, file_name: file_name } }
+ let(:service) { described_class.new(project, user, params) }
describe '#execute' do
using RSpec::Parameterized::TableSyntax
- subject { described_class.new(project, user, params).execute }
+ subject { service.execute }
- RSpec.shared_examples 'reuse existing package' do
- it { expect { subject}.not_to change { Packages::Package.count } }
+ shared_examples 'reuse existing package' do
+ it { expect { subject }.not_to change { Packages::Package.count } }
- it { is_expected.to eq(existing_package) }
+ it 'returns the existing package' do
+ expect(subject.payload).to eq(package: existing_package)
+ end
end
- RSpec.shared_examples 'create package' do
+ shared_examples 'create package' do
it { expect { subject }.to change { Packages::Package.count }.by(1) }
- it 'sets the proper name and version' do
- pkg = subject
+ it 'sets the proper name and version', :aggregate_failures do
+ pkg = subject.payload[:package]
expect(pkg.name).to eq(path)
expect(pkg.version).to eq(version)
end
- it_behaves_like 'assigns build to package'
+ context 'with a build' do
+ subject { service.execute.payload[:package] }
+
+ it_behaves_like 'assigns build to package'
+ end
end
context 'path with version' do
@@ -90,5 +97,36 @@ RSpec.describe Packages::Maven::FindOrCreatePackageService do
expect { subject }.to change { Packages::BuildInfo.count }.by(1)
end
end
+
+ context 'when package duplicates are not allowed' do
+ let_it_be_with_refind(:package_settings) { create(:namespace_package_setting, :group, maven_duplicates_allowed: false) }
+ let_it_be_with_refind(:group) { package_settings.namespace }
+ let_it_be_with_refind(:project) { create(:project, group: group) }
+ let!(:existing_package) { create(:maven_package, name: path, version: version, project: project) }
+
+ it { expect { subject }.not_to change { project.package_files.count } }
+
+ it 'returns an error', :aggregate_failures do
+ expect(subject.payload).to be_empty
+ expect(subject.errors).to include('Duplicate package is not allowed')
+ end
+
+ context 'when uploading different non-duplicate files to the same package' do
+ before do
+ package_file = existing_package.package_files.find_by(file_name: 'my-app-1.0-20180724.124855-1.jar')
+ package_file.destroy!
+ end
+
+ it_behaves_like 'reuse existing package'
+ end
+
+ context 'when the package name matches the exception regex' do
+ before do
+ package_settings.update!(maven_duplicate_exception_regex: '.*')
+ end
+
+ it_behaves_like 'reuse existing package'
+ end
+ end
end
end
diff --git a/spec/services/packages/nuget/search_service_spec.rb b/spec/services/packages/nuget/search_service_spec.rb
index d163e7087e4..db758dc6672 100644
--- a/spec/services/packages/nuget/search_service_spec.rb
+++ b/spec/services/packages/nuget/search_service_spec.rb
@@ -1,8 +1,12 @@
# frozen_string_literal: true
+
require 'spec_helper'
RSpec.describe Packages::Nuget::SearchService do
- let_it_be(:project) { create(:project) }
+ let_it_be(:user) { create(:user) }
+ let_it_be(:group) { create(:group) }
+ let_it_be(:subgroup) { create(:group, parent: group) }
+ let_it_be(:project) { create(:project, namespace: subgroup) }
let_it_be(:package_a) { create(:nuget_package, project: project, name: 'DummyPackageA') }
let_it_be(:packages_b) { create_list(:nuget_package, 5, project: project, name: 'DummyPackageB') }
let_it_be(:packages_c) { create_list(:nuget_package, 5, project: project, name: 'DummyPackageC') }
@@ -16,94 +20,126 @@ RSpec.describe Packages::Nuget::SearchService do
let(:options) { { include_prerelease_versions: include_prerelease_versions, per_page: per_page, padding: padding } }
describe '#execute' do
- subject { described_class.new(project, search_term, options).execute }
+ subject { described_class.new(user, target, search_term, options).execute }
- it { expect_search_results 3, package_a, packages_b, packages_c }
+ shared_examples 'handling all the conditions' do
+ it { expect_search_results 3, package_a, packages_b, packages_c }
- context 'with a smaller per page count' do
- let(:per_page) { 2 }
+ context 'with a smaller per page count' do
+ let(:per_page) { 2 }
- it { expect_search_results 3, package_a, packages_b }
- end
+ it { expect_search_results 3, package_a, packages_b }
+ end
- context 'with 0 per page count' do
- let(:per_page) { 0 }
+ context 'with 0 per page count' do
+ let(:per_page) { 0 }
- it { expect_search_results 3, [] }
- end
+ it { expect_search_results 3, [] }
+ end
- context 'with a negative per page count' do
- let(:per_page) { -1 }
+ context 'with a negative per page count' do
+ let(:per_page) { -1 }
- it { expect { subject }.to raise_error(ArgumentError, 'negative per_page') }
- end
+ it { expect { subject }.to raise_error(ArgumentError, 'negative per_page') }
+ end
- context 'with a padding' do
- let(:padding) { 2 }
+ context 'with a padding' do
+ let(:padding) { 2 }
- it { expect_search_results 3, packages_c }
- end
+ it { expect_search_results 3, packages_c }
+ end
- context 'with a too big padding' do
- let(:padding) { 5 }
+ context 'with a too big padding' do
+ let(:padding) { 5 }
- it { expect_search_results 3, [] }
- end
+ it { expect_search_results 3, [] }
+ end
- context 'with a negative padding' do
- let(:padding) { -1 }
+ context 'with a negative padding' do
+ let(:padding) { -1 }
- it { expect { subject }.to raise_error(ArgumentError, 'negative padding') }
- end
+ it { expect { subject }.to raise_error(ArgumentError, 'negative padding') }
+ end
- context 'with search term' do
- let(:search_term) { 'umm' }
+ context 'with search term' do
+ let(:search_term) { 'umm' }
- it { expect_search_results 3, package_a, packages_b, packages_c }
- end
+ it { expect_search_results 3, package_a, packages_b, packages_c }
+ end
- context 'with nil search term' do
- let(:search_term) { nil }
+ context 'with nil search term' do
+ let(:search_term) { nil }
- it { expect_search_results 4, package_a, packages_b, packages_c, package_d }
- end
+ it { expect_search_results 4, package_a, packages_b, packages_c, package_d }
+ end
- context 'with empty search term' do
- let(:search_term) { '' }
+ context 'with empty search term' do
+ let(:search_term) { '' }
- it { expect_search_results 4, package_a, packages_b, packages_c, package_d }
- end
+ it { expect_search_results 4, package_a, packages_b, packages_c, package_d }
+ end
- context 'with prefix search term' do
- let(:search_term) { 'dummy' }
+ context 'with prefix search term' do
+ let(:search_term) { 'dummy' }
- it { expect_search_results 3, package_a, packages_b, packages_c }
- end
+ it { expect_search_results 3, package_a, packages_b, packages_c }
+ end
+
+ context 'with suffix search term' do
+ let(:search_term) { 'packagec' }
+
+ it { expect_search_results 1, packages_c }
+ end
+
+ context 'with pre release packages' do
+ let_it_be(:package_e) { create(:nuget_package, project: project, name: 'DummyPackageE', version: '3.2.1-alpha') }
+
+ context 'including them' do
+ it { expect_search_results 4, package_a, packages_b, packages_c, package_e }
+ end
+
+ context 'excluding them' do
+ let(:include_prerelease_versions) { false }
- context 'with suffix search term' do
- let(:search_term) { 'packagec' }
+ it { expect_search_results 3, package_a, packages_b, packages_c }
- it { expect_search_results 1, packages_c }
+ context 'when mixed with release versions' do
+ let_it_be(:package_e_release) { create(:nuget_package, project: project, name: 'DummyPackageE', version: '3.2.1') }
+
+ it { expect_search_results 4, package_a, packages_b, packages_c, package_e_release }
+ end
+ end
+ end
end
- context 'with pre release packages' do
- let_it_be(:package_e) { create(:nuget_package, project: project, name: 'DummyPackageE', version: '3.2.1-alpha') }
+ context 'with project' do
+ let(:target) { project }
- context 'including them' do
- it { expect_search_results 4, package_a, packages_b, packages_c, package_e }
+ before do
+ project.add_developer(user)
end
- context 'excluding them' do
- let(:include_prerelease_versions) { false }
+ it_behaves_like 'handling all the conditions'
+ end
- it { expect_search_results 3, package_a, packages_b, packages_c }
+ context 'with subgroup' do
+ let(:target) { subgroup }
- context 'when mixed with release versions' do
- let_it_be(:package_e_release) { create(:nuget_package, project: project, name: 'DummyPackageE', version: '3.2.1') }
+ before do
+ subgroup.add_developer(user)
+ end
- it { expect_search_results 4, package_a, packages_b, packages_c, package_e_release }
- end
+ it_behaves_like 'handling all the conditions'
+ end
+
+ context 'with group' do
+ let(:target) { group }
+
+ before do
+ group.add_developer(user)
end
+
+ it_behaves_like 'handling all the conditions'
end
def expect_search_results(total_count, *results)