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 |
|
---|
24 | it "provides the same 'random' value on subsequent calls for the same host" do
|
---|
25 | expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
|
---|
26 | end
|
---|
27 |
|
---|
28 | it "considers the same host and same extra arguments to have the same random sequence" do
|
---|
29 | first_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
|
---|
30 | second_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
|
---|
31 |
|
---|
32 | expect(first_random).to eql(second_random)
|
---|
33 | end
|
---|
34 |
|
---|
35 | it "allows extra arguments to control the random value on a single host" do
|
---|
36 | first_random = fqdn_rand_string(10, :extra_identifier => [1, "different", "host"])
|
---|
37 | second_different_random = fqdn_rand_string(10, :extra_identifier => [2, "different", "host"])
|
---|
38 |
|
---|
39 | expect(first_random).not_to eql(second_different_random)
|
---|
40 | end
|
---|
41 |
|
---|
42 | it "should return different strings for different hosts" do
|
---|
43 | val1 = fqdn_rand_string(10, :host => "first.host.com")
|
---|
44 | val2 = fqdn_rand_string(10, :host => "second.host.com")
|
---|
45 |
|
---|
46 | expect(val1).not_to eql(val2)
|
---|
47 | end
|
---|
48 |
|
---|
49 | def fqdn_rand_string(max, args = {})
|
---|
50 | host = args[:host] || '127.0.0.1'
|
---|
51 | charset = args[:charset]
|
---|
52 | extra = args[:extra_identifier] || []
|
---|
53 |
|
---|
54 | # workaround not being able to use let(:facts) because some tests need
|
---|
55 | # multiple different hostnames in one context
|
---|
56 | scope.stubs(:lookupvar).with("::fqdn", {}).returns(host)
|
---|
57 |
|
---|
58 | function_args = [max]
|
---|
59 | if args.has_key?(:charset) or !extra.empty?
|
---|
60 | function_args << charset
|
---|
61 | end
|
---|
62 | function_args += extra
|
---|
63 | scope.function_fqdn_rand_string(function_args)
|
---|
64 | end
|
---|
65 | end
|
---|