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