diff options
-rw-r--r-- | lib/puppet/provider/ini_setting/ruby.rb | 28 | ||||
-rw-r--r-- | lib/puppet/util/ini_file.rb | 10 | ||||
-rw-r--r-- | lib/puppet/util/ini_file/section.rb | 4 | ||||
-rw-r--r-- | spec/unit/puppet/provider/ini_setting/ruby_spec.rb | 118 |
4 files changed, 158 insertions, 2 deletions
diff --git a/lib/puppet/provider/ini_setting/ruby.rb b/lib/puppet/provider/ini_setting/ruby.rb index 2c13847..af7a073 100644 --- a/lib/puppet/provider/ini_setting/ruby.rb +++ b/lib/puppet/provider/ini_setting/ruby.rb @@ -2,6 +2,28 @@ require File.expand_path('../../../util/ini_file', __FILE__) Puppet::Type.type(:ini_setting).provide(:ruby) do + def self.instances + if self.respond_to?(:file_path) + # figure out what to do about the seperator + ini_file = Puppet::Util::IniFile.new(file_path, '=') + resources = [] + ini_file.section_names.each do |section_name| + ini_file.get_settings(section_name).each do |setting, value| + resources.push( + new( + :name => "#{section_name}/#{setting}", + :value => value, + :ensure => :present + ) + ) + end + end + resources + else + raise(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded') + end + end + def exists? ini_file.get_value(section, setting) end @@ -36,7 +58,11 @@ Puppet::Type.type(:ini_setting).provide(:ruby) do end def file_path - resource[:path] + if self.class.respond_to?(:file_path) + self.class.file_path + else + resource[:path] + end end def separator diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index 90b0c7b..b6927f9 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -23,6 +23,14 @@ module Util @section_names end + def get_settings(section_name) + section = @sections_hash[section_name] + section.setting_names.inject({}) do |result, setting| + result[setting] = section.get_value(setting) + result + end + end + def get_value(section_name, setting) if (@sections_hash.has_key?(section_name)) @sections_hash[section_name].get_value(setting) @@ -256,7 +264,7 @@ module Util def insert_inline_setting_line(result, section, setting, value) line_num = result[:line_num] match = result[:match] - lines.insert(line_num + 1, "#{' ' * section.indentation}#{setting}#{match[4]}#{value}") + lines.insert(line_num + 1, "#{' ' * (section.indentation || 0 )}#{setting}#{match[4]}#{value}") end # Utility method; given a section index (index into the @section_names diff --git a/lib/puppet/util/ini_file/section.rb b/lib/puppet/util/ini_file/section.rb index ba1a783..9682d7f 100644 --- a/lib/puppet/util/ini_file/section.rb +++ b/lib/puppet/util/ini_file/section.rb @@ -32,6 +32,10 @@ class IniFile @end_line.nil? end + def setting_names + @existing_settings.keys | @additional_settings.keys + end + def get_value(setting_name) @existing_settings[setting_name] || @additional_settings[setting_name] end diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index 4f8bdc1..b73a3fa 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -28,6 +28,84 @@ describe provider_class do end end + context 'when calling instances' do + + let :orig_content do + '' + end + + it 'should fail when file path is not set' do + expect { + described_class.instances + }.to raise_error(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded') + end + + context 'when file path is set' do + it 'should return [] when file is empty' do + provider_class.stubs(:file_path).returns(emptyfile) + provider_class.instances.should == [] + end + it 'should override the provider instances file_path' do + provider_class.stubs(:file_path).returns('/some/file/path') + resource = Puppet::Type::Ini_setting.new(common_params) + provider = provider_class.new(resource) + provider.file_path.should == '/some/file/path' + end + context 'when file has contecnts' do + let(:orig_content) { + <<-EOS +# This is a comment +[section1] +; This is also a comment +foo=foovalue + +bar = barvalue +master = true +[section2] + +foo= foovalue2 +baz=bazvalue +url = http://192.168.1.1:8080 +[section:sub] +subby=bar + #another comment + ; yet another comment + EOS + } + + it 'should be able to parse the results' do + provider_class.stubs(:file_path).returns(tmpfile) + provider_class.instances.size == 7 + expected_array = [ + {:name => 'section1/foo', :value => 'foovalue' }, + {:name => 'section1/bar', :value => 'barvalue' }, + {:name => 'section1/master', :value => 'true' }, + {:name => 'section2/foo', :value => 'foovalue2' }, + {:name => 'section2/baz', :value => 'bazvalue' }, + {:name => 'section2/url', :value => 'http://192.168.1.1:8080' }, + {:name => 'section:sub/subby', :value => 'bar' } + ] + real_array = [] + ensure_array = [] + provider_class.instances.each do |x| + prop_hash = x.instance_variable_get(:@property_hash) + ensure_value = prop_hash.delete(:ensure) + ensure_array.push(ensure_value) + real_array.push(prop_hash) + end + puts ensure_array.inspect + puts real_array.inspect + ensure_array.uniq.should == [:present] + ((real_array - expected_array) && (expected_array - real_array)).should == [] + + end + + end + + end + + end + context "when ensuring that a setting is present" do let(:orig_content) { <<-EOS @@ -809,6 +887,46 @@ blah = blah ) end + context 'when a section only contains comments' do + let(:orig_content) { + <<-EOS +[section1] +# foo=foovalue +# bar=bar2 +EOS + } + it 'should be able to add a new setting when a section contains only comments' do + resource = Puppet::Type::Ini_setting.new( + common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foovalue2') + ) + provider = described_class.new(resource) + provider.exists?.should be_false + provider.create + validate_file(<<-EOS +[section1] +# foo=foovalue +foo=foovalue2 +# bar=bar2 + EOS + ) + end + it 'should be able to add a new setting when it matches a commented out line other than the first one' do + resource = Puppet::Type::Ini_setting.new( + common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2') + ) + provider = described_class.new(resource) + provider.exists?.should be_false + provider.create + validate_file(<<-EOS +[section1] +# foo=foovalue +# bar=bar2 +bar=barvalue2 + EOS + ) + end + end + end end |