ini_setting_spec.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. require 'spec_helper_acceptance'
  2. tmpdir = default.tmpdir('tmp')
  3. describe 'ini_setting 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) { should match 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 parameter' do
  40. context '=> present for global and section' do
  41. pp = <<-EOS
  42. ini_setting { 'ensure => present for section':
  43. ensure => present,
  44. path => "#{tmpdir}/ini_setting.ini",
  45. section => 'one',
  46. setting => 'two',
  47. value => 'three',
  48. }
  49. ini_setting { 'ensure => present for global':
  50. ensure => present,
  51. path => "#{tmpdir}/ini_setting.ini",
  52. section => '',
  53. setting => 'four',
  54. value => 'five',
  55. }
  56. EOS
  57. it 'applies the manifest twice' do
  58. apply_manifest(pp, :catch_failures => true)
  59. apply_manifest(pp, :catch_changes => true)
  60. end
  61. it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, /four = five\n\n\[one\]\ntwo = three/
  62. end
  63. context '=> present for global and section (from previous blank value)' do
  64. before :all do
  65. if fact('osfamily') == 'Darwin'
  66. shell("echo \"four =[one]\ntwo =\" > #{tmpdir}/ini_setting.ini")
  67. else
  68. shell("echo -e \"four =\n[one]\ntwo =\" > #{tmpdir}/ini_setting.ini")
  69. end
  70. end
  71. pp = <<-EOS
  72. ini_setting { 'ensure => present for section':
  73. ensure => present,
  74. path => "#{tmpdir}/ini_setting.ini",
  75. section => 'one',
  76. setting => 'two',
  77. value => 'three',
  78. }
  79. ini_setting { 'ensure => present for global':
  80. ensure => present,
  81. path => "#{tmpdir}/ini_setting.ini",
  82. section => '',
  83. setting => 'four',
  84. value => 'five',
  85. }
  86. EOS
  87. it 'applies the manifest twice' do
  88. apply_manifest(pp, :catch_failures => true)
  89. apply_manifest(pp, :catch_changes => true)
  90. end
  91. it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, /four = five\n\n\[one\]\ntwo = three/
  92. end
  93. context '=> absent for key/value' do
  94. before :all do
  95. if fact('osfamily') == 'Darwin'
  96. shell("echo \"four = five[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  97. else
  98. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  99. end
  100. end
  101. pp = <<-EOS
  102. ini_setting { 'ensure => absent for key/value':
  103. ensure => absent,
  104. path => "#{tmpdir}/ini_setting.ini",
  105. section => 'one',
  106. setting => 'two',
  107. value => 'three',
  108. }
  109. EOS
  110. it 'applies the manifest twice' do
  111. apply_manifest(pp, :catch_failures => true)
  112. apply_manifest(pp, :catch_changes => true)
  113. end
  114. describe file("#{tmpdir}/ini_setting.ini") do
  115. it { should be_file }
  116. its(:content) {
  117. should match /four = five/
  118. should_not match /\[one\]/
  119. should_not match /two = three/
  120. }
  121. end
  122. end
  123. context '=> absent for global' do
  124. before :all do
  125. if fact('osfamily') == 'Darwin'
  126. shell("echo \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  127. else
  128. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  129. end
  130. end
  131. after :all do
  132. shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0, 1, 2])
  133. shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0, 1, 2])
  134. end
  135. pp = <<-EOS
  136. ini_setting { 'ensure => absent for global':
  137. ensure => absent,
  138. path => "#{tmpdir}/ini_setting.ini",
  139. section => '',
  140. setting => 'four',
  141. value => 'five',
  142. }
  143. EOS
  144. it 'applies the manifest twice' do
  145. apply_manifest(pp, :catch_failures => true)
  146. apply_manifest(pp, :catch_changes => true)
  147. end
  148. describe file("#{tmpdir}/ini_setting.ini") do
  149. it { should be_file }
  150. its(:content) {
  151. should_not match /four = five/
  152. should match /\[one\]/
  153. should match /two = three/
  154. }
  155. end
  156. end
  157. end
  158. describe 'section, setting, value parameters' do
  159. {
  160. "section => 'test', setting => 'foo', value => 'bar'," => /\[test\]\nfoo = bar/,
  161. "section => 'more', setting => 'baz', value => 'quux'," => /\[more\]\nbaz = quux/,
  162. "section => '', setting => 'top', value => 'level'," => /top = level/,
  163. "section => 'z', setting => 'sp aces', value => 'foo bar'," => /\[z\]\nsp aces = foo bar/,
  164. }.each do |parameter_list, content|
  165. context parameter_list do
  166. pp = <<-EOS
  167. ini_setting { "#{parameter_list}":
  168. ensure => present,
  169. path => "#{tmpdir}/ini_setting.ini",
  170. #{parameter_list}
  171. }
  172. EOS
  173. it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, content
  174. end
  175. end
  176. {
  177. "section => 'test'," => /setting is a required.+value is a required/,
  178. "setting => 'foo', value => 'bar'," => /section is a required/,
  179. "section => 'test', setting => 'foo'," => /value is a required/,
  180. "section => 'test', value => 'bar'," => /setting is a required/,
  181. "value => 'bar'," => /section is a required.+setting is a required/,
  182. "setting => 'foo'," => /section is a required.+value is a required/,
  183. }.each do |parameter_list, error|
  184. context parameter_list, :pending => 'no error checking yet' do
  185. pp = <<-EOS
  186. ini_setting { "#{parameter_list}":
  187. ensure => present,
  188. path => "#{tmpdir}/ini_setting.ini",
  189. #{parameter_list}
  190. }
  191. EOS
  192. it_behaves_like 'has_error', "#{tmpdir}/ini_setting.ini", pp, error
  193. end
  194. end
  195. end
  196. describe 'path parameter' do
  197. [
  198. "#{tmpdir}/one.ini",
  199. "#{tmpdir}/two.ini",
  200. "#{tmpdir}/three.ini",
  201. ].each do |path|
  202. context "path => #{path}" do
  203. pp = <<-EOS
  204. ini_setting { 'path => #{path}':
  205. ensure => present,
  206. section => 'one',
  207. setting => 'two',
  208. value => 'three',
  209. path => '#{path}',
  210. }
  211. EOS
  212. it_behaves_like 'has_content', path, pp, /\[one\]\ntwo = three/
  213. end
  214. end
  215. context "path => foo" do
  216. pp = <<-EOS
  217. ini_setting { 'path => foo':
  218. ensure => present,
  219. section => 'one',
  220. setting => 'two',
  221. value => 'three',
  222. path => 'foo',
  223. }
  224. EOS
  225. it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/
  226. end
  227. end
  228. describe 'key_val_separator parameter' do
  229. {
  230. "" => /two = three/,
  231. "key_val_separator => '='," => /two=three/,
  232. "key_val_separator => ' = '," => /two = three/,
  233. "key_val_separator => ' '," => /two three/,
  234. "key_val_separator => ' '," => /two three/,
  235. }.each do |parameter, content|
  236. context "with \"#{parameter}\" makes \"#{content}\"" do
  237. pp = <<-EOS
  238. ini_setting { "with #{parameter} makes #{content}":
  239. ensure => present,
  240. section => 'one',
  241. setting => 'two',
  242. value => 'three',
  243. path => "#{tmpdir}/key_val_separator.ini",
  244. #{parameter}
  245. }
  246. EOS
  247. it_behaves_like 'has_content', "#{tmpdir}/key_val_separator.ini", pp, content
  248. end
  249. end
  250. end
  251. describe 'show_diff parameter and logging:' do
  252. [ {:value => "initial_value", :matcher => "created", :show_diff => true},
  253. {:value => "public_value", :matcher => /initial_value.*public_value/, :show_diff => true},
  254. {:value => "secret_value", :matcher => /redacted sensitive information.*redacted sensitive information/, :show_diff => false},
  255. {:value => "md5_value", :matcher => /{md5}881671aa2bbc680bc530c4353125052b.*{md5}ed0903a7fa5de7886ca1a7a9ad06cf51/, :show_diff => :md5}
  256. ].each do |i|
  257. context "show_diff => #{i[:show_diff]}" do
  258. pp = <<-EOS
  259. ini_setting { 'test_show_diff':
  260. ensure => present,
  261. section => 'test',
  262. setting => 'something',
  263. value => '#{i[:value]}',
  264. path => "#{tmpdir}/test_show_diff.ini",
  265. show_diff => #{i[:show_diff]}
  266. }
  267. EOS
  268. it "applies manifest and expects changed value to be logged in proper form" do
  269. config = {
  270. 'main' => {
  271. 'show_diff' => true
  272. }
  273. }
  274. configure_puppet_on(default, config)
  275. res = apply_manifest(pp, :expect_changes => true)
  276. expect(res.stdout).to match(i[:matcher])
  277. expect(res.stdout).not_to match(i[:value]) unless (i[:show_diff] == true)
  278. end
  279. end
  280. end
  281. end
  282. end