Skip to content

Commit 4255f5c

Browse files
committed
Rework the tests to check actual values
Rather that testing that the function return the same or different values, test the actual returned value so that we are sure the behavior does not change unexpectedly.
1 parent a241039 commit 4255f5c

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

spec/functions/fqdn_rand_string_spec.rb

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,31 @@
2525
it { is_expected.to run.with_params(100, 'ab').and_return(%r{\A[ab]{100}\z}) }
2626
it { is_expected.to run.with_params(100, 'ãβ').and_return(%r{\A[ãβ]{100}\z}) }
2727

28-
it "provides the same 'random' value on subsequent calls for the same host" do
29-
expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
30-
end
31-
32-
it 'considers the same host and same extra arguments to have the same random sequence' do
33-
first_random = fqdn_rand_string(10, extra_identifier: [1, 'same', 'host'])
34-
second_random = fqdn_rand_string(10, extra_identifier: [1, 'same', 'host'])
35-
36-
expect(first_random).to eql(second_random)
37-
end
38-
39-
it 'allows extra arguments to control the random value on a single host' do
40-
first_random = fqdn_rand_string(10, extra_identifier: [1, 'different', 'host'])
41-
second_different_random = fqdn_rand_string(10, extra_identifier: [2, 'different', 'host'])
42-
43-
expect(first_random).not_to eql(second_different_random)
44-
end
45-
46-
it 'returns different strings for different hosts' do
47-
val1 = fqdn_rand_string(10, host: 'first.host.com')
48-
val2 = fqdn_rand_string(10, host: 'second.host.com')
49-
50-
expect(val1).not_to eql(val2)
51-
end
28+
context 'produce predictible and reproducible results' do
29+
before(:each) do
30+
if Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('7.23.0')
31+
allow(scope).to receive(:lookupvar).with('::fqdn', {}).and_return(fqdn)
32+
else
33+
allow(scope).to receive(:lookupvar).with('facts', {}).and_return({ 'networking' => { 'fqdn' => fqdn } })
34+
end
35+
end
5236

53-
def fqdn_rand_string(max, args = {})
54-
host = args[:host] || '127.0.0.1'
55-
charset = args[:charset]
56-
extra = args[:extra_identifier] || []
37+
context 'on a node named example.com' do
38+
let(:fqdn) { 'example.com' }
5739

58-
# workaround not being able to use let(:facts) because some tests need
59-
# multiple different hostnames in one context
60-
if Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('7.23.0')
61-
allow(scope).to receive(:lookupvar).with('::fqdn', {}).and_return(host)
62-
else
63-
allow(scope).to receive(:lookupvar).with('facts', {}).and_return({ 'networking' => { 'fqdn' => host } })
40+
it { is_expected.to run.with_params(5).and_return('Pw5NP') }
41+
it { is_expected.to run.with_params(10, 'abcd').and_return('cdadaaacaa') }
42+
it { is_expected.to run.with_params(20, '', 'custom seed').and_return('3QKQHP4wmEObY3a6hkeg') }
43+
it { is_expected.to run.with_params(20, '', 'custom seed', 1, 'extra').and_return('OA19SVDoc3QPY5NlSQ28') }
6444
end
6545

66-
function_args = [max]
67-
if args.key?(:charset) || !extra.empty?
68-
function_args << charset
46+
context 'on a node named desktop-fln40kq.lan' do
47+
let(:fqdn) { 'desktop-fln40kq.lan' }
48+
49+
it { is_expected.to run.with_params(5).and_return('bgQsB') }
50+
it { is_expected.to run.with_params(10, 'abcd').and_return('bcdbcdacad') }
51+
it { is_expected.to run.with_params(20, '', 'custom seed').and_return('KaZsFlWkUo5SeA3gBEf0') }
52+
it { is_expected.to run.with_params(20, '', 'custom seed', 1, 'extra').and_return('dcAzn1e8AA7hhoLpxAD6') }
6953
end
70-
function_args += extra
71-
scope.function_fqdn_rand_string(function_args)
7254
end
7355
end

0 commit comments

Comments
 (0)