Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / fqdn_rand_string_spec.rb
1 require 'spec_helper'
2
3 describe 'fqdn_rand_string' do
4   let(:default_charset) { %r{\A[a-zA-Z0-9]{100}\z} }
5
6   it { is_expected.not_to eq(nil) }
7   it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
8   it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
9   it { is_expected.to run.with_params(1.5).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
10   it { is_expected.to run.with_params(-10).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
11   it { is_expected.to run.with_params('-10').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
12   it { is_expected.to run.with_params('string').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
13   it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
14   it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
15   it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) }
16   it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) }
17   it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) }
18   it { is_expected.to run.with_params(100).and_return(default_charset) }
19   it { is_expected.to run.with_params('100').and_return(default_charset) }
20   it { is_expected.to run.with_params(100, nil).and_return(default_charset) }
21   it { is_expected.to run.with_params(100, '').and_return(default_charset) }
22   it { is_expected.to run.with_params(100, 'a').and_return(%r{\Aa{100}\z}) }
23   it { is_expected.to run.with_params(100, 'ab').and_return(%r{\A[ab]{100}\z}) }
24   it { is_expected.to run.with_params(100, 'ãβ').and_return(%r{\A[ãβ]{100}\z}) }
25
26   it "provides the same 'random' value on subsequent calls for the same host" do
27     expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
28   end
29
30   it 'considers the same host and same extra arguments to have the same random sequence' do
31     first_random = fqdn_rand_string(10, :extra_identifier => [1, 'same', 'host'])
32     second_random = fqdn_rand_string(10, :extra_identifier => [1, 'same', 'host'])
33
34     expect(first_random).to eql(second_random)
35   end
36
37   it 'allows extra arguments to control the random value on a single host' do
38     first_random = fqdn_rand_string(10, :extra_identifier => [1, 'different', 'host'])
39     second_different_random = fqdn_rand_string(10, :extra_identifier => [2, 'different', 'host'])
40
41     expect(first_random).not_to eql(second_different_random)
42   end
43
44   it 'returns different strings for different hosts' do
45     val1 = fqdn_rand_string(10, :host => 'first.host.com')
46     val2 = fqdn_rand_string(10, :host => 'second.host.com')
47
48     expect(val1).not_to eql(val2)
49   end
50
51   def fqdn_rand_string(max, args = {})
52     host = args[:host] || '127.0.0.1'
53     charset = args[:charset]
54     extra = args[:extra_identifier] || []
55
56     # workaround not being able to use let(:facts) because some tests need
57     # multiple different hostnames in one context
58     allow(scope).to receive(:lookupvar).with('::fqdn', {}).and_return(host)
59
60     function_args = [max]
61     if args.key?(:charset) || !extra.empty?
62       function_args << charset
63     end
64     function_args += extra
65     scope.function_fqdn_rand_string(function_args)
66   end
67 end