ini_setting_spec.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #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' do
  61. apply_manifest(pp, :catch_failures => true)
  62. apply_manifest(pp, :catch_changes => true)
  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. if fact('osfamily') == 'Darwin'
  75. shell("echo \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  76. else
  77. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  78. end
  79. end
  80. pp = <<-EOS
  81. ini_setting { 'ensure => absent for key/value':
  82. ensure => absent,
  83. path => "#{tmpdir}/ini_setting.ini",
  84. section => 'one',
  85. setting => 'two',
  86. value => 'three',
  87. }
  88. EOS
  89. it 'applies the manifest twice' do
  90. apply_manifest(pp, :catch_failures => true)
  91. apply_manifest(pp, :catch_changes => true)
  92. end
  93. describe file("#{tmpdir}/ini_setting.ini") do
  94. it { should be_file }
  95. it { should contain('four = five') }
  96. it { should contain('[one]') }
  97. it { should_not contain('two = three') }
  98. end
  99. end
  100. context '=> absent for section', :pending => "cannot ensure absent on a section" do
  101. before :all do
  102. if fact('osfamily') == 'Darwin'
  103. shell("echo \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  104. else
  105. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  106. end
  107. end
  108. after :all do
  109. shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  110. shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  111. end
  112. pp = <<-EOS
  113. ini_setting { 'ensure => absent for section':
  114. ensure => absent,
  115. path => "#{tmpdir}/ini_setting.ini",
  116. section => 'one',
  117. }
  118. EOS
  119. it 'applies the manifest twice' do
  120. apply_manifest(pp, :catch_failures => true)
  121. apply_manifest(pp, :catch_changes => true)
  122. end
  123. describe file("#{tmpdir}/ini_setting.ini") do
  124. it { should be_file }
  125. it { should contain('four = five') }
  126. it { should_not contain('[one]') }
  127. it { should_not contain('two = three') }
  128. end
  129. end
  130. context '=> absent for global' do
  131. before :all do
  132. if fact('osfamily') == 'Darwin'
  133. shell("echo \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  134. else
  135. shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
  136. end
  137. end
  138. after :all do
  139. shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  140. shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0,1,2])
  141. end
  142. pp = <<-EOS
  143. ini_setting { 'ensure => absent for global':
  144. ensure => absent,
  145. path => "#{tmpdir}/ini_setting.ini",
  146. section => '',
  147. setting => 'four',
  148. value => 'five',
  149. }
  150. EOS
  151. it 'applies the manifest twice' do
  152. apply_manifest(pp, :catch_failures => true)
  153. apply_manifest(pp, :catch_changes => true)
  154. end
  155. describe file("#{tmpdir}/ini_setting.ini") do
  156. it { should be_file }
  157. it { should_not contain('four = five') }
  158. it { should contain('[one]') }
  159. it { should contain('two = three') }
  160. end
  161. end
  162. end
  163. describe 'section, setting, value parameters' do
  164. {
  165. "section => 'test', setting => 'foo', value => 'bar'," => "[test]\nfoo = bar",
  166. "section => 'more', setting => 'baz', value => 'quux'," => "[more]\nbaz = quux",
  167. "section => '', setting => 'top', value => 'level'," => "top = level",
  168. }.each do |parameter_list, content|
  169. context parameter_list do
  170. pp = <<-EOS
  171. ini_setting { "#{parameter_list}":
  172. ensure => present,
  173. path => "#{tmpdir}/ini_setting.ini",
  174. #{parameter_list}
  175. }
  176. EOS
  177. it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, content
  178. end
  179. end
  180. {
  181. "section => 'test'," => /setting is a required.+value is a required/,
  182. "setting => 'foo', value => 'bar'," => /section is a required/,
  183. "section => 'test', setting => 'foo'," => /value is a required/,
  184. "section => 'test', value => 'bar'," => /setting is a required/,
  185. "value => 'bar'," => /section is a required.+setting is a required/,
  186. "setting => 'foo'," => /section is a required.+value is a required/,
  187. }.each do |parameter_list, error|
  188. context parameter_list, :pending => 'no error checking yet' do
  189. pp = <<-EOS
  190. ini_setting { "#{parameter_list}":
  191. ensure => present,
  192. path => "#{tmpdir}/ini_setting.ini",
  193. #{parameter_list}
  194. }
  195. EOS
  196. it_behaves_like 'has_error', "#{tmpdir}/ini_setting.ini", pp, error
  197. end
  198. end
  199. end
  200. describe 'path parameter' do
  201. [
  202. "#{tmpdir}/one.ini",
  203. "#{tmpdir}/two.ini",
  204. "#{tmpdir}/three.ini",
  205. ].each do |path|
  206. context "path => #{path}" do
  207. pp = <<-EOS
  208. ini_setting { 'path => #{path}':
  209. ensure => present,
  210. section => 'one',
  211. setting => 'two',
  212. value => 'three',
  213. path => '#{path}',
  214. }
  215. EOS
  216. it_behaves_like 'has_content', path, pp, "[one]\ntwo = three"
  217. end
  218. end
  219. context "path => foo" do
  220. pp = <<-EOS
  221. ini_setting { 'path => foo':
  222. ensure => present,
  223. section => 'one',
  224. setting => 'two',
  225. value => 'three',
  226. path => 'foo',
  227. }
  228. EOS
  229. it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/
  230. end
  231. end
  232. describe 'key_val_separator parameter' do
  233. {
  234. "" => "two = three",
  235. "key_val_separator => '='," => "two=three",
  236. "key_val_separator => ' = '," => "two = three",
  237. }.each do |parameter, content|
  238. context "with \"#{parameter}\" makes \"#{content}\"" do
  239. pp = <<-EOS
  240. ini_setting { "with #{parameter} makes #{content}":
  241. ensure => present,
  242. section => 'one',
  243. setting => 'two',
  244. value => 'three',
  245. path => "#{tmpdir}/key_val_separator.ini",
  246. #{parameter}
  247. }
  248. EOS
  249. it_behaves_like 'has_content', "#{tmpdir}/key_val_separator.ini", pp, content
  250. end
  251. end
  252. {
  253. "key_val_separator => ''," => /must contain exactly one/,
  254. "key_val_separator => ','," => /must contain exactly one/,
  255. "key_val_separator => ' '," => /must contain exactly one/,
  256. "key_val_separator => ' == '," => /must contain exactly one/,
  257. }.each do |parameter, error|
  258. context "with \"#{parameter}\" raises \"#{error}\"" do
  259. pp = <<-EOS
  260. ini_setting { "with #{parameter} raises #{error}":
  261. ensure => present,
  262. section => 'one',
  263. setting => 'two',
  264. value => 'three',
  265. path => "#{tmpdir}/key_val_separator.ini",
  266. #{parameter}
  267. }
  268. EOS
  269. it_behaves_like 'has_error', "#{tmpdir}/key_val_separator.ini", pp, error
  270. end
  271. end
  272. end
  273. end