ini_setting_spec.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 with no stderr' do
  16. expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
  17. expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
  18. end
  19. describe file(path) do
  20. it { should be_file }
  21. #XXX Solaris 10 doesn't support multi-line grep
  22. it("should contain #{content}", :unless => fact('osfamily') == 'Solaris') {
  23. should contain(content)
  24. }
  25. end
  26. end
  27. shared_examples 'has_error' do |path,pp,error|
  28. before :all do
  29. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  30. end
  31. after :all do
  32. shell("cat #{path}", :acceptable_exit_codes => [0,1,2])
  33. shell("rm #{path}", :acceptable_exit_codes => [0,1,2])
  34. end
  35. it 'applies the manifest and gets a failure message' do
  36. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
  37. end
  38. describe file(path) do
  39. it { should_not be_file }
  40. end
  41. end
  42. describe 'ensure parameter' do
  43. context '=> present for global and section' do
  44. pp = <<-EOS
  45. ini_setting { 'ensure => present for section':
  46. ensure => present,
  47. path => "#{tmpdir}/ini_setting.ini",
  48. section => 'one',
  49. setting => 'two',
  50. value => 'three',
  51. }
  52. ini_setting { 'ensure => present for global':
  53. ensure => present,
  54. path => "#{tmpdir}/ini_setting.ini",
  55. section => '',
  56. setting => 'four',
  57. value => 'five',
  58. }
  59. EOS
  60. it 'applies the manifest twice with no stderr' do
  61. expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
  62. expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
  63. end
  64. describe file("#{tmpdir}/ini_setting.ini") do
  65. it { should be_file }
  66. #XXX Solaris 10 doesn't support multi-line grep
  67. it("should contain four = five\n[one]\ntwo = three", :unless => fact('osfamily') == 'Solaris') {
  68. should contain("four = five\n[one]\ntwo = three")
  69. }
  70. end
  71. end
  72. context '=> absent for key/value' do
  73. before :all do
  74. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  75. end
  76. pp = <<-EOS
  77. ini_setting { 'ensure => absent for key/value':
  78. ensure => absent,
  79. path => "#{tmpdir}/ini_setting.ini",
  80. section => 'one',
  81. setting => 'two',
  82. value => 'three',
  83. }
  84. EOS
  85. it 'applies the manifest twice with no stderr' do
  86. expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
  87. expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
  88. end
  89. describe file("#{tmpdir}/ini_setting.ini") do
  90. it { should be_file }
  91. it { should contain('four = five') }
  92. it { should contain('[one]') }
  93. it { should_not contain('two = three') }
  94. end
  95. end
  96. context '=> absent for section', :pending => "cannot ensure absent on a section" do
  97. before :all do
  98. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  99. end
  100. after :all do
  101. shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  102. shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  103. end
  104. pp = <<-EOS
  105. ini_setting { 'ensure => absent for section':
  106. ensure => absent,
  107. path => "#{tmpdir}/ini_setting.ini",
  108. section => 'one',
  109. }
  110. EOS
  111. it 'applies the manifest twice with no stderr' do
  112. expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
  113. expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
  114. end
  115. describe file("#{tmpdir}/ini_setting.ini") do
  116. it { should be_file }
  117. it { should contain('four = five') }
  118. it { should_not contain('[one]') }
  119. it { should_not contain('two = three') }
  120. end
  121. end
  122. context '=> absent for global' do
  123. before :all do
  124. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  125. end
  126. after :all do
  127. shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  128. shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  129. end
  130. pp = <<-EOS
  131. ini_setting { 'ensure => absent for global':
  132. ensure => absent,
  133. path => "#{tmpdir}/ini_setting.ini",
  134. section => '',
  135. setting => 'four',
  136. value => 'five',
  137. }
  138. EOS
  139. it 'applies the manifest twice with no stderr' do
  140. expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
  141. expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
  142. end
  143. describe file("#{tmpdir}/ini_setting.ini") do
  144. it { should be_file }
  145. it { should_not contain('four = five') }
  146. it { should contain('[one]') }
  147. it { should contain('two = three') }
  148. end
  149. end
  150. end
  151. describe 'section, setting, value parameters' do
  152. {
  153. "section => 'test', setting => 'foo', value => 'bar'," => "[test]\nfoo = bar",
  154. "section => 'more', setting => 'baz', value => 'quux'," => "[more]\nbaz = quux",
  155. "section => '', setting => 'top', value => 'level'," => "top = level",
  156. }.each do |parameter_list, content|
  157. context parameter_list do
  158. pp = <<-EOS
  159. ini_setting { "#{parameter_list}":
  160. ensure => present,
  161. path => "#{tmpdir}/ini_setting.ini",
  162. #{parameter_list}
  163. }
  164. EOS
  165. it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, content
  166. end
  167. end
  168. {
  169. "section => 'test'," => /setting is a required.+value is a required/,
  170. "setting => 'foo', value => 'bar'," => /section is a required/,
  171. "section => 'test', setting => 'foo'," => /value is a required/,
  172. "section => 'test', value => 'bar'," => /setting is a required/,
  173. "value => 'bar'," => /section is a required.+setting is a required/,
  174. "setting => 'foo'," => /section is a required.+value is a required/,
  175. }.each do |parameter_list, error|
  176. context parameter_list, :pending => 'no error checking yet' do
  177. pp = <<-EOS
  178. ini_setting { "#{parameter_list}":
  179. ensure => present,
  180. path => "#{tmpdir}/ini_setting.ini",
  181. #{parameter_list}
  182. }
  183. EOS
  184. it_behaves_like 'has_error', "#{tmpdir}/ini_setting.ini", pp, error
  185. end
  186. end
  187. end
  188. describe 'path parameter' do
  189. [
  190. "#{tmpdir}/one.ini",
  191. "#{tmpdir}/two.ini",
  192. "#{tmpdir}/three.ini",
  193. ].each do |path|
  194. context "path => #{path}" do
  195. pp = <<-EOS
  196. ini_setting { 'path => #{path}':
  197. ensure => present,
  198. section => 'one',
  199. setting => 'two',
  200. value => 'three',
  201. path => '#{path}',
  202. }
  203. EOS
  204. it_behaves_like 'has_content', path, pp, "[one]\ntwo = three"
  205. end
  206. end
  207. context "path => foo" do
  208. pp = <<-EOS
  209. ini_setting { 'path => foo':
  210. ensure => present,
  211. section => 'one',
  212. setting => 'two',
  213. value => 'three',
  214. path => 'foo',
  215. }
  216. EOS
  217. it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/
  218. end
  219. end
  220. describe 'key_val_separator parameter' do
  221. {
  222. "" => "two = three",
  223. "key_val_separator => '='," => "two=three",
  224. "key_val_separator => ' = '," => "two = three",
  225. }.each do |parameter, content|
  226. context "with \"#{parameter}\" makes \"#{content}\"" do
  227. pp = <<-EOS
  228. ini_setting { "with #{parameter} makes #{content}":
  229. ensure => present,
  230. section => 'one',
  231. setting => 'two',
  232. value => 'three',
  233. path => "#{tmpdir}/key_val_separator.ini",
  234. #{parameter}
  235. }
  236. EOS
  237. it_behaves_like 'has_content', "#{tmpdir}/key_val_separator.ini", pp, content
  238. end
  239. end
  240. {
  241. "key_val_separator => ''," => /must contain exactly one/,
  242. "key_val_separator => ','," => /must contain exactly one/,
  243. "key_val_separator => ' '," => /must contain exactly one/,
  244. "key_val_separator => ' == '," => /must contain exactly one/,
  245. }.each do |parameter, error|
  246. context "with \"#{parameter}\" raises \"#{error}\"" do
  247. pp = <<-EOS
  248. ini_setting { "with #{parameter} raises #{error}":
  249. ensure => present,
  250. section => 'one',
  251. setting => 'two',
  252. value => 'three',
  253. path => "#{tmpdir}/key_val_separator.ini",
  254. #{parameter}
  255. }
  256. EOS
  257. it_behaves_like 'has_error', "#{tmpdir}/key_val_separator.ini", pp, error
  258. end
  259. end
  260. end
  261. end