ruby_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. require 'spec_helper'
  2. require 'puppet'
  3. provider_class = Puppet::Type.type(:ini_subsetting).provider(:ruby)
  4. describe provider_class do
  5. include PuppetlabsSpec::Files
  6. let(:tmpfile) { tmpfilename("ini_setting_test") }
  7. def validate_file(expected_content, tmpfile)
  8. expect(File.read(tmpfile)).to eq expected_content
  9. end
  10. before :each do
  11. File.open(tmpfile, 'w') do |fh|
  12. fh.write(orig_content)
  13. end
  14. end
  15. context "when ensuring that a subsetting is present" do
  16. let(:common_params) { {
  17. :title => 'ini_setting_ensure_present_test',
  18. :path => tmpfile,
  19. :section => '',
  20. :key_val_separator => '=',
  21. :setting => 'JAVA_ARGS',
  22. } }
  23. let(:orig_content) {
  24. <<-EOS
  25. JAVA_ARGS="-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  26. EOS
  27. }
  28. it "should add a missing subsetting" do
  29. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  30. :subsetting => '-Xms', :value => '128m'))
  31. provider = described_class.new(resource)
  32. expect(provider.exists?).to be_nil
  33. provider.create
  34. expected_content = <<-EOS
  35. JAVA_ARGS="-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof -Xms128m"
  36. EOS
  37. validate_file(expected_content, tmpfile)
  38. end
  39. it 'should add a missing subsetting element at the beginning of the line' do
  40. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  41. :subsetting => '-Xms', :value => '128m', :insert_type => :start))
  42. provider = described_class.new(resource)
  43. expect(provider.exists?).to be_nil
  44. provider.create
  45. expected_content = <<-EOS
  46. JAVA_ARGS="-Xms128m -Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  47. EOS
  48. validate_file(expected_content, tmpfile)
  49. end
  50. it 'should add a missing subsetting element at the end of the line' do
  51. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  52. :subsetting => '-Xms', :value => '128m', :insert_type => :end))
  53. provider = described_class.new(resource)
  54. expect(provider.exists?).to be_nil
  55. provider.create
  56. expected_content = <<-EOS
  57. JAVA_ARGS="-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof -Xms128m"
  58. EOS
  59. validate_file(expected_content, tmpfile)
  60. end
  61. it 'should add a missing subsetting element after the given item' do
  62. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  63. :subsetting => '-Xms', :value => '128m', :insert_type => :after, :insert_value => '-Xmx'))
  64. provider = described_class.new(resource)
  65. expect(provider.exists?).to be_nil
  66. provider.create
  67. expected_content = <<-EOS
  68. JAVA_ARGS="-Xmx192m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  69. EOS
  70. validate_file(expected_content, tmpfile)
  71. end
  72. it 'should add a missing subsetting element before the given item' do
  73. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  74. :subsetting => '-Xms', :value => '128m', :insert_type => :before, :insert_value => '-Xmx'))
  75. provider = described_class.new(resource)
  76. expect(provider.exists?).to be_nil
  77. provider.create
  78. expected_content = <<-EOS
  79. JAVA_ARGS="-Xms128m -Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  80. EOS
  81. validate_file(expected_content, tmpfile)
  82. end
  83. it 'should add a missing subsetting element at the given index' do
  84. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  85. :subsetting => '-Xms', :value => '128m', :insert_type => :index, :insert_value => '1'))
  86. provider = described_class.new(resource)
  87. expect(provider.exists?).to be_nil
  88. provider.create
  89. expected_content = <<-EOS
  90. JAVA_ARGS="-Xmx192m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  91. EOS
  92. validate_file(expected_content, tmpfile)
  93. end
  94. it "should remove an existing subsetting" do
  95. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  96. :subsetting => '-Xmx'))
  97. provider = described_class.new(resource)
  98. expect(provider.exists?).to eq '192m'
  99. provider.destroy
  100. expected_content = <<-EOS
  101. JAVA_ARGS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  102. EOS
  103. validate_file(expected_content, tmpfile)
  104. end
  105. it 'should be able to remove several subsettings with the same name' do
  106. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  107. :subsetting => '-XX'))
  108. provider = described_class.new(resource)
  109. expect(provider.exists?).to eq ':+HeapDumpOnOutOfMemoryError'
  110. provider.destroy
  111. expected_content = <<-EOS
  112. JAVA_ARGS="-Xmx192m"
  113. EOS
  114. validate_file(expected_content, tmpfile)
  115. end
  116. it "should modify an existing subsetting" do
  117. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  118. :subsetting => '-Xmx', :value => '256m'))
  119. provider = described_class.new(resource)
  120. expect(provider.exists?).to eq '192m'
  121. provider.value=('256m')
  122. expected_content = <<-EOS
  123. JAVA_ARGS="-Xmx256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
  124. EOS
  125. validate_file(expected_content, tmpfile)
  126. end
  127. it 'should be able to modify several subsettings with the same name' do
  128. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  129. :subsetting => '-XX', :value => 'test'))
  130. provider = described_class.new(resource)
  131. expect(provider.exists?).to eq ':+HeapDumpOnOutOfMemoryError'
  132. provider.value=('test')
  133. expected_content = <<-EOS
  134. JAVA_ARGS="-Xmx192m -XXtest -XXtest"
  135. EOS
  136. validate_file(expected_content, tmpfile)
  137. end
  138. end
  139. context "when working with subsettings in files with unquoted settings values" do
  140. let(:common_params) { {
  141. :title => 'ini_setting_ensure_present_test',
  142. :path => tmpfile,
  143. :section => 'master',
  144. :setting => 'reports',
  145. } }
  146. let(:orig_content) {
  147. <<-EOS
  148. [master]
  149. reports = http,foo
  150. EOS
  151. }
  152. it "should remove an existing subsetting" do
  153. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  154. :subsetting => 'http', :subsetting_separator => ','))
  155. provider = described_class.new(resource)
  156. expect(provider.exists?).to eq ''
  157. provider.destroy
  158. expected_content = <<-EOS
  159. [master]
  160. reports = foo
  161. EOS
  162. validate_file(expected_content, tmpfile)
  163. end
  164. it "should add a new subsetting when the 'parent' setting already exists" do
  165. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  166. :subsetting => 'puppetdb', :subsetting_separator => ','))
  167. provider = described_class.new(resource)
  168. expect(provider.exists?).to be_nil
  169. provider.value=('')
  170. expected_content = <<-EOS
  171. [master]
  172. reports = http,foo,puppetdb
  173. EOS
  174. validate_file(expected_content, tmpfile)
  175. end
  176. it "should add a new subsetting when the 'parent' setting does not already exist" do
  177. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  178. :setting => 'somenewsetting',
  179. :subsetting => 'puppetdb',
  180. :subsetting_separator => ','))
  181. provider = described_class.new(resource)
  182. expect(provider.exists?).to be_nil
  183. provider.value=('')
  184. expected_content = <<-EOS
  185. [master]
  186. reports = http,foo
  187. somenewsetting = puppetdb
  188. EOS
  189. validate_file(expected_content, tmpfile)
  190. end
  191. end
  192. context "when working with subsettings in files with use_exact_match" do
  193. let(:common_params) { {
  194. :title => 'ini_setting_ensure_present_test',
  195. :path => tmpfile,
  196. :section => 'master',
  197. :setting => 'reports',
  198. :use_exact_match => true,
  199. } }
  200. let(:orig_content) {
  201. <<-EOS
  202. [master]
  203. reports = http,foo
  204. EOS
  205. }
  206. it "should add a new subsetting when the 'parent' setting already exists" do
  207. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  208. :subsetting => 'fo', :subsetting_separator => ','))
  209. provider = described_class.new(resource)
  210. provider.value=('')
  211. expected_content = <<-EOS
  212. [master]
  213. reports = http,foo,fo
  214. EOS
  215. validate_file(expected_content, tmpfile)
  216. end
  217. it "should not remove substring subsettings" do
  218. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  219. :subsetting => 'fo', :subsetting_separator => ','))
  220. provider = described_class.new(resource)
  221. provider.value=('')
  222. provider.destroy
  223. expected_content = <<-EOS
  224. [master]
  225. reports = http,foo
  226. EOS
  227. validate_file(expected_content, tmpfile)
  228. end
  229. end
  230. context 'when working with subsettings in files with subsetting_key_val_separator' do
  231. let(:common_params) { {
  232. :title => 'ini_setting_ensure_present_test',
  233. :path => tmpfile,
  234. :section => 'master',
  235. :setting => 'reports',
  236. :subsetting_separator => ',',
  237. :subsetting_key_val_separator => ':',
  238. } }
  239. let(:orig_content) {
  240. <<-EOS
  241. [master]
  242. reports = a:1,b:2
  243. EOS
  244. }
  245. it "should add a new subsetting when the 'parent' setting already exists" do
  246. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  247. :subsetting => 'c',
  248. :value => '3',
  249. ))
  250. provider = described_class.new(resource)
  251. provider.value=('3')
  252. expected_content = <<-EOS
  253. [master]
  254. reports = a:1,b:2,c:3
  255. EOS
  256. validate_file(expected_content, tmpfile)
  257. end
  258. it "should add a new subsetting when the 'parent' setting does not already exist" do
  259. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  260. :subsetting => 'c',
  261. :value => '3',
  262. :setting => 'somenewsetting',
  263. ))
  264. provider = described_class.new(resource)
  265. expect(provider.exists?).to be_nil
  266. provider.value=('3')
  267. expected_content = <<-EOS
  268. [master]
  269. reports = a:1,b:2
  270. somenewsetting = c:3
  271. EOS
  272. validate_file(expected_content, tmpfile)
  273. end
  274. it 'should be able to remove the existing subsetting' do
  275. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  276. :subsetting => 'b',
  277. ))
  278. provider = described_class.new(resource)
  279. expect(provider.exists?).to eq '2'
  280. provider.destroy
  281. expected_content = <<-EOS
  282. [master]
  283. reports = a:1
  284. EOS
  285. validate_file(expected_content, tmpfile)
  286. end
  287. it 'should be able to modify the existing subsetting' do
  288. resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
  289. :subsetting => 'b',
  290. :value => '5',
  291. ))
  292. provider = described_class.new(resource)
  293. expect(provider.exists?).to eq '2'
  294. provider.value=('5')
  295. expected_content = <<-EOS
  296. [master]
  297. reports = a:1,b:5
  298. EOS
  299. validate_file(expected_content, tmpfile)
  300. end
  301. end
  302. end