diff options
Diffstat (limited to 'spec/services/packages')
10 files changed, 404 insertions, 11 deletions
diff --git a/spec/services/packages/composer/create_package_service_spec.rb b/spec/services/packages/composer/create_package_service_spec.rb index d10356cfda7..4f1a46e7e45 100644 --- a/spec/services/packages/composer/create_package_service_spec.rb +++ b/spec/services/packages/composer/create_package_service_spec.rb @@ -43,6 +43,7 @@ RSpec.describe Packages::Composer::CreatePackageService do end it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' end context 'with a tag' do @@ -66,6 +67,7 @@ RSpec.describe Packages::Composer::CreatePackageService do end it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' end end diff --git a/spec/services/packages/conan/create_package_service_spec.rb b/spec/services/packages/conan/create_package_service_spec.rb index ca783475503..6f644f5ef95 100644 --- a/spec/services/packages/conan/create_package_service_spec.rb +++ b/spec/services/packages/conan/create_package_service_spec.rb @@ -31,6 +31,7 @@ RSpec.describe Packages::Conan::CreatePackageService do it_behaves_like 'assigns the package creator' it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' end context 'invalid params' do diff --git a/spec/services/packages/debian/create_distribution_service_spec.rb b/spec/services/packages/debian/create_distribution_service_spec.rb new file mode 100644 index 00000000000..87cf1070075 --- /dev/null +++ b/spec/services/packages/debian/create_distribution_service_spec.rb @@ -0,0 +1,122 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Debian::CreateDistributionService do + RSpec.shared_examples 'Create Debian Distribution' do |expected_message, expected_components, expected_architectures| + it 'returns ServiceResponse', :aggregate_failures do + if expected_message.nil? + expect { response } + .to change { container.debian_distributions.klass.all.count } + .from(0).to(1) + .and change { container.debian_distributions.count } + .from(0).to(1) + .and change { container.debian_distributions.first&.components&.count } + .from(nil).to(expected_components.count) + .and change { container.debian_distributions.first&.architectures&.count } + .from(nil).to(expected_architectures.count) + .and not_change { Packages::Debian::ProjectComponentFile.count } + .and not_change { Packages::Debian::GroupComponentFile.count } + else + expect { response } + .to not_change { container.debian_distributions.klass.all.count } + .and not_change { container.debian_distributions.count } + .and not_change { Packages::Debian::ProjectComponent.count } + .and not_change { Packages::Debian::GroupComponent.count } + .and not_change { Packages::Debian::ProjectArchitecture.count } + .and not_change { Packages::Debian::GroupArchitecture.count } + .and not_change { Packages::Debian::ProjectComponentFile.count } + .and not_change { Packages::Debian::GroupComponentFile.count } + end + + expect(response).to be_a(ServiceResponse) + expect(response.success?).to eq(expected_message.nil?) + expect(response.error?).to eq(!expected_message.nil?) + expect(response.message).to eq(expected_message) + + distribution = response.payload[:distribution] + expect(distribution.persisted?).to eq(expected_message.nil?) + expect(distribution.container).to eq(container) + expect(distribution.creator).to eq(user) + params.each_pair do |name, value| + expect(distribution.send(name)).to eq(value) + end + + expect(distribution.components.map(&:name)).to contain_exactly(*expected_components) + expect(distribution.architectures.map(&:name)).to contain_exactly(*expected_architectures) + end + end + + shared_examples 'Debian Create Distribution Service' do + context 'with only the codename param' do + let(:params) { { codename: 'my-codename' } } + + it_behaves_like 'Create Debian Distribution', nil, %w[main], %w[all amd64] + end + + context 'with codename, components and architectures' do + let(:params) do + { + codename: 'my-codename', + components: %w[contrib non-free], + architectures: %w[arm64] + } + end + + it_behaves_like 'Create Debian Distribution', nil, %w[contrib non-free], %w[all arm64] + end + + context 'with invalid suite' do + let(:params) do + { + codename: 'my-codename', + suite: 'erroné' + } + end + + it_behaves_like 'Create Debian Distribution', 'Suite is invalid', %w[], %w[] + end + + context 'with invalid component name' do + let(:params) do + { + codename: 'my-codename', + components: %w[before erroné after], + architectures: %w[arm64] + } + end + + it_behaves_like 'Create Debian Distribution', 'Component Name is invalid', %w[before erroné], %w[] + end + + context 'with invalid architecture name' do + let(:params) do + { + codename: 'my-codename', + components: %w[contrib non-free], + architectures: %w[before erroné after'] + } + end + + it_behaves_like 'Create Debian Distribution', 'Architecture Name is invalid', %w[contrib non-free], %w[before erroné] + end + end + + let_it_be(:user) { create(:user) } + + subject { described_class.new(container, user, params) } + + let(:response) { subject.execute } + + context 'within a projet' do + let_it_be(:container) { create(:project) } + + it_behaves_like 'Debian Create Distribution Service' + end + + context 'within a group' do + let_it_be(:container) { create(:group) } + + it_behaves_like 'Debian Create Distribution Service' + end +end diff --git a/spec/services/packages/debian/destroy_distribution_service_spec.rb b/spec/services/packages/debian/destroy_distribution_service_spec.rb new file mode 100644 index 00000000000..e4c43884bb4 --- /dev/null +++ b/spec/services/packages/debian/destroy_distribution_service_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Debian::DestroyDistributionService do + RSpec.shared_examples 'Destroy Debian Distribution' do |expected_message| + it 'returns ServiceResponse', :aggregate_failures do + if expected_message.nil? + expect { response } + .to change { container.debian_distributions.klass.all.count } + .from(1).to(0) + .and change { container.debian_distributions.count } + .from(1).to(0) + .and change { component1.class.all.count } + .from(2).to(0) + .and change { architecture1.class.all.count } + .from(3).to(0) + .and change { component_file1.class.all.count } + .from(4).to(0) + else + expect { response } + .to not_change { container.debian_distributions.klass.all.count } + .and not_change { container.debian_distributions.count } + .and not_change { component1.class.all.count } + .and not_change { architecture1.class.all.count } + .and not_change { component_file1.class.all.count } + end + + expect(response).to be_a(ServiceResponse) + expect(response.success?).to eq(expected_message.nil?) + expect(response.error?).to eq(!expected_message.nil?) + expect(response.message).to eq(expected_message) + + if expected_message.nil? + expect(response.payload).to eq({}) + else + expect(response.payload).to eq(distribution: distribution) + end + end + end + + RSpec.shared_examples 'Debian Destroy Distribution Service' do |container_type, can_freeze| + context "with a Debian #{container_type} distribution" do + let_it_be(:container, freeze: can_freeze) { create(container_type) } # rubocop:disable Rails/SaveBang + let_it_be(:distribution, freeze: can_freeze) { create("debian_#{container_type}_distribution", container: container) } + let_it_be(:component1, freeze: can_freeze) { create("debian_#{container_type}_component", distribution: distribution, name: 'component1') } + let_it_be(:component2, freeze: can_freeze) { create("debian_#{container_type}_component", distribution: distribution, name: 'component2') } + let_it_be(:architecture0, freeze: true) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'all') } + let_it_be(:architecture1, freeze: can_freeze) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'architecture1') } + let_it_be(:architecture2, freeze: can_freeze) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'architecture2') } + let_it_be(:component_file1, freeze: can_freeze) { create("debian_#{container_type}_component_file", :source, component: component1) } + let_it_be(:component_file2, freeze: can_freeze) { create("debian_#{container_type}_component_file", component: component1, architecture: architecture1) } + let_it_be(:component_file3, freeze: can_freeze) { create("debian_#{container_type}_component_file", :source, component: component2) } + let_it_be(:component_file4, freeze: can_freeze) { create("debian_#{container_type}_component_file", component: component2, architecture: architecture2) } + + subject { described_class.new(distribution) } + + let(:response) { subject.execute } + + context 'with a distribution' do + it_behaves_like 'Destroy Debian Distribution' + end + + context 'when destroy fails' do + let(:distribution) { create("debian_#{container_type}_distribution", container: container) } + + before do + expect(distribution).to receive(:destroy).and_return(false) + end + + it_behaves_like 'Destroy Debian Distribution', "Unable to destroy Debian #{container_type} distribution" + end + end + end + + it_behaves_like 'Debian Destroy Distribution Service', :project, true + it_behaves_like 'Debian Destroy Distribution Service', :group, false +end diff --git a/spec/services/packages/debian/update_distribution_service_spec.rb b/spec/services/packages/debian/update_distribution_service_spec.rb new file mode 100644 index 00000000000..852fc713e34 --- /dev/null +++ b/spec/services/packages/debian/update_distribution_service_spec.rb @@ -0,0 +1,159 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Debian::UpdateDistributionService do + RSpec.shared_examples 'Update Debian Distribution' do |expected_message, expected_components, expected_architectures, component_file_delta = 0| + it 'returns ServiceResponse', :aggregate_failures do + expect(distribution).to receive(:update).with(simple_params).and_call_original if expected_message.nil? + + if component_file_delta.zero? + expect { response } + .to not_change { container.debian_distributions.klass.all.count } + .and not_change { container.debian_distributions.count } + .and not_change { component1.class.all.count } + .and not_change { architecture1.class.all.count } + .and not_change { component_file1.class.all.count } + else + expect { response } + .to not_change { container.debian_distributions.klass.all.count } + .and not_change { container.debian_distributions.count } + .and not_change { component1.class.all.count } + .and not_change { architecture1.class.all.count } + .and change { component_file1.class.all.count } + .from(4).to(4 + component_file_delta) + end + + expect(response).to be_a(ServiceResponse) + expect(response.success?).to eq(expected_message.nil?) + expect(response.error?).to eq(!expected_message.nil?) + expect(response.message).to eq(expected_message) + + expect(response.payload).to eq(distribution: distribution) + + distribution.reload + distribution.components.reload + distribution.architectures.reload + + if expected_message.nil? + simple_params.each_pair do |name, value| + expect(distribution.send(name)).to eq(value) + end + else + original_params.each_pair do |name, value| + expect(distribution.send(name)).to eq(value) + end + end + + expect(distribution.components.map(&:name)).to contain_exactly(*expected_components) + expect(distribution.architectures.map(&:name)).to contain_exactly(*expected_architectures) + end + end + + RSpec.shared_examples 'Debian Update Distribution Service' do |container_type, can_freeze| + context "with a Debian #{container_type} distribution" do + let_it_be(:container, freeze: can_freeze) { create(container_type) } # rubocop:disable Rails/SaveBang + let_it_be(:distribution, reload: true) { create("debian_#{container_type}_distribution", container: container) } + let_it_be(:component1) { create("debian_#{container_type}_component", distribution: distribution, name: 'component1') } + let_it_be(:component2) { create("debian_#{container_type}_component", distribution: distribution, name: 'component2') } + let_it_be(:architecture0) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'all') } + let_it_be(:architecture1) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'architecture1') } + let_it_be(:architecture2) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'architecture2') } + let_it_be(:component_file1) { create("debian_#{container_type}_component_file", :source, component: component1) } + let_it_be(:component_file2) { create("debian_#{container_type}_component_file", component: component1, architecture: architecture1) } + let_it_be(:component_file3) { create("debian_#{container_type}_component_file", :source, component: component2) } + let_it_be(:component_file4) { create("debian_#{container_type}_component_file", component: component2, architecture: architecture2) } + + let(:original_params) do + { + suite: nil, + origin: nil, + label: nil, + version: nil, + description: nil, + valid_time_duration_seconds: nil, + automatic: true, + automatic_upgrades: false + } + end + + let(:params) { {} } + let(:simple_params) { params.except(:components, :architectures) } + + subject { described_class.new(distribution, params) } + + let(:response) { subject.execute } + + context 'with valid simple params' do + let(:params) do + { + suite: 'my-suite', + origin: 'my-origin', + label: 'my-label', + version: '42.0', + description: 'my-description', + valid_time_duration_seconds: 7.days, + automatic: false, + automatic_upgrades: true + } + end + + it_behaves_like 'Update Debian Distribution', nil, %w[component1 component2], %w[all architecture1 architecture2] + end + + context 'with invalid simple params' do + let(:params) do + { + suite: 'suite erronée', + origin: 'origin erronée', + label: 'label erronée', + version: 'version erronée', + description: 'description erronée', + valid_time_duration_seconds: 1.hour + } + end + + it_behaves_like 'Update Debian Distribution', 'Suite is invalid, Origin is invalid, Label is invalid, Version is invalid, and Valid time duration seconds must be greater than or equal to 86400', %w[component1 component2], %w[all architecture1 architecture2] + end + + context 'with valid components and architectures' do + let(:params) do + { + suite: 'my-suite', + components: %w[component2 component3], + architectures: %w[architecture2 architecture3] + } + end + + it_behaves_like 'Update Debian Distribution', nil, %w[component2 component3], %w[all architecture2 architecture3], -2 + end + + context 'with invalid components' do + let(:params) do + { + suite: 'my-suite', + components: %w[component2 erroné], + architectures: %w[architecture2 architecture3] + } + end + + it_behaves_like 'Update Debian Distribution', 'Component Name is invalid', %w[component1 component2], %w[all architecture1 architecture2] + end + + context 'with invalid architectures' do + let(:params) do + { + suite: 'my-suite', + components: %w[component2 component3], + architectures: %w[architecture2 erroné] + } + end + + it_behaves_like 'Update Debian Distribution', 'Architecture Name is invalid', %w[component1 component2], %w[all architecture1 architecture2] + end + end + end + + it_behaves_like 'Debian Update Distribution Service', :project, true + it_behaves_like 'Debian Update Distribution Service', :group, false +end 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 816e728c342..10c54369f26 100644 --- a/spec/services/packages/generic/create_package_file_service_spec.rb +++ b/spec/services/packages/generic/create_package_file_service_spec.rb @@ -13,6 +13,8 @@ RSpec.describe Packages::Generic::CreatePackageFileService do 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 { package_name: 'mypackage', @@ -23,31 +25,34 @@ RSpec.describe Packages::Generic::CreatePackageFileService do } end + let(:package_params) do + { + name: params[:package_name], + version: params[:package_version], + build: params[:build], + status: nil + } + end + subject { described_class.new(project, user, params).execute } before do FileUtils.touch(temp_file) + expect(::Packages::Generic::FindOrCreatePackageService).to receive(:new).with(project, user, package_params).and_return(package_service) + expect(package_service).to receive(:execute).and_return(package) end after do FileUtils.rm_f(temp_file) end - it 'creates package file' do - package_service = double - package_params = { - name: params[:package_name], - version: params[:package_version], - build: params[:build] - } - expect(::Packages::Generic::FindOrCreatePackageService).to receive(:new).with(project, user, package_params).and_return(package_service) - expect(package_service).to receive(:execute).and_return(package) - + it 'creates package file', :aggregate_failures do expect { subject }.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.size).to eq(file.size) @@ -55,6 +60,21 @@ RSpec.describe Packages::Generic::CreatePackageFileService do end end + context 'with a status' do + let(:params) { super().merge(status: 'hidden') } + let(:package_params) { super().merge(status: 'hidden') } + + it 'updates an existing packages status' do + expect { subject }.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('hidden') + end + end + end + it_behaves_like 'assigns build to package file' 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 82dffeefcde..2543ab0c669 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 @@ -36,10 +36,11 @@ RSpec.describe Packages::Maven::FindOrCreatePackageService do expect(pkg.version).to eq(version) end - context 'with a build' do + context 'with optional attributes' do subject { service.execute.payload[:package] } it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' end end @@ -111,6 +112,13 @@ RSpec.describe Packages::Maven::FindOrCreatePackageService do expect(subject.errors).to include('Duplicate package is not allowed') end + context 'when uploading to the versionless package which contains metadata about all versions' do + let(:version) { nil } + let(:param_path) { path } + + it_behaves_like 'reuse existing package' + 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') diff --git a/spec/services/packages/npm/create_package_service_spec.rb b/spec/services/packages/npm/create_package_service_spec.rb index 6db3777cde8..10fce6c1651 100644 --- a/spec/services/packages/npm/create_package_service_spec.rb +++ b/spec/services/packages/npm/create_package_service_spec.rb @@ -53,6 +53,7 @@ RSpec.describe Packages::Npm::CreatePackageService do let(:params) { super().merge(build: job) } it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' it 'creates a package file build info' do expect { subject }.to change { Packages::PackageFileBuildInfo.count }.by(1) diff --git a/spec/services/packages/nuget/create_package_service_spec.rb b/spec/services/packages/nuget/create_package_service_spec.rb index 5289ad40d61..e338ac36fc3 100644 --- a/spec/services/packages/nuget/create_package_service_spec.rb +++ b/spec/services/packages/nuget/create_package_service_spec.rb @@ -32,5 +32,6 @@ RSpec.describe Packages::Nuget::CreatePackageService do it_behaves_like 'assigns the package creator' it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' end end diff --git a/spec/services/packages/pypi/create_package_service_spec.rb b/spec/services/packages/pypi/create_package_service_spec.rb index 28a727c4a09..a932cf73eb7 100644 --- a/spec/services/packages/pypi/create_package_service_spec.rb +++ b/spec/services/packages/pypi/create_package_service_spec.rb @@ -52,6 +52,7 @@ RSpec.describe Packages::Pypi::CreatePackageService do end it_behaves_like 'assigns build to package' + it_behaves_like 'assigns status to package' context 'with an existing package' do before do |