Skip to content

Commit a3553ca

Browse files
authored
Merge pull request #8581 from joshcooper/fqdn_rand_casecmp_10922
(PUP-10922) Add optional argument to downcase fqdn
2 parents 17f0fdc + a783242 commit a3553ca

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

lib/puppet/parser/functions/fqdn_rand.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
require 'digest/sha2'
33

44
Puppet::Parser::Functions::newfunction(:fqdn_rand, :arity => -2, :type => :rvalue, :doc =>
5-
"Usage: `fqdn_rand(MAX, [SEED])`. MAX is required and must be a positive
6-
integer; SEED is optional and may be any number or string.
5+
"Usage: `fqdn_rand(MAX, [SEED], [DOWNCASE])`. MAX is required and must be a positive
6+
integer; SEED is optional and may be any number or string; DOWNCASE is optional
7+
and should be a boolean true or false.
78
89
Generates a random Integer number greater than or equal to 0 and less than MAX,
910
combining the `$fqdn` fact and the value of SEED for repeatable randomness.
1011
(That is, each node will get a different random number from this function, but
11-
a given node's result will be the same every time unless its hostname changes.)
12+
a given node's result will be the same every time unless its hostname changes.) If
13+
DOWNCASE is true, then the `fqdn` fact will be downcased when computing the value
14+
so that the result is not sensitive to the case of the `fqdn` fact.
1215
1316
This function is usually used for spacing out runs of resource-intensive cron
1417
tasks that run on many nodes, which could cause a thundering herd or degrade
@@ -17,7 +20,12 @@
1720
node. (For example, `fqdn_rand(30)`, `fqdn_rand(30, 'expensive job 1')`, and
1821
`fqdn_rand(30, 'expensive job 2')` will produce totally different numbers.)") do |args|
1922
max = args.shift.to_i
20-
23+
initial_seed = args.shift
24+
downcase = !!args.shift
25+
26+
fqdn = self['::fqdn']
27+
fqdn = fqdn.downcase if downcase
28+
2129
# Puppet 5.4's fqdn_rand function produces a different value than earlier versions
2230
# for the same set of inputs.
2331
# This causes problems because the values are often written into service configuration files.
@@ -27,9 +35,9 @@
2735
# when running on a non-FIPS enabled platform and only using SHA256 on FIPS enabled
2836
# platforms.
2937
if Puppet::Util::Platform.fips_enabled?
30-
seed = Digest::SHA256.hexdigest([self['::fqdn'],max,args].join(':')).hex
38+
seed = Digest::SHA256.hexdigest([fqdn,max,initial_seed].join(':')).hex
3139
else
32-
seed = Digest::MD5.hexdigest([self['::fqdn'],max,args].join(':')).hex
40+
seed = Digest::MD5.hexdigest([fqdn,max,initial_seed].join(':')).hex
3341
end
3442

3543
Puppet::Util.deterministic_rand_int(seed,max)

spec/unit/parser/functions/fqdn_rand_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,26 @@
6161
expect(fqdn_rand(5000, :extra_identifier => ['expensive job 33'])).to eql(2389)
6262
end
6363

64+
it "returns the same value if only host differs by case" do
65+
val1 = fqdn_rand(1000000000, :host => "host.example.com", :extra_identifier => [nil, true])
66+
val2 = fqdn_rand(1000000000, :host => "HOST.example.com", :extra_identifier => [nil, true])
67+
68+
expect(val1).to eql(val2)
69+
end
70+
71+
it "returns the same value if only host differs by case and an initial seed is given" do
72+
val1 = fqdn_rand(1000000000, :host => "host.example.com", :extra_identifier => ['a seed', true])
73+
val2 = fqdn_rand(1000000000, :host => "HOST.example.com", :extra_identifier => ['a seed', true])
74+
75+
expect(val1).to eql(val2)
76+
end
77+
6478
def fqdn_rand(max, args = {})
6579
host = args[:host] || '127.0.0.1'
6680
extra = args[:extra_identifier] || []
6781

6882
scope = create_test_scope_for_node('localhost')
69-
allow(scope).to receive(:[]).with("::fqdn").and_return(host)
83+
scope.compiler.topscope['fqdn'] = host.freeze
7084

7185
scope.function_fqdn_rand([max] + extra)
7286
end

0 commit comments

Comments
 (0)