diff options
author | Colleen Murphy <cmurphy@users.noreply.github.com> | 2015-02-12 15:45:23 -0800 |
---|---|---|
committer | Colleen Murphy <cmurphy@users.noreply.github.com> | 2015-02-12 15:45:23 -0800 |
commit | ad5727266a5c243cebf8007b9b00ffc80674b4fc (patch) | |
tree | 7e0792a13888ccd15bee9d2da13bebfd79071dd3 | |
parent | afc83ea564520378ac507251ddfea46f8d571829 (diff) | |
parent | 84f866ffafbb27a6aa6a1eea92c393a63829e242 (diff) |
Merge pull request #406 from elyscape/fix/fqdn_rotate_pollutes_global_seed
(MODULES-1738) Don't modify the global seed in fqdn_rotate()
-rw-r--r-- | lib/puppet/parser/functions/fqdn_rotate.rb | 16 | ||||
-rwxr-xr-x | spec/functions/fqdn_rotate_spec.rb | 17 |
2 files changed, 31 insertions, 2 deletions
diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index 7f4d37d..cf22d36 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -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 } diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb index 40057d4..673a8a3 100755 --- a/spec/functions/fqdn_rotate_spec.rb +++ b/spec/functions/fqdn_rotate_spec.rb @@ -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 |