From 2658757eacb279e7e08e32f91a933a7b607ceb8e Mon Sep 17 00:00:00 2001 From: Aria Li Date: Mon, 3 Jun 2024 13:14:24 -0700 Subject: [PATCH] Add pre_resource_eval to POSIX provider This commit adds API documentation, updates a call to, and adds a spec test for the pre_resource_eval class function in the POSIX file provider (which was added in #9349). When called, pre_resource_eval will create a class variable, selinux_handle, which can be used when handling data for SELinux. Since the handle is a class variable, we can avoid running into performance issues since the handle can be re-used instead of needing to make a new one each time. Additionally, since the old method wasn't completely removed & replaced (and instead deprecated), less changes to old spec tests are needed as their calls to the deprecated method aren't impacted. --- lib/puppet/provider.rb | 8 ++++++++ lib/puppet/transaction.rb | 1 + spec/unit/transaction_spec.rb | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/lib/puppet/provider.rb b/lib/puppet/provider.rb index a0996576f51..6ebf5b137a1 100644 --- a/lib/puppet/provider.rb +++ b/lib/puppet/provider.rb @@ -592,6 +592,14 @@ def <=>(other) # @return [void] # @api public + # @comment Document pre_resource_eval here as it does not exist anywhere else + # (called from transaction if implemented) @!method self.pre_resource_eval() + # @abstract A subclass may implement this - it is not implemented in the + # Provider class This method may be implemented by a provider in order to + # perform any setup actions needed, such as creating a handle. It will be + # called at the beginning of the transaction if the provider has implemented + # the method @return [void] + # @comment Document post_resource_eval here as it does not exist anywhere else (called from transaction if implemented) # @!method self.post_resource_eval() # @since 3.4.0 diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index 27d5152997f..9947fe78867 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -110,6 +110,7 @@ def evaluate(&block) post_evalable_providers = Set.new pre_process = lambda do |resource| prov_class = resource.provider.class + prov_class.pre_resource_eval if prov_class.respond_to?(:pre_resource_eval) post_evalable_providers << prov_class if prov_class.respond_to?(:post_resource_eval) prefetch_if_necessary(resource) diff --git a/spec/unit/transaction_spec.rb b/spec/unit/transaction_spec.rb index c7c278bf1c7..bd2473045b4 100644 --- a/spec/unit/transaction_spec.rb +++ b/spec/unit/transaction_spec.rb @@ -420,6 +420,19 @@ def generate transaction.evaluate end + it "should call ::pre_resource_eval on provider classes that support it" do + skip if Puppet::Util::Platform.windows? + selinux = double('selinux', is_selinux_enabled: true) + stub_const('Selinux', selinux) + + resource = Puppet::Type.type(:file).new(:path => make_absolute("/tmp/foo")) + transaction = transaction_with_resource(resource) + + expect(resource.provider.class).to receive(:pre_resource_eval) + + transaction.evaluate + end + it "should abort the transaction on failure" do expect(resource).to receive(:pre_run_check).and_raise(Puppet::Error, spec_exception)