fqdn_rotate_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. require 'spec_helper'
  2. describe 'fqdn_rotate' do
  3. it { is_expected.not_to eq(nil) }
  4. it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  5. it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) }
  6. it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) }
  7. it { is_expected.to run.with_params('').and_return('') }
  8. it { is_expected.to run.with_params('a').and_return('a') }
  9. it { is_expected.to run.with_params('ã').and_return('ã') }
  10. it { is_expected.to run.with_params([]).and_return([]) }
  11. it { is_expected.to run.with_params(['a']).and_return(['a']) }
  12. it 'rotates a string and the result should be the same size' do
  13. expect(fqdn_rotate('asdf').size).to eq(4)
  14. end
  15. it 'rotates a string to give the same results for one host' do
  16. val1 = fqdn_rotate('abcdefg', :host => 'one')
  17. val2 = fqdn_rotate('abcdefg', :host => 'one')
  18. expect(val1).to eq(val2)
  19. end
  20. it 'allows extra arguments to control the random rotation on a single host' do
  21. val1 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'different', 'host'])
  22. val2 = fqdn_rotate('abcdefg', :extra_identifier => [2, 'different', 'host'])
  23. expect(val1).not_to eq(val2)
  24. end
  25. it 'considers the same host and same extra arguments to have the same random rotation' do
  26. val1 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'same', 'host'])
  27. val2 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'same', 'host'])
  28. expect(val1).to eq(val2)
  29. end
  30. it 'rotates a string to give different values on different hosts' do
  31. val1 = fqdn_rotate('abcdefg', :host => 'one')
  32. val2 = fqdn_rotate('abcdefg', :host => 'two')
  33. expect(val1).not_to eq(val2)
  34. end
  35. it 'accepts objects which extend String' do
  36. result = fqdn_rotate(AlsoString.new('asdf'))
  37. expect(result).to eq('dfas')
  38. end
  39. it 'uses the Puppet::Util.deterministic_rand function' do
  40. skip 'Puppet::Util#deterministic_rand not available' unless Puppet::Util.respond_to?(:deterministic_rand)
  41. Puppet::Util.expects(:deterministic_rand).with(44_489_829_212_339_698_569_024_999_901_561_968_770, 4)
  42. fqdn_rotate('asdf')
  43. end
  44. it 'does not leave the global seed in a deterministic state' do
  45. fqdn_rotate('asdf')
  46. rand1 = rand
  47. fqdn_rotate('asdf')
  48. rand2 = rand
  49. expect(rand1).not_to eql(rand2)
  50. end
  51. def fqdn_rotate(value, args = {})
  52. host = args[:host] || '127.0.0.1'
  53. extra = args[:extra_identifier] || []
  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. function_args = [value] + extra
  58. scope.function_fqdn_rotate(function_args)
  59. end
  60. end