diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
commit | 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch) | |
tree | 5423a1c7516cffe36384133ade12572cf709398d /spec/services/packages | |
parent | e570267f2f6b326480d284e0164a6464ba4081bc (diff) |
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'spec/services/packages')
9 files changed, 334 insertions, 12 deletions
diff --git a/spec/services/packages/debian/generate_distribution_key_service_spec.rb b/spec/services/packages/debian/generate_distribution_key_service_spec.rb new file mode 100644 index 00000000000..b31830c2d3b --- /dev/null +++ b/spec/services/packages/debian/generate_distribution_key_service_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Debian::GenerateDistributionKeyService do + let_it_be(:user) { create(:user) } + + let(:params) { {} } + + subject { described_class.new(current_user: user, params: params) } + + let(:response) { subject.execute } + + context 'with a user' do + it 'returns an Hash', :aggregate_failures do + expect(GPGME::Ctx).to receive(:new).with(armor: true, offline: true).and_call_original + expect(User).to receive(:random_password).with(no_args).and_call_original + + expect(response).to be_a Hash + expect(response.keys).to contain_exactly(:private_key, :public_key, :fingerprint, :passphrase) + expect(response[:private_key]).to start_with('-----BEGIN PGP PRIVATE KEY BLOCK-----') + expect(response[:public_key]).to start_with('-----BEGIN PGP PUBLIC KEY BLOCK-----') + expect(response[:fingerprint].length).to eq(40) + expect(response[:passphrase].length).to be > 10 + end + end + + context 'without a user' do + let(:user) { nil } + + it 'raises an ArgumentError' do + expect { response }.to raise_error(ArgumentError, 'Please provide a user') + end + end +end diff --git a/spec/services/packages/debian/generate_distribution_service_spec.rb b/spec/services/packages/debian/generate_distribution_service_spec.rb new file mode 100644 index 00000000000..0547d18c8bc --- /dev/null +++ b/spec/services/packages/debian/generate_distribution_service_spec.rb @@ -0,0 +1,182 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Debian::GenerateDistributionService do + let_it_be(:group) { create(:group, :public) } + let_it_be(:project) { create(:project, :public, group: group) } + let_it_be(:project_distribution) { create("debian_project_distribution", container: project, codename: 'unstable', valid_time_duration_seconds: 48.hours.to_i) } + + let_it_be(:incoming) { create(:debian_incoming, project: project) } + + before_all do + ::Packages::Debian::ProcessChangesService.new(incoming.package_files.last, nil).execute + end + + let(:service) { described_class.new(distribution) } + + describe '#execute' do + subject { service.execute } + + shared_examples 'Generate Distribution' do |container_type| + context "for #{container_type}" do + if container_type == :group + let_it_be(:container) { group } + let_it_be(:distribution, reload: true) { create('debian_group_distribution', container: group, codename: 'unstable', valid_time_duration_seconds: 48.hours.to_i) } + else + let_it_be(:container) { project } + let_it_be(:distribution, reload: true) { project_distribution } + end + + context 'with components and architectures' do + let_it_be(:component_main ) { create("debian_#{container_type}_component", distribution: distribution, name: 'main') } + let_it_be(:component_contrib) { create("debian_#{container_type}_component", distribution: distribution, name: 'contrib') } + + let_it_be(:architecture_all ) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'all') } + let_it_be(:architecture_amd64) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'amd64') } + let_it_be(:architecture_arm64) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'arm64') } + + let_it_be(:component_file1) { create("debian_#{container_type}_component_file", component: component_main, architecture: architecture_all, created_at: '2020-01-24T09:00:00.000Z') } # destroyed + let_it_be(:component_file2) { create("debian_#{container_type}_component_file", component: component_main, architecture: architecture_amd64, created_at: '2020-01-24T10:29:59.000Z') } # destroyed + let_it_be(:component_file3) { create("debian_#{container_type}_component_file", component: component_contrib, architecture: architecture_all, created_at: '2020-01-24T10:30:00.000Z') } # kept + let_it_be(:component_file4) { create("debian_#{container_type}_component_file", component: component_contrib, architecture: architecture_amd64, created_at: '2020-01-24T11:30:00.000Z') } # kept + + def check_component_file(component_name, component_file_type, architecture_name, expected_content) + component_file = distribution + .component_files + .with_component_name(component_name) + .with_file_type(component_file_type) + .with_architecture_name(architecture_name) + .last + + expect(component_file).not_to be_nil + expect(component_file.file.exists?).to eq(!expected_content.nil?) + + unless expected_content.nil? + component_file.file.use_file do |file_path| + expect(File.read(file_path)).to eq(expected_content) + end + end + end + + it 'updates distribution and component files', :aggregate_failures do + travel_to(Time.utc(2020, 01, 25, 15, 17, 18, 123456)) do + expect(Gitlab::ErrorTracking).not_to receive(:log_exception) + + expect { subject } + .to not_change { Packages::Package.count } + .and not_change { Packages::PackageFile.count } + .and change { distribution.component_files.count }.from(4).to(2 + 6) + + expected_main_amd64_content = <<~EOF + Package: libsample0 + Source: sample + Version: 1.2.3~alpha2 + Installed-Size: 7 + Maintainer: John Doe <john.doe@example.com> + Architecture: amd64 + Description: Some mostly empty lib + Used in GitLab tests. + . + Testing another paragraph. + Multi-Arch: same + Homepage: https://gitlab.com/ + Section: libs + Priority: optional + Filename: pool/unstable/#{project.id}/s/sample/libsample0_1.2.3~alpha2_amd64.deb + Size: 409600 + MD5sum: fb0842b21adc44207996296fe14439dd + SHA256: 1c383a525bfcba619c7305ccd106d61db501a6bbaf0003bf8d0c429fbdb7fcc1 + + Package: sample-dev + Source: sample (1.2.3~alpha2) + Version: 1.2.3~binary + Installed-Size: 7 + Maintainer: John Doe <john.doe@example.com> + Architecture: amd64 + Depends: libsample0 (= 1.2.3~binary) + Description: Some mostly empty developpement files + Used in GitLab tests. + . + Testing another paragraph. + Multi-Arch: same + Homepage: https://gitlab.com/ + Section: libdevel + Priority: optional + Filename: pool/unstable/#{project.id}/s/sample/sample-dev_1.2.3~binary_amd64.deb + Size: 409600 + MD5sum: d2afbd28e4d74430d22f9504e18bfdf5 + SHA256: 9fbeee2191ce4dab5288fad5ecac1bd369f58fef9a992a880eadf0caf25f086d + EOF + + check_component_file('main', :packages, 'all', nil) + check_component_file('main', :packages, 'amd64', expected_main_amd64_content) + check_component_file('main', :packages, 'arm64', nil) + + check_component_file('contrib', :packages, 'all', nil) + check_component_file('contrib', :packages, 'amd64', nil) + check_component_file('contrib', :packages, 'arm64', nil) + + size = expected_main_amd64_content.length + md5sum = Digest::MD5.hexdigest(expected_main_amd64_content) + sha256 = Digest::SHA256.hexdigest(expected_main_amd64_content) + + expected_release_content = <<~EOF + Codename: unstable + Date: Sat, 25 Jan 2020 15:17:18 +0000 + Valid-Until: Mon, 27 Jan 2020 15:17:18 +0000 + Architectures: all amd64 arm64 + Components: contrib main + MD5Sum: + d41d8cd98f00b204e9800998ecf8427e 0 contrib/binary-all/Packages + d41d8cd98f00b204e9800998ecf8427e 0 contrib/binary-amd64/Packages + d41d8cd98f00b204e9800998ecf8427e 0 contrib/binary-arm64/Packages + d41d8cd98f00b204e9800998ecf8427e 0 main/binary-all/Packages + #{md5sum} #{size} main/binary-amd64/Packages + d41d8cd98f00b204e9800998ecf8427e 0 main/binary-arm64/Packages + SHA256: + e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0 contrib/binary-all/Packages + e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0 contrib/binary-amd64/Packages + e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0 contrib/binary-arm64/Packages + e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0 main/binary-all/Packages + #{sha256} #{size} main/binary-amd64/Packages + e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0 main/binary-arm64/Packages + EOF + + distribution.file.use_file do |file_path| + expect(File.read(file_path)).to eq(expected_release_content) + end + end + end + end + + context 'without components and architectures' do + it 'updates distribution and component files', :aggregate_failures do + travel_to(Time.utc(2020, 01, 25, 15, 17, 18, 123456)) do + expect(Gitlab::ErrorTracking).not_to receive(:log_exception) + + expect { subject } + .to not_change { Packages::Package.count } + .and not_change { Packages::PackageFile.count } + .and not_change { distribution.component_files.count } + + expected_release_content = <<~EOF + Codename: unstable + Date: Sat, 25 Jan 2020 15:17:18 +0000 + Valid-Until: Mon, 27 Jan 2020 15:17:18 +0000 + MD5Sum: + SHA256: + EOF + + distribution.file.use_file do |file_path| + expect(File.read(file_path)).to eq(expected_release_content) + end + end + end + end + end + end + + it_behaves_like 'Generate Distribution', :project + it_behaves_like 'Generate Distribution', :group + end +end diff --git a/spec/services/packages/debian/process_changes_service_spec.rb b/spec/services/packages/debian/process_changes_service_spec.rb index 98b531bde10..f23471659bc 100644 --- a/spec/services/packages/debian/process_changes_service_spec.rb +++ b/spec/services/packages/debian/process_changes_service_spec.rb @@ -17,6 +17,7 @@ RSpec.describe Packages::Debian::ProcessChangesService do .to change { Packages::Package.count }.from(1).to(2) .and not_change { Packages::PackageFile.count } .and change { incoming.package_files.count }.from(7).to(0) + .and change { package_file.debian_file_metadatum&.reload&.file_type }.from('unknown').to('changes') created_package = Packages::Package.last expect(created_package.name).to eq 'sample' diff --git a/spec/services/packages/generic/create_package_file_service_spec.rb b/spec/services/packages/generic/create_package_file_service_spec.rb index 10c54369f26..1c9eb53cfc7 100644 --- a/spec/services/packages/generic/create_package_file_service_spec.rb +++ b/spec/services/packages/generic/create_package_file_service_spec.rb @@ -6,13 +6,16 @@ RSpec.describe Packages::Generic::CreatePackageFileService do let_it_be(:project) { create(:project) } let_it_be(:user) { create(:user) } let_it_be(:pipeline) { create(:ci_pipeline, user: user) } + let_it_be(:file_name) { 'myfile.tar.gz.1' } + let(:build) { double('build', pipeline: pipeline) } describe '#execute' do + let_it_be(:package) { create(:generic_package, project: project) } + let(:sha256) { '440e5e148a25331bbd7991575f7d54933c0ebf6cc735a18ee5066ac1381bb590' } let(:temp_file) { Tempfile.new("test") } let(:file) { UploadedFile.new(temp_file.path, sha256: sha256) } - let(:package) { create(:generic_package, project: project) } let(:package_service) { double } let(:params) do @@ -20,7 +23,7 @@ RSpec.describe Packages::Generic::CreatePackageFileService do package_name: 'mypackage', package_version: '0.0.1', file: file, - file_name: 'myfile.tar.gz.1', + file_name: file_name, build: build } end @@ -34,7 +37,7 @@ RSpec.describe Packages::Generic::CreatePackageFileService do } end - subject { described_class.new(project, user, params).execute } + subject(:execute_service) { described_class.new(project, user, params).execute } before do FileUtils.touch(temp_file) @@ -47,14 +50,14 @@ RSpec.describe Packages::Generic::CreatePackageFileService do end it 'creates package file', :aggregate_failures do - expect { subject }.to change { package.package_files.count }.by(1) + expect { execute_service }.to change { package.package_files.count }.by(1) .and change { Packages::PackageFileBuildInfo.count }.by(1) package_file = package.package_files.last aggregate_failures do expect(package_file.package.status).to eq('default') expect(package_file.package).to eq(package) - expect(package_file.file_name).to eq('myfile.tar.gz.1') + expect(package_file.file_name).to eq(file_name) expect(package_file.size).to eq(file.size) expect(package_file.file_sha256).to eq(sha256) end @@ -65,7 +68,7 @@ RSpec.describe Packages::Generic::CreatePackageFileService do let(:package_params) { super().merge(status: 'hidden') } it 'updates an existing packages status' do - expect { subject }.to change { package.package_files.count }.by(1) + expect { execute_service }.to change { package.package_files.count }.by(1) .and change { Packages::PackageFileBuildInfo.count }.by(1) package_file = package.package_files.last @@ -76,5 +79,32 @@ RSpec.describe Packages::Generic::CreatePackageFileService do end it_behaves_like 'assigns build to package file' + + context 'with existing package' do + before do + create(:package_file, package: package, file_name: file_name) + end + + it { expect { execute_service }.to change { project.package_files.count }.by(1) } + + context 'when duplicates are not allowed' do + before do + package.project.namespace.package_settings.update!(generic_duplicates_allowed: false) + end + + it 'does not allow duplicates' do + expect { execute_service }.to raise_error(::Packages::DuplicatePackageError) + .and change { project.package_files.count }.by(0) + end + + context 'when the package name matches the exception regex' do + before do + package.project.namespace.package_settings.update!(generic_duplicate_exception_regex: '.*') + end + + it { expect { execute_service }.to change { project.package_files.count }.by(1) } + end + 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 2543ab0c669..803371af4bf 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 @@ -130,7 +130,15 @@ RSpec.describe Packages::Maven::FindOrCreatePackageService do context 'when the package name matches the exception regex' do before do - package_settings.update!(maven_duplicate_exception_regex: '.*') + package_settings.update!(maven_duplicate_exception_regex: existing_package.name) + end + + it_behaves_like 'reuse existing package' + end + + context 'when the package version matches the exception regex' do + before do + package_settings.update!(maven_duplicate_exception_regex: existing_package.version) end it_behaves_like 'reuse existing package' diff --git a/spec/services/packages/nuget/search_service_spec.rb b/spec/services/packages/nuget/search_service_spec.rb index db758dc6672..1838065c5be 100644 --- a/spec/services/packages/nuget/search_service_spec.rb +++ b/spec/services/packages/nuget/search_service_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Packages::Nuget::SearchService do 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_with_refind(: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') } let_it_be(:package_d) { create(:nuget_package, project: project, name: 'FooBarD') } @@ -79,6 +79,16 @@ RSpec.describe Packages::Nuget::SearchService do it { expect_search_results 4, package_a, packages_b, packages_c, package_d } end + context 'with non-displayable packages' do + let(:search_term) { '' } + + before do + package_a.update_column(:status, 1) + end + + it { expect_search_results 3, packages_b, packages_c, package_d } + end + context 'with prefix search term' do let(:search_term) { 'dummy' } diff --git a/spec/services/packages/rubygems/dependency_resolver_service_spec.rb b/spec/services/packages/rubygems/dependency_resolver_service_spec.rb index 206bffe53f8..78abfc96ed5 100644 --- a/spec/services/packages/rubygems/dependency_resolver_service_spec.rb +++ b/spec/services/packages/rubygems/dependency_resolver_service_spec.rb @@ -92,7 +92,7 @@ RSpec.describe Packages::Rubygems::DependencyResolverService do ] }] - expect(subject.payload).to eq(expected_result) + expect(subject.payload).to match_array(expected_result) end end end diff --git a/spec/services/packages/rubygems/process_gem_service_spec.rb b/spec/services/packages/rubygems/process_gem_service_spec.rb index 83e868d9579..64deb39c6d8 100644 --- a/spec/services/packages/rubygems/process_gem_service_spec.rb +++ b/spec/services/packages/rubygems/process_gem_service_spec.rb @@ -16,12 +16,11 @@ RSpec.describe Packages::Rubygems::ProcessGemService do describe '#execute' do subject { service.execute } - context 'no gem file', :aggregate_failures do + context 'no gem file' do let(:package_file) { nil } it 'returns an error' do - expect(subject.error?).to be(true) - expect(subject.message).to eq('Gem was not processed') + expect { subject }.to raise_error(::Packages::Rubygems::ProcessGemService::ExtractionError, 'Gem was not processed - package_file is not set') end end diff --git a/spec/services/packages/terraform_module/create_package_service_spec.rb b/spec/services/packages/terraform_module/create_package_service_spec.rb new file mode 100644 index 00000000000..f911bb5b82c --- /dev/null +++ b/spec/services/packages/terraform_module/create_package_service_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::TerraformModule::CreatePackageService do + let_it_be(:namespace) { create(:namespace) } + let_it_be(:project) { create(:project, namespace: namespace) } + let_it_be(:user) { create(:user) } + let_it_be(:sha256) { '440e5e148a25331bbd7991575f7d54933c0ebf6cc735a18ee5066ac1381bb590' } + let_it_be(:temp_file) { Tempfile.new('test') } + let_it_be(:file) { UploadedFile.new(temp_file.path, sha256: sha256) } + + let(:overrides) { {} } + + let(:params) do + { + module_name: 'foo', + module_system: 'bar', + module_version: '1.0.1', + file: file, + file_name: 'foo-bar-1.0.1.tgz' + }.merge(overrides) + end + + subject { described_class.new(project, user, params).execute } + + describe '#execute' do + context 'valid package' do + it 'creates a package' do + expect { subject } + .to change { ::Packages::Package.count }.by(1) + .and change { ::Packages::Package.terraform_module.count }.by(1) + end + end + + context 'package already exists elsewhere' do + let(:project2) { create(:project, namespace: namespace) } + let!(:existing_package) { create(:terraform_module_package, project: project2, name: 'foo/bar', version: '1.0.0') } + + it { expect(subject[:http_status]).to eq 403 } + it { expect(subject[:message]).to be 'Package already exists.' } + end + + context 'version already exists' do + let!(:existing_version) { create(:terraform_module_package, project: project, name: 'foo/bar', version: '1.0.1') } + + it { expect(subject[:http_status]).to eq 403 } + it { expect(subject[:message]).to be 'Package version already exists.' } + end + + context 'with empty version' do + let(:overrides) { { module_version: '' } } + + it { expect(subject[:http_status]).to eq 400 } + it { expect(subject[:message]).to eq 'Version is empty.' } + end + end +end |