diff options
Diffstat (limited to 'spec/services/packages')
12 files changed, 302 insertions, 114 deletions
diff --git a/spec/services/packages/composer/composer_json_service_spec.rb b/spec/services/packages/composer/composer_json_service_spec.rb index 378016a6ffb..d2187688c4c 100644 --- a/spec/services/packages/composer/composer_json_service_spec.rb +++ b/spec/services/packages/composer/composer_json_service_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Packages::Composer::ComposerJsonService do subject { described_class.new(project, target).execute } context 'with an existing file' do - let(:project) { create(:project, :custom_repo, files: { 'composer.json' => json } ) } + let(:project) { create(:project, :custom_repo, files: { 'composer.json' => json }) } context 'with a valid file' do let(:json) { '{ "name": "package-name"}' } diff --git a/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb b/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb index 39c6feb5d12..70c2bbad87a 100644 --- a/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb +++ b/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb @@ -65,6 +65,23 @@ RSpec.describe ::Packages::Maven::Metadata::CreateVersionsXmlService do let(:versions_in_database) { versions_in_xml + additional_versions } it_behaves_like 'returning an xml with versions in the database' + + context 'with an xml without a release version' do + let(:version_release) { nil } + + it_behaves_like 'returning an xml with versions in the database' + + it 'logs a warn with the reason' do + expect(Gitlab::AppJsonLogger).to receive(:warn).with( + message: 'A malformed metadata file has been encountered', + reason: 'Missing release tag', + project_id: package.project_id, + package_id: package.id + ) + + subject + end + end end end diff --git a/spec/services/packages/npm/create_package_service_spec.rb b/spec/services/packages/npm/create_package_service_spec.rb index a3e59913918..ef8cdf2e8ab 100644 --- a/spec/services/packages/npm/create_package_service_spec.rb +++ b/spec/services/packages/npm/create_package_service_spec.rb @@ -148,7 +148,7 @@ RSpec.describe Packages::Npm::CreatePackageService do end context 'when file size is faked by setting the attachment length param to a lower size' do - let(:params) { super().deep_merge!( { _attachments: { "#{package_name}-#{version}.tgz" => { data: encoded_package_data, length: 1 } } }) } + let(:params) { super().deep_merge!({ _attachments: { "#{package_name}-#{version}.tgz" => { data: encoded_package_data, length: 1 } } }) } # TODO (technical debt): Extract the package size calculation outside the service and add separate specs for it. # Right now we have several contexts here to test the calculation's different scenarios. @@ -193,7 +193,7 @@ RSpec.describe Packages::Npm::CreatePackageService do end context 'with empty versions' do - let(:params) { super().merge!({ versions: {} } ) } + let(:params) { super().merge!({ versions: {} }) } it { expect(subject[:http_status]).to eq 400 } it { expect(subject[:message]).to eq 'Version is empty.' } diff --git a/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb b/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb deleted file mode 100644 index 524c224177b..00000000000 --- a/spec/services/packages/rpm/repository_metadata/base_builder_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true -require 'spec_helper' - -RSpec.describe Packages::Rpm::RepositoryMetadata::BaseBuilder do - describe '#execute' do - subject { described_class.new(xml: xml, data: data).execute } - - let(:xml) { nil } - let(:data) { {} } - - before do - stub_const("#{described_class}::ROOT_TAG", 'test') - stub_const("#{described_class}::ROOT_ATTRIBUTES", { foo1: 'bar1', foo2: 'bar2' }) - end - - it 'generate valid xml' do - result = Nokogiri::XML::Document.parse(subject) - - expect(result.children.count).to eq(1) - expect(result.children.first.attributes.count).to eq(2) - expect(result.children.first.attributes['foo1'].value).to eq('bar1') - expect(result.children.first.attributes['foo2'].value).to eq('bar2') - end - - context 'when call with parameters' do - let(:xml) { 'test' } - - it 'raise NotImplementedError' do - expect { subject }.to raise_error NotImplementedError - end - end - end -end diff --git a/spec/services/packages/rpm/repository_metadata/build_filelist_xml_service_spec.rb b/spec/services/packages/rpm/repository_metadata/build_filelist_xml_service_spec.rb new file mode 100644 index 00000000000..d93d6ab9fcb --- /dev/null +++ b/spec/services/packages/rpm/repository_metadata/build_filelist_xml_service_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Rpm::RepositoryMetadata::BuildFilelistXmlService do + describe '#execute' do + subject { described_class.new(data).execute } + + include_context 'with rpm package data' + + let(:data) { xml_update_params } + let(:file_xpath) { "//package/file" } + + it 'adds all file nodes' do + result = subject + + expect(result.xpath(file_xpath).count).to eq(data[:files].count) + end + + describe 'setting type attribute' do + context 'when all files are directories' do + let(:dirs) do + 3.times.map { generate_directory } # rubocop:disable Performance/TimesMap + end + + let(:files) do + 5.times.map { FFaker::Filesystem.file_name(dirs.sample) } # rubocop:disable Performance/TimesMap + end + + let(:data) do + { + directories: dirs.map { "#{_1}/" }, # Add trailing slash as in original package + files: dirs + files + } + end + + it 'set dir type attribute for directories only' do + result = subject + + result.xpath(file_xpath).each do |tag| + if dirs.include?(tag.content) + expect(tag.attributes['type']&.value).to eq('dir') + else + expect(tag.attributes['type']).to be_nil + end + end + end + end + + def generate_directory + FFaker::Lorem.words(3).join('/') + end + end + end +end diff --git a/spec/services/packages/rpm/repository_metadata/build_filelist_xml_spec.rb b/spec/services/packages/rpm/repository_metadata/build_filelist_xml_spec.rb deleted file mode 100644 index 2feb44c7c1b..00000000000 --- a/spec/services/packages/rpm/repository_metadata/build_filelist_xml_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true -require 'spec_helper' - -RSpec.describe Packages::Rpm::RepositoryMetadata::BuildFilelistXml do - describe '#execute' do - subject { described_class.new.execute } - - context "when generate empty xml" do - let(:expected_xml) do - <<~XML - <?xml version="1.0" encoding="UTF-8"?> - <filelists xmlns="http://linux.duke.edu/metadata/filelists" packages="0"/> - XML - end - - it 'generate expected xml' do - expect(subject).to eq(expected_xml) - end - end - end -end diff --git a/spec/services/packages/rpm/repository_metadata/build_other_xml_service_spec.rb b/spec/services/packages/rpm/repository_metadata/build_other_xml_service_spec.rb new file mode 100644 index 00000000000..201f9e67ce9 --- /dev/null +++ b/spec/services/packages/rpm/repository_metadata/build_other_xml_service_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Rpm::RepositoryMetadata::BuildOtherXmlService do + describe '#execute' do + subject { described_class.new(data).execute } + + include_context 'with rpm package data' + + let(:data) { xml_update_params } + let(:changelog_xpath) { "//package/changelog" } + + it 'adds all changelog nodes' do + result = subject + + expect(result.xpath(changelog_xpath).count).to eq(data[:changelogs].count) + end + + it 'set required date attribute' do + result = subject + + data[:changelogs].each do |changelog| + expect(result.at("#{changelog_xpath}[@date=\"#{changelog[:changelogtime]}\"]")).not_to be_nil + end + end + end +end diff --git a/spec/services/packages/rpm/repository_metadata/build_other_xml_spec.rb b/spec/services/packages/rpm/repository_metadata/build_other_xml_spec.rb deleted file mode 100644 index 823aa18808a..00000000000 --- a/spec/services/packages/rpm/repository_metadata/build_other_xml_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true -require 'spec_helper' - -RSpec.describe Packages::Rpm::RepositoryMetadata::BuildOtherXml do - describe '#execute' do - subject { described_class.new.execute } - - context "when generate empty xml" do - let(:expected_xml) do - <<~XML - <?xml version="1.0" encoding="UTF-8"?> - <otherdata xmlns="http://linux.duke.edu/metadata/other" packages="0"/> - XML - end - - it 'generate expected xml' do - expect(subject).to eq(expected_xml) - end - end - end -end diff --git a/spec/services/packages/rpm/repository_metadata/build_primary_xml_service_spec.rb b/spec/services/packages/rpm/repository_metadata/build_primary_xml_service_spec.rb new file mode 100644 index 00000000000..9bbfa5c9863 --- /dev/null +++ b/spec/services/packages/rpm/repository_metadata/build_primary_xml_service_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Rpm::RepositoryMetadata::BuildPrimaryXmlService do + describe '#execute' do + subject { described_class.new(data).execute } + + include_context 'with rpm package data' + + let(:data) { xml_update_params } + let(:required_text_only_attributes) { %i[description summary arch name] } + + it 'adds node with required_text_only_attributes' do + result = subject + + required_text_only_attributes.each do |attribute| + expect( + result.at("//package/#{attribute}").text + ).to eq(data[attribute]) + end + end + end +end diff --git a/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb b/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb deleted file mode 100644 index 147d5862a71..00000000000 --- a/spec/services/packages/rpm/repository_metadata/build_primary_xml_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true -require 'spec_helper' - -RSpec.describe Packages::Rpm::RepositoryMetadata::BuildPrimaryXml do - describe '#execute' do - subject { described_class.new(xml: xml, data: data).execute } - - let(:empty_xml) do - <<~XML - <?xml version="1.0" encoding="UTF-8"?> - <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"/> - XML - end - - it_behaves_like 'handling rpm xml file' - - context 'when updating existing xml' do - include_context 'with rpm package data' - - let(:xml) { empty_xml } - let(:data) { xml_update_params } - let(:required_text_only_attributes) { %i[description summary arch name] } - - it 'adds node with required_text_only_attributes' do - result = Nokogiri::XML::Document.parse(subject).remove_namespaces! - - required_text_only_attributes.each do |attribute| - expect( - result.at("//#{described_class::ROOT_TAG}/package/#{attribute}").text - ).to eq(data[attribute]) - end - end - end - end -end diff --git a/spec/services/packages/rpm/repository_metadata/build_repomd_xml_spec.rb b/spec/services/packages/rpm/repository_metadata/build_repomd_xml_service_spec.rb index 0843a983b7e..cf28301fa2c 100644 --- a/spec/services/packages/rpm/repository_metadata/build_repomd_xml_spec.rb +++ b/spec/services/packages/rpm/repository_metadata/build_repomd_xml_service_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'spec_helper' -RSpec.describe Packages::Rpm::RepositoryMetadata::BuildRepomdXml do +RSpec.describe Packages::Rpm::RepositoryMetadata::BuildRepomdXmlService do describe '#execute' do subject { described_class.new(data).execute } diff --git a/spec/services/packages/rpm/repository_metadata/update_xml_service_spec.rb b/spec/services/packages/rpm/repository_metadata/update_xml_service_spec.rb new file mode 100644 index 00000000000..e351392ba1c --- /dev/null +++ b/spec/services/packages/rpm/repository_metadata/update_xml_service_spec.rb @@ -0,0 +1,177 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Rpm::RepositoryMetadata::UpdateXmlService do + describe '#execute' do + subject { described_class.new(filename: filename, xml: xml, data: data).execute } + + let(:xml) { nil } + let(:data) { nil } + + shared_examples 'handling not implemented xml filename' do + let(:filename) { :not_implemented_yet } + let(:empty_xml) { '' } + + it 'raise error' do + expect { subject }.to raise_error(ArgumentError) + end + end + + shared_context 'with primary xml file data' do + let(:filename) { :primary } + let(:empty_xml) do + <<~XML + <?xml version="1.0" encoding="UTF-8"?> + <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"/> + XML + end + end + + shared_context 'with other xml file data' do + let(:filename) { :other } + let(:empty_xml) do + <<~XML + <?xml version="1.0" encoding="UTF-8"?> + <otherdata xmlns="http://linux.duke.edu/metadata/other" packages="0"/> + XML + end + end + + shared_context 'with filelist xml file data' do + let(:filename) { :filelist } + let(:empty_xml) do + <<~XML + <?xml version="1.0" encoding="UTF-8"?> + <filelists xmlns="http://linux.duke.edu/metadata/filelists" packages="0"/> + XML + end + end + + context 'when building empty xml' do + shared_examples 'generating empty xml' do + it 'generate expected xml' do + expect(subject).to eq(empty_xml) + end + end + + it_behaves_like 'handling not implemented xml filename' + + context "for 'primary' xml file" do + include_context 'with primary xml file data' + + it_behaves_like 'generating empty xml' + end + + context "for 'other' xml file" do + include_context 'with other xml file data' + + it_behaves_like 'generating empty xml' + end + + context "for 'filelist' xml file" do + include_context 'with filelist xml file data' + + it_behaves_like 'generating empty xml' + end + end + + context 'when updating xml file' do + include_context 'with rpm package data' + + let(:xml) { empty_xml } + let(:data) { xml_update_params } + let(:builder_class) { described_class::BUILDERS[filename] } + + shared_examples 'updating rpm xml file' do + context 'when updating existing xml' do + shared_examples 'changing root tag attribute' do + it "increment previous 'packages' value by 1" do + previous_value = Nokogiri::XML(xml).at(builder_class::ROOT_TAG).attributes["packages"].value.to_i + new_value = Nokogiri::XML(subject).at(builder_class::ROOT_TAG).attributes["packages"].value.to_i + + expect(previous_value + 1).to eq(new_value) + end + end + + it 'generate valid xml add expected xml node to existing xml' do + # Have one root attribute + result = Nokogiri::XML::Document.parse(subject).remove_namespaces! + expect(result.children.count).to eq(1) + + # Root node has 1 child with generated node + expect(result.xpath("//#{builder_class::ROOT_TAG}/package").count).to eq(1) + end + + context 'when empty xml' do + it_behaves_like 'changing root tag attribute' + end + + context 'when xml has children' do + context "when node with given 'pkgid' does not exist yet" do + let(:uniq_node_data) do + xml_update_params.tap do |data| + data[:pkgid] = SecureRandom.uuid + end + end + + let(:xml) { build_xml_from(uniq_node_data) } + + it 'has children nodes' do + existing_xml = Nokogiri::XML::Document.parse(xml).remove_namespaces! + expect(existing_xml.xpath('//package').count).to eq(1) + end + + it_behaves_like 'changing root tag attribute' + end + + context "when node with given 'pkgid' already exist" do + let(:existing_node_data) do + existing_data = data.dup + existing_data[:name] = FFaker::Lorem.word + existing_data + end + + let(:xml) { build_xml_from(existing_node_data) } + + it 'has children nodes' do + existing_xml = Nokogiri::XML::Document.parse(xml).remove_namespaces! + expect(existing_xml.xpath('//package').count).to eq(1) + end + + it 'replace existing node with new data' do + existing_xml = Nokogiri::XML::Document.parse(xml).remove_namespaces! + result = Nokogiri::XML::Document.parse(subject).remove_namespaces! + expect(result.xpath('//package').count).to eq(1) + expect(result.xpath('//package').first.to_xml).not_to eq(existing_xml.xpath('//package').first.to_xml) + end + end + + def build_xml_from(data) + described_class.new(filename: filename, xml: empty_xml, data: data).execute + end + end + end + end + + it_behaves_like 'handling not implemented xml filename' + + context "for 'primary' xml file" do + include_context 'with primary xml file data' + + it_behaves_like 'updating rpm xml file' + end + + context "for 'other' xml file" do + include_context 'with other xml file data' + + it_behaves_like 'updating rpm xml file' + end + + context "for 'filelist' xml file" do + include_context 'with filelist xml file data' + + it_behaves_like 'updating rpm xml file' + end + end + end +end |