Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add information on mocking an external function #51

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/documentation/functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,28 @@ describe 'ensure_resource' do
end
end
{% endhighlight %}

## Mocking a function for use in all tests

You may not need to test a function itself, but a calling class may require the function
to return a value. You can mock the function once and make it available to all test.
Modify *spec/spec_helper_local.rb* and use `Puppet::Parser::Functions.newfunction()`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rspec-puppet really doesn't mention spec_helper_local.rb anywhere. That's really a PDK-thing and I'd avoid it here.

to add the function inside of an `RSpec.configure` block. For example:

{% highlight ruby %}
RSpec.configure do | c|
c.before :each do
# The vault_lookup function takes a single argument and returns a hash with three keys
Puppet::Parser::Functions.newfunction(:vault_lookup, type: :rvalue) do |_args|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the legacy function API. The current standard should be used everywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some discussion of that in slack and how it would apply to tests, there's not a lot of clarity there and the "obvious" code doesn't seem to work. Very happy to move to that if we can determine the correct syntax to use.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking at the function testing code and Puppet.override looks promising. It is a private API of Puppet, but looks promising.

def find_function(function_name = self.class.top_level_description)
with_vardir do
env = adapter.current_environment
if Puppet.version.to_f >= 4.0
context_overrides = compiler.context_overrides
func = nil
loaders = Puppet.lookup(:loaders)
Puppet.override(context_overrides, 'rspec-test scope') do
func = V4FunctionWrapper.new(function_name,
loaders.private_environment_loader.load(:function, function_name), context_overrides)
@scope = context_overrides[:global_scope]
end
return func if func.func
end
if Puppet::Parser::Functions.function(function_name)
V3FunctionWrapper.new(function_name, scope.method("function_#{function_name}".intern))
end
end
end

Another problem you may have with the modern API is that it prefers namespaced functions, or there's a difference in priority.

{
domain: 'test',
username: 'test',
password: 'test'
}
end
end
end
{% endhighlight %}

You may add multiple functions and other configuration elements inside the `c.before :each do`
block and all will be available inside every test.