ini_subsetting_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 'subsetting_key_val_separator' do
  139. {
  140. '' => /two = twinethree foobar/,
  141. "subsetting_key_val_separator => ':'," => /two = twine:three foo:bar/,
  142. "subsetting_key_val_separator => '-'," => /two = twine-three foo-bar/,
  143. }.each do |parameter, content|
  144. context "with '#{parameter}' makes '#{content}'" do
  145. pp = <<-EOS
  146. ini_subsetting { "with #{parameter} makes #{content}":
  147. ensure => 'present',
  148. section => 'one',
  149. setting => 'two',
  150. subsetting => 'twine',
  151. value => 'three',
  152. path => "#{tmpdir}/subsetting_key_val_separator.ini",
  153. before => Ini_subsetting['foobar'],
  154. #{parameter}
  155. }
  156. ini_subsetting { "foobar":
  157. ensure => 'present',
  158. section => 'one',
  159. setting => 'two',
  160. subsetting => 'foo',
  161. value => 'bar',
  162. path => "#{tmpdir}/subsetting_key_val_separator.ini",
  163. #{parameter}
  164. }
  165. EOS
  166. it_behaves_like 'has_content', "#{tmpdir}/subsetting_key_val_separator.ini", pp, content
  167. end
  168. end
  169. end
  170. describe 'quote_char' do
  171. {
  172. ['-Xmx'] => /args=""/,
  173. ['-Xmx', '256m'] => /args=-Xmx256m/,
  174. ['-Xmx', '512m'] => /args="-Xmx512m"/,
  175. ['-Xms', '256m'] => /args="-Xmx256m -Xms256m"/,
  176. }.each do |parameter, content|
  177. context %Q{with '#{parameter.first}' #{parameter.length > 1 ? '=> \'' << parameter[1] << '\'' : 'absent'} makes '#{content}'} do
  178. path = File.join(tmpdir, 'ini_subsetting.ini')
  179. before :all do
  180. shell(%Q{echo '[java]\nargs=-Xmx256m' > #{path}})
  181. end
  182. after :all do
  183. shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
  184. shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
  185. end
  186. pp = <<-EOS
  187. ini_subsetting { '#{parameter.first}':
  188. ensure => #{parameter.length > 1 ? 'present' : 'absent'},
  189. path => '#{path}',
  190. section => 'java',
  191. setting => 'args',
  192. quote_char => '"',
  193. subsetting => '#{parameter.first}',
  194. value => '#{parameter.length > 1 ? parameter[1] : ''}'
  195. }
  196. EOS
  197. it 'applies the manifest twice' do
  198. apply_manifest(pp, :catch_failures => true)
  199. apply_manifest(pp, :catch_changes => true)
  200. end
  201. describe file("#{tmpdir}/ini_subsetting.ini") do
  202. it { should be_file }
  203. its(:content) {
  204. should match content
  205. }
  206. end
  207. end
  208. end
  209. end
  210. describe 'show_diff parameter and logging:' do
  211. [ {:value => "initial_value", :matcher => "created", :show_diff => true},
  212. {:value => "public_value", :matcher => /initial_value.*public_value/, :show_diff => true},
  213. {:value => "secret_value", :matcher => /redacted sensitive information.*redacted sensitive information/, :show_diff => false},
  214. {:value => "md5_value", :matcher => /{md5}881671aa2bbc680bc530c4353125052b.*{md5}ed0903a7fa5de7886ca1a7a9ad06cf51/, :show_diff => :md5}
  215. ].each do |i|
  216. context "show_diff => #{i[:show_diff]}" do
  217. pp = <<-EOS
  218. ini_subsetting { 'test_show_diff':
  219. ensure => present,
  220. section => 'test',
  221. setting => 'something',
  222. subsetting => 'xxx',
  223. value => '#{i[:value]}',
  224. path => "#{tmpdir}/test_show_diff.ini",
  225. show_diff => #{i[:show_diff]}
  226. }
  227. EOS
  228. it "applies manifest and expects changed value to be logged in proper form" do
  229. config = {
  230. 'main' => {
  231. 'show_diff' => true
  232. }
  233. }
  234. configure_puppet_on(default, config)
  235. res = apply_manifest(pp, :expect_changes => true)
  236. expect(res.stdout).to match(i[:matcher])
  237. expect(res.stdout).not_to match(i[:value]) unless (i[:show_diff] == true)
  238. end
  239. end
  240. end
  241. end
  242. describe 'insert types:' do
  243. [
  244. {
  245. :insert_type => :start,
  246. :content => /d a b c/,
  247. },
  248. {
  249. :insert_type => :end,
  250. :content => /a b c d/,
  251. },
  252. {
  253. :insert_type => :before,
  254. :insert_value => 'c',
  255. :content => /a b d c/,
  256. },
  257. {
  258. :insert_type => :after,
  259. :insert_value => 'a',
  260. :content => /a d b c/,
  261. },
  262. {
  263. :insert_type => :index,
  264. :insert_value => 2,
  265. :content => /a b d c/,
  266. }
  267. ].each do |params|
  268. context "with '#{params[:insert_type]}' makes '#{params[:content]}'" do
  269. pp = <<-EOS
  270. ini_subsetting { "a":
  271. ensure => present,
  272. section => 'one',
  273. setting => 'two',
  274. subsetting => 'a',
  275. path => "#{tmpdir}/insert_types.ini",
  276. } ->
  277. ini_subsetting { "b":
  278. ensure => present,
  279. section => 'one',
  280. setting => 'two',
  281. subsetting => 'b',
  282. path => "#{tmpdir}/insert_types.ini",
  283. } ->
  284. ini_subsetting { "c":
  285. ensure => present,
  286. section => 'one',
  287. setting => 'two',
  288. subsetting => 'c',
  289. path => "#{tmpdir}/insert_types.ini",
  290. } ->
  291. ini_subsetting { "insert makes #{params[:content]}":
  292. ensure => present,
  293. section => 'one',
  294. setting => 'two',
  295. subsetting => 'd',
  296. path => "#{tmpdir}/insert_types.ini",
  297. insert_type => '#{params[:insert_type]}',
  298. insert_value => '#{params[:insert_value]}',
  299. }
  300. EOS
  301. it_behaves_like 'has_content', "#{tmpdir}/insert_types.ini", pp, params[:content]
  302. end
  303. end
  304. end
  305. end