diff --git a/lib/puppet/parser/functions/digest.rb b/lib/puppet/parser/functions/digest.rb new file mode 100644 index 000000000..8cd75c3fb --- /dev/null +++ b/lib/puppet/parser/functions/digest.rb @@ -0,0 +1,5 @@ +require 'puppet/util/checksums' +Puppet::Parser::Functions::newfunction(:digest, :type => :rvalue, :arity => 1, :doc => "Returns a hash value from a provided string using the digest_algorithm setting from the Puppet config file.") do |args| + algo = Puppet[:digest_algorithm] + Puppet::Util::Checksums.method(algo.intern).call args[0] +end diff --git a/spec/unit/parser/functions/digest_spec.rb b/spec/unit/parser/functions/digest_spec.rb new file mode 100755 index 000000000..e3c0762d4 --- /dev/null +++ b/spec/unit/parser/functions/digest_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the digest function", :uses_checksums => true do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + n = Puppet::Node.new('unnamed') + c = Puppet::Parser::Compiler.new(n) + @scope = Puppet::Parser::Scope.new(c) + end + + it "should exist" do + Puppet::Parser::Functions.function("digest").should == "function_digest" + end + + with_digest_algorithms do + it "should use the proper digest function" do + result = @scope.function_digest([plaintext]) + result.should(eql( checksum )) + end + + it "should only accept one parameter" do + expect do + @scope.function_digest(['foo', 'bar']) + end.to raise_error(ArgumentError) + end + end +end