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 support for pre_condition with functions #36

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/rspec-puppet/example/function_example_group.rb
Expand Up @@ -9,9 +9,36 @@ def subject
Puppet[:libdir] = Dir["#{Puppet[:modulepath]}/*/lib"].entries.join(File::PATH_SEPARATOR) Puppet[:libdir] = Dir["#{Puppet[:modulepath]}/*/lib"].entries.join(File::PATH_SEPARATOR)
Puppet::Parser::Functions.autoloader.loadall Puppet::Parser::Functions.autoloader.loadall


scope = Puppet::Parser::Scope.new # if we specify a pre_condition, we should ensure that we compile that code
# into a catalog that is accessible from the scope where the function is called
if self.respond_to? :pre_condition
Puppet[:code] = pre_condition
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
facts_val = {
'hostname' => nodename.split('.').first,
'fqdn' => nodename,
'domain' => nodename.split('.').last,
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
# we need to get a compiler, b/c we can attach that to a scope
@compiler = build_compiler(nodename, facts_val)
else
@compiler = nil
end

scope = Puppet::Parser::Scope.new(:compiler => @compiler)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Today, rspec-puppet should not reach directly into Puppet to obtain a scope instance. Instead, it should use the puppetlabs_spec_helper to obtain a scope instance in Puppet version-agnostic manner. As a concrete example, this line:

scope = Puppet::Parser::Scope.new(:compiler => @compiler)

should be:

scope = PuppetlabsSpec::PuppetInternals.scope

This is important because if we change the behavior of the scope object again we will also change PuppetlabsSpec::PuppetInternals.scope to work correctly. rspec-puppet will then "just work" instead of breaking again.

It's very likely that the method signature of the scope object will change as we inject more dependencies through the initializer rather than relying on global process state.



scope.method "function_#{function_name}".to_sym scope.method "function_#{function_name}".to_sym
end end

def compiler
@compiler
end
# get a compiler with an attached compiled catalog
def build_compiler(node_name, fact_values)
compiler = Puppet::Parser::Compiler.new(Puppet::Node.new(node_name, :parameters => fact_values))
compiler.compile
compiler
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same logic applies here. rspec-puppet should not reach deep into Puppet to build a compiler instance. Instead, it should use puppetlabs_spec_helper to obtain a compiler instance in a Puppet version agnostic way.

end end
end end