Skip to content

Commit 2050371

Browse files
committed
Rewrite seeded_rand() as a Puppet 4.x function
The 3.x function rely on is_integer() which is deprecated. Rewrite it using the more modern puppet 4.x function to rely on data types for better parameters validation.
1 parent 607b684 commit 2050371

File tree

3 files changed

+33
-42
lines changed

3 files changed

+33
-42
lines changed

lib/puppet/functions/seeded_rand.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Generates a random whole number greater than or equal to 0 and less than max, using the value of seed for repeatable randomness.
5+
Puppet::Functions.create_function(:seeded_rand) do
6+
# @param max The maximum value.
7+
# @param seed The seed used for repeatable randomness.
8+
#
9+
# @return [Integer]
10+
# A random number greater than or equal to 0 and less than max
11+
dispatch :seeded_rand do
12+
param 'Integer[1]', :max
13+
param 'String', :seed
14+
end
15+
16+
def seeded_rand(max, seed)
17+
require 'digest/md5'
18+
19+
seed = Digest::MD5.hexdigest(seed).hex
20+
Puppet::Util.deterministic_rand_int(seed, max)
21+
end
22+
end

lib/puppet/parser/functions/seeded_rand.rb

-30
This file was deleted.

spec/functions/seeded_rand_spec.rb

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44

55
describe 'seeded_rand' do
66
it { is_expected.not_to eq(nil) }
7-
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
8-
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
9-
it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
10-
it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
11-
it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
12-
it { is_expected.to run.with_params('-10', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
13-
it { is_expected.to run.with_params('string', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
14-
it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
15-
it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
16-
it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{second argument must be a string}) }
17-
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be a string}) }
18-
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be a string}) }
7+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'seeded_rand' expects 2 arguments, got none}i) }
8+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{'seeded_rand' expects 2 arguments, got 1}i) }
9+
it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer\[1\] value, got Integer\[0, 0\]}) }
10+
it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer value, got Float}) }
11+
it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer\[1\] value, got Integer\[-10, -10\]}) }
12+
it { is_expected.to run.with_params('string', '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer value, got String}) }
13+
it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer value, got Array}) }
14+
it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer value, got Hash}) }
15+
it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{parameter 'seed' expects a String value, got Integer}) }
16+
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{parameter 'seed' expects a String value, got Array}) }
17+
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{parameter 'seed' expects a String value, got Hash}) }
1918

2019
context 'produce predictible and reproducible results' do
2120
it { is_expected.to run.with_params(20, 'foo').and_return(1) }

0 commit comments

Comments
 (0)