Skip to content

Commit 0cfa5d9

Browse files
authored
Merge branch 'main' into pdksync_CAT-1366-fix_issue_url
2 parents 7976477 + 139f6e9 commit 0cfa5d9

File tree

8 files changed

+318
-231
lines changed

8 files changed

+318
-231
lines changed

REFERENCE.md

+230-138
Large diffs are not rendered by default.

lib/puppet/functions/fqdn_rotate.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::fqdn_rotate`](#stdlibfqdn_rotate) instead.
6+
Puppet::Functions.create_function(:fqdn_rotate) do
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
9+
end
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'fqdn_rotate', 'This function is deprecated, please use stdlib::fqdn_rotate instead.', false)
12+
call_function('stdlib::fqdn_rotate', *args)
13+
end
14+
end
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
# @summary Rotates an array or string a random number of times, combining the `fqdn` fact and an optional seed for repeatable randomness.
4+
Puppet::Functions.create_function(:'stdlib::fqdn_rotate') do
5+
# @param input
6+
# The String you want rotated a random number of times
7+
# @param seeds
8+
# One of more values to use as a custom seed. These will be combined with the host's FQDN
9+
#
10+
# @return [String] Returns the rotated String
11+
#
12+
# @example Rotating a String
13+
# stdlib::fqdn_rotate('abcd')
14+
# @example Using a custom seed
15+
# stdlib::fqdn_rotate('abcd', 'custom seed')
16+
dispatch :fqdn_rotate_string do
17+
param 'String', :input
18+
optional_repeated_param 'Variant[Integer,String]', :seeds
19+
return_type 'String'
20+
end
21+
22+
# @param input
23+
# The Array you want rotated a random number of times
24+
# @param seeds
25+
# One of more values to use as a custom seed. These will be combined with the host's FQDN
26+
#
27+
# @return [String] Returns the rotated Array
28+
#
29+
# @example Rotating an Array
30+
# stdlib::fqdn_rotate(['a', 'b', 'c', 'd'])
31+
# @example Using custom seeds
32+
# stdlib::fqdn_rotate([1, 2, 3], 'custom', 'seed', 1)
33+
dispatch :fqdn_rotate_array do
34+
param 'Array', :input
35+
optional_repeated_param 'Variant[Integer,String]', :seeds
36+
return_type 'Array'
37+
end
38+
39+
def fqdn_rotate_array(input, *seeds)
40+
# Check whether it makes sense to rotate ...
41+
return input if input.size <= 1
42+
43+
result = input.clone
44+
45+
require 'digest/md5'
46+
seed = Digest::MD5.hexdigest([fqdn_fact, seeds].join(':')).hex
47+
48+
offset = Puppet::Util.deterministic_rand(seed, result.size).to_i
49+
50+
offset.times do
51+
result.push result.shift
52+
end
53+
54+
result
55+
end
56+
57+
def fqdn_rotate_string(input, *seeds)
58+
fqdn_rotate_array(input.chars, seeds).join
59+
end
60+
61+
private
62+
63+
def fqdn_fact
64+
closure_scope['facts']['networking']['fqdn']
65+
end
66+
end

lib/puppet/parser/functions/deprecation.rb

-25
This file was deleted.

lib/puppet/parser/functions/fqdn_rotate.rb

-59
This file was deleted.

spec/functions/fqdn_rotate_spec.rb

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

55
describe 'fqdn_rotate' do
66
it { is_expected.not_to be_nil }
7-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
8-
it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) }
9-
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) }
7+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects at least 1 argument, got none}) }
8+
it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, %r{parameter 'input' expects a value of type String or Array, got Integer}) }
9+
it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{parameter 'input' expects a value of type String or Array, got Hash}) }
1010
it { is_expected.to run.with_params('').and_return('') }
1111
it { is_expected.to run.with_params('a').and_return('a') }
1212
it { is_expected.to run.with_params('ã').and_return('ã') }
@@ -48,8 +48,6 @@
4848
end
4949

5050
it 'uses the Puppet::Util.deterministic_rand function' do
51-
skip 'Puppet::Util#deterministic_rand not available' unless Puppet::Util.respond_to?(:deterministic_rand)
52-
5351
expect(Puppet::Util).to receive(:deterministic_rand).with(44_489_829_212_339_698_569_024_999_901_561_968_770, 4)
5452
fqdn_rotate('asdf')
5553
end
@@ -68,9 +66,9 @@ def fqdn_rotate(value, args = {})
6866

6967
# workaround not being able to use let(:facts) because some tests need
7068
# multiple different hostnames in one context
71-
allow(scope).to receive(:lookupvar).with('facts').and_return(host)
69+
allow(subject.func.closure_scope).to receive(:[]).with('facts').and_return({ 'networking' => { 'fqdn' => host } })
7270

7371
function_args = [value] + extra
74-
scope.function_fqdn_rotate(function_args)
72+
scope.call_function('fqdn_rotate', function_args)
7573
end
7674
end

spec/unit/puppet/provider/file_line/ruby_spec_alter.rb renamed to spec/unit/puppet/provider/file_line/ruby_alter_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
end
165165

166166
context 'when match and after set' do
167-
shared_context 'when resource_create' do
167+
shared_context 'resource_create' do
168168
let(:match) { '^foo2$' }
169169
let(:after) { '^foo1$' }
170170
let(:resource) do
@@ -251,7 +251,7 @@
251251

252252
it 'appends the specified line to the file' do
253253
provider.create
254-
expect(File.read(tmpfile)).to eq(content << resource[:line] << "\n")
254+
expect(File.read(tmpfile)).to eq("#{content}#{resource[:line]}\n")
255255
end
256256
end
257257
end
@@ -385,4 +385,5 @@
385385
expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
386386
end
387387
end
388+
# rubocop:enable RSpec/InstanceVariable
388389
end

0 commit comments

Comments
 (0)