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 |
|
---|
11 | it { is_expected.to run.with_params([]).and_return([]) }
|
---|
12 | it { is_expected.to run.with_params(['a']).and_return(['a']) }
|
---|
13 |
|
---|
14 | it "should rotate a string and the result should be the same size" do
|
---|
15 | expect(fqdn_rotate("asdf").size).to eq(4)
|
---|
16 | end
|
---|
17 |
|
---|
18 | it "should rotate a string to give the same results for one host" do
|
---|
19 | val1 = fqdn_rotate("abcdefg", :host => 'one')
|
---|
20 | val2 = fqdn_rotate("abcdefg", :host => 'one')
|
---|
21 | expect(val1).to eq(val2)
|
---|
22 | end
|
---|
23 |
|
---|
24 | it "allows extra arguments to control the random rotation on a single host" do
|
---|
25 | val1 = fqdn_rotate("abcdefg", :extra_identifier => [1, "different", "host"])
|
---|
26 | val2 = fqdn_rotate("abcdefg", :extra_identifier => [2, "different", "host"])
|
---|
27 | expect(val1).not_to eq(val2)
|
---|
28 | end
|
---|
29 |
|
---|
30 | it "considers the same host and same extra arguments to have the same random rotation" do
|
---|
31 | val1 = fqdn_rotate("abcdefg", :extra_identifier => [1, "same", "host"])
|
---|
32 | val2 = fqdn_rotate("abcdefg", :extra_identifier => [1, "same", "host"])
|
---|
33 | expect(val1).to eq(val2)
|
---|
34 | end
|
---|
35 |
|
---|
36 | it "should rotate a string to give different values on different hosts" do
|
---|
37 | val1 = fqdn_rotate("abcdefg", :host => 'one')
|
---|
38 | val2 = fqdn_rotate("abcdefg", :host => 'two')
|
---|
39 | expect(val1).not_to eq(val2)
|
---|
40 | end
|
---|
41 |
|
---|
42 | it "should accept objects which extend String" do
|
---|
43 | result = fqdn_rotate(AlsoString.new('asdf'))
|
---|
44 | expect(result).to eq('dfas')
|
---|
45 | end
|
---|
46 |
|
---|
47 | it "should use the Puppet::Util.deterministic_rand function" do
|
---|
48 | if Puppet::Util.respond_to?(:deterministic_rand)
|
---|
49 | Puppet::Util.expects(:deterministic_rand).with(44489829212339698569024999901561968770,4)
|
---|
50 | fqdn_rotate("asdf")
|
---|
51 | else
|
---|
52 | skip 'Puppet::Util#deterministic_rand not available'
|
---|
53 | end
|
---|
54 | end
|
---|
55 |
|
---|
56 | it "should not leave the global seed in a deterministic state" do
|
---|
57 | fqdn_rotate("asdf")
|
---|
58 | rand1 = rand()
|
---|
59 | fqdn_rotate("asdf")
|
---|
60 | rand2 = rand()
|
---|
61 | expect(rand1).not_to eql(rand2)
|
---|
62 | end
|
---|
63 |
|
---|
64 | def fqdn_rotate(value, args = {})
|
---|
65 | host = args[:host] || '127.0.0.1'
|
---|
66 | extra = args[:extra_identifier] || []
|
---|
67 |
|
---|
68 | # workaround not being able to use let(:facts) because some tests need
|
---|
69 | # multiple different hostnames in one context
|
---|
70 | scope.stubs(:lookupvar).with("::fqdn").returns(host)
|
---|
71 |
|
---|
72 | function_args = [value] + extra
|
---|
73 | scope.function_fqdn_rotate(function_args)
|
---|
74 | end
|
---|
75 | end
|
---|