Signed-off-by: Steven Jenkins <[email protected]>
---
 lib/puppet/parser/functions/fqdn_rand.rb |    3 +-
 spec/unit/parser/functions/fqdn_rand.rb  |   51 ++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletions(-)
 create mode 100755 spec/unit/parser/functions/fqdn_rand.rb

diff --git a/lib/puppet/parser/functions/fqdn_rand.rb 
b/lib/puppet/parser/functions/fqdn_rand.rb
index 3741d2d..fa63678 100644
--- a/lib/puppet/parser/functions/fqdn_rand.rb
+++ b/lib/puppet/parser/functions/fqdn_rand.rb
@@ -2,10 +2,11 @@ Puppet::Parser::Functions::newfunction(:fqdn_rand, :type => 
:rvalue, :doc =>
     "Generates random numbers based on the node's fqdn. The first argument
     sets the range.  The second argument specifies a number to add to the
     seed and is optional.") do |args|
+        raise Puppet::ParseError, "Must have 0, 1, or 2 arguments" unless 
args.length < 3
         require 'md5'
         max = args[0]
         if args[1] then
-             seed = args[1]
+             seed = args[1].to_i
         else
              seed = 1
         end
diff --git a/spec/unit/parser/functions/fqdn_rand.rb 
b/spec/unit/parser/functions/fqdn_rand.rb
new file mode 100755
index 0000000..8124110
--- /dev/null
+++ b/spec/unit/parser/functions/fqdn_rand.rb
@@ -0,0 +1,51 @@
+#! /usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe "the fqdn_rand function" do
+
+    before :each do
+        @scope = Puppet::Parser::Scope.new()
+    end
+
+    it "should exist" do
+        Puppet::Parser::Functions.function("fqdn_rand").should == 
"function_fqdn_rand"
+    end
+
+    it "should handle 0 arguments" do
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+        lambda { @scope.function_fqdn_rand([]) }.should_not 
raise_error(Puppet::ParseError)
+    end
+
+    it "should handle 1 argument" do
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+        lambda { @scope.function_fqdn_rand([3]) }.should_not 
raise_error(Puppet::ParseError)
+    end
+
+    it "should handle 2 arguments" do
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+        lambda { @scope.function_fqdn_rand([3, 4]) }.should_not 
raise_error(Puppet::ParseError)
+    end
+
+    it "should raise a ParseError if there is more than 2 arguments" do
+        lambda { @scope.function_fqdn_rand([3, 4, 5]) }.should 
raise_error(Puppet::ParseError)
+    end
+
+    it "should return a value less than max" do
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+        @scope.function_fqdn_rand([3]).should satisfy {|n| n.to_i < 3 }
+    end
+    
+    it "should return the same values on subsequent invocations for the same 
host" do
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1").twice
+        @scope.function_fqdn_rand([3,4]).should 
eql(@scope.function_fqdn_rand([3, 4]))
+    end
+
+    it "should return different values for different hosts" do
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+        val1 = @scope.function_fqdn_rand([3,4])
+        @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.2")
+        val2 = @scope.function_fqdn_rand([3,4])
+        val1.should_not eql(val2)
+    end
+end
-- 
1.6.1.3


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to