7c1038a3d222df06f6f8f4ecc296fc7e89f7acc7
[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, /wrong number of arguments/i) }
6   it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
7   it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /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 "should rotate 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 "should rotate 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 "should rotate 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 "should accept objects which extend String" do
44     result = fqdn_rotate(AlsoString.new('asdf'))
45     expect(result).to eq('dfas')
46   end
47
48   it "should use the Puppet::Util.deterministic_rand function" do
49     if Puppet::Util.respond_to?(:deterministic_rand)
50       Puppet::Util.expects(:deterministic_rand).with(44489829212339698569024999901561968770,4)
51       fqdn_rotate("asdf")
52     else
53       skip 'Puppet::Util#deterministic_rand not available'
54     end
55   end
56
57   it "should not leave the global seed in a deterministic state" do
58     fqdn_rotate("asdf")
59     rand1 = rand()
60     fqdn_rotate("asdf")
61     rand2 = rand()
62     expect(rand1).not_to eql(rand2)
63   end
64
65   def fqdn_rotate(value, args = {})
66     host = args[:host] || '127.0.0.1'
67     extra = args[:extra_identifier] || []
68
69     # workaround not being able to use let(:facts) because some tests need
70     # multiple different hostnames in one context
71     scope.stubs(:lookupvar).with("::fqdn").returns(host)
72
73     function_args = [value] + extra
74     scope.function_fqdn_rotate(function_args)
75   end
76 end