Skip to content

Commit ad57272

Browse files
committed
Merge pull request #406 from elyscape/fix/fqdn_rotate_pollutes_global_seed
(MODULES-1738) Don't modify the global seed in fqdn_rotate()
2 parents afc83ea + 84f866f commit ad57272

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/puppet/parser/functions/fqdn_rotate.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff 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

spec/functions/fqdn_rotate_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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
4360
end

0 commit comments

Comments
 (0)