|
| 1 | +require 'spec_helper' |
| 2 | + |
| 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, /wrong number of arguments/i) } |
| 6 | + it { is_expected.to run.with_params(1).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 a string/) } |
| 15 | + it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, /second argument must be a string/) } |
| 16 | + it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, /second argument must be a string/) } |
| 17 | + |
| 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 } |
| 20 | + end |
| 21 | + |
| 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 } |
| 24 | + end |
| 25 | + |
| 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')) |
| 28 | + end |
| 29 | + |
| 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') |
| 33 | + |
| 34 | + expect(first_random).not_to eql(second_different_random) |
| 35 | + end |
| 36 | + |
| 37 | + it "should 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") |
| 40 | + |
| 41 | + expect(val1).to eql(val2) |
| 42 | + end |
| 43 | + |
| 44 | + def seeded_rand(max, seed, args = {}) |
| 45 | + host = args[:host] || '127.0.0.1' |
| 46 | + |
| 47 | + # workaround not being able to use let(:facts) because some tests need |
| 48 | + # multiple different hostnames in one context |
| 49 | + scope.stubs(:lookupvar).with("::fqdn", {}).returns(host) |
| 50 | + |
| 51 | + scope.function_seeded_rand([max, seed]) |
| 52 | + end |
| 53 | +end |
0 commit comments