3 describe 'seeded_rand' do
4 it { is_expected.not_to eq(nil) }
5 it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
6 it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
7 it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
8 it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
9 it { is_expected.to run.with_params(-10, '').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('string', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
12 it { is_expected.to run.with_params([], '').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(1, 1).and_raise_error(ArgumentError, %r{second argument must be a string}) }
15 it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be a string}) }
16 it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be a string}) }
18 it 'provides a random number strictly less than the given max' do
19 expect(seeded_rand(3, 'seed')).to satisfy { |n| n.to_i < 3 } # rubocop:disable Lint/AmbiguousBlockAssociation : Cannot parenthesize without break code or violating other Rubocop rules
22 it 'provides a random number greater or equal to zero' do
23 expect(seeded_rand(3, 'seed')).to satisfy { |n| n.to_i >= 0 } # rubocop:disable Lint/AmbiguousBlockAssociation : Cannot parenthesize without break code or violating other Rubocop rules
26 it "provides the same 'random' value on subsequent calls for the same host" do
27 expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed'))
30 it 'allows seed to control the random value on a single host' do
31 first_random = seeded_rand(1000, 'seed1')
32 second_different_random = seeded_rand(1000, 'seed2')
34 expect(first_random).not_to eql(second_different_random)
37 it 'does not return different values for different hosts' do
38 val1 = seeded_rand(1000, 'foo', :host => 'first.host.com')
39 val2 = seeded_rand(1000, 'foo', :host => 'second.host.com')
41 expect(val1).to eql(val2)
44 def seeded_rand(max, seed, args = {})
45 host = args[:host] || '127.0.0.1'
47 # workaround not being able to use let(:facts) because some tests need
48 # multiple different hostnames in one context
49 allow(scope).to receive(:lookupvar).with('::fqdn', {}).and_return(host)
51 scope.function_seeded_rand([max, seed])
54 context 'with UTF8 and double byte characters' do
55 it { is_expected.to run.with_params(1000, 'ǿňè') }
56 it { is_expected.to run.with_params(1000, '文字列') }