ruby_spec.rb 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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 a different boolean value" do
  234. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  235. :section => 'section1', :setting => 'master', :value => false))
  236. provider = described_class.new(resource)
  237. provider.exists?.should be true
  238. expect(Puppet::Transaction::ResourceHarness.new(nil).evaluate(provider.resource).out_of_sync).to eq(true)
  239. validate_file(<<-EOS
  240. # This is a comment
  241. [section1]
  242. ; This is also a comment
  243. foo=foovalue
  244. bar = barvalue
  245. master = false
  246. [section2]
  247. foo= foovalue2
  248. baz=bazvalue
  249. url = http://192.168.1.1:8080
  250. [section:sub]
  251. subby=bar
  252. #another comment
  253. ; yet another comment
  254. -nonstandard-
  255. shoes = purple
  256. EOS
  257. )
  258. end
  259. it "should modify an existing setting with pre/suffix with a different value" do
  260. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  261. :section => 'nonstandard',
  262. :setting => 'shoes', :value => 'orange',
  263. :section_prefix => '-', :section_suffix => '-' ))
  264. provider = described_class.new(resource)
  265. provider.exists?.should be true
  266. provider.value=('orange')
  267. validate_file(<<-EOS
  268. # This is a comment
  269. [section1]
  270. ; This is also a comment
  271. foo=foovalue
  272. bar = barvalue
  273. master = true
  274. [section2]
  275. foo= foovalue2
  276. baz=bazvalue
  277. url = http://192.168.1.1:8080
  278. [section:sub]
  279. subby=bar
  280. #another comment
  281. ; yet another comment
  282. -nonstandard-
  283. shoes = orange
  284. EOS
  285. )
  286. end
  287. it "should modify an existing setting with a different value - with colon in section" do
  288. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  289. :section => 'section:sub', :setting => 'subby', :value => 'foo'))
  290. provider = described_class.new(resource)
  291. provider.exists?.should be true
  292. provider.value.should eq('bar')
  293. provider.value=('foo')
  294. validate_file(<<-EOS
  295. # This is a comment
  296. [section1]
  297. ; This is also a comment
  298. foo=foovalue
  299. bar = barvalue
  300. master = true
  301. [section2]
  302. foo= foovalue2
  303. baz=bazvalue
  304. url = http://192.168.1.1:8080
  305. [section:sub]
  306. subby=foo
  307. #another comment
  308. ; yet another comment
  309. -nonstandard-
  310. shoes = purple
  311. EOS
  312. )
  313. end
  314. it "should be able to handle settings with non alphanumbering settings " do
  315. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  316. :setting => 'url', :value => 'http://192.168.0.1:8080'))
  317. provider = described_class.new(resource)
  318. provider.exists?.should be true
  319. provider.value.should eq('http://192.168.1.1:8080')
  320. provider.value=('http://192.168.0.1:8080')
  321. validate_file( <<-EOS
  322. # This is a comment
  323. [section1]
  324. ; This is also a comment
  325. foo=foovalue
  326. bar = barvalue
  327. master = true
  328. [section2]
  329. foo= foovalue2
  330. baz=bazvalue
  331. url = http://192.168.0.1:8080
  332. [section:sub]
  333. subby=bar
  334. #another comment
  335. ; yet another comment
  336. -nonstandard-
  337. shoes = purple
  338. EOS
  339. )
  340. end
  341. it "should be able to handle settings with pre/suffix with non alphanumbering settings " do
  342. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  343. :section => 'nonstandard',
  344. :setting => 'shoes', :value => 'http://192.168.0.1:8080',
  345. :section_prefix => '-', :section_suffix => '-' ))
  346. provider = described_class.new(resource)
  347. provider.exists?.should be true
  348. provider.value.should eq('purple')
  349. provider.value=('http://192.168.0.1:8080')
  350. validate_file( <<-EOS
  351. # This is a comment
  352. [section1]
  353. ; This is also a comment
  354. foo=foovalue
  355. bar = barvalue
  356. master = true
  357. [section2]
  358. foo= foovalue2
  359. baz=bazvalue
  360. url = http://192.168.1.1:8080
  361. [section:sub]
  362. subby=bar
  363. #another comment
  364. ; yet another comment
  365. -nonstandard-
  366. shoes = http://192.168.0.1:8080
  367. EOS
  368. )
  369. end
  370. it "should recognize an existing setting with the specified value" do
  371. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  372. :setting => 'baz', :value => 'bazvalue'))
  373. provider = described_class.new(resource)
  374. provider.exists?.should be true
  375. end
  376. it "should recognize an existing setting with pre/suffix with the specified value" do
  377. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  378. :section => 'nonstandard',
  379. :setting => 'shoes', :value => 'purple',
  380. :section_prefix => '-', :section_suffix => '-' ))
  381. provider = described_class.new(resource)
  382. provider.exists?.should be true
  383. end
  384. it "should add a new section if the section does not exist" do
  385. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  386. :section => "section3", :setting => 'huzzah', :value => 'shazaam'))
  387. provider = described_class.new(resource)
  388. provider.exists?.should be false
  389. provider.create
  390. validate_file(<<-EOS
  391. # This is a comment
  392. [section1]
  393. ; This is also a comment
  394. foo=foovalue
  395. bar = barvalue
  396. master = true
  397. [section2]
  398. foo= foovalue2
  399. baz=bazvalue
  400. url = http://192.168.1.1:8080
  401. [section:sub]
  402. subby=bar
  403. #another comment
  404. ; yet another comment
  405. -nonstandard-
  406. shoes = purple
  407. [section3]
  408. huzzah = shazaam
  409. EOS
  410. )
  411. end
  412. it "should add a new section with pre/suffix if the section does not exist" do
  413. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  414. :section => "section3", :setting => 'huzzah', :value => 'shazaam',
  415. :section_prefix => '-', :section_suffix => '-' ))
  416. provider = described_class.new(resource)
  417. provider.exists?.should be false
  418. provider.create
  419. validate_file(<<-EOS
  420. # This is a comment
  421. [section1]
  422. ; This is also a comment
  423. foo=foovalue
  424. bar = barvalue
  425. master = true
  426. [section2]
  427. foo= foovalue2
  428. baz=bazvalue
  429. url = http://192.168.1.1:8080
  430. [section:sub]
  431. subby=bar
  432. #another comment
  433. ; yet another comment
  434. -nonstandard-
  435. shoes = purple
  436. -section3-
  437. huzzah = shazaam
  438. EOS
  439. )
  440. end
  441. it "should add a new section if the section does not exist - with colon" do
  442. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  443. :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
  444. provider = described_class.new(resource)
  445. provider.exists?.should be false
  446. provider.create
  447. validate_file(<<-EOS
  448. # This is a comment
  449. [section1]
  450. ; This is also a comment
  451. foo=foovalue
  452. bar = barvalue
  453. master = true
  454. [section2]
  455. foo= foovalue2
  456. baz=bazvalue
  457. url = http://192.168.1.1:8080
  458. [section:sub]
  459. subby=bar
  460. #another comment
  461. ; yet another comment
  462. -nonstandard-
  463. shoes = purple
  464. [section:subsection]
  465. huzzah = shazaam
  466. EOS
  467. )
  468. end
  469. it "should add a new section with pre/suffix if the section does not exist - with colon" do
  470. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  471. :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam',
  472. :section_prefix => '-', :section_suffix => '-' ))
  473. provider = described_class.new(resource)
  474. provider.exists?.should be false
  475. provider.create
  476. validate_file(<<-EOS
  477. # This is a comment
  478. [section1]
  479. ; This is also a comment
  480. foo=foovalue
  481. bar = barvalue
  482. master = true
  483. [section2]
  484. foo= foovalue2
  485. baz=bazvalue
  486. url = http://192.168.1.1:8080
  487. [section:sub]
  488. subby=bar
  489. #another comment
  490. ; yet another comment
  491. -nonstandard-
  492. shoes = purple
  493. -section:subsection-
  494. huzzah = shazaam
  495. EOS
  496. )
  497. end
  498. it "should add a new section if no sections exists" do
  499. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  500. :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
  501. provider = described_class.new(resource)
  502. provider.exists?.should be false
  503. provider.create
  504. validate_file("
  505. [section1]
  506. setting1 = hellowworld
  507. ", emptyfile)
  508. end
  509. it "should add a new section with pre/suffix if no sections exists" do
  510. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  511. :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
  512. :section_prefix => '-', :section_suffix => '-' ))
  513. provider = described_class.new(resource)
  514. provider.exists?.should be false
  515. provider.create
  516. validate_file("
  517. -section1-
  518. setting1 = hellowworld
  519. ", emptyfile)
  520. end
  521. it "should add a new section with colon if no sections exists" do
  522. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  523. :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
  524. provider = described_class.new(resource)
  525. provider.exists?.should be false
  526. provider.create
  527. validate_file("
  528. [section:subsection]
  529. setting1 = hellowworld
  530. ", emptyfile)
  531. end
  532. it "should add a new section with pre/suffix with colon if no sections exists" do
  533. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  534. :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
  535. :section_prefix => '-', :section_suffix => '-' ))
  536. provider = described_class.new(resource)
  537. provider.exists?.should be false
  538. provider.create
  539. validate_file("
  540. -section:subsection-
  541. setting1 = hellowworld
  542. ", emptyfile)
  543. end
  544. it "should be able to handle variables of any type" do
  545. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  546. :section => "section1", :setting => 'master', :value => true))
  547. provider = described_class.new(resource)
  548. provider.exists?.should be true
  549. provider.value.should == 'true'
  550. end
  551. end
  552. context "when dealing with a global section" do
  553. let(:orig_content) {
  554. <<-EOS
  555. # This is a comment
  556. foo=blah
  557. [section2]
  558. foo = http://192.168.1.1:8080
  559. ; yet another comment
  560. EOS
  561. }
  562. it "should add a missing setting if it doesn't exist" do
  563. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  564. :section => '', :setting => 'bar', :value => 'yippee'))
  565. provider = described_class.new(resource)
  566. provider.exists?.should be false
  567. provider.create
  568. validate_file(<<-EOS
  569. # This is a comment
  570. foo=blah
  571. bar = yippee
  572. [section2]
  573. foo = http://192.168.1.1:8080
  574. ; yet another comment
  575. EOS
  576. )
  577. end
  578. it "should modify an existing setting with a different value" do
  579. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  580. :section => '', :setting => 'foo', :value => 'yippee'))
  581. provider = described_class.new(resource)
  582. provider.exists?.should be true
  583. provider.value.should eq('blah')
  584. provider.value=('yippee')
  585. validate_file(<<-EOS
  586. # This is a comment
  587. foo=yippee
  588. [section2]
  589. foo = http://192.168.1.1:8080
  590. ; yet another comment
  591. EOS
  592. )
  593. end
  594. it "should recognize an existing setting with the specified value" do
  595. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  596. :section => '', :setting => 'foo', :value => 'blah'))
  597. provider = described_class.new(resource)
  598. provider.exists?.should be true
  599. end
  600. end
  601. context "when the first line of the file is a section" do
  602. let(:orig_content) {
  603. <<-EOS
  604. [section2]
  605. foo = http://192.168.1.1:8080
  606. EOS
  607. }
  608. it "should be able to add a global setting" do
  609. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  610. :section => '', :setting => 'foo', :value => 'yippee'))
  611. provider = described_class.new(resource)
  612. provider.exists?.should be false
  613. provider.create
  614. validate_file(<<-EOS
  615. foo = yippee
  616. [section2]
  617. foo = http://192.168.1.1:8080
  618. EOS
  619. )
  620. end
  621. it "should modify an existing setting" do
  622. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  623. :section => 'section2', :setting => 'foo', :value => 'yippee'))
  624. provider = described_class.new(resource)
  625. provider.exists?.should be true
  626. provider.value.should eq('http://192.168.1.1:8080')
  627. provider.value=('yippee')
  628. validate_file(<<-EOS
  629. [section2]
  630. foo = yippee
  631. EOS
  632. )
  633. end
  634. it "should add a new setting" do
  635. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  636. :section => 'section2', :setting => 'bar', :value => 'baz'))
  637. provider = described_class.new(resource)
  638. provider.exists?.should be false
  639. provider.create
  640. validate_file(<<-EOS
  641. [section2]
  642. foo = http://192.168.1.1:8080
  643. bar = baz
  644. EOS
  645. )
  646. end
  647. end
  648. context "when overriding the separator" do
  649. let(:orig_content) {
  650. <<-EOS
  651. [section2]
  652. foo=bar
  653. EOS
  654. }
  655. it "should modify an existing setting" do
  656. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  657. :section => 'section2',
  658. :setting => 'foo',
  659. :value => 'yippee',
  660. :key_val_separator => '='))
  661. provider = described_class.new(resource)
  662. provider.exists?.should be true
  663. provider.value.should eq('bar')
  664. provider.value=('yippee')
  665. validate_file(<<-EOS
  666. [section2]
  667. foo=yippee
  668. EOS
  669. )
  670. end
  671. end
  672. context "when overriding the separator to something other than =" do
  673. let(:orig_content) {
  674. <<-EOS
  675. [section2]
  676. foo: bar
  677. EOS
  678. }
  679. it "should modify an existing setting" do
  680. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  681. :section => 'section2',
  682. :setting => 'foo',
  683. :value => 'yippee',
  684. :key_val_separator => ': '))
  685. provider = described_class.new(resource)
  686. provider.exists?.should be true
  687. provider.value.should eq('bar')
  688. provider.value=('yippee')
  689. validate_file(<<-EOS
  690. [section2]
  691. foo: yippee
  692. EOS
  693. )
  694. end
  695. it "should add a new setting" do
  696. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  697. :section => 'section2',
  698. :setting => 'bar',
  699. :value => 'baz',
  700. :key_val_separator => ': '))
  701. provider = described_class.new(resource)
  702. provider.exists?.should be false
  703. provider.create
  704. validate_file(<<-EOS
  705. [section2]
  706. foo: bar
  707. bar: baz
  708. EOS
  709. )
  710. end
  711. end
  712. context "when overriding the separator to a space" do
  713. let(:orig_content) {
  714. <<-EOS
  715. [section2]
  716. foo bar
  717. EOS
  718. }
  719. it "should modify an existing setting" do
  720. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  721. :section => 'section2',
  722. :setting => 'foo',
  723. :value => 'yippee',
  724. :key_val_separator => ' '))
  725. provider = described_class.new(resource)
  726. provider.exists?.should be true
  727. provider.value.should eq('bar')
  728. provider.value=('yippee')
  729. validate_file(<<-EOS
  730. [section2]
  731. foo yippee
  732. EOS
  733. )
  734. end
  735. it "should add a new setting" do
  736. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  737. :section => 'section2',
  738. :setting => 'bar',
  739. :value => 'baz',
  740. :key_val_separator => ' '))
  741. provider = described_class.new(resource)
  742. provider.exists?.should be false
  743. provider.create
  744. validate_file(<<-EOS
  745. [section2]
  746. foo bar
  747. bar baz
  748. EOS
  749. )
  750. end
  751. end
  752. context "when ensuring that a setting is absent" do
  753. let(:orig_content) {
  754. <<-EOS
  755. [section1]
  756. ; This is also a comment
  757. foo=foovalue
  758. bar = barvalue
  759. master = true
  760. [section2]
  761. foo= foovalue2
  762. baz=bazvalue
  763. url = http://192.168.1.1:8080
  764. [section3]
  765. # com = ment
  766. uncom = ment
  767. [section4]
  768. uncom = ment
  769. [section:sub]
  770. subby=bar
  771. #another comment
  772. ; yet another comment
  773. -nonstandard-
  774. shoes = purple
  775. EOS
  776. }
  777. it "should remove a setting that exists" do
  778. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  779. :section => 'section1', :setting => 'foo', :ensure => 'absent'))
  780. provider = described_class.new(resource)
  781. provider.exists?.should be true
  782. provider.destroy
  783. validate_file(<<-EOS
  784. [section1]
  785. ; This is also a comment
  786. bar = barvalue
  787. master = true
  788. [section2]
  789. foo= foovalue2
  790. baz=bazvalue
  791. url = http://192.168.1.1:8080
  792. [section3]
  793. # com = ment
  794. uncom = ment
  795. [section4]
  796. uncom = ment
  797. [section:sub]
  798. subby=bar
  799. #another comment
  800. ; yet another comment
  801. -nonstandard-
  802. shoes = purple
  803. EOS
  804. )
  805. end
  806. it "should remove a setting with pre/suffix that exists" do
  807. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  808. :section => 'nonstandard', :setting => 'shoes', :ensure => 'absent',
  809. :section_prefix => '-', :section_suffix => '-' ))
  810. provider = described_class.new(resource)
  811. provider.exists?.should be true
  812. provider.destroy
  813. validate_file(<<-EOS
  814. [section1]
  815. ; This is also a comment
  816. foo=foovalue
  817. bar = barvalue
  818. master = true
  819. [section2]
  820. foo= foovalue2
  821. baz=bazvalue
  822. url = http://192.168.1.1:8080
  823. [section3]
  824. # com = ment
  825. uncom = ment
  826. [section4]
  827. uncom = ment
  828. [section:sub]
  829. subby=bar
  830. #another comment
  831. ; yet another comment
  832. EOS
  833. )
  834. end
  835. it "should do nothing for a setting that does not exist" do
  836. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  837. :section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
  838. provider = described_class.new(resource)
  839. provider.exists?.should be false
  840. provider.destroy
  841. validate_file(<<-EOS
  842. [section1]
  843. ; This is also a comment
  844. foo=foovalue
  845. bar = barvalue
  846. master = true
  847. [section2]
  848. foo= foovalue2
  849. baz=bazvalue
  850. url = http://192.168.1.1:8080
  851. [section3]
  852. # com = ment
  853. uncom = ment
  854. [section4]
  855. uncom = ment
  856. [section:sub]
  857. subby=bar
  858. #another comment
  859. ; yet another comment
  860. -nonstandard-
  861. shoes = purple
  862. EOS
  863. )
  864. end
  865. it "should do nothing for a setting with pre/suffix that does not exist" do
  866. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  867. :section => 'nonstandard', :setting => 'foo', :ensure => 'absent',
  868. :section_prefix => '-', :section_suffix => '-' ))
  869. provider = described_class.new(resource)
  870. provider.exists?.should be false
  871. provider.destroy
  872. validate_file(<<-EOS
  873. [section1]
  874. ; This is also a comment
  875. foo=foovalue
  876. bar = barvalue
  877. master = true
  878. [section2]
  879. foo= foovalue2
  880. baz=bazvalue
  881. url = http://192.168.1.1:8080
  882. [section3]
  883. # com = ment
  884. uncom = ment
  885. [section4]
  886. uncom = ment
  887. [section:sub]
  888. subby=bar
  889. #another comment
  890. ; yet another comment
  891. -nonstandard-
  892. shoes = purple
  893. EOS
  894. )
  895. end
  896. it "does not remove a section when the last uncommented setting is removed if there are comments" do
  897. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  898. :section => 'section3',
  899. :setting => 'uncom',
  900. :ensure => 'absent',
  901. ))
  902. provider = described_class.new(resource)
  903. provider.exists?.should be true
  904. provider.destroy
  905. validate_file(<<-EOS
  906. [section1]
  907. ; This is also a comment
  908. foo=foovalue
  909. bar = barvalue
  910. master = true
  911. [section2]
  912. foo= foovalue2
  913. baz=bazvalue
  914. url = http://192.168.1.1:8080
  915. [section3]
  916. # com = ment
  917. [section4]
  918. uncom = ment
  919. [section:sub]
  920. subby=bar
  921. #another comment
  922. ; yet another comment
  923. -nonstandard-
  924. shoes = purple
  925. EOS
  926. )
  927. end
  928. it "removes the section when removing the last line in the section" do
  929. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  930. :section => 'section4',
  931. :setting => 'uncom',
  932. :ensure => 'absent',
  933. ))
  934. provider = described_class.new(resource)
  935. provider.exists?.should be true
  936. provider.destroy
  937. validate_file(<<-EOS
  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. [section3]
  948. # com = ment
  949. uncom = ment
  950. [section:sub]
  951. subby=bar
  952. #another comment
  953. ; yet another comment
  954. -nonstandard-
  955. shoes = purple
  956. EOS
  957. )
  958. end
  959. end
  960. context "when dealing with indentation in sections" do
  961. let(:orig_content) {
  962. <<-EOS
  963. # This is a comment
  964. [section1]
  965. ; This is also a comment
  966. foo=foovalue
  967. bar = barvalue
  968. master = true
  969. [section2]
  970. foo= foovalue2
  971. baz=bazvalue
  972. url = http://192.168.1.1:8080
  973. [section:sub]
  974. subby=bar
  975. #another comment
  976. fleezy = flam
  977. ; yet another comment
  978. EOS
  979. }
  980. it "should add a missing setting at the correct indentation when the header is aligned" do
  981. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  982. :section => 'section1', :setting => 'yahoo', :value => 'yippee'))
  983. provider = described_class.new(resource)
  984. provider.exists?.should be false
  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 = barvalue
  992. master = true
  993. yahoo = yippee
  994. [section2]
  995. foo= foovalue2
  996. baz=bazvalue
  997. url = http://192.168.1.1:8080
  998. [section:sub]
  999. subby=bar
  1000. #another comment
  1001. fleezy = flam
  1002. ; yet another comment
  1003. EOS
  1004. )
  1005. end
  1006. it "should update an existing setting at the correct indentation when the header is aligned" do
  1007. resource = Puppet::Type::Ini_setting.new(
  1008. common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2'))
  1009. provider = described_class.new(resource)
  1010. provider.exists?.should be true
  1011. provider.create
  1012. validate_file(<<-EOS
  1013. # This is a comment
  1014. [section1]
  1015. ; This is also a comment
  1016. foo=foovalue
  1017. bar = barvalue2
  1018. master = true
  1019. [section2]
  1020. foo= foovalue2
  1021. baz=bazvalue
  1022. url = http://192.168.1.1:8080
  1023. [section:sub]
  1024. subby=bar
  1025. #another comment
  1026. fleezy = flam
  1027. ; yet another comment
  1028. EOS
  1029. )
  1030. end
  1031. it "should add a missing setting at the correct indentation when the header is not aligned" do
  1032. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  1033. :section => 'section2', :setting => 'yahoo', :value => 'yippee'))
  1034. provider = described_class.new(resource)
  1035. provider.exists?.should be false
  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=bazvalue
  1047. url = http://192.168.1.1:8080
  1048. yahoo = yippee
  1049. [section:sub]
  1050. subby=bar
  1051. #another comment
  1052. fleezy = flam
  1053. ; yet another comment
  1054. EOS
  1055. )
  1056. end
  1057. it "should update an existing setting at the correct indentation when the header is not aligned" do
  1058. resource = Puppet::Type::Ini_setting.new(
  1059. common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue2'))
  1060. provider = described_class.new(resource)
  1061. provider.exists?.should be true
  1062. provider.create
  1063. validate_file(<<-EOS
  1064. # This is a comment
  1065. [section1]
  1066. ; This is also a comment
  1067. foo=foovalue
  1068. bar = barvalue
  1069. master = true
  1070. [section2]
  1071. foo= foovalue2
  1072. baz=bazvalue2
  1073. url = http://192.168.1.1:8080
  1074. [section:sub]
  1075. subby=bar
  1076. #another comment
  1077. fleezy = flam
  1078. ; yet another comment
  1079. EOS
  1080. )
  1081. end
  1082. it "should add a missing setting at the min indentation when the section is not aligned" do
  1083. resource = Puppet::Type::Ini_setting.new(
  1084. common_params.merge(:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
  1085. provider = described_class.new(resource)
  1086. provider.exists?.should be false
  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 = flam
  1103. ; yet another comment
  1104. yahoo = yippee
  1105. EOS
  1106. )
  1107. end
  1108. it "should update an existing setting at the previous indentation when the section is not aligned" do
  1109. resource = Puppet::Type::Ini_setting.new(
  1110. common_params.merge(:section => 'section:sub', :setting => 'fleezy', :value => 'flam2'))
  1111. provider = described_class.new(resource)
  1112. provider.exists?.should be true
  1113. provider.create
  1114. validate_file(<<-EOS
  1115. # This is a comment
  1116. [section1]
  1117. ; This is also a comment
  1118. foo=foovalue
  1119. bar = barvalue
  1120. master = true
  1121. [section2]
  1122. foo= foovalue2
  1123. baz=bazvalue
  1124. url = http://192.168.1.1:8080
  1125. [section:sub]
  1126. subby=bar
  1127. #another comment
  1128. fleezy = flam2
  1129. ; yet another comment
  1130. EOS
  1131. )
  1132. end
  1133. end
  1134. context "when dealing settings that have a commented version present" do
  1135. let(:orig_content) {
  1136. <<-EOS
  1137. [section1]
  1138. # foo=foovalue
  1139. bar=barvalue
  1140. foo = foovalue2
  1141. [section2]
  1142. # foo = foovalue
  1143. ;bar=barvalue
  1144. blah = blah
  1145. #baz=
  1146. EOS
  1147. }
  1148. it "should add a new setting below a commented version of that setting" do
  1149. resource = Puppet::Type::Ini_setting.new(
  1150. common_params.merge(:section => 'section2', :setting => 'foo', :value => 'foo3'))
  1151. provider = described_class.new(resource)
  1152. provider.exists?.should be false
  1153. provider.create
  1154. validate_file(<<-EOS
  1155. [section1]
  1156. # foo=foovalue
  1157. bar=barvalue
  1158. foo = foovalue2
  1159. [section2]
  1160. # foo = foovalue
  1161. foo = foo3
  1162. ;bar=barvalue
  1163. blah = blah
  1164. #baz=
  1165. EOS
  1166. )
  1167. end
  1168. it "should update an existing setting in place, even if there is a commented version of that setting" do
  1169. resource = Puppet::Type::Ini_setting.new(
  1170. common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foo3'))
  1171. provider = described_class.new(resource)
  1172. provider.exists?.should be true
  1173. provider.create
  1174. validate_file(<<-EOS
  1175. [section1]
  1176. # foo=foovalue
  1177. bar=barvalue
  1178. foo = foo3
  1179. [section2]
  1180. # foo = foovalue
  1181. ;bar=barvalue
  1182. blah = blah
  1183. #baz=
  1184. EOS
  1185. )
  1186. end
  1187. it "should add a new setting below a commented version of that setting, respecting semicolons as comments" do
  1188. resource = Puppet::Type::Ini_setting.new(
  1189. common_params.merge(:section => 'section2', :setting => 'bar', :value => 'bar3'))
  1190. provider = described_class.new(resource)
  1191. provider.exists?.should be false
  1192. provider.create
  1193. validate_file(<<-EOS
  1194. [section1]
  1195. # foo=foovalue
  1196. bar=barvalue
  1197. foo = foovalue2
  1198. [section2]
  1199. # foo = foovalue
  1200. ;bar=barvalue
  1201. bar=bar3
  1202. blah = blah
  1203. #baz=
  1204. EOS
  1205. )
  1206. end
  1207. it "should add a new setting below an empty commented version of that setting" do
  1208. resource = Puppet::Type::Ini_setting.new(
  1209. common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue'))
  1210. provider = described_class.new(resource)
  1211. provider.exists?.should be false
  1212. provider.create
  1213. validate_file(<<-EOS
  1214. [section1]
  1215. # foo=foovalue
  1216. bar=barvalue
  1217. foo = foovalue2
  1218. [section2]
  1219. # foo = foovalue
  1220. ;bar=barvalue
  1221. blah = blah
  1222. #baz=
  1223. baz=bazvalue
  1224. EOS
  1225. )
  1226. end
  1227. context 'when a section only contains comments' do
  1228. let(:orig_content) {
  1229. <<-EOS
  1230. [section1]
  1231. # foo=foovalue
  1232. # bar=bar2
  1233. EOS
  1234. }
  1235. it 'should be able to add a new setting when a section contains only comments' do
  1236. resource = Puppet::Type::Ini_setting.new(
  1237. common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foovalue2')
  1238. )
  1239. provider = described_class.new(resource)
  1240. provider.exists?.should be false
  1241. provider.create
  1242. validate_file(<<-EOS
  1243. [section1]
  1244. # foo=foovalue
  1245. foo=foovalue2
  1246. # bar=bar2
  1247. EOS
  1248. )
  1249. end
  1250. it 'should be able to add a new setting when it matches a commented out line other than the first one' do
  1251. resource = Puppet::Type::Ini_setting.new(
  1252. common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2')
  1253. )
  1254. provider = described_class.new(resource)
  1255. provider.exists?.should be false
  1256. provider.create
  1257. validate_file(<<-EOS
  1258. [section1]
  1259. # foo=foovalue
  1260. # bar=bar2
  1261. bar=barvalue2
  1262. EOS
  1263. )
  1264. end
  1265. end
  1266. context "when sections have spaces and dashes" do
  1267. let(:orig_content) {
  1268. <<-EOS
  1269. # This is a comment
  1270. [section - one]
  1271. ; This is also a comment
  1272. foo=foovalue
  1273. bar = barvalue
  1274. master = true
  1275. [section - two]
  1276. foo= foovalue2
  1277. baz=bazvalue
  1278. url = http://192.168.1.1:8080
  1279. [section:sub]
  1280. subby=bar
  1281. #another comment
  1282. ; yet another comment
  1283. EOS
  1284. }
  1285. it "should add a missing setting to the correct section" do
  1286. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  1287. :section => 'section - two', :setting => 'yahoo', :value => 'yippee'))
  1288. provider = described_class.new(resource)
  1289. provider.exists?.should be false
  1290. provider.create
  1291. validate_file(<<-EOS
  1292. # This is a comment
  1293. [section - one]
  1294. ; This is also a comment
  1295. foo=foovalue
  1296. bar = barvalue
  1297. master = true
  1298. [section - two]
  1299. foo= foovalue2
  1300. baz=bazvalue
  1301. url = http://192.168.1.1:8080
  1302. yahoo = yippee
  1303. [section:sub]
  1304. subby=bar
  1305. #another comment
  1306. ; yet another comment
  1307. EOS
  1308. )
  1309. end
  1310. end
  1311. end
  1312. context "when sections have spaces and quotations" do
  1313. let(:orig_content) do
  1314. <<-EOS
  1315. [branch "master"]
  1316. remote = origin
  1317. merge = refs/heads/master
  1318. [alias]
  1319. to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
  1320. [branch "production"]
  1321. remote = origin
  1322. merge = refs/heads/production
  1323. EOS
  1324. end
  1325. it "should add a missing setting to the correct section" do
  1326. resource = Puppet::Type::Ini_setting.new(common_params.merge(
  1327. :section => 'alias',
  1328. :setting => 'foo',
  1329. :value => 'bar'
  1330. ))
  1331. provider = described_class.new(resource)
  1332. provider.exists?.should be false
  1333. provider.create
  1334. validate_file(<<-EOS
  1335. [branch "master"]
  1336. remote = origin
  1337. merge = refs/heads/master
  1338. [alias]
  1339. to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
  1340. foo = bar
  1341. [branch "production"]
  1342. remote = origin
  1343. merge = refs/heads/production
  1344. EOS
  1345. )
  1346. end
  1347. end
  1348. end