Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #36 from bodepd/pre_condition_catalogs_function_te…

…sting

Add support for pre_condition with functions
  • Loading branch information...
commit e3495af21bafff179eee6db170b5610f4c871756 2 parents eed11c7 + 41a2fc8
@rodjek authored
Showing with 28 additions and 1 deletion.
  1. +28 −1 lib/rspec-puppet/example/function_example_group.rb
View
29 lib/rspec-puppet/example/function_example_group.rb
@@ -9,9 +9,36 @@ def subject
Puppet[:libdir] = Dir["#{Puppet[:modulepath]}/*/lib"].entries.join(File::PATH_SEPARATOR)
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
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
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.