ini_subsetting_spec.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. require 'spec_helper_acceptance'
  2. tmpdir = default.tmpdir('tmp')
  3. describe 'ini_subsetting resource' do
  4. after :all do
  5. shell("rm #{tmpdir}/*.ini", :acceptable_exit_codes => [0,1,2])
  6. end
  7. shared_examples 'has_content' do |path,pp,content|
  8. before :all do
  9. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  10. end
  11. after :all do
  12. shell("cat #{path}", :acceptable_exit_codes => [0,1,2])
  13. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  14. end
  15. it 'applies the manifest twice' do
  16. apply_manifest(pp, :catch_failures => true)
  17. apply_manifest(pp, :catch_changes => true)
  18. end
  19. describe file(path) do
  20. it { should be_file }
  21. it { should contain(content) }
  22. end
  23. end
  24. shared_examples 'has_error' do |path,pp,error|
  25. before :all do
  26. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  27. end
  28. after :all do
  29. shell("cat #{path}", :acceptable_exit_codes => [0,1,2])
  30. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  31. end
  32. it 'applies the manifest and gets a failure message' do
  33. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
  34. end
  35. describe file(path) do
  36. it { should_not be_file }
  37. end
  38. end
  39. describe 'ensure, section, setting, subsetting, & value parameters' do
  40. context '=> present with subsections' do
  41. pp = <<-EOS
  42. ini_subsetting { 'ensure => present for alpha':
  43. ensure => present,
  44. path => "#{tmpdir}/ini_subsetting.ini",
  45. section => 'one',
  46. setting => 'key',
  47. subsetting => 'alpha',
  48. value => 'bet',
  49. }
  50. ini_subsetting { 'ensure => present for beta':
  51. ensure => present,
  52. path => "#{tmpdir}/ini_subsetting.ini",
  53. section => 'one',
  54. setting => 'key',
  55. subsetting => 'beta',
  56. value => 'trons',
  57. }
  58. EOS
  59. it 'applies the manifest twice' do
  60. apply_manifest(pp, :catch_failures => true)
  61. apply_manifest(pp, :catch_changes => true)
  62. end
  63. describe file("#{tmpdir}/ini_subsetting.ini") do
  64. it { should be_file }
  65. #XXX Solaris 10 doesn't support multi-line grep
  66. it("should contain [one]\nkey = alphabet betatrons", :unless => fact('osfamily') == 'Solaris') {
  67. should contain("[one]\nkey = alphabet betatrons")
  68. }
  69. end
  70. end
  71. context 'ensure => absent' do
  72. before :all do
  73. if fact('osfamily') == 'Darwin'
  74. shell("echo \"[one]\nkey = alphabet betatrons\" > #{tmpdir}/ini_subsetting.ini")
  75. else
  76. shell("echo -e \"[one]\nkey = alphabet betatrons\" > #{tmpdir}/ini_subsetting.ini")
  77. end
  78. end
  79. pp = <<-EOS
  80. ini_subsetting { 'ensure => absent for subsetting':
  81. ensure => absent,
  82. path => "#{tmpdir}/ini_subsetting.ini",
  83. section => 'one',
  84. setting => 'key',
  85. subsetting => 'alpha',
  86. }
  87. EOS
  88. it 'applies the manifest twice' do
  89. apply_manifest(pp, :catch_failures => true)
  90. apply_manifest(pp, :catch_changes => true)
  91. end
  92. describe file("#{tmpdir}/ini_subsetting.ini") do
  93. it { should be_file }
  94. it { should contain('[one]') }
  95. it { should contain('key = betatrons') }
  96. it { should_not contain('alphabet') }
  97. end
  98. end
  99. end
  100. describe 'subsetting_separator' do
  101. {
  102. "" => "two = twinethree foobar",
  103. #"subsetting_separator => ''," => "two = twinethreefoobar", # breaks regex
  104. "subsetting_separator => ','," => "two = twinethree,foobar",
  105. "subsetting_separator => ' '," => "two = twinethree foobar",
  106. "subsetting_separator => ' == '," => "two = twinethree == foobar",
  107. "subsetting_separator => '='," => "two = twinethree=foobar",
  108. #"subsetting_separator => '---'," => "two = twinethree---foobar", # breaks regex
  109. }.each do |parameter, content|
  110. context "with \"#{parameter}\" makes \"#{content}\"" do
  111. pp = <<-EOS
  112. ini_subsetting { "with #{parameter} makes #{content}":
  113. ensure => present,
  114. section => 'one',
  115. setting => 'two',
  116. subsetting => 'twine',
  117. value => 'three',
  118. path => "#{tmpdir}/subsetting_separator.ini",
  119. #{parameter}
  120. }
  121. ini_subsetting { "foobar":
  122. ensure => present,
  123. section => 'one',
  124. setting => 'two',
  125. subsetting => 'foo',
  126. value => 'bar',
  127. path => "#{tmpdir}/subsetting_separator.ini",
  128. #{parameter}
  129. }
  130. EOS
  131. it_behaves_like 'has_content', "#{tmpdir}/subsetting_separator.ini", pp, content
  132. end
  133. end
  134. end
  135. describe 'quote_char' do
  136. {
  137. ['-Xmx'] => 'args=""',
  138. ['-Xmx', '256m'] => 'args=-Xmx256m',
  139. ['-Xmx', '512m'] => 'args="-Xmx512m"',
  140. ['-Xms', '256m'] => 'args="-Xmx256m -Xms256m"',
  141. }.each do |parameter, content|
  142. context %Q{with '#{parameter.first}' #{parameter.length > 1 ? '=> \'' << parameter[1] << '\'' : 'absent'} makes '#{content}'} do
  143. path = File.join(tmpdir, 'ini_subsetting.ini')
  144. before :all do
  145. shell(%Q{echo '[java]\nargs=-Xmx256m' > #{path}})
  146. end
  147. after :all do
  148. shell("cat #{path}", :acceptable_exit_codes => [0,1,2])
  149. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  150. end
  151. pp = <<-EOS
  152. ini_subsetting { '#{parameter.first}':
  153. ensure => #{parameter.length > 1 ? 'present' : 'absent'},
  154. path => '#{path}',
  155. section => 'java',
  156. setting => 'args',
  157. quote_char => '"',
  158. subsetting => '#{parameter.first}',
  159. value => '#{parameter.length > 1 ? parameter[1] : ''}'
  160. }
  161. EOS
  162. it 'applies the manifest twice' do
  163. apply_manifest(pp, :catch_failures => true)
  164. apply_manifest(pp, :catch_changes => true)
  165. end
  166. describe file("#{tmpdir}/ini_subsetting.ini") do
  167. it { should be_file }
  168. it { should contain(content) }
  169. end
  170. end
  171. end
  172. end
  173. end