3 describe 'fqdn_rotate' do
4 it { is_expected.not_to eq(nil) }
5 it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
6 it { is_expected.to run.with_params(0).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(Puppet::ParseError, %r{Requires either array or string to work with}) }
8 it { is_expected.to run.with_params('').and_return('') }
9 it { is_expected.to run.with_params('a').and_return('a') }
10 it { is_expected.to run.with_params('ã').and_return('ã') }
12 it { is_expected.to run.with_params([]).and_return([]) }
13 it { is_expected.to run.with_params(['a']).and_return(['a']) }
15 it 'rotates a string and the result should be the same size' do
16 expect(fqdn_rotate('asdf').size).to eq(4)
19 it 'rotates a string to give the same results for one host' do
20 val1 = fqdn_rotate('abcdefg', :host => 'one')
21 val2 = fqdn_rotate('abcdefg', :host => 'one')
22 expect(val1).to eq(val2)
25 it 'allows extra arguments to control the random rotation on a single host' do
26 val1 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'different', 'host'])
27 val2 = fqdn_rotate('abcdefg', :extra_identifier => [2, 'different', 'host'])
28 expect(val1).not_to eq(val2)
31 it 'considers the same host and same extra arguments to have the same random rotation' do
32 val1 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'same', 'host'])
33 val2 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'same', 'host'])
34 expect(val1).to eq(val2)
37 it 'rotates a string to give different values on different hosts' do
38 val1 = fqdn_rotate('abcdefg', :host => 'one')
39 val2 = fqdn_rotate('abcdefg', :host => 'two')
40 expect(val1).not_to eq(val2)
43 it 'accepts objects which extend String' do
44 result = fqdn_rotate(AlsoString.new('asdf'))
45 expect(result).to eq('dfas')
48 it 'uses the Puppet::Util.deterministic_rand function' do
49 skip 'Puppet::Util#deterministic_rand not available' unless Puppet::Util.respond_to?(:deterministic_rand)
51 expect(Puppet::Util).to receive(:deterministic_rand).with(44_489_829_212_339_698_569_024_999_901_561_968_770, 4)
55 it 'does not leave the global seed in a deterministic state' do
60 expect(rand1).not_to eql(rand2)
63 def fqdn_rotate(value, args = {})
64 host = args[:host] || '127.0.0.1'
65 extra = args[:extra_identifier] || []
67 # workaround not being able to use let(:facts) because some tests need
68 # multiple different hostnames in one context
69 allow(scope).to receive(:lookupvar).with('::fqdn').and_return(host)
71 function_args = [value] + extra
72 scope.function_fqdn_rotate(function_args)