From a18ecdcd031227793140808fe9d55aabbb44c2de Mon Sep 17 00:00:00 2001 From: snehaldwivedi Date: Thu, 7 Oct 2021 13:50:57 +0000 Subject: Fix for upgrade on preinstalled dnf_package resource downgrades Signed-off-by: snehaldwivedi --- lib/chef/provider/package/dnf.rb | 20 +++++- spec/functional/resource/dnf_package_spec.rb | 100 +++++++-------------------- 2 files changed, 42 insertions(+), 78 deletions(-) diff --git a/lib/chef/provider/package/dnf.rb b/lib/chef/provider/package/dnf.rb index 5c74ad0414..f46c5434c5 100644 --- a/lib/chef/provider/package/dnf.rb +++ b/lib/chef/provider/package/dnf.rb @@ -120,8 +120,24 @@ class Chef flushcache end - # dnf upgrade does not work on uninstalled packaged, while install will upgrade - alias upgrade_package install_package + # dnf upgrade does not work on uninstalled packaged in that case redirecting them to install and upgrade will not work with allow_downgrade option + def upgrade_package(names, versions) + begin + if new_resource.source + dnf(options, "-y", "upgrade", new_resource.source) + else + resolved_names = names.each_with_index.map { |name, i| available_version(i).to_s unless name.nil? } + dnf(options, "-y", "upgrade", resolved_names) + end + rescue => e + if e.message.match("Error: No packages marked for upgrade.") + Chef::Log.error(e.message.slice(/\STDERR(.*?)not installed./)) + else + raise e + end + end + flushcache + end def remove_package(names, versions) resolved_names = names.each_with_index.map { |name, i| magical_version(i).to_s unless name.nil? } diff --git a/spec/functional/resource/dnf_package_spec.rb b/spec/functional/resource/dnf_package_spec.rb index 9e03db5123..d421b0679b 100644 --- a/spec/functional/resource/dnf_package_spec.rb +++ b/spec/functional/resource/dnf_package_spec.rb @@ -1049,7 +1049,7 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end context "with source arguments" do - it "installs the package when using the source argument" do + it "will not install the package with upgrade action when using the source argument" do flush_cache dnf_package "something" do options default_options @@ -1057,39 +1057,25 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do source("#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm") action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "something" do - options default_options - package_name "somethingelse" - source("#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm") - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$") end - it "installs the package when the name is a path to a file" do + it "will not install the package with upgrade action when the name is a path to a file" do flush_cache dnf_package "#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$") end - it "downgrades the package when allow_downgrade is true" do + it "upgrade action should not downgrades the package when allow_downgrade is true" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end it "upgrades the package" do @@ -1126,18 +1112,14 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") end - it "works with a local source" do + it "will not works with a local source if package is not already installed" do FileUtils.rm_f "/etc/yum.repos.d/chef-dnf-localtesting.repo" flush_cache dnf_package "#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm" do options "--nogpgcheck --disablerepo=*" action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "#{CHEF_SPEC_ASSETS}/yumrepo/chef_rpm-1.2-1.#{pkg_arch}.rpm" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$") end end @@ -1157,19 +1139,14 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end.should_not_be_updated end - it "with a full version pin in the name it downgrades the package" do + it "with a full version pin in the name it should not downgrades the package" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "chef_rpm" do options default_options version "1.2-1" action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm" do - options default_options - version "1.2-1" - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end it "with a partial (no release) version pin it installs a later package" do @@ -1187,19 +1164,14 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end.should_not_be_updated end - it "with a partial (no release) version pin in the name it downgrades the package" do + it "with a partial (no release) version pin in the name it should not downgrades the package" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "chef_rpm" do options default_options version("1.2") action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm" do - options default_options - version("1.2") - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end it "with a full version pin it installs a later package" do @@ -1215,17 +1187,13 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end.should_not_be_updated end - it "with a full version pin in the name it downgrades the package" do + it "with a full version pin in the name it should not downgrades the package" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "chef_rpm-1.2-1" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm-1.2-1" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end it "with a partial (no release) version pin it installs a later package" do @@ -1241,17 +1209,13 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end.should_not_be_updated end - it "with a partial (no release) version pin in the name it downgrades the package" do + it "with a partial (no release) version pin in the name it won't downgrades the package" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "chef_rpm-1.2" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm-1.2" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end it "with a prco equality pin in the name it upgrades a prior package" do @@ -1267,43 +1231,31 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end.should_not_be_updated end - it "with a prco equality pin in the name it downgrades a later package" do + it "with a prco equality pin in the name it wont downgrades a later package" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "chef_rpm = 1.2" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm = 1.2" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end - it "with a > pin in the name and no rpm installed it installs" do + it "with a > pin in the name and no rpm installed it should not install with upgrade action" do flush_cache dnf_package "chef_rpm > 1.2" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") - dnf_package "chef_rpm > 1.2" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$") end - it "with a < pin in the name and no rpm installed it installs" do + it "with a < pin in the name and no rpm installed it should not install with upgrade action" do flush_cache dnf_package "chef_rpm < 1.10" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm < 1.10" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$") end it "with a > pin in the name and matching rpm installed it does nothing" do @@ -1337,17 +1289,13 @@ describe Chef::Resource::DnfPackage, :requires_root, external: exclude_test do end.should_not_be_updated end - it "with a < pin in the name and non-matching rpm installed it downgrades" do + it "with a < pin in the name and non-matching rpm installed it should not downgrade package" do preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm") dnf_package "chef_rpm < 1.10" do options default_options action :upgrade end.should_be_updated - expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$") - dnf_package "chef_rpm < 1.10" do - options default_options - action :upgrade - end.should_not_be_updated + expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$") end end end -- cgit v1.2.1