File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
lib/puppet/parser/functions Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -31,8 +31,20 @@ module Puppet::Parser::Functions
3131
3232 elements = result . size
3333
34- srand ( Digest ::MD5 . hexdigest ( [ lookupvar ( '::fqdn' ) , arguments ] . join ( ':' ) ) . hex )
35- rand ( elements ) . times {
34+ seed = Digest ::MD5 . hexdigest ( [ lookupvar ( '::fqdn' ) , arguments ] . join ( ':' ) ) . hex
35+ # deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
36+ if Puppet ::Util . respond_to? ( :deterministic_rand )
37+ offset = Puppet ::Util . deterministic_rand ( seed , elements ) . to_i
38+ else
39+ if defined? ( Random ) == 'constant' && Random . class == Class
40+ offset = Random . new ( seed ) . rand ( elements )
41+ else
42+ srand ( seed )
43+ offset = rand ( elements )
44+ srand ( )
45+ end
46+ end
47+ offset . times {
3648 result . push result . shift
3749 }
3850
Original file line number Diff line number Diff line change @@ -40,4 +40,21 @@ class AlsoString < String
4040 result = scope . function_fqdn_rotate ( [ value ] )
4141 result . size . should ( eq ( 4 ) )
4242 end
43+
44+ it "should use the Puppet::Util.deterministic_rand function if available" do
45+ scope . expects ( :lookupvar ) . with ( "::fqdn" ) . returns ( "127.0.0.1" )
46+ if Puppet ::Util . respond_to? ( :deterministic_rand )
47+ Puppet ::Util . expects ( :deterministic_rand ) . with ( 113646079810780526294648115052177588845 , 4 )
48+ end
49+ scope . function_fqdn_rotate ( [ "asdf" ] )
50+ end
51+
52+ it "should not leave the global seed in a deterministic state" do
53+ scope . expects ( :lookupvar ) . with ( "::fqdn" ) . returns ( "127.0.0.1" ) . twice
54+ scope . function_fqdn_rotate ( [ "asdf" ] )
55+ rand1 = rand ( )
56+ scope . function_fqdn_rotate ( [ "asdf" ] )
57+ rand2 = rand ( )
58+ expect ( rand1 ) . not_to eql ( rand2 )
59+ end
4360end
You can’t perform that action at this time.
0 commit comments