+1 On Sep 15, 2009, at 1:22 PM, Markus Roberts wrote:
> > 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 <[email protected]> > --- > lib/puppet/parser/functions/fqdn_rand.rb | 14 ++----- > spec/unit/parser/functions/fqdn_rand.rb | 62 +++++++++++++++++++++ > +++++++++ > 2 files changed, 66 insertions(+), 10 deletions(-) > create mode 100644 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..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 > -- > 1.6.4 > > > > -- Discovery consists of seeing what everybody has seen and thinking what nobody has thought. -- Albert Szent-Gyorgyi --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
