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
rodjek
merged 1 commit into
rodjek:master
from
bodepd:pre_condition_catalogs_function_testing
Aug 8, 2012
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -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) | |||
|
|
||
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 | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same logic applies here. |
|||
end | end | ||
end | end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:should be:
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.