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