Skip to content

Commit 42ad4f6

Browse files
committed
Rewrite fqdn_rand_string() 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 4255f5c commit 42ad4f6

File tree

3 files changed

+50
-54
lines changed

3 files changed

+50
-54
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Generates a random alphanumeric string. Combining the `$fqdn` fact and an
5+
# optional seed for repeatable randomness.
6+
#
7+
# Optionally, you can specify a character set for the function (defaults to alphanumeric).
8+
Puppet::Functions.create_function(:fqdn_rand_string) do
9+
# @param length The length of the resulting string.
10+
# @param charset The character set to use.
11+
# @param The seed for repeatable randomness.
12+
#
13+
# @return [String]
14+
#
15+
# @example Example Usage:
16+
# fqdn_rand_string(10)
17+
# fqdn_rand_string(10, 'ABCDEF!@$%^')
18+
# fqdn_rand_string(10, '', 'custom seed')
19+
dispatch :fqdn_rand_string do
20+
param 'Integer[1]', :length
21+
optional_param 'String', :charset
22+
optional_repeated_param 'Any', :seed
23+
end
24+
25+
def fqdn_rand_string(length, charset = '', *seed)
26+
charset = charset.chars.to_a
27+
28+
charset = (0..9).map { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty?
29+
30+
rand_string = ''
31+
length.times do |current|
32+
rand_string += charset[call_function('fqdn_rand', charset.size, (seed + [current + 1]).join(':'))]
33+
end
34+
35+
rand_string
36+
end
37+
end

lib/puppet/parser/functions/fqdn_rand_string.rb

-41
This file was deleted.

spec/functions/fqdn_rand_string_spec.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
let(:default_charset) { %r{\A[a-zA-Z0-9]{100}\z} }
77

88
it { is_expected.not_to eq(nil) }
9-
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
10-
it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
11-
it { is_expected.to run.with_params(1.5).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('-10').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
14-
it { is_expected.to run.with_params('string').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({}).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
17-
it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) }
18-
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) }
19-
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) }
9+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects at least 1 argument, got none}i) }
10+
it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\[1\] value, got Integer\[0, 0\]}) }
11+
it { is_expected.to run.with_params(1.5).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\ value, got Float}) }
12+
it { is_expected.to run.with_params(-10).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\[1\] value, got Integer\[-10, -10\]}) }
13+
it { is_expected.to run.with_params('-10').and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\ value, got String}) }
14+
it { is_expected.to run.with_params('string').and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\ value, got String}) }
15+
it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer value, got Array}) }
16+
it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer value, got Hash}) }
17+
it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Integer}) }
18+
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Array}) }
19+
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Hash}) }
20+
it { is_expected.to run.with_params('100').and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer value, got String}) }
21+
it { is_expected.to run.with_params(100, nil).and_raise_error(ArgumentError, %r{parameter 'charset' expects a String value, got Undef}) }
2022
it { is_expected.to run.with_params(100).and_return(default_charset) }
21-
it { is_expected.to run.with_params('100').and_return(default_charset) }
22-
it { is_expected.to run.with_params(100, nil).and_return(default_charset) }
2323
it { is_expected.to run.with_params(100, '').and_return(default_charset) }
2424
it { is_expected.to run.with_params(100, 'a').and_return(%r{\Aa{100}\z}) }
2525
it { is_expected.to run.with_params(100, 'ab').and_return(%r{\A[ab]{100}\z}) }

0 commit comments

Comments
 (0)