summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpratixha <pratiksha.prajapati@msystechnologies.com>2022-05-10 17:56:00 +0530
committerpratixha <pratiksha.prajapati@msystechnologies.com>2022-05-25 12:13:25 +0530
commitfa552fab9be9551dbc8f06f98c35a28b21fe7d82 (patch)
tree625444b40f9ffe88313295443e9e2f1c1617d2a1
parent9e40fb06c3659a8f3c2d53b0fa476fcc6775f66b (diff)
downloadchef-fa552fab9be9551dbc8f06f98c35a28b21fe7d82.tar.gz
Missing source path unit and functional specs
Signed-off-by: pratixha <pratiksha.prajapati@msystechnologies.com>
-rw-r--r--lib/chef/provider/package/zypper.rb26
-rw-r--r--spec/functional/resource/zypper_package_spec.rb5
-rw-r--r--spec/unit/provider/package/zypper_spec.rb13
3 files changed, 43 insertions, 1 deletions
diff --git a/lib/chef/provider/package/zypper.rb b/lib/chef/provider/package/zypper.rb
index 1a1cdea47b..9cd95aade7 100644
--- a/lib/chef/provider/package/zypper.rb
+++ b/lib/chef/provider/package/zypper.rb
@@ -33,6 +33,15 @@ class Chef
provides :package, platform_family: "suse"
provides :zypper_package
+ def define_resource_requirements
+ super
+ requirements.assert(:install, :upgrade) do |a|
+ a.assertion { source_files_exist? }
+ a.failure_message Chef::Exceptions::Package, "#{new_resource} source file(s) do not exist: #{missing_sources}"
+ a.whyrun "Assuming they would have been previously created."
+ end
+ end
+
def load_current_resource
@current_resource = Chef::Resource::ZypperPackage.new(new_resource.name)
current_resource.package_name(new_resource.package_name)
@@ -77,6 +86,23 @@ class Chef
end
end
+ # returns true if all sources exist. Returns false if any do not, or if no
+ # sources were specified.
+ # @return [Boolean] True if all sources exist
+ def source_files_exist?
+ if !new_resource.source.nil?
+ resolved_source_array.all? { |s| s && ::File.exist?(s) }
+ else
+ true
+ end
+ end
+
+ # Helper to return all the names of the missing sources for error messages.
+ # @return [Array<String>] Array of missing sources
+ def missing_sources
+ resolved_source_array.select { |s| s.nil? || !::File.exist?(s) }
+ end
+
def resolve_source_to_version
shell_out!("rpm -qp --queryformat '%{NAME} %{EPOCH} %{VERSION} %{RELEASE} %{ARCH}\n' #{new_resource.source}").stdout.each_line do |line|
case line
diff --git a/spec/functional/resource/zypper_package_spec.rb b/spec/functional/resource/zypper_package_spec.rb
index 87c5f0e5e3..fbf394bae8 100644
--- a/spec/functional/resource/zypper_package_spec.rb
+++ b/spec/functional/resource/zypper_package_spec.rb
@@ -81,6 +81,11 @@ describe Chef::Resource::ZypperPackage, :requires_root, :suse_only do
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 "raises an error when passed a source that does not exist" do
+ zypper_package.source = "#{CHEF_SPEC_ASSETS}/false/zypprepo/chef_rpm-1.10-1.#{pkg_arch}.rpm"
+ expect { zypper_package.run_action(:install) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed)
+ end
+
it "does not install if the package is installed" do
preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm")
zypper_package.run_action(:install)
diff --git a/spec/unit/provider/package/zypper_spec.rb b/spec/unit/provider/package/zypper_spec.rb
index 95810edcb4..2850a70560 100644
--- a/spec/unit/provider/package/zypper_spec.rb
+++ b/spec/unit/provider/package/zypper_spec.rb
@@ -174,6 +174,12 @@ describe Chef::Provider::Package::Zypper do
)
provider.install_package(["wget"], ["1.11.4-1ubuntu1_amd64"])
end
+
+ it "should raise an exception if a source is supplied but not found when :install" do
+ new_resource.source "/tmp/blah/wget_1.11.4-1ubuntu1_amd64.rpm"
+ allow(::File).to receive(:exist?).with(new_resource.source).and_return(false)
+ expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
+ end
end
describe "upgrade_package" do
@@ -218,9 +224,14 @@ describe Chef::Provider::Package::Zypper do
shell_out_expectation!(
"zypper", "--non-interactive", "install", "--auto-agree-with-licenses", "--oldpackage", "/tmp/wget_1.11.4-1ubuntu1_amd64.rpm"
)
- provider.install_package(["wget"], ["1.11.4-1ubuntu1_amd64"])
+ provider.upgrade_package(["wget"], ["1.11.4-1ubuntu1_amd64"])
end
+ it "should raise an exception if a source is supplied but not found when :upgrade" do
+ new_resource.source "/tmp/blah/wget_1.11.4-1ubuntu1_amd64.rpm"
+ allow(::File).to receive(:exist?).with(new_resource.source).and_return(false)
+ expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
+ end
end
describe "remove_package" do