[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. puppet-0.24.5-rc3-1456-g2f0b1e5
James Turnbull
james at lovedthanlost.net
Tue Oct 27 17:04:58 UTC 2009
The following commit has been merged in the upstream branch:
commit cb90528c041b63c1c483ac8650d1d9c67a12d791
Author: Markus Roberts <Markus at reality.com>
Date: Tue Sep 15 13:18:22 2009 -0700
Merged fix for #2601
This patch rolls up the changeses discussed on the list & the ticket
The fqdn_rand now takes any number of additional arguments of any
type that has a string representation (typically integers or strings)
and concatenats them on to the salt.
The tests have been adjusted to reflect this.
Signed-off-by: Markus Roberts <Markus at reality.com>
diff --git a/lib/puppet/parser/functions/fqdn_rand.rb b/lib/puppet/parser/functions/fqdn_rand.rb
index 3741d2d..9297539 100644
--- a/lib/puppet/parser/functions/fqdn_rand.rb
+++ b/lib/puppet/parser/functions/fqdn_rand.rb
@@ -1,15 +1,9 @@
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|
+ sets the range. Additional (optional) arguments may be used to further
+ distinguish the seed.") do |args|
require 'md5'
- max = args[0]
- if args[1] then
- seed = args[1]
- else
- seed = 1
- end
- fqdn_seed = MD5.new(lookupvar('fqdn')).to_s.hex
- srand(seed+fqdn_seed)
+ max = args.shift
+ srand MD5.new([lookupvar('fqdn'),args].join(':')).to_s.hex
rand(max).to_s
end
diff --git a/spec/unit/parser/functions/fqdn_rand.rb b/spec/unit/parser/functions/fqdn_rand.rb
new file mode 100644
index 0000000..cde899e
--- /dev/null
+++ b/spec/unit/parser/functions/fqdn_rand.rb
@@ -0,0 +1,62 @@
+#! /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
+
+
+ (1..10).each { |n|
+ it "should handle #{n} additional arguments" do
+ @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+ lambda { @scope.function_fqdn_rand([3,1,2,3,4,5,6,7,8,9,10][0..n]) }.should_not raise_error(Puppet::ParseError)
+ end
+ it "should handle #{n} additional string arguments" do
+ @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+ lambda { @scope.function_fqdn_rand([3,%w{ 1 2 3 4 5 6 7 8 9 10}].flatten[0..n]) }.should_not 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 sequences of value for different hosts" do
+ @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+ val1 = @scope.function_fqdn_rand([10000000,4])
+ @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.2")
+ val2 = @scope.function_fqdn_rand([10000000,4])
+ val1.should_not eql(val2)
+ end
+
+ it "should return different values for the same hosts with different seeds" do
+ @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+ val1 = @scope.function_fqdn_rand([10000000,4])
+ @scope.expects(:lookupvar).with("fqdn").returns("127.0.0.1")
+ val2 = @scope.function_fqdn_rand([10000000,42])
+ val1.should_not eql(val2)
+ end
+end
--
Puppet packaging for Debian
More information about the Pkg-puppet-devel
mailing list