From d00b145f7fc4d22bfa75d8e5b8d3c04e7ccdcf4f Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 7 Feb 2022 17:20:28 -0800 Subject: (Backport) Eager load ffi-libarchive to resolve centos-7 failures backport of #12552 Signed-off-by: Lamont Granquist --- lib/chef/resource/archive_file.rb | 12 ++++++------ spec/unit/resource/archive_file_spec.rb | 14 +++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/chef/resource/archive_file.rb b/lib/chef/resource/archive_file.rb index 4d77ee979b..7af4fc7cb4 100644 --- a/lib/chef/resource/archive_file.rb +++ b/lib/chef/resource/archive_file.rb @@ -20,6 +20,12 @@ require_relative "../resource" require "fileutils" unless defined?(FileUtils) +begin + # ffi-libarchive must be eager loaded see: https://github.com/chef/chef/issues/12228 + require "ffi-libarchive" unless defined?(Archive::Reader) +rescue LoadError + STDERR.puts "ffi-libarchive could not be loaded, libarchive is probably not installed on system, archive_file will not be available" +end class Chef class Resource @@ -88,8 +94,6 @@ class Chef action :extract do description "Extract and archive file." - require_libarchive - unless ::File.exist?(new_resource.path) raise Errno::ENOENT, "No archive found at #{new_resource.path}! Cannot continue." end @@ -127,10 +131,6 @@ class Chef end action_class do - def require_libarchive - require "ffi-libarchive" - end - def define_resource_requirements if new_resource.mode.is_a?(Integer) Chef.deprecated(:archive_file_integer_file_mode, "The mode property should be passed to archive_file resources as a String and not an Integer to ensure the value is properly interpreted.") diff --git a/spec/unit/resource/archive_file_spec.rb b/spec/unit/resource/archive_file_spec.rb index 6effe550db..6017d68b7e 100644 --- a/spec/unit/resource/archive_file_spec.rb +++ b/spec/unit/resource/archive_file_spec.rb @@ -17,7 +17,19 @@ require "spec_helper" -describe Chef::Resource::ArchiveFile do +begin + require "ffi-libarchive" +rescue LoadError + module Archive + class Reader + def close; end + def each_entry; end + def extract(entry, flags = 0, destination: nil); end + end + end +end + +describe Chef::Resource::ArchiveFile, :not_supported_on_aix do let(:node) { Chef::Node.new } let(:events) { Chef::EventDispatch::Dispatcher.new } let(:run_context) { Chef::RunContext.new(node, {}, events) } -- cgit v1.2.1