Skip to content

Commit

Permalink
Add pre_resource_eval to POSIX provider
Browse files Browse the repository at this point in the history
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 puppetlabs#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.
  • Loading branch information
AriaXLi committed Jun 3, 2024
1 parent c260871 commit 2658757
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/puppet/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/puppet/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2658757

Please sign in to comment.