ruby_spec.rb 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 == 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 == [: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. EOS
  121. }
  122. it "should add a missing setting to the correct section" do
  123. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  124. :setting => 'yahoo', :value => 'yippee'))
  125. provider = described_class.new(resource)
  126. provider.exists?.should be_nil
  127. provider.create
  128. validate_file(<<-EOS
  129. # This is a comment
  130. [section1]
  131. ; This is also a comment
  132. foo=foovalue
  133. bar = barvalue
  134. master = true
  135. [section2]
  136. foo= foovalue2
  137. baz=bazvalue
  138. url = http://192.168.1.1:8080
  139. yahoo = yippee
  140. [section:sub]
  141. subby=bar
  142. #another comment
  143. ; yet another comment
  144. EOS
  145. )
  146. end
  147. it "should add a missing setting to the correct section with colon" do
  148. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  149. :section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
  150. provider = described_class.new(resource)
  151. provider.exists?.should be_nil
  152. provider.create
  153. validate_file(<<-EOS
  154. # This is a comment
  155. [section1]
  156. ; This is also a comment
  157. foo=foovalue
  158. bar = barvalue
  159. master = true
  160. [section2]
  161. foo= foovalue2
  162. baz=bazvalue
  163. url = http://192.168.1.1:8080
  164. [section:sub]
  165. subby=bar
  166. #another comment
  167. ; yet another comment
  168. yahoo = yippee
  169. EOS
  170. )
  171. end
  172. it "should modify an existing setting with a different value" do
  173. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  174. :setting => 'baz', :value => 'bazvalue2'))
  175. provider = described_class.new(resource)
  176. provider.exists?.should == 'bazvalue'
  177. provider.value=('bazvalue2')
  178. validate_file(<<-EOS
  179. # This is a comment
  180. [section1]
  181. ; This is also a comment
  182. foo=foovalue
  183. bar = barvalue
  184. master = true
  185. [section2]
  186. foo= foovalue2
  187. baz=bazvalue2
  188. url = http://192.168.1.1:8080
  189. [section:sub]
  190. subby=bar
  191. #another comment
  192. ; yet another comment
  193. EOS
  194. )
  195. end
  196. it "should modify an existing setting with a different value - with colon in section" do
  197. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  198. :section => 'section:sub', :setting => 'subby', :value => 'foo'))
  199. provider = described_class.new(resource)
  200. provider.exists?.should == 'bar'
  201. provider.value.should == 'bar'
  202. provider.value=('foo')
  203. validate_file(<<-EOS
  204. # This is a comment
  205. [section1]
  206. ; This is also a comment
  207. foo=foovalue
  208. bar = barvalue
  209. master = true
  210. [section2]
  211. foo= foovalue2
  212. baz=bazvalue
  213. url = http://192.168.1.1:8080
  214. [section:sub]
  215. subby=foo
  216. #another comment
  217. ; yet another comment
  218. EOS
  219. )
  220. end
  221. it "should be able to handle settings with non alphanumbering settings " do
  222. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  223. :setting => 'url', :value => 'http://192.168.0.1:8080'))
  224. provider = described_class.new(resource)
  225. provider.exists?.should == 'http://192.168.1.1:8080'
  226. provider.value.should == 'http://192.168.1.1:8080'
  227. provider.value=('http://192.168.0.1:8080')
  228. validate_file( <<-EOS
  229. # This is a comment
  230. [section1]
  231. ; This is also a comment
  232. foo=foovalue
  233. bar = barvalue
  234. master = true
  235. [section2]
  236. foo= foovalue2
  237. baz=bazvalue
  238. url = http://192.168.0.1:8080
  239. [section:sub]
  240. subby=bar
  241. #another comment
  242. ; yet another comment
  243. EOS
  244. )
  245. end
  246. it "should recognize an existing setting with the specified value" do
  247. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  248. :setting => 'baz', :value => 'bazvalue'))
  249. provider = described_class.new(resource)
  250. provider.exists?.should == 'bazvalue'
  251. end
  252. it "should add a new section if the section does not exist" do
  253. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  254. :section => "section3", :setting => 'huzzah', :value => 'shazaam'))
  255. provider = described_class.new(resource)
  256. provider.exists?.should be_nil
  257. provider.create
  258. validate_file(<<-EOS
  259. # This is a comment
  260. [section1]
  261. ; This is also a comment
  262. foo=foovalue
  263. bar = barvalue
  264. master = true
  265. [section2]
  266. foo= foovalue2
  267. baz=bazvalue
  268. url = http://192.168.1.1:8080
  269. [section:sub]
  270. subby=bar
  271. #another comment
  272. ; yet another comment
  273. [section3]
  274. huzzah = shazaam
  275. EOS
  276. )
  277. end
  278. it "should add a new section if the section does not exist - with colon" do
  279. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  280. :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
  281. provider = described_class.new(resource)
  282. provider.exists?.should be_nil
  283. provider.create
  284. validate_file(<<-EOS
  285. # This is a comment
  286. [section1]
  287. ; This is also a comment
  288. foo=foovalue
  289. bar = barvalue
  290. master = true
  291. [section2]
  292. foo= foovalue2
  293. baz=bazvalue
  294. url = http://192.168.1.1:8080
  295. [section:sub]
  296. subby=bar
  297. #another comment
  298. ; yet another comment
  299. [section:subsection]
  300. huzzah = shazaam
  301. EOS
  302. )
  303. end
  304. it "should add a new section if no sections exists" do
  305. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  306. :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
  307. provider = described_class.new(resource)
  308. provider.exists?.should be_nil
  309. provider.create
  310. validate_file("
  311. [section1]
  312. setting1 = hellowworld
  313. ", emptyfile)
  314. end
  315. it "should add a new section with colon if no sections exists" do
  316. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  317. :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
  318. provider = described_class.new(resource)
  319. provider.exists?.should be_nil
  320. provider.create
  321. validate_file("
  322. [section:subsection]
  323. setting1 = hellowworld
  324. ", emptyfile)
  325. end
  326. it "should be able to handle variables of any type" do
  327. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  328. :section => "section1", :setting => 'master', :value => true))
  329. provider = described_class.new(resource)
  330. provider.exists?.should == 'true'
  331. provider.value.should == 'true'
  332. end
  333. end
  334. context "when dealing with a global section" do
  335. let(:orig_content) {
  336. <<-EOS
  337. # This is a comment
  338. foo=blah
  339. [section2]
  340. foo = http://192.168.1.1:8080
  341. ; yet another comment
  342. EOS
  343. }
  344. it "should add a missing setting if it doesn't exist" do
  345. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  346. :section => '', :setting => 'bar', :value => 'yippee'))
  347. provider = described_class.new(resource)
  348. provider.exists?.should be_nil
  349. provider.create
  350. validate_file(<<-EOS
  351. # This is a comment
  352. foo=blah
  353. bar = yippee
  354. [section2]
  355. foo = http://192.168.1.1:8080
  356. ; yet another comment
  357. EOS
  358. )
  359. end
  360. it "should modify an existing setting with a different value" do
  361. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  362. :section => '', :setting => 'foo', :value => 'yippee'))
  363. provider = described_class.new(resource)
  364. provider.exists?.should == 'blah'
  365. provider.value.should == 'blah'
  366. provider.value=('yippee')
  367. validate_file(<<-EOS
  368. # This is a comment
  369. foo=yippee
  370. [section2]
  371. foo = http://192.168.1.1:8080
  372. ; yet another comment
  373. EOS
  374. )
  375. end
  376. it "should recognize an existing setting with the specified value" do
  377. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  378. :section => '', :setting => 'foo', :value => 'blah'))
  379. provider = described_class.new(resource)
  380. provider.exists?.should == 'blah'
  381. end
  382. end
  383. context "when the first line of the file is a section" do
  384. let(:orig_content) {
  385. <<-EOS
  386. [section2]
  387. foo = http://192.168.1.1:8080
  388. EOS
  389. }
  390. it "should be able to add a global setting" do
  391. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  392. :section => '', :setting => 'foo', :value => 'yippee'))
  393. provider = described_class.new(resource)
  394. provider.exists?.should be_nil
  395. provider.create
  396. validate_file(<<-EOS
  397. foo = yippee
  398. [section2]
  399. foo = http://192.168.1.1:8080
  400. EOS
  401. )
  402. end
  403. it "should modify an existing setting" do
  404. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  405. :section => 'section2', :setting => 'foo', :value => 'yippee'))
  406. provider = described_class.new(resource)
  407. provider.exists?.should == 'http://192.168.1.1:8080'
  408. provider.value.should == 'http://192.168.1.1:8080'
  409. provider.value=('yippee')
  410. validate_file(<<-EOS
  411. [section2]
  412. foo = yippee
  413. EOS
  414. )
  415. end
  416. it "should add a new setting" do
  417. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  418. :section => 'section2', :setting => 'bar', :value => 'baz'))
  419. provider = described_class.new(resource)
  420. provider.exists?.should be_nil
  421. provider.create
  422. validate_file(<<-EOS
  423. [section2]
  424. foo = http://192.168.1.1:8080
  425. bar = baz
  426. EOS
  427. )
  428. end
  429. end
  430. context "when overriding the separator" do
  431. let(:orig_content) {
  432. <<-EOS
  433. [section2]
  434. foo=bar
  435. EOS
  436. }
  437. it "should fail if the separator doesn't include an equals sign" do
  438. expect {
  439. Puppet::Type::Ini_setting.new(common_params.merge(
  440. :section => 'section2',
  441. :setting => 'foo',
  442. :value => 'yippee',
  443. :key_val_separator => '+'))
  444. }.to raise_error Puppet::Error, /must contain exactly one/
  445. end
  446. it "should fail if the separator includes more than one equals sign" do
  447. expect {
  448. Puppet::Type::Ini_setting.new(common_params.merge(
  449. :section => 'section2',
  450. :setting => 'foo',
  451. :value => 'yippee',
  452. :key_val_separator => ' = foo = '))
  453. }.to raise_error Puppet::Error, /must contain exactly one/
  454. end
  455. it "should modify an existing setting" do
  456. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  457. :section => 'section2',
  458. :setting => 'foo',
  459. :value => 'yippee',
  460. :key_val_separator => '='))
  461. provider = described_class.new(resource)
  462. provider.exists?.should == 'bar'
  463. provider.value.should == 'bar'
  464. provider.value=('yippee')
  465. validate_file(<<-EOS
  466. [section2]
  467. foo=yippee
  468. EOS
  469. )
  470. end
  471. it "should add a new setting" do
  472. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  473. :section => 'section2',
  474. :setting => 'bar',
  475. :value => 'baz',
  476. :key_val_separator => '='))
  477. provider = described_class.new(resource)
  478. provider.exists?.should be_nil
  479. provider.create
  480. validate_file(<<-EOS
  481. [section2]
  482. foo=bar
  483. bar=baz
  484. EOS
  485. )
  486. end
  487. end
  488. context "when ensuring that a setting is absent" do
  489. let(:orig_content) {
  490. <<-EOS
  491. [section1]
  492. ; This is also a comment
  493. foo=foovalue
  494. bar = barvalue
  495. master = true
  496. [section2]
  497. foo= foovalue2
  498. baz=bazvalue
  499. url = http://192.168.1.1:8080
  500. [section:sub]
  501. subby=bar
  502. #another comment
  503. ; yet another comment
  504. EOS
  505. }
  506. it "should remove a setting that exists" do
  507. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  508. :section => 'section1', :setting => 'foo', :ensure => 'absent'))
  509. provider = described_class.new(resource)
  510. provider.exists?.should be_true
  511. provider.destroy
  512. validate_file(<<-EOS
  513. [section1]
  514. ; This is also a comment
  515. bar = barvalue
  516. master = true
  517. [section2]
  518. foo= foovalue2
  519. baz=bazvalue
  520. url = http://192.168.1.1:8080
  521. [section:sub]
  522. subby=bar
  523. #another comment
  524. ; yet another comment
  525. EOS
  526. )
  527. end
  528. it "should do nothing for a setting that does not exist" do
  529. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  530. :section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
  531. provider = described_class.new(resource)
  532. provider.exists?.should be_nil
  533. provider.destroy
  534. validate_file(<<-EOS
  535. [section1]
  536. ; This is also a comment
  537. foo=foovalue
  538. bar = barvalue
  539. master = true
  540. [section2]
  541. foo= foovalue2
  542. baz=bazvalue
  543. url = http://192.168.1.1:8080
  544. [section:sub]
  545. subby=bar
  546. #another comment
  547. ; yet another comment
  548. EOS
  549. )
  550. end
  551. end
  552. context "when dealing with indentation in sections" do
  553. let(:orig_content) {
  554. <<-EOS
  555. # This is a comment
  556. [section1]
  557. ; This is also a comment
  558. foo=foovalue
  559. bar = barvalue
  560. master = true
  561. [section2]
  562. foo= foovalue2
  563. baz=bazvalue
  564. url = http://192.168.1.1:8080
  565. [section:sub]
  566. subby=bar
  567. #another comment
  568. fleezy = flam
  569. ; yet another comment
  570. EOS
  571. }
  572. it "should add a missing setting at the correct indentation when the header is aligned" do
  573. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  574. :section => 'section1', :setting => 'yahoo', :value => 'yippee'))
  575. provider = described_class.new(resource)
  576. provider.exists?.should be_nil
  577. provider.create
  578. validate_file(<<-EOS
  579. # This is a comment
  580. [section1]
  581. ; This is also a comment
  582. foo=foovalue
  583. bar = barvalue
  584. master = true
  585. yahoo = yippee
  586. [section2]
  587. foo= foovalue2
  588. baz=bazvalue
  589. url = http://192.168.1.1:8080
  590. [section:sub]
  591. subby=bar
  592. #another comment
  593. fleezy = flam
  594. ; yet another comment
  595. EOS
  596. )
  597. end
  598. it "should update an existing setting at the correct indentation when the header is aligned" do
  599. resource = Puppet::Type::Ini_setting.new(
  600. common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2'))
  601. provider = described_class.new(resource)
  602. provider.exists?.should be_true
  603. provider.create
  604. validate_file(<<-EOS
  605. # This is a comment
  606. [section1]
  607. ; This is also a comment
  608. foo=foovalue
  609. bar = barvalue2
  610. master = true
  611. [section2]
  612. foo= foovalue2
  613. baz=bazvalue
  614. url = http://192.168.1.1:8080
  615. [section:sub]
  616. subby=bar
  617. #another comment
  618. fleezy = flam
  619. ; yet another comment
  620. EOS
  621. )
  622. end
  623. it "should add a missing setting at the correct indentation when the header is not aligned" do
  624. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  625. :section => 'section2', :setting => 'yahoo', :value => 'yippee'))
  626. provider = described_class.new(resource)
  627. provider.exists?.should be_nil
  628. provider.create
  629. validate_file(<<-EOS
  630. # This is a comment
  631. [section1]
  632. ; This is also a comment
  633. foo=foovalue
  634. bar = barvalue
  635. master = true
  636. [section2]
  637. foo= foovalue2
  638. baz=bazvalue
  639. url = http://192.168.1.1:8080
  640. yahoo = yippee
  641. [section:sub]
  642. subby=bar
  643. #another comment
  644. fleezy = flam
  645. ; yet another comment
  646. EOS
  647. )
  648. end
  649. it "should update an existing setting at the correct indentation when the header is not aligned" do
  650. resource = Puppet::Type::Ini_setting.new(
  651. common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue2'))
  652. provider = described_class.new(resource)
  653. provider.exists?.should be_true
  654. provider.create
  655. validate_file(<<-EOS
  656. # This is a comment
  657. [section1]
  658. ; This is also a comment
  659. foo=foovalue
  660. bar = barvalue
  661. master = true
  662. [section2]
  663. foo= foovalue2
  664. baz=bazvalue2
  665. url = http://192.168.1.1:8080
  666. [section:sub]
  667. subby=bar
  668. #another comment
  669. fleezy = flam
  670. ; yet another comment
  671. EOS
  672. )
  673. end
  674. it "should add a missing setting at the min indentation when the section is not aligned" do
  675. resource = Puppet::Type::Ini_setting.new(
  676. common_params.merge(:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
  677. provider = described_class.new(resource)
  678. provider.exists?.should be_nil
  679. provider.create
  680. validate_file(<<-EOS
  681. # This is a comment
  682. [section1]
  683. ; This is also a comment
  684. foo=foovalue
  685. bar = barvalue
  686. master = true
  687. [section2]
  688. foo= foovalue2
  689. baz=bazvalue
  690. url = http://192.168.1.1:8080
  691. [section:sub]
  692. subby=bar
  693. #another comment
  694. fleezy = flam
  695. ; yet another comment
  696. yahoo = yippee
  697. EOS
  698. )
  699. end
  700. it "should update an existing setting at the previous indentation when the section is not aligned" do
  701. resource = Puppet::Type::Ini_setting.new(
  702. common_params.merge(:section => 'section:sub', :setting => 'fleezy', :value => 'flam2'))
  703. provider = described_class.new(resource)
  704. provider.exists?.should be_true
  705. provider.create
  706. validate_file(<<-EOS
  707. # This is a comment
  708. [section1]
  709. ; This is also a comment
  710. foo=foovalue
  711. bar = barvalue
  712. master = true
  713. [section2]
  714. foo= foovalue2
  715. baz=bazvalue
  716. url = http://192.168.1.1:8080
  717. [section:sub]
  718. subby=bar
  719. #another comment
  720. fleezy = flam2
  721. ; yet another comment
  722. EOS
  723. )
  724. end
  725. end
  726. context "when dealing settings that have a commented version present" do
  727. let(:orig_content) {
  728. <<-EOS
  729. [section1]
  730. # foo=foovalue
  731. bar=barvalue
  732. foo = foovalue2
  733. [section2]
  734. # foo = foovalue
  735. ;bar=barvalue
  736. blah = blah
  737. EOS
  738. }
  739. it "should add a new setting below a commented version of that setting" do
  740. resource = Puppet::Type::Ini_setting.new(
  741. common_params.merge(:section => 'section2', :setting => 'foo', :value => 'foo3'))
  742. provider = described_class.new(resource)
  743. provider.exists?.should be_false
  744. provider.create
  745. validate_file(<<-EOS
  746. [section1]
  747. # foo=foovalue
  748. bar=barvalue
  749. foo = foovalue2
  750. [section2]
  751. # foo = foovalue
  752. foo = foo3
  753. ;bar=barvalue
  754. blah = blah
  755. EOS
  756. )
  757. end
  758. it "should update an existing setting in place, even if there is a commented version of that setting" do
  759. resource = Puppet::Type::Ini_setting.new(
  760. common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foo3'))
  761. provider = described_class.new(resource)
  762. provider.exists?.should be_true
  763. provider.create
  764. validate_file(<<-EOS
  765. [section1]
  766. # foo=foovalue
  767. bar=barvalue
  768. foo = foo3
  769. [section2]
  770. # foo = foovalue
  771. ;bar=barvalue
  772. blah = blah
  773. EOS
  774. )
  775. end
  776. it "should add a new setting below a commented version of that setting, respecting semicolons as comments" do
  777. resource = Puppet::Type::Ini_setting.new(
  778. common_params.merge(:section => 'section2', :setting => 'bar', :value => 'bar3'))
  779. provider = described_class.new(resource)
  780. provider.exists?.should be_false
  781. provider.create
  782. validate_file(<<-EOS
  783. [section1]
  784. # foo=foovalue
  785. bar=barvalue
  786. foo = foovalue2
  787. [section2]
  788. # foo = foovalue
  789. ;bar=barvalue
  790. bar=bar3
  791. blah = blah
  792. EOS
  793. )
  794. end
  795. context 'when a section only contains comments' do
  796. let(:orig_content) {
  797. <<-EOS
  798. [section1]
  799. # foo=foovalue
  800. # bar=bar2
  801. EOS
  802. }
  803. it 'should be able to add a new setting when a section contains only comments' do
  804. resource = Puppet::Type::Ini_setting.new(
  805. common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foovalue2')
  806. )
  807. provider = described_class.new(resource)
  808. provider.exists?.should be_false
  809. provider.create
  810. validate_file(<<-EOS
  811. [section1]
  812. # foo=foovalue
  813. foo=foovalue2
  814. # bar=bar2
  815. EOS
  816. )
  817. end
  818. it 'should be able to add a new setting when it matches a commented out line other than the first one' do
  819. resource = Puppet::Type::Ini_setting.new(
  820. common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2')
  821. )
  822. provider = described_class.new(resource)
  823. provider.exists?.should be_false
  824. provider.create
  825. validate_file(<<-EOS
  826. [section1]
  827. # foo=foovalue
  828. # bar=bar2
  829. bar=barvalue2
  830. EOS
  831. )
  832. end
  833. end
  834. end
  835. end