Skip to content

Commit 3dcf66b

Browse files
committed
(PUP-4698) Change fqdn_rand's return type to Integer
Before this, fqdn_rand always returned a String. That is not the best choice for a numeric value now that the evaluator is aware of different data types as comparissons will typically be wrong (since '2' > '19' when compared leicographically). This fix adds a utility method that returns an integer version while the old utility method producing a string is retained for those that use Puppet as an API. The function now uses the integer version of this utility method. A test is added that asserts that an Integer is produced. At some point it would be beneficial to reimplement the function using the 4.x function API as it will simply raise a generic error if given input of the wrong type. It will also magically change a floating point input value to integer.
1 parent 4de4750 commit 3dcf66b

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

lib/puppet/parser/functions/fqdn_rand.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Usage: `fqdn_rand(MAX, [SEED])`. MAX is required and must be a positive
55
integer; SEED is optional and may be any number or string.
66
7-
Generates a random whole number greater than or equal to 0 and less than MAX,
7+
Generates a random Integer number greater than or equal to 0 and less than MAX,
88
combining the `$fqdn` fact and the value of SEED for repeatable randomness.
99
(That is, each node will get a different random number from this function, but
1010
a given node's result will be the same every time unless its hostname changes.)
@@ -17,5 +17,5 @@
1717
`fqdn_rand(30, 'expensive job 2')` will produce totally different numbers.)") do |args|
1818
max = args.shift.to_i
1919
seed = Digest::MD5.hexdigest([self['::fqdn'],args].join(':')).hex
20-
Puppet::Util.deterministic_rand(seed,max)
20+
Puppet::Util.deterministic_rand_int(seed,max)
2121
end

lib/puppet/util.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,21 @@ def exit_on_fail(message, code = 1)
454454
module_function :exit_on_fail
455455

456456
def deterministic_rand(seed,max)
457+
deterministic_rand_int(seed, max).to_s
458+
end
459+
module_function :deterministic_rand
460+
461+
def deterministic_rand_int(seed,max)
457462
if defined?(Random) == 'constant' && Random.class == Class
458-
Random.new(seed).rand(max).to_s
463+
Random.new(seed).rand(max)
459464
else
460465
srand(seed)
461-
result = rand(max).to_s
466+
result = rand(max)
462467
srand()
463468
result
464469
end
465470
end
466-
module_function :deterministic_rand
471+
module_function :deterministic_rand_int
467472
end
468473
end
469474

spec/unit/parser/functions/fqdn_rand_spec.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
describe "the fqdn_rand function" do
66
include PuppetSpec::Scope
7-
7+
8+
it "returns an integer" do
9+
expect(fqdn_rand(3)).to satisfy {|n| n.is_a?(Integer) }
10+
end
11+
812
it "provides a random number strictly less than the given max" do
913
expect(fqdn_rand(3)).to satisfy {|n| n.to_i < 3 }
1014
end

0 commit comments

Comments
 (0)