4 provider_class = Puppet::Type.type(:ini_setting).provider(:ruby)
5 describe provider_class do
6 include PuppetlabsSpec::Files
8 let(:tmpfile) { tmpfilename("ini_setting_test") }
9 let(:emptyfile) { tmpfilename("ini_setting_test_empty") }
11 let(:common_params) { {
12 :title => 'ini_setting_ensure_present_test',
14 :section => 'section2',
17 def validate_file(expected_content,tmpfile = tmpfile)
18 File.read(tmpfile).should == expected_content
23 File.open(tmpfile, 'w') do |fh|
24 fh.write(orig_content)
26 File.open(emptyfile, 'w') do |fh|
31 context 'when calling instances' do
37 it 'should fail when file path is not set' do
39 provider_class.instances
40 }.to raise_error(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded')
43 context 'when file path is set by a child class' do
44 it 'should return [] when file is empty' do
45 child_one = Class.new(provider_class) do
50 child_one.stubs(:file_path).returns(emptyfile)
51 child_one.instances.should == []
53 it 'should override the provider instances file_path' do
54 child_two = Class.new(provider_class) do
59 resource = Puppet::Type::Ini_setting.new(common_params)
60 provider = child_two.new(resource)
61 provider.file_path.should == '/some/file/path'
63 context 'when file has contecnts' do
68 ; This is also a comment
77 url = http://192.168.1.1:8080
85 it 'should be able to parse the results' do
86 child_three = Class.new(provider_class) do
91 child_three.stubs(:file_path).returns(tmpfile)
92 child_three.instances.size == 7
94 {:name => 'section1/foo', :value => 'foovalue' },
95 {:name => 'section1/bar', :value => 'barvalue' },
96 {:name => 'section1/master', :value => 'true' },
97 {:name => 'section2/foo', :value => 'foovalue2' },
98 {:name => 'section2/baz', :value => 'bazvalue' },
99 {:name => 'section2/url', :value => 'http://192.168.1.1:8080' },
100 {:name => 'section:sub/subby', :value => 'bar' }
104 child_three.instances.each do |x|
105 prop_hash = x.instance_variable_get(:@property_hash)
106 ensure_value = prop_hash.delete(:ensure)
107 ensure_array.push(ensure_value)
108 real_array.push(prop_hash)
110 ensure_array.uniq.should == [:present]
111 ((real_array - expected_array) && (expected_array - real_array)).should == []
121 context "when ensuring that a setting is present" do
126 ; This is also a comment
135 url = http://192.168.1.1:8080
139 ; yet another comment
146 it "should add a missing setting to the correct section" do
147 resource = Puppet::Type::Ini_setting.new(common_params.merge(
148 :setting => 'yahoo', :value => 'yippee'))
149 provider = described_class.new(resource)
150 provider.exists?.should be false
155 ; This is also a comment
164 url = http://192.168.1.1:8080
169 ; yet another comment
177 it "should add a missing setting to the correct section with pre/suffix" do
178 resource = Puppet::Type::Ini_setting.new(common_params.merge(
179 :section => 'nonstandard',
180 :setting => 'yahoo', :value => 'yippee',
181 :section_prefix => '-', :section_suffix => '-'))
182 provider = described_class.new(resource)
183 provider.exists?.should be false
188 ; This is also a comment
197 url = http://192.168.1.1:8080
201 ; yet another comment
210 it "should add a missing setting to the correct section with colon" do
211 resource = Puppet::Type::Ini_setting.new(common_params.merge(
212 :section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
213 provider = described_class.new(resource)
214 provider.exists?.should be false
219 ; This is also a comment
228 url = http://192.168.1.1:8080
232 ; yet another comment
241 it "should modify an existing setting with a different value" do
242 resource = Puppet::Type::Ini_setting.new(common_params.merge(
243 :setting => 'baz', :value => 'bazvalue2'))
244 provider = described_class.new(resource)
245 provider.exists?.should be true
246 provider.value=('bazvalue2')
250 ; This is also a comment
259 url = http://192.168.1.1:8080
263 ; yet another comment
271 it "should modify an existing setting with pre/suffix with a different value" do
272 resource = Puppet::Type::Ini_setting.new(common_params.merge(
273 :section => 'nonstandard',
274 :setting => 'shoes', :value => 'orange',
275 :section_prefix => '-', :section_suffix => '-' ))
276 provider = described_class.new(resource)
277 provider.exists?.should be true
278 provider.value=('orange')
282 ; This is also a comment
291 url = http://192.168.1.1:8080
295 ; yet another comment
303 it "should modify an existing setting with a different value - with colon in section" do
304 resource = Puppet::Type::Ini_setting.new(common_params.merge(
305 :section => 'section:sub', :setting => 'subby', :value => 'foo'))
306 provider = described_class.new(resource)
307 provider.exists?.should be true
308 provider.value.should == 'bar'
309 provider.value=('foo')
313 ; This is also a comment
322 url = http://192.168.1.1:8080
326 ; yet another comment
334 it "should be able to handle settings with non alphanumbering settings " do
335 resource = Puppet::Type::Ini_setting.new(common_params.merge(
336 :setting => 'url', :value => 'http://192.168.0.1:8080'))
337 provider = described_class.new(resource)
338 provider.exists?.should be true
339 provider.value.should == 'http://192.168.1.1:8080'
340 provider.value=('http://192.168.0.1:8080')
342 validate_file( <<-EOS
345 ; This is also a comment
354 url = http://192.168.0.1:8080
358 ; yet another comment
366 it "should be able to handle settings with pre/suffix with non alphanumbering settings " do
367 resource = Puppet::Type::Ini_setting.new(common_params.merge(
368 :section => 'nonstandard',
369 :setting => 'shoes', :value => 'http://192.168.0.1:8080',
370 :section_prefix => '-', :section_suffix => '-' ))
371 provider = described_class.new(resource)
372 provider.exists?.should be true
373 provider.value.should == 'purple'
374 provider.value=('http://192.168.0.1:8080')
376 validate_file( <<-EOS
379 ; This is also a comment
388 url = http://192.168.1.1:8080
392 ; yet another comment
395 shoes = http://192.168.0.1:8080
400 it "should recognize an existing setting with the specified value" do
401 resource = Puppet::Type::Ini_setting.new(common_params.merge(
402 :setting => 'baz', :value => 'bazvalue'))
403 provider = described_class.new(resource)
404 provider.exists?.should be true
407 it "should recognize an existing setting with pre/suffix with the specified value" do
408 resource = Puppet::Type::Ini_setting.new(common_params.merge(
409 :section => 'nonstandard',
410 :setting => 'shoes', :value => 'purple',
411 :section_prefix => '-', :section_suffix => '-' ))
412 provider = described_class.new(resource)
413 provider.exists?.should be true
416 it "should add a new section if the section does not exist" do
417 resource = Puppet::Type::Ini_setting.new(common_params.merge(
418 :section => "section3", :setting => 'huzzah', :value => 'shazaam'))
419 provider = described_class.new(resource)
420 provider.exists?.should be false
425 ; This is also a comment
434 url = http://192.168.1.1:8080
438 ; yet another comment
449 it "should add a new section with pre/suffix if the section does not exist" do
450 resource = Puppet::Type::Ini_setting.new(common_params.merge(
451 :section => "section3", :setting => 'huzzah', :value => 'shazaam',
452 :section_prefix => '-', :section_suffix => '-' ))
453 provider = described_class.new(resource)
454 provider.exists?.should be false
459 ; This is also a comment
468 url = http://192.168.1.1:8080
472 ; yet another comment
483 it "should add a new section if the section does not exist - with colon" do
484 resource = Puppet::Type::Ini_setting.new(common_params.merge(
485 :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
486 provider = described_class.new(resource)
487 provider.exists?.should be false
492 ; This is also a comment
501 url = http://192.168.1.1:8080
505 ; yet another comment
516 it "should add a new section with pre/suffix if the section does not exist - with colon" do
517 resource = Puppet::Type::Ini_setting.new(common_params.merge(
518 :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam',
519 :section_prefix => '-', :section_suffix => '-' ))
520 provider = described_class.new(resource)
521 provider.exists?.should be false
526 ; This is also a comment
535 url = http://192.168.1.1:8080
539 ; yet another comment
550 it "should add a new section if no sections exists" do
551 resource = Puppet::Type::Ini_setting.new(common_params.merge(
552 :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
553 provider = described_class.new(resource)
554 provider.exists?.should be false
558 setting1 = hellowworld
562 it "should add a new section with pre/suffix if no sections exists" do
563 resource = Puppet::Type::Ini_setting.new(common_params.merge(
564 :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
565 :section_prefix => '-', :section_suffix => '-' ))
566 provider = described_class.new(resource)
567 provider.exists?.should be false
571 setting1 = hellowworld
575 it "should add a new section with colon if no sections exists" do
576 resource = Puppet::Type::Ini_setting.new(common_params.merge(
577 :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
578 provider = described_class.new(resource)
579 provider.exists?.should be false
583 setting1 = hellowworld
587 it "should add a new section with pre/suffix with colon if no sections exists" do
588 resource = Puppet::Type::Ini_setting.new(common_params.merge(
589 :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
590 :section_prefix => '-', :section_suffix => '-' ))
591 provider = described_class.new(resource)
592 provider.exists?.should be false
596 setting1 = hellowworld
599 it "should be able to handle variables of any type" do
600 resource = Puppet::Type::Ini_setting.new(common_params.merge(
601 :section => "section1", :setting => 'master', :value => true))
602 provider = described_class.new(resource)
603 provider.exists?.should be true
604 provider.value.should == 'true'
609 context "when dealing with a global section" do
615 foo = http://192.168.1.1:8080
616 ; yet another comment
621 it "should add a missing setting if it doesn't exist" do
622 resource = Puppet::Type::Ini_setting.new(common_params.merge(
623 :section => '', :setting => 'bar', :value => 'yippee'))
624 provider = described_class.new(resource)
625 provider.exists?.should be false
632 foo = http://192.168.1.1:8080
633 ; yet another comment
638 it "should modify an existing setting with a different value" do
639 resource = Puppet::Type::Ini_setting.new(common_params.merge(
640 :section => '', :setting => 'foo', :value => 'yippee'))
641 provider = described_class.new(resource)
642 provider.exists?.should be true
643 provider.value.should == 'blah'
644 provider.value=('yippee')
649 foo = http://192.168.1.1:8080
650 ; yet another comment
655 it "should recognize an existing setting with the specified value" do
656 resource = Puppet::Type::Ini_setting.new(common_params.merge(
657 :section => '', :setting => 'foo', :value => 'blah'))
658 provider = described_class.new(resource)
659 provider.exists?.should be true
663 context "when the first line of the file is a section" do
667 foo = http://192.168.1.1:8080
671 it "should be able to add a global setting" do
672 resource = Puppet::Type::Ini_setting.new(common_params.merge(
673 :section => '', :setting => 'foo', :value => 'yippee'))
674 provider = described_class.new(resource)
675 provider.exists?.should be false
681 foo = http://192.168.1.1:8080
686 it "should modify an existing setting" do
687 resource = Puppet::Type::Ini_setting.new(common_params.merge(
688 :section => 'section2', :setting => 'foo', :value => 'yippee'))
689 provider = described_class.new(resource)
690 provider.exists?.should be true
691 provider.value.should == 'http://192.168.1.1:8080'
692 provider.value=('yippee')
700 it "should add a new setting" do
701 resource = Puppet::Type::Ini_setting.new(common_params.merge(
702 :section => 'section2', :setting => 'bar', :value => 'baz'))
703 provider = described_class.new(resource)
704 provider.exists?.should be false
708 foo = http://192.168.1.1:8080
715 context "when overriding the separator" do
723 it "should modify an existing setting" do
724 resource = Puppet::Type::Ini_setting.new(common_params.merge(
725 :section => 'section2',
728 :key_val_separator => '='))
729 provider = described_class.new(resource)
730 provider.exists?.should be true
731 provider.value.should == 'bar'
732 provider.value=('yippee')
742 context "when overriding the separator to something other than =" do
750 it "should modify an existing setting" do
751 resource = Puppet::Type::Ini_setting.new(common_params.merge(
752 :section => 'section2',
755 :key_val_separator => ': '))
756 provider = described_class.new(resource)
757 provider.exists?.should be true
758 provider.value.should == 'bar'
759 provider.value=('yippee')
767 it "should add a new setting" do
768 resource = Puppet::Type::Ini_setting.new(common_params.merge(
769 :section => 'section2',
772 :key_val_separator => ': '))
773 provider = described_class.new(resource)
774 provider.exists?.should be false
786 context "when ensuring that a setting is absent" do
790 ; This is also a comment
799 url = http://192.168.1.1:8080
803 ; yet another comment
810 it "should remove a setting that exists" do
811 resource = Puppet::Type::Ini_setting.new(common_params.merge(
812 :section => 'section1', :setting => 'foo', :ensure => 'absent'))
813 provider = described_class.new(resource)
814 provider.exists?.should be true
818 ; This is also a comment
826 url = http://192.168.1.1:8080
830 ; yet another comment
838 it "should remove a setting with pre/suffix that exists" do
839 resource = Puppet::Type::Ini_setting.new(common_params.merge(
840 :section => 'nonstandard', :setting => 'shoes', :ensure => 'absent',
841 :section_prefix => '-', :section_suffix => '-' ))
842 provider = described_class.new(resource)
843 provider.exists?.should be true
847 ; This is also a comment
856 url = http://192.168.1.1:8080
860 ; yet another comment
867 it "should do nothing for a setting that does not exist" do
868 resource = Puppet::Type::Ini_setting.new(common_params.merge(
869 :section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
870 provider = described_class.new(resource)
871 provider.exists?.should be false
875 ; This is also a comment
884 url = http://192.168.1.1:8080
888 ; yet another comment
896 it "should do nothing for a setting with pre/suffix that does not exist" do
897 resource = Puppet::Type::Ini_setting.new(common_params.merge(
898 :section => 'nonstandard', :setting => 'foo', :ensure => 'absent',
899 :section_prefix => '-', :section_suffix => '-' ))
900 provider = described_class.new(resource)
901 provider.exists?.should be false
905 ; This is also a comment
914 url = http://192.168.1.1:8080
918 ; yet another comment
927 context "when dealing with indentation in sections" do
932 ; This is also a comment
941 url = http://192.168.1.1:8080
946 ; yet another comment
950 it "should add a missing setting at the correct indentation when the header is aligned" do
951 resource = Puppet::Type::Ini_setting.new(common_params.merge(
952 :section => 'section1', :setting => 'yahoo', :value => 'yippee'))
953 provider = described_class.new(resource)
954 provider.exists?.should be false
959 ; This is also a comment
969 url = http://192.168.1.1:8080
974 ; yet another comment
979 it "should update an existing setting at the correct indentation when the header is aligned" do
980 resource = Puppet::Type::Ini_setting.new(
981 common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2'))
982 provider = described_class.new(resource)
983 provider.exists?.should be true
988 ; This is also a comment
997 url = http://192.168.1.1:8080
1002 ; yet another comment
1007 it "should add a missing setting at the correct indentation when the header is not aligned" do
1008 resource = Puppet::Type::Ini_setting.new(common_params.merge(
1009 :section => 'section2', :setting => 'yahoo', :value => 'yippee'))
1010 provider = described_class.new(resource)
1011 provider.exists?.should be false
1013 validate_file(<<-EOS
1016 ; This is also a comment
1025 url = http://192.168.1.1:8080
1031 ; yet another comment
1036 it "should update an existing setting at the correct indentation when the header is not aligned" do
1037 resource = Puppet::Type::Ini_setting.new(
1038 common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue2'))
1039 provider = described_class.new(resource)
1040 provider.exists?.should be true
1042 validate_file(<<-EOS
1045 ; This is also a comment
1054 url = http://192.168.1.1:8080
1059 ; yet another comment
1064 it "should add a missing setting at the min indentation when the section is not aligned" do
1065 resource = Puppet::Type::Ini_setting.new(
1066 common_params.merge(:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
1067 provider = described_class.new(resource)
1068 provider.exists?.should be false
1070 validate_file(<<-EOS
1073 ; This is also a comment
1082 url = http://192.168.1.1:8080
1087 ; yet another comment
1093 it "should update an existing setting at the previous indentation when the section is not aligned" do
1094 resource = Puppet::Type::Ini_setting.new(
1095 common_params.merge(:section => 'section:sub', :setting => 'fleezy', :value => 'flam2'))
1096 provider = described_class.new(resource)
1097 provider.exists?.should be true
1099 validate_file(<<-EOS
1102 ; This is also a comment
1111 url = http://192.168.1.1:8080
1116 ; yet another comment
1124 context "when dealing settings that have a commented version present" do
1125 let(:orig_content) {
1140 it "should add a new setting below a commented version of that setting" do
1141 resource = Puppet::Type::Ini_setting.new(
1142 common_params.merge(:section => 'section2', :setting => 'foo', :value => 'foo3'))
1143 provider = described_class.new(resource)
1144 provider.exists?.should be false
1146 validate_file(<<-EOS
1162 it "should update an existing setting in place, even if there is a commented version of that setting" do
1163 resource = Puppet::Type::Ini_setting.new(
1164 common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foo3'))
1165 provider = described_class.new(resource)
1166 provider.exists?.should be true
1168 validate_file(<<-EOS
1183 it "should add a new setting below a commented version of that setting, respecting semicolons as comments" do
1184 resource = Puppet::Type::Ini_setting.new(
1185 common_params.merge(:section => 'section2', :setting => 'bar', :value => 'bar3'))
1186 provider = described_class.new(resource)
1187 provider.exists?.should be false
1189 validate_file(<<-EOS
1205 it "should add a new setting below an empty commented version of that setting" do
1206 resource = Puppet::Type::Ini_setting.new(
1207 common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue'))
1208 provider = described_class.new(resource)
1209 provider.exists?.should be false
1211 validate_file(<<-EOS
1227 context 'when a section only contains comments' do
1228 let(:orig_content) {
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')
1239 provider = described_class.new(resource)
1240 provider.exists?.should be false
1242 validate_file(<<-EOS
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')
1254 provider = described_class.new(resource)
1255 provider.exists?.should be false
1257 validate_file(<<-EOS
1267 context "when sections have spaces and dashes" do
1268 let(:orig_content) {
1272 ; This is also a comment
1281 url = http://192.168.1.1:8080
1285 ; yet another comment
1289 it "should add a missing setting to the correct section" do
1290 resource = Puppet::Type::Ini_setting.new(common_params.merge(
1291 :section => 'section - two', :setting => 'yahoo', :value => 'yippee'))
1292 provider = described_class.new(resource)
1293 provider.exists?.should be false
1295 validate_file(<<-EOS
1298 ; This is also a comment
1307 url = http://192.168.1.1:8080
1312 ; yet another comment
1321 context "when sections have spaces and quotations" do
1322 let(:orig_content) do
1326 merge = refs/heads/master
1329 to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
1330 [branch "production"]
1332 merge = refs/heads/production
1336 it "should add a missing setting to the correct section" do
1337 resource = Puppet::Type::Ini_setting.new(common_params.merge(
1338 :section => 'alias',
1342 provider = described_class.new(resource)
1343 provider.exists?.should be false
1345 validate_file(<<-EOS
1348 merge = refs/heads/master
1351 to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
1353 [branch "production"]
1355 merge = refs/heads/production