ini_subsetting_spec.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. its(:content) {
  22. should match content
  23. }
  24. end
  25. end
  26. shared_examples 'has_error' do |path, pp, error|
  27. before :all do
  28. shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
  29. end
  30. after :all do
  31. shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
  32. shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
  33. end
  34. it 'applies the manifest and gets a failure message' do
  35. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
  36. end
  37. describe file(path) do
  38. it { should_not be_file }
  39. end
  40. end
  41. describe 'ensure, section, setting, subsetting, & value parameters' do
  42. context '=> present with subsections' do
  43. pp = <<-EOS
  44. ini_subsetting { 'ensure => present for alpha':
  45. ensure => present,
  46. path => "#{tmpdir}/ini_subsetting.ini",
  47. section => 'one',
  48. setting => 'key',
  49. subsetting => 'alpha',
  50. value => 'bet',
  51. }
  52. ini_subsetting { 'ensure => present for beta':
  53. ensure => present,
  54. path => "#{tmpdir}/ini_subsetting.ini",
  55. section => 'one',
  56. setting => 'key',
  57. subsetting => 'beta',
  58. value => 'trons',
  59. require => Ini_subsetting['ensure => present for alpha'],
  60. }
  61. EOS
  62. it 'applies the manifest twice' do
  63. apply_manifest(pp, :catch_failures => true)
  64. apply_manifest(pp, :catch_changes => true)
  65. end
  66. describe file("#{tmpdir}/ini_subsetting.ini") do
  67. it { should be_file }
  68. its(:content) {
  69. should match /\[one\]\nkey = alphabet betatrons/
  70. }
  71. end
  72. end
  73. context 'ensure => absent' do
  74. before :all do
  75. if fact('osfamily') == 'Darwin'
  76. shell("echo \"[one]\nkey = alphabet betatrons\" > #{tmpdir}/ini_subsetting.ini")
  77. else
  78. shell("echo -e \"[one]\nkey = alphabet betatrons\" > #{tmpdir}/ini_subsetting.ini")
  79. end
  80. end
  81. pp = <<-EOS
  82. ini_subsetting { 'ensure => absent for subsetting':
  83. ensure => absent,
  84. path => "#{tmpdir}/ini_subsetting.ini",
  85. section => 'one',
  86. setting => 'key',
  87. subsetting => 'alpha',
  88. }
  89. EOS
  90. it 'applies the manifest twice' do
  91. apply_manifest(pp, :catch_failures => true)
  92. apply_manifest(pp, :catch_changes => true)
  93. end
  94. describe file("#{tmpdir}/ini_subsetting.ini") do
  95. it { should be_file }
  96. its(:content) {
  97. should match /\[one\]/
  98. should match /key = betatrons/
  99. should_not match /alphabet/
  100. }
  101. end
  102. end
  103. end
  104. describe 'subsetting_separator' do
  105. {
  106. "" => /two = twinethree foobar/,
  107. "subsetting_separator => ','," => /two = twinethree,foobar/,
  108. "subsetting_separator => ' '," => /two = twinethree foobar/,
  109. "subsetting_separator => ' == '," => /two = twinethree == foobar/,
  110. "subsetting_separator => '='," => /two = twinethree=foobar/,
  111. }.each do |parameter, content|
  112. context "with \"#{parameter}\" makes \"#{content}\"" do
  113. pp = <<-EOS
  114. ini_subsetting { "with #{parameter} makes #{content}":
  115. ensure => present,
  116. section => 'one',
  117. setting => 'two',
  118. subsetting => 'twine',
  119. value => 'three',
  120. path => "#{tmpdir}/subsetting_separator.ini",
  121. before => Ini_subsetting['foobar'],
  122. #{parameter}
  123. }
  124. ini_subsetting { "foobar":
  125. ensure => present,
  126. section => 'one',
  127. setting => 'two',
  128. subsetting => 'foo',
  129. value => 'bar',
  130. path => "#{tmpdir}/subsetting_separator.ini",
  131. #{parameter}
  132. }
  133. EOS
  134. it_behaves_like 'has_content', "#{tmpdir}/subsetting_separator.ini", pp, content
  135. end
  136. end
  137. end
  138. describe 'quote_char' do
  139. {
  140. ['-Xmx'] => /args=""/,
  141. ['-Xmx', '256m'] => /args=-Xmx256m/,
  142. ['-Xmx', '512m'] => /args="-Xmx512m"/,
  143. ['-Xms', '256m'] => /args="-Xmx256m -Xms256m"/,
  144. }.each do |parameter, content|
  145. context %Q{with '#{parameter.first}' #{parameter.length > 1 ? '=> \'' << parameter[1] << '\'' : 'absent'} makes '#{content}'} do
  146. path = File.join(tmpdir, 'ini_subsetting.ini')
  147. before :all do
  148. shell(%Q{echo '[java]\nargs=-Xmx256m' > #{path}})
  149. end
  150. after :all do
  151. shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
  152. shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
  153. end
  154. pp = <<-EOS
  155. ini_subsetting { '#{parameter.first}':
  156. ensure => #{parameter.length > 1 ? 'present' : 'absent'},
  157. path => '#{path}',
  158. section => 'java',
  159. setting => 'args',
  160. quote_char => '"',
  161. subsetting => '#{parameter.first}',
  162. value => '#{parameter.length > 1 ? parameter[1] : ''}'
  163. }
  164. EOS
  165. it 'applies the manifest twice' do
  166. apply_manifest(pp, :catch_failures => true)
  167. apply_manifest(pp, :catch_changes => true)
  168. end
  169. describe file("#{tmpdir}/ini_subsetting.ini") do
  170. it { should be_file }
  171. its(:content) {
  172. should match content
  173. }
  174. end
  175. end
  176. end
  177. end
  178. describe 'show_diff parameter and logging:' do
  179. [ {:value => "initial_value", :matcher => "created", :show_diff => true},
  180. {:value => "public_value", :matcher => /initial_value.*public_value/, :show_diff => true},
  181. {:value => "secret_value", :matcher => /redacted sensitive information.*redacted sensitive information/, :show_diff => false},
  182. {:value => "md5_value", :matcher => /{md5}881671aa2bbc680bc530c4353125052b.*{md5}ed0903a7fa5de7886ca1a7a9ad06cf51/, :show_diff => :md5}
  183. ].each do |i|
  184. context "show_diff => #{i[:show_diff]}" do
  185. pp = <<-EOS
  186. ini_subsetting { 'test_show_diff':
  187. ensure => present,
  188. section => 'test',
  189. setting => 'something',
  190. subsetting => 'xxx',
  191. value => '#{i[:value]}',
  192. path => "#{tmpdir}/test_show_diff.ini",
  193. show_diff => #{i[:show_diff]}
  194. }
  195. EOS
  196. it "applies manifest and expects changed value to be logged in proper form" do
  197. config = {
  198. 'main' => {
  199. 'show_diff' => true
  200. }
  201. }
  202. configure_puppet_on(default, config)
  203. res = apply_manifest(pp, :expect_changes => true)
  204. expect(res.stdout).to match(i[:matcher])
  205. expect(res.stdout).not_to match(i[:value]) unless (i[:show_diff] == true)
  206. end
  207. end
  208. end
  209. end
  210. end