Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / fqdn_rotate_spec.rb
1 require 'spec_helper'
2
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('ã') }
11
12   it { is_expected.to run.with_params([]).and_return([]) }
13   it { is_expected.to run.with_params(['a']).and_return(['a']) }
14
15   it 'rotates a string and the result should be the same size' do
16     expect(fqdn_rotate('asdf').size).to eq(4)
17   end
18
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)
23   end
24
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)
29   end
30
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)
35   end
36
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)
41   end
42
43   it 'accepts objects which extend String' do
44     result = fqdn_rotate(AlsoString.new('asdf'))
45     expect(result).to eq('dfas')
46   end
47
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)
50
51     expect(Puppet::Util).to receive(:deterministic_rand).with(44_489_829_212_339_698_569_024_999_901_561_968_770, 4)
52     fqdn_rotate('asdf')
53   end
54
55   it 'does not leave the global seed in a deterministic state' do
56     fqdn_rotate('asdf')
57     rand1 = rand
58     fqdn_rotate('asdf')
59     rand2 = rand
60     expect(rand1).not_to eql(rand2)
61   end
62
63   def fqdn_rotate(value, args = {})
64     host = args[:host] || '127.0.0.1'
65     extra = args[:extra_identifier] || []
66
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)
70
71     function_args = [value] + extra
72     scope.function_fqdn_rotate(function_args)
73   end
74 end