Skip to content

Commit 93600e4

Browse files
committed
Merge pull request #554 from kjetilho/ticket/2886-seeded_rand
(#2886) seeded_rand: new function
2 parents 7a745de + 8aecd63 commit 93600e4

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

README.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,12 @@ Reverses the order of a string or array. *Type*: rvalue.
697697

698698
Strips spaces to the right of the string. *Type*: rvalue.
699699

700+
#### `seeded_rand`
701+
702+
Takes an integer max value and a string seed value and returns a
703+
repeatable random integer smaller than max. Like `fqdn_rand`, but
704+
this does not add node specific data to the seed. *Type*: rvalue.
705+
700706
#### `shuffle`
701707

702708
Randomizes the order of a string or array elements. *Type*: rvalue.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Puppet::Parser::Functions::newfunction(
2+
:seeded_rand,
3+
:arity => 2,
4+
:type => :rvalue,
5+
:doc => <<-EOS
6+
Usage: `seeded_rand(MAX, SEED)`. MAX must be a positive integer; SEED is any string.
7+
8+
Generates a random whole number greater than or equal to 0 and less
9+
than MAX, using the value of SEED for repeatable randomness. If SEED
10+
starts with "$fqdn:", this is behaves the same as `fqdn_rand`.
11+
12+
EOS
13+
) do |args|
14+
require 'digest/md5'
15+
16+
raise(ArgumentError, "seeded_rand(): first argument must be a positive integer") unless function_is_integer([args[0]]) and args[0].to_i > 0
17+
raise(ArgumentError, "seeded_rand(): second argument must be a string") unless args[1].is_a? String
18+
19+
max = args[0].to_i
20+
seed = Digest::MD5.hexdigest(args[1]).hex
21+
Puppet::Util.deterministic_rand(seed,max)
22+
end

spec/functions/seeded_rand_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'spec_helper'
2+
3+
describe 'seeded_rand' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
6+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, /wrong number of arguments/i) }
7+
it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
8+
it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
9+
it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
10+
it { is_expected.to run.with_params("-10", '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
11+
it { is_expected.to run.with_params("string", '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
12+
it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
13+
it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
14+
it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, /second argument must be a string/) }
15+
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, /second argument must be a string/) }
16+
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, /second argument must be a string/) }
17+
18+
it "provides a random number strictly less than the given max" do
19+
expect(seeded_rand(3, 'seed')).to satisfy {|n| n.to_i < 3 }
20+
end
21+
22+
it "provides a random number greater or equal to zero" do
23+
expect(seeded_rand(3, 'seed')).to satisfy {|n| n.to_i >= 0 }
24+
end
25+
26+
it "provides the same 'random' value on subsequent calls for the same host" do
27+
expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed'))
28+
end
29+
30+
it "allows seed to control the random value on a single host" do
31+
first_random = seeded_rand(1000, 'seed1')
32+
second_different_random = seeded_rand(1000, 'seed2')
33+
34+
expect(first_random).not_to eql(second_different_random)
35+
end
36+
37+
it "should not return different values for different hosts" do
38+
val1 = seeded_rand(1000, 'foo', :host => "first.host.com")
39+
val2 = seeded_rand(1000, 'foo', :host => "second.host.com")
40+
41+
expect(val1).to eql(val2)
42+
end
43+
44+
def seeded_rand(max, seed, args = {})
45+
host = args[:host] || '127.0.0.1'
46+
47+
# workaround not being able to use let(:facts) because some tests need
48+
# multiple different hostnames in one context
49+
scope.stubs(:lookupvar).with("::fqdn", {}).returns(host)
50+
51+
scope.function_seeded_rand([max, seed])
52+
end
53+
end

0 commit comments

Comments
 (0)