ruby_spec.rb 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. require 'spec_helper'
  2. require 'puppet'
  3. provider_class = Puppet::Type.type(:ini_setting).provider(:ruby)
  4. describe provider_class do
  5. include PuppetlabsSpec::Files
  6. let(:tmpfile) { tmpfilename("ini_setting_test") }
  7. let(:emptyfile) { tmpfilename("ini_setting_test_empty") }
  8. let(:common_params) { {
  9. :title => 'ini_setting_ensure_present_test',
  10. :path => tmpfile,
  11. :section => 'section2',
  12. } }
  13. def validate_file(expected_content,tmpfile = tmpfile)
  14. File.read(tmpfile).should == expected_content
  15. end
  16. before :each do
  17. File.open(tmpfile, 'w') do |fh|
  18. fh.write(orig_content)
  19. end
  20. File.open(emptyfile, 'w') do |fh|
  21. fh.write("")
  22. end
  23. end
  24. context 'when calling instances' do
  25. let :orig_content do
  26. ''
  27. end
  28. it 'should fail when file path is not set' do
  29. expect {
  30. provider_class.instances
  31. }.to raise_error(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded')
  32. end
  33. context 'when file path is set by a child class' do
  34. it 'should return [] when file is empty' do
  35. child_one = Class.new(provider_class) do
  36. def self.file_path
  37. emptyfile
  38. end
  39. end
  40. child_one.stubs(:file_path).returns(emptyfile)
  41. child_one.instances.should == []
  42. end
  43. it 'should override the provider instances file_path' do
  44. child_two = Class.new(provider_class) do
  45. def self.file_path
  46. '/some/file/path'
  47. end
  48. end
  49. resource = Puppet::Type::Ini_setting.new(common_params)
  50. provider = child_two.new(resource)
  51. provider.file_path.should == '/some/file/path'
  52. end
  53. context 'when file has contecnts' do
  54. let(:orig_content) {
  55. <<-EOS
  56. # This is a comment
  57. [section1]
  58. ; This is also a comment
  59. foo=foovalue
  60. bar = barvalue
  61. master = true
  62. [section2]
  63. foo= foovalue2
  64. baz=bazvalue
  65. url = http://192.168.1.1:8080
  66. [section:sub]
  67. subby=bar
  68. #another comment
  69. ; yet another comment
  70. EOS
  71. }
  72. it 'should be able to parse the results' do
  73. child_three = Class.new(provider_class) do
  74. def self.file_path
  75. '/some/file/path'
  76. end
  77. end
  78. child_three.stubs(:file_path).returns(tmpfile)
  79. child_three.instances.size.should eq(7)
  80. expected_array = [
  81. {:name => 'section1/foo', :value => 'foovalue' },
  82. {:name => 'section1/bar', :value => 'barvalue' },
  83. {:name => 'section1/master', :value => 'true' },
  84. {:name => 'section2/foo', :value => 'foovalue2' },
  85. {:name => 'section2/baz', :value => 'bazvalue' },
  86. {:name => 'section2/url', :value => 'http://192.168.1.1:8080' },
  87. {:name => 'section:sub/subby', :value => 'bar' }
  88. ]
  89. real_array = []
  90. ensure_array = []
  91. child_three.instances.each do |x|
  92. prop_hash = x.instance_variable_get(:@property_hash)
  93. ensure_value = prop_hash.delete(:ensure)
  94. ensure_array.push(ensure_value)
  95. real_array.push(prop_hash)
  96. end
  97. ensure_array.uniq.should eq([:present])
  98. ((real_array - expected_array) && (expected_array - real_array)).should == []
  99. end
  100. end
  101. end
  102. end
  103. context "when ensuring that a setting is present" do
  104. let(:orig_content) {
  105. <<-EOS
  106. # This is a comment
  107. [section1]
  108. ; This is also a comment
  109. foo=foovalue
  110. bar = barvalue
  111. master = true
  112. [section2]
  113. foo= foovalue2
  114. baz=bazvalue
  115. url = http://192.168.1.1:8080
  116. [section:sub]
  117. subby=bar
  118. #another comment
  119. ; yet another comment
  120. -nonstandard-
  121. shoes = purple
  122. EOS
  123. }
  124. it "should add a missing setting to the correct section" do
  125. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  126. :setting => 'yahoo', :value => 'yippee'))
  127. provider = described_class.new(resource)
  128. provider.exists?.should be false
  129. provider.create
  130. validate_file(<<-EOS
  131. # This is a comment
  132. [section1]
  133. ; This is also a comment
  134. foo=foovalue
  135. bar = barvalue
  136. master = true
  137. [section2]
  138. foo= foovalue2
  139. baz=bazvalue
  140. url = http://192.168.1.1:8080
  141. yahoo = yippee
  142. [section:sub]
  143. subby=bar
  144. #another comment
  145. ; yet another comment
  146. -nonstandard-
  147. shoes = purple
  148. EOS
  149. )
  150. end
  151. it "should add a missing setting to the correct section with pre/suffix" do
  152. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  153. :section => 'nonstandard',
  154. :setting => 'yahoo', :value => 'yippee',
  155. :section_prefix => '-', :section_suffix => '-'))
  156. provider = described_class.new(resource)
  157. provider.exists?.should be false
  158. provider.create
  159. validate_file(<<-EOS
  160. # This is a comment
  161. [section1]
  162. ; This is also a comment
  163. foo=foovalue
  164. bar = barvalue
  165. master = true
  166. [section2]
  167. foo= foovalue2
  168. baz=bazvalue
  169. url = http://192.168.1.1:8080
  170. [section:sub]
  171. subby=bar
  172. #another comment
  173. ; yet another comment
  174. -nonstandard-
  175. shoes = purple
  176. yahoo = yippee
  177. EOS
  178. )
  179. end
  180. it "should add a missing setting to the correct section with colon" do
  181. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  182. :section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
  183. provider = described_class.new(resource)
  184. provider.exists?.should be false
  185. provider.create
  186. validate_file(<<-EOS
  187. # This is a comment
  188. [section1]
  189. ; This is also a comment
  190. foo=foovalue
  191. bar = barvalue
  192. master = true
  193. [section2]
  194. foo= foovalue2
  195. baz=bazvalue
  196. url = http://192.168.1.1:8080
  197. [section:sub]
  198. subby=bar
  199. #another comment
  200. ; yet another comment
  201. -nonstandard-
  202. shoes = purple
  203. yahoo = yippee
  204. EOS
  205. )
  206. end
  207. it "should modify an existing setting with a different value" do
  208. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  209. :setting => 'baz', :value => 'bazvalue2'))
  210. provider = described_class.new(resource)
  211. provider.exists?.should be true
  212. provider.value=('bazvalue2')
  213. validate_file(<<-EOS
  214. # This is a comment
  215. [section1]
  216. ; This is also a comment
  217. foo=foovalue
  218. bar = barvalue
  219. master = true
  220. [section2]
  221. foo= foovalue2
  222. baz=bazvalue2
  223. url = http://192.168.1.1:8080
  224. [section:sub]
  225. subby=bar
  226. #another comment
  227. ; yet another comment
  228. -nonstandard-
  229. shoes = purple
  230. EOS
  231. )
  232. end
  233. it "should modify an existing setting with pre/suffix with a different value" do
  234. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  235. :section => 'nonstandard',
  236. :setting => 'shoes', :value => 'orange',
  237. :section_prefix => '-', :section_suffix => '-' ))
  238. provider = described_class.new(resource)
  239. provider.exists?.should be true
  240. provider.value=('orange')
  241. validate_file(<<-EOS
  242. # This is a comment
  243. [section1]
  244. ; This is also a comment
  245. foo=foovalue
  246. bar = barvalue
  247. master = true
  248. [section2]
  249. foo= foovalue2
  250. baz=bazvalue
  251. url = http://192.168.1.1:8080
  252. [section:sub]
  253. subby=bar
  254. #another comment
  255. ; yet another comment
  256. -nonstandard-
  257. shoes = orange
  258. EOS
  259. )
  260. end
  261. it "should modify an existing setting with a different value - with colon in section" do
  262. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  263. :section => 'section:sub', :setting => 'subby', :value => 'foo'))
  264. provider = described_class.new(resource)
  265. provider.exists?.should be true
  266. provider.value.should eq('bar')
  267. provider.value=('foo')
  268. validate_file(<<-EOS
  269. # This is a comment
  270. [section1]
  271. ; This is also a comment
  272. foo=foovalue
  273. bar = barvalue
  274. master = true
  275. [section2]
  276. foo= foovalue2
  277. baz=bazvalue
  278. url = http://192.168.1.1:8080
  279. [section:sub]
  280. subby=foo
  281. #another comment
  282. ; yet another comment
  283. -nonstandard-
  284. shoes = purple
  285. EOS
  286. )
  287. end
  288. it "should be able to handle settings with non alphanumbering settings " do
  289. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  290. :setting => 'url', :value => 'http://192.168.0.1:8080'))
  291. provider = described_class.new(resource)
  292. provider.exists?.should be true
  293. provider.value.should eq('http://192.168.1.1:8080')
  294. provider.value=('http://192.168.0.1:8080')
  295. validate_file( <<-EOS
  296. # This is a comment
  297. [section1]
  298. ; This is also a comment
  299. foo=foovalue
  300. bar = barvalue
  301. master = true
  302. [section2]
  303. foo= foovalue2
  304. baz=bazvalue
  305. url = http://192.168.0.1:8080
  306. [section:sub]
  307. subby=bar
  308. #another comment
  309. ; yet another comment
  310. -nonstandard-
  311. shoes = purple
  312. EOS
  313. )
  314. end
  315. it "should be able to handle settings with pre/suffix with non alphanumbering settings " do
  316. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  317. :section => 'nonstandard',
  318. :setting => 'shoes', :value => 'http://192.168.0.1:8080',
  319. :section_prefix => '-', :section_suffix => '-' ))
  320. provider = described_class.new(resource)
  321. provider.exists?.should be true
  322. provider.value.should eq('purple')
  323. provider.value=('http://192.168.0.1:8080')
  324. validate_file( <<-EOS
  325. # This is a comment
  326. [section1]
  327. ; This is also a comment
  328. foo=foovalue
  329. bar = barvalue
  330. master = true
  331. [section2]
  332. foo= foovalue2
  333. baz=bazvalue
  334. url = http://192.168.1.1:8080
  335. [section:sub]
  336. subby=bar
  337. #another comment
  338. ; yet another comment
  339. -nonstandard-
  340. shoes = http://192.168.0.1:8080
  341. EOS
  342. )
  343. end
  344. it "should recognize an existing setting with the specified value" do
  345. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  346. :setting => 'baz', :value => 'bazvalue'))
  347. provider = described_class.new(resource)
  348. provider.exists?.should be true
  349. end
  350. it "should recognize an existing setting with pre/suffix with the specified value" do
  351. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  352. :section => 'nonstandard',
  353. :setting => 'shoes', :value => 'purple',
  354. :section_prefix => '-', :section_suffix => '-' ))
  355. provider = described_class.new(resource)
  356. provider.exists?.should be true
  357. end
  358. it "should add a new section if the section does not exist" do
  359. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  360. :section => "section3", :setting => 'huzzah', :value => 'shazaam'))
  361. provider = described_class.new(resource)
  362. provider.exists?.should be false
  363. provider.create
  364. validate_file(<<-EOS
  365. # This is a comment
  366. [section1]
  367. ; This is also a comment
  368. foo=foovalue
  369. bar = barvalue
  370. master = true
  371. [section2]
  372. foo= foovalue2
  373. baz=bazvalue
  374. url = http://192.168.1.1:8080
  375. [section:sub]
  376. subby=bar
  377. #another comment
  378. ; yet another comment
  379. -nonstandard-
  380. shoes = purple
  381. [section3]
  382. huzzah = shazaam
  383. EOS
  384. )
  385. end
  386. it "should add a new section with pre/suffix if the section does not exist" do
  387. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  388. :section => "section3", :setting => 'huzzah', :value => 'shazaam',
  389. :section_prefix => '-', :section_suffix => '-' ))
  390. provider = described_class.new(resource)
  391. provider.exists?.should be false
  392. provider.create
  393. validate_file(<<-EOS
  394. # This is a comment
  395. [section1]
  396. ; This is also a comment
  397. foo=foovalue
  398. bar = barvalue
  399. master = true
  400. [section2]
  401. foo= foovalue2
  402. baz=bazvalue
  403. url = http://192.168.1.1:8080
  404. [section:sub]
  405. subby=bar
  406. #another comment
  407. ; yet another comment
  408. -nonstandard-
  409. shoes = purple
  410. -section3-
  411. huzzah = shazaam
  412. EOS
  413. )
  414. end
  415. it "should add a new section if the section does not exist - with colon" do
  416. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  417. :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
  418. provider = described_class.new(resource)
  419. provider.exists?.should be false
  420. provider.create
  421. validate_file(<<-EOS
  422. # This is a comment
  423. [section1]
  424. ; This is also a comment
  425. foo=foovalue
  426. bar = barvalue
  427. master = true
  428. [section2]
  429. foo= foovalue2
  430. baz=bazvalue
  431. url = http://192.168.1.1:8080
  432. [section:sub]
  433. subby=bar
  434. #another comment
  435. ; yet another comment
  436. -nonstandard-
  437. shoes = purple
  438. [section:subsection]
  439. huzzah = shazaam
  440. EOS
  441. )
  442. end
  443. it "should add a new section with pre/suffix if the section does not exist - with colon" do
  444. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  445. :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam',
  446. :section_prefix => '-', :section_suffix => '-' ))
  447. provider = described_class.new(resource)
  448. provider.exists?.should be false
  449. provider.create
  450. validate_file(<<-EOS
  451. # This is a comment
  452. [section1]
  453. ; This is also a comment
  454. foo=foovalue
  455. bar = barvalue
  456. master = true
  457. [section2]
  458. foo= foovalue2
  459. baz=bazvalue
  460. url = http://192.168.1.1:8080
  461. [section:sub]
  462. subby=bar
  463. #another comment
  464. ; yet another comment
  465. -nonstandard-
  466. shoes = purple
  467. -section:subsection-
  468. huzzah = shazaam
  469. EOS
  470. )
  471. end
  472. it "should add a new section if no sections exists" do
  473. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  474. :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
  475. provider = described_class.new(resource)
  476. provider.exists?.should be false
  477. provider.create
  478. validate_file("
  479. [section1]
  480. setting1 = hellowworld
  481. ", emptyfile)
  482. end
  483. it "should add a new section with pre/suffix if no sections exists" do
  484. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  485. :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
  486. :section_prefix => '-', :section_suffix => '-' ))
  487. provider = described_class.new(resource)
  488. provider.exists?.should be false
  489. provider.create
  490. validate_file("
  491. -section1-
  492. setting1 = hellowworld
  493. ", emptyfile)
  494. end
  495. it "should add a new section with colon if no sections exists" do
  496. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  497. :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
  498. provider = described_class.new(resource)
  499. provider.exists?.should be false
  500. provider.create
  501. validate_file("
  502. [section:subsection]
  503. setting1 = hellowworld
  504. ", emptyfile)
  505. end
  506. it "should add a new section with pre/suffix with colon if no sections exists" do
  507. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  508. :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
  509. :section_prefix => '-', :section_suffix => '-' ))
  510. provider = described_class.new(resource)
  511. provider.exists?.should be false
  512. provider.create
  513. validate_file("
  514. -section:subsection-
  515. setting1 = hellowworld
  516. ", emptyfile)
  517. end
  518. it "should be able to handle variables of any type" do
  519. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  520. :section => "section1", :setting => 'master', :value => true))
  521. provider = described_class.new(resource)
  522. provider.exists?.should be true
  523. provider.value.should == 'true'
  524. end
  525. end
  526. context "when dealing with a global section" do
  527. let(:orig_content) {
  528. <<-EOS
  529. # This is a comment
  530. foo=blah
  531. [section2]
  532. foo = http://192.168.1.1:8080
  533. ; yet another comment
  534. EOS
  535. }
  536. it "should add a missing setting if it doesn't exist" do
  537. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  538. :section => '', :setting => 'bar', :value => 'yippee'))
  539. provider = described_class.new(resource)
  540. provider.exists?.should be false
  541. provider.create
  542. validate_file(<<-EOS
  543. # This is a comment
  544. foo=blah
  545. bar = yippee
  546. [section2]
  547. foo = http://192.168.1.1:8080
  548. ; yet another comment
  549. EOS
  550. )
  551. end
  552. it "should modify an existing setting with a different value" do
  553. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  554. :section => '', :setting => 'foo', :value => 'yippee'))
  555. provider = described_class.new(resource)
  556. provider.exists?.should be true
  557. provider.value.should eq('blah')
  558. provider.value=('yippee')
  559. validate_file(<<-EOS
  560. # This is a comment
  561. foo=yippee
  562. [section2]
  563. foo = http://192.168.1.1:8080
  564. ; yet another comment
  565. EOS
  566. )
  567. end
  568. it "should recognize an existing setting with the specified value" do
  569. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  570. :section => '', :setting => 'foo', :value => 'blah'))
  571. provider = described_class.new(resource)
  572. provider.exists?.should be true
  573. end
  574. end
  575. context "when the first line of the file is a section" do
  576. let(:orig_content) {
  577. <<-EOS
  578. [section2]
  579. foo = http://192.168.1.1:8080
  580. EOS
  581. }
  582. it "should be able to add a global setting" do
  583. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  584. :section => '', :setting => 'foo', :value => 'yippee'))
  585. provider = described_class.new(resource)
  586. provider.exists?.should be false
  587. provider.create
  588. validate_file(<<-EOS
  589. foo = yippee
  590. [section2]
  591. foo = http://192.168.1.1:8080
  592. EOS
  593. )
  594. end
  595. it "should modify an existing setting" do
  596. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  597. :section => 'section2', :setting => 'foo', :value => 'yippee'))
  598. provider = described_class.new(resource)
  599. provider.exists?.should be true
  600. provider.value.should eq('http://192.168.1.1:8080')
  601. provider.value=('yippee')
  602. validate_file(<<-EOS
  603. [section2]
  604. foo = yippee
  605. EOS
  606. )
  607. end
  608. it "should add a new setting" do
  609. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  610. :section => 'section2', :setting => 'bar', :value => 'baz'))
  611. provider = described_class.new(resource)
  612. provider.exists?.should be false
  613. provider.create
  614. validate_file(<<-EOS
  615. [section2]
  616. foo = http://192.168.1.1:8080
  617. bar = baz
  618. EOS
  619. )
  620. end
  621. end
  622. context "when overriding the separator" do
  623. let(:orig_content) {
  624. <<-EOS
  625. [section2]
  626. foo=bar
  627. EOS
  628. }
  629. it "should modify an existing setting" do
  630. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  631. :section => 'section2',
  632. :setting => 'foo',
  633. :value => 'yippee',
  634. :key_val_separator => '='))
  635. provider = described_class.new(resource)
  636. provider.exists?.should be true
  637. provider.value.should eq('bar')
  638. provider.value=('yippee')
  639. validate_file(<<-EOS
  640. [section2]
  641. foo=yippee
  642. EOS
  643. )
  644. end
  645. end
  646. context "when overriding the separator to something other than =" do
  647. let(:orig_content) {
  648. <<-EOS
  649. [section2]
  650. foo: bar
  651. EOS
  652. }
  653. it "should modify an existing setting" do
  654. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  655. :section => 'section2',
  656. :setting => 'foo',
  657. :value => 'yippee',
  658. :key_val_separator => ': '))
  659. provider = described_class.new(resource)
  660. provider.exists?.should be true
  661. provider.value.should eq('bar')
  662. provider.value=('yippee')
  663. validate_file(<<-EOS
  664. [section2]
  665. foo: yippee
  666. EOS
  667. )
  668. end
  669. it "should add a new setting" do
  670. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  671. :section => 'section2',
  672. :setting => 'bar',
  673. :value => 'baz',
  674. :key_val_separator => ': '))
  675. provider = described_class.new(resource)
  676. provider.exists?.should be false
  677. provider.create
  678. validate_file(<<-EOS
  679. [section2]
  680. foo: bar
  681. bar: baz
  682. EOS
  683. )
  684. end
  685. end
  686. context "when overriding the separator to a space" do
  687. let(:orig_content) {
  688. <<-EOS
  689. [section2]
  690. foo bar
  691. EOS
  692. }
  693. it "should modify an existing setting" do
  694. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  695. :section => 'section2',
  696. :setting => 'foo',
  697. :value => 'yippee',
  698. :key_val_separator => ' '))
  699. provider = described_class.new(resource)
  700. provider.exists?.should be true
  701. provider.value.should eq('bar')
  702. provider.value=('yippee')
  703. validate_file(<<-EOS
  704. [section2]
  705. foo yippee
  706. EOS
  707. )
  708. end
  709. it "should add a new setting" do
  710. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  711. :section => 'section2',
  712. :setting => 'bar',
  713. :value => 'baz',
  714. :key_val_separator => ' '))
  715. provider = described_class.new(resource)
  716. provider.exists?.should be false
  717. provider.create
  718. validate_file(<<-EOS
  719. [section2]
  720. foo bar
  721. bar baz
  722. EOS
  723. )
  724. end
  725. end
  726. context "when ensuring that a setting is absent" do
  727. let(:orig_content) {
  728. <<-EOS
  729. [section1]
  730. ; This is also a comment
  731. foo=foovalue
  732. bar = barvalue
  733. master = true
  734. [section2]
  735. foo= foovalue2
  736. baz=bazvalue
  737. url = http://192.168.1.1:8080
  738. [section3]
  739. # com = ment
  740. uncom = ment
  741. [section4]
  742. uncom = ment
  743. [section:sub]
  744. subby=bar
  745. #another comment
  746. ; yet another comment
  747. -nonstandard-
  748. shoes = purple
  749. EOS
  750. }
  751. it "should remove a setting that exists" do
  752. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  753. :section => 'section1', :setting => 'foo', :ensure => 'absent'))
  754. provider = described_class.new(resource)
  755. provider.exists?.should be true
  756. provider.destroy
  757. validate_file(<<-EOS
  758. [section1]
  759. ; This is also a comment
  760. bar = barvalue
  761. master = true
  762. [section2]
  763. foo= foovalue2
  764. baz=bazvalue
  765. url = http://192.168.1.1:8080
  766. [section3]
  767. # com = ment
  768. uncom = ment
  769. [section4]
  770. uncom = ment
  771. [section:sub]
  772. subby=bar
  773. #another comment
  774. ; yet another comment
  775. -nonstandard-
  776. shoes = purple
  777. EOS
  778. )
  779. end
  780. it "should remove a setting with pre/suffix that exists" do
  781. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  782. :section => 'nonstandard', :setting => 'shoes', :ensure => 'absent',
  783. :section_prefix => '-', :section_suffix => '-' ))
  784. provider = described_class.new(resource)
  785. provider.exists?.should be true
  786. provider.destroy
  787. validate_file(<<-EOS
  788. [section1]
  789. ; This is also a comment
  790. foo=foovalue
  791. bar = barvalue
  792. master = true
  793. [section2]
  794. foo= foovalue2
  795. baz=bazvalue
  796. url = http://192.168.1.1:8080
  797. [section3]
  798. # com = ment
  799. uncom = ment
  800. [section4]
  801. uncom = ment
  802. [section:sub]
  803. subby=bar
  804. #another comment
  805. ; yet another comment
  806. EOS
  807. )
  808. end
  809. it "should do nothing for a setting that does not exist" do
  810. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  811. :section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
  812. provider = described_class.new(resource)
  813. provider.exists?.should be false
  814. provider.destroy
  815. validate_file(<<-EOS
  816. [section1]
  817. ; This is also a comment
  818. foo=foovalue
  819. bar = barvalue
  820. master = true
  821. [section2]
  822. foo= foovalue2
  823. baz=bazvalue
  824. url = http://192.168.1.1:8080
  825. [section3]
  826. # com = ment
  827. uncom = ment
  828. [section4]
  829. uncom = ment
  830. [section:sub]
  831. subby=bar
  832. #another comment
  833. ; yet another comment
  834. -nonstandard-
  835. shoes = purple
  836. EOS
  837. )
  838. end
  839. it "should do nothing for a setting with pre/suffix that does not exist" do
  840. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  841. :section => 'nonstandard', :setting => 'foo', :ensure => 'absent',
  842. :section_prefix => '-', :section_suffix => '-' ))
  843. provider = described_class.new(resource)
  844. provider.exists?.should be false
  845. provider.destroy
  846. validate_file(<<-EOS
  847. [section1]
  848. ; This is also a comment
  849. foo=foovalue
  850. bar = barvalue
  851. master = true
  852. [section2]
  853. foo= foovalue2
  854. baz=bazvalue
  855. url = http://192.168.1.1:8080
  856. [section3]
  857. # com = ment
  858. uncom = ment
  859. [section4]
  860. uncom = ment
  861. [section:sub]
  862. subby=bar
  863. #another comment
  864. ; yet another comment
  865. -nonstandard-
  866. shoes = purple
  867. EOS
  868. )
  869. end
  870. it "does not remove a section when the last uncommented setting is removed if there are comments" do
  871. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  872. :section => 'section3',
  873. :setting => 'uncom',
  874. :ensure => 'absent',
  875. ))
  876. provider = described_class.new(resource)
  877. provider.exists?.should be true
  878. provider.destroy
  879. validate_file(<<-EOS
  880. [section1]
  881. ; This is also a comment
  882. foo=foovalue
  883. bar = barvalue
  884. master = true
  885. [section2]
  886. foo= foovalue2
  887. baz=bazvalue
  888. url = http://192.168.1.1:8080
  889. [section3]
  890. # com = ment
  891. [section4]
  892. uncom = ment
  893. [section:sub]
  894. subby=bar
  895. #another comment
  896. ; yet another comment
  897. -nonstandard-
  898. shoes = purple
  899. EOS
  900. )
  901. end
  902. it "removes the section when removing the last line in the section" do
  903. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  904. :section => 'section4',
  905. :setting => 'uncom',
  906. :ensure => 'absent',
  907. ))
  908. provider = described_class.new(resource)
  909. provider.exists?.should be true
  910. provider.destroy
  911. validate_file(<<-EOS
  912. [section1]
  913. ; This is also a comment
  914. foo=foovalue
  915. bar = barvalue
  916. master = true
  917. [section2]
  918. foo= foovalue2
  919. baz=bazvalue
  920. url = http://192.168.1.1:8080
  921. [section3]
  922. # com = ment
  923. uncom = ment
  924. [section:sub]
  925. subby=bar
  926. #another comment
  927. ; yet another comment
  928. -nonstandard-
  929. shoes = purple
  930. EOS
  931. )
  932. end
  933. end
  934. context "when dealing with indentation in sections" do
  935. let(:orig_content) {
  936. <<-EOS
  937. # This is a comment
  938. [section1]
  939. ; This is also a comment
  940. foo=foovalue
  941. bar = barvalue
  942. master = true
  943. [section2]
  944. foo= foovalue2
  945. baz=bazvalue
  946. url = http://192.168.1.1:8080
  947. [section:sub]
  948. subby=bar
  949. #another comment
  950. fleezy = flam
  951. ; yet another comment
  952. EOS
  953. }
  954. it "should add a missing setting at the correct indentation when the header is aligned" do
  955. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  956. :section => 'section1', :setting => 'yahoo', :value => 'yippee'))
  957. provider = described_class.new(resource)
  958. provider.exists?.should be false
  959. provider.create
  960. validate_file(<<-EOS
  961. # This is a comment
  962. [section1]
  963. ; This is also a comment
  964. foo=foovalue
  965. bar = barvalue
  966. master = true
  967. yahoo = yippee
  968. [section2]
  969. foo= foovalue2
  970. baz=bazvalue
  971. url = http://192.168.1.1:8080
  972. [section:sub]
  973. subby=bar
  974. #another comment
  975. fleezy = flam
  976. ; yet another comment
  977. EOS
  978. )
  979. end
  980. it "should update an existing setting at the correct indentation when the header is aligned" do
  981. resource = Puppet::Type::Ini_setting.new(
  982. common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2'))
  983. provider = described_class.new(resource)
  984. provider.exists?.should be true
  985. provider.create
  986. validate_file(<<-EOS
  987. # This is a comment
  988. [section1]
  989. ; This is also a comment
  990. foo=foovalue
  991. bar = barvalue2
  992. master = true
  993. [section2]
  994. foo= foovalue2
  995. baz=bazvalue
  996. url = http://192.168.1.1:8080
  997. [section:sub]
  998. subby=bar
  999. #another comment
  1000. fleezy = flam
  1001. ; yet another comment
  1002. EOS
  1003. )
  1004. end
  1005. it "should add a missing setting at the correct indentation when the header is not aligned" do
  1006. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  1007. :section => 'section2', :setting => 'yahoo', :value => 'yippee'))
  1008. provider = described_class.new(resource)
  1009. provider.exists?.should be false
  1010. provider.create
  1011. validate_file(<<-EOS
  1012. # This is a comment
  1013. [section1]
  1014. ; This is also a comment
  1015. foo=foovalue
  1016. bar = barvalue
  1017. master = true
  1018. [section2]
  1019. foo= foovalue2
  1020. baz=bazvalue
  1021. url = http://192.168.1.1:8080
  1022. yahoo = yippee
  1023. [section:sub]
  1024. subby=bar
  1025. #another comment
  1026. fleezy = flam
  1027. ; yet another comment
  1028. EOS
  1029. )
  1030. end
  1031. it "should update an existing setting at the correct indentation when the header is not aligned" do
  1032. resource = Puppet::Type::Ini_setting.new(
  1033. common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue2'))
  1034. provider = described_class.new(resource)
  1035. provider.exists?.should be true
  1036. provider.create
  1037. validate_file(<<-EOS
  1038. # This is a comment
  1039. [section1]
  1040. ; This is also a comment
  1041. foo=foovalue
  1042. bar = barvalue
  1043. master = true
  1044. [section2]
  1045. foo= foovalue2
  1046. baz=bazvalue2
  1047. url = http://192.168.1.1:8080
  1048. [section:sub]
  1049. subby=bar
  1050. #another comment
  1051. fleezy = flam
  1052. ; yet another comment
  1053. EOS
  1054. )
  1055. end
  1056. it "should add a missing setting at the min indentation when the section is not aligned" do
  1057. resource = Puppet::Type::Ini_setting.new(
  1058. common_params.merge(:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
  1059. provider = described_class.new(resource)
  1060. provider.exists?.should be false
  1061. provider.create
  1062. validate_file(<<-EOS
  1063. # This is a comment
  1064. [section1]
  1065. ; This is also a comment
  1066. foo=foovalue
  1067. bar = barvalue
  1068. master = true
  1069. [section2]
  1070. foo= foovalue2
  1071. baz=bazvalue
  1072. url = http://192.168.1.1:8080
  1073. [section:sub]
  1074. subby=bar
  1075. #another comment
  1076. fleezy = flam
  1077. ; yet another comment
  1078. yahoo = yippee
  1079. EOS
  1080. )
  1081. end
  1082. it "should update an existing setting at the previous indentation when the section is not aligned" do
  1083. resource = Puppet::Type::Ini_setting.new(
  1084. common_params.merge(:section => 'section:sub', :setting => 'fleezy', :value => 'flam2'))
  1085. provider = described_class.new(resource)
  1086. provider.exists?.should be true
  1087. provider.create
  1088. validate_file(<<-EOS
  1089. # This is a comment
  1090. [section1]
  1091. ; This is also a comment
  1092. foo=foovalue
  1093. bar = barvalue
  1094. master = true
  1095. [section2]
  1096. foo= foovalue2
  1097. baz=bazvalue
  1098. url = http://192.168.1.1:8080
  1099. [section:sub]
  1100. subby=bar
  1101. #another comment
  1102. fleezy = flam2
  1103. ; yet another comment
  1104. EOS
  1105. )
  1106. end
  1107. end
  1108. context "when dealing settings that have a commented version present" do
  1109. let(:orig_content) {
  1110. <<-EOS
  1111. [section1]
  1112. # foo=foovalue
  1113. bar=barvalue
  1114. foo = foovalue2
  1115. [section2]
  1116. # foo = foovalue
  1117. ;bar=barvalue
  1118. blah = blah
  1119. #baz=
  1120. EOS
  1121. }
  1122. it "should add a new setting below a commented version of that setting" do
  1123. resource = Puppet::Type::Ini_setting.new(
  1124. common_params.merge(:section => 'section2', :setting => 'foo', :value => 'foo3'))
  1125. provider = described_class.new(resource)
  1126. provider.exists?.should be false
  1127. provider.create
  1128. validate_file(<<-EOS
  1129. [section1]
  1130. # foo=foovalue
  1131. bar=barvalue
  1132. foo = foovalue2
  1133. [section2]
  1134. # foo = foovalue
  1135. foo = foo3
  1136. ;bar=barvalue
  1137. blah = blah
  1138. #baz=
  1139. EOS
  1140. )
  1141. end
  1142. it "should update an existing setting in place, even if there is a commented version of that setting" do
  1143. resource = Puppet::Type::Ini_setting.new(
  1144. common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foo3'))
  1145. provider = described_class.new(resource)
  1146. provider.exists?.should be true
  1147. provider.create
  1148. validate_file(<<-EOS
  1149. [section1]
  1150. # foo=foovalue
  1151. bar=barvalue
  1152. foo = foo3
  1153. [section2]
  1154. # foo = foovalue
  1155. ;bar=barvalue
  1156. blah = blah
  1157. #baz=
  1158. EOS
  1159. )
  1160. end
  1161. it "should add a new setting below a commented version of that setting, respecting semicolons as comments" do
  1162. resource = Puppet::Type::Ini_setting.new(
  1163. common_params.merge(:section => 'section2', :setting => 'bar', :value => 'bar3'))
  1164. provider = described_class.new(resource)
  1165. provider.exists?.should be false
  1166. provider.create
  1167. validate_file(<<-EOS
  1168. [section1]
  1169. # foo=foovalue
  1170. bar=barvalue
  1171. foo = foovalue2
  1172. [section2]
  1173. # foo = foovalue
  1174. ;bar=barvalue
  1175. bar=bar3
  1176. blah = blah
  1177. #baz=
  1178. EOS
  1179. )
  1180. end
  1181. it "should add a new setting below an empty commented version of that setting" do
  1182. resource = Puppet::Type::Ini_setting.new(
  1183. common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue'))
  1184. provider = described_class.new(resource)
  1185. provider.exists?.should be false
  1186. provider.create
  1187. validate_file(<<-EOS
  1188. [section1]
  1189. # foo=foovalue
  1190. bar=barvalue
  1191. foo = foovalue2
  1192. [section2]
  1193. # foo = foovalue
  1194. ;bar=barvalue
  1195. blah = blah
  1196. #baz=
  1197. baz=bazvalue
  1198. EOS
  1199. )
  1200. end
  1201. context 'when a section only contains comments' do
  1202. let(:orig_content) {
  1203. <<-EOS
  1204. [section1]
  1205. # foo=foovalue
  1206. # bar=bar2
  1207. EOS
  1208. }
  1209. it 'should be able to add a new setting when a section contains only comments' do
  1210. resource = Puppet::Type::Ini_setting.new(
  1211. common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foovalue2')
  1212. )
  1213. provider = described_class.new(resource)
  1214. provider.exists?.should be false
  1215. provider.create
  1216. validate_file(<<-EOS
  1217. [section1]
  1218. # foo=foovalue
  1219. foo=foovalue2
  1220. # bar=bar2
  1221. EOS
  1222. )
  1223. end
  1224. it 'should be able to add a new setting when it matches a commented out line other than the first one' do
  1225. resource = Puppet::Type::Ini_setting.new(
  1226. common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2')
  1227. )
  1228. provider = described_class.new(resource)
  1229. provider.exists?.should be false
  1230. provider.create
  1231. validate_file(<<-EOS
  1232. [section1]
  1233. # foo=foovalue
  1234. # bar=bar2
  1235. bar=barvalue2
  1236. EOS
  1237. )
  1238. end
  1239. end
  1240. context "when sections have spaces and dashes" do
  1241. let(:orig_content) {
  1242. <<-EOS
  1243. # This is a comment
  1244. [section - one]
  1245. ; This is also a comment
  1246. foo=foovalue
  1247. bar = barvalue
  1248. master = true
  1249. [section - two]
  1250. foo= foovalue2
  1251. baz=bazvalue
  1252. url = http://192.168.1.1:8080
  1253. [section:sub]
  1254. subby=bar
  1255. #another comment
  1256. ; yet another comment
  1257. EOS
  1258. }
  1259. it "should add a missing setting to the correct section" do
  1260. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  1261. :section => 'section - two', :setting => 'yahoo', :value => 'yippee'))
  1262. provider = described_class.new(resource)
  1263. provider.exists?.should be false
  1264. provider.create
  1265. validate_file(<<-EOS
  1266. # This is a comment
  1267. [section - one]
  1268. ; This is also a comment
  1269. foo=foovalue
  1270. bar = barvalue
  1271. master = true
  1272. [section - two]
  1273. foo= foovalue2
  1274. baz=bazvalue
  1275. url = http://192.168.1.1:8080
  1276. yahoo = yippee
  1277. [section:sub]
  1278. subby=bar
  1279. #another comment
  1280. ; yet another comment
  1281. EOS
  1282. )
  1283. end
  1284. end
  1285. end
  1286. context "when sections have spaces and quotations" do
  1287. let(:orig_content) do
  1288. <<-EOS
  1289. [branch "master"]
  1290. remote = origin
  1291. merge = refs/heads/master
  1292. [alias]
  1293. to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
  1294. [branch "production"]
  1295. remote = origin
  1296. merge = refs/heads/production
  1297. EOS
  1298. end
  1299. it "should add a missing setting to the correct section" do
  1300. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  1301. :section => 'alias',
  1302. :setting => 'foo',
  1303. :value => 'bar'
  1304. ))
  1305. provider = described_class.new(resource)
  1306. provider.exists?.should be false
  1307. provider.create
  1308. validate_file(<<-EOS
  1309. [branch "master"]
  1310. remote = origin
  1311. merge = refs/heads/master
  1312. [alias]
  1313. to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
  1314. foo = bar
  1315. [branch "production"]
  1316. remote = origin
  1317. merge = refs/heads/production
  1318. EOS
  1319. )
  1320. end
  1321. end
  1322. end