Merge pull request #406 from elyscape/fix/fqdn_rotate_pollutes_global_seed
(MODULES-1738) Don't modify the global seed in fqdn_rotate()
This commit is contained in:
commit
ad5727266a
2 changed files with 31 additions and 2 deletions
|
@ -31,8 +31,20 @@ Rotates an array a random number of times based on a nodes fqdn.
|
|||
|
||||
elements = result.size
|
||||
|
||||
srand(Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex)
|
||||
rand(elements).times {
|
||||
seed = Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex
|
||||
# deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
|
||||
if Puppet::Util.respond_to?(:deterministic_rand)
|
||||
offset = Puppet::Util.deterministic_rand(seed, elements).to_i
|
||||
else
|
||||
if defined?(Random) == 'constant' && Random.class == Class
|
||||
offset = Random.new(seed).rand(elements)
|
||||
else
|
||||
srand(seed)
|
||||
offset = rand(elements)
|
||||
srand()
|
||||
end
|
||||
end
|
||||
offset.times {
|
||||
result.push result.shift
|
||||
}
|
||||
|
||||
|
|
|
@ -40,4 +40,21 @@ describe "the fqdn_rotate function" do
|
|||
result = scope.function_fqdn_rotate([value])
|
||||
result.size.should(eq(4))
|
||||
end
|
||||
|
||||
it "should use the Puppet::Util.deterministic_rand function if available" do
|
||||
scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
|
||||
if Puppet::Util.respond_to?(:deterministic_rand)
|
||||
Puppet::Util.expects(:deterministic_rand).with(113646079810780526294648115052177588845,4)
|
||||
end
|
||||
scope.function_fqdn_rotate(["asdf"])
|
||||
end
|
||||
|
||||
it "should not leave the global seed in a deterministic state" do
|
||||
scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
|
||||
scope.function_fqdn_rotate(["asdf"])
|
||||
rand1 = rand()
|
||||
scope.function_fqdn_rotate(["asdf"])
|
||||
rand2 = rand()
|
||||
expect(rand1).not_to eql(rand2)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue