ensure_resource_spec.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. require 'spec_helper'
  2. describe 'ensure_resource' do
  3. it { is_expected.not_to eq(nil) }
  4. it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a type}) }
  5. it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{Must specify a title}) }
  6. if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0
  7. it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(ArgumentError) }
  8. else
  9. it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(Puppet::ParseError) }
  10. end
  11. it {
  12. pending('should not accept numbers as arguments')
  13. is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError)
  14. }
  15. context 'when given an empty catalog' do
  16. describe 'after running ensure_resource("user", "username1", {})' do
  17. before(:each) { subject.call(['User', 'username1', {}]) }
  18. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  19. it { expect(-> { catalogue }).to contain_user('username1').without_ensure }
  20. end
  21. describe 'after running ensure_resource("user", "username1", { gid => undef })' do
  22. before(:each) { subject.call(['User', 'username1', { 'gid' => :undef }]) }
  23. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  24. it { expect(-> { catalogue }).to contain_user('username1').without_ensure }
  25. it { expect(-> { catalogue }).to contain_user('username1').without_gid }
  26. end
  27. describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do
  28. before(:each) { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) }
  29. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  30. it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
  31. it { expect(-> { catalogue }).to contain_user('username1').without_gid }
  32. end
  33. describe 'after running ensure_resource("test::deftype", "foo", {})' do
  34. before(:each) { subject.call(['test::deftype', 'foo', {}]) }
  35. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  36. it { expect(-> { catalogue }).to contain_test__deftype('foo').without_ensure }
  37. end
  38. end
  39. context 'when given a catalog with UTF8 chars' do
  40. describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", {})' do
  41. before(:each) { subject.call(['User', 'Şắოрŀễ Ţëם', {}]) }
  42. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  43. it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
  44. end
  45. describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { gid => undef })' do
  46. before(:each) { subject.call(['User', 'Şắოрŀễ Ţëם', { 'gid' => :undef }]) }
  47. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  48. it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
  49. it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
  50. end
  51. describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { ensure => present, gid => undef })' do
  52. before(:each) { subject.call(['User', 'Şắოрŀễ Ţëם', { 'ensure' => 'present', 'gid' => :undef }]) }
  53. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  54. it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').with_ensure('present') }
  55. it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
  56. end
  57. end
  58. context 'when given a catalog with "user { username1: ensure => present }"' do
  59. let(:pre_condition) { 'user { username1: ensure => present }' }
  60. describe 'after running ensure_resource("user", "username1", {})' do
  61. before(:each) { subject.call(['User', 'username1', {}]) }
  62. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  63. it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
  64. end
  65. describe 'after running ensure_resource("user", "username2", {})' do
  66. before(:each) { subject.call(['User', 'username2', {}]) }
  67. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  68. it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
  69. it { expect(-> { catalogue }).to contain_user('username2').without_ensure }
  70. end
  71. describe 'after running ensure_resource("user", "username1", { gid => undef })' do
  72. before(:each) { subject.call(['User', 'username1', { 'gid' => :undef }]) }
  73. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  74. it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
  75. end
  76. describe 'after running ensure_resource("user", ["username1", "username2"], {})' do
  77. before(:each) { subject.call(['User', %w[username1 username2], {}]) }
  78. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  79. it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
  80. it { expect(-> { catalogue }).to contain_user('username2').without_ensure }
  81. end
  82. describe 'when providing already set params' do
  83. let(:params) { { 'ensure' => 'present' } }
  84. before(:each) { subject.call(['User', %w[username2 username3], params]) }
  85. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  86. it { expect(-> { catalogue }).to contain_user('username1').with(params) }
  87. it { expect(-> { catalogue }).to contain_user('username2').with(params) }
  88. end
  89. context 'when trying to add params' do
  90. it {
  91. is_expected.to run \
  92. .with_params('User', 'username1', 'ensure' => 'present', 'shell' => true) \
  93. .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, %r{User\[username1\] is already declared})
  94. }
  95. end
  96. end
  97. context 'when given a catalog with "test::deftype { foo: }"' do
  98. let(:pre_condition) { 'test::deftype { "foo": }' }
  99. describe 'after running ensure_resource("test::deftype", "foo", {})' do
  100. before(:each) { subject.call(['test::deftype', 'foo', {}]) }
  101. # this lambda is required due to strangeness within rspec-puppet's expectation handling
  102. it { expect(-> { catalogue }).to contain_test__deftype('foo').without_ensure }
  103. end
  104. end
  105. end