validate_augeas_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'spec_helper'
  2. describe 'validate_augeas' do
  3. if Puppet.features.augeas?
  4. describe 'signature validation' do
  5. it { is_expected.not_to eq(nil) }
  6. it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  7. it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  8. it { is_expected.to run.with_params('', '', [], '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  9. it { is_expected.to run.with_params('one', 'one', 'MSG to User', '4th arg').and_raise_error(NoMethodError) }
  10. end
  11. describe 'valid inputs' do
  12. inputs = [
  13. ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns'],
  14. ["proc /proc proc nodev,noexec,nosuid 0 0\n", 'Fstab.lns'],
  15. ]
  16. inputs.each do |input|
  17. it { is_expected.to run.with_params(*input) }
  18. end
  19. end
  20. describe 'valid inputs which fail augeas validation' do
  21. # The intent here is to make sure valid inputs raise exceptions when they
  22. # don't specify an error message to display. This is the behvior in
  23. # 2.2.x and prior.
  24. inputs = [
  25. ["root:x:0:0:root\n", 'Passwd.lns'],
  26. ["127.0.1.1\n", 'Hosts.lns'],
  27. ]
  28. inputs.each do |input|
  29. it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{validate_augeas.*?matched less than it should}) }
  30. end
  31. end
  32. describe 'when specifying nice error messages' do
  33. # The intent here is to make sure the function returns the 4th argument
  34. # in the exception thrown
  35. inputs = [
  36. ["root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content'],
  37. ["127.0.1.1\n", 'Hosts.lns', [], 'Wrong hosts content'],
  38. ]
  39. inputs.each do |input|
  40. it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{#{input[3]}}) }
  41. end
  42. end
  43. describe 'matching additional tests' do
  44. inputs = [
  45. ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
  46. ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
  47. ]
  48. inputs.each do |input|
  49. it { is_expected.to run.with_params(*input) }
  50. end
  51. end
  52. describe 'failing additional tests' do
  53. inputs = [
  54. ["foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
  55. ["root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
  56. ]
  57. inputs.each do |input|
  58. it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{testing path}) }
  59. end
  60. end
  61. else
  62. skip 'ruby-augeas not installed'
  63. end
  64. end