diff options
author | Luke Kanies <luke@madstop.com> | 2009-08-19 11:31:19 -0700 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-08-24 11:36:20 +1000 |
commit | 058514aaf44f981a3239b402f2ff116ebf0d951f (patch) | |
tree | 12ee14aa7e1f693671167c2190ad1021e10d4fee | |
parent | b0f219ad177c8e9c261578586f0324cef0c71cd2 (diff) | |
download | puppet-058514aaf44f981a3239b402f2ff116ebf0d951f.tar.gz puppet-058514aaf44f981a3239b402f2ff116ebf0d951f.tar.xz puppet-058514aaf44f981a3239b402f2ff116ebf0d951f.zip |
Moving Setting classes into separate files
This isn't really a refactor, just moving code around.
I did some simple method renaming, also.
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r-- | lib/puppet/defaults.rb | 4 | ||||
-rw-r--r-- | lib/puppet/util/settings.rb | 316 | ||||
-rw-r--r-- | lib/puppet/util/settings/boolean_setting.rb | 33 | ||||
-rw-r--r-- | lib/puppet/util/settings/file_setting.rb | 102 | ||||
-rw-r--r-- | lib/puppet/util/settings/setting.rb | 110 | ||||
-rwxr-xr-x | spec/integration/defaults.rb | 10 | ||||
-rwxr-xr-x | spec/unit/util/settings.rb | 122 | ||||
-rwxr-xr-x | spec/unit/util/settings/file_setting.rb | 117 | ||||
-rwxr-xr-x | test/util/settings.rb | 18 |
9 files changed, 423 insertions, 409 deletions
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index 010a98db5..9997acb5f 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -415,7 +415,7 @@ module Puppet :ca => [true, "Wether the master should function as a certificate authority."], :modulepath => {:default => "$confdir/modules:/usr/share/puppet/modules", :desc => "The search path for modules as a colon-separated list of - directories.", :type => :element }, # We don't want this to be considered a file, since it's multiple files. + directories.", :type => :setting }, # We don't want this to be considered a file, since it's multiple files. :ssl_client_header => ["HTTP_X_CLIENT_DN", "The header containing an authenticated client's SSL DN. Only used with Mongrel. This header must be set by the proxy to the authenticated client's SSL DN (e.g., ``/CN=puppet.reductivelabs.com``). @@ -588,7 +588,7 @@ module Puppet :desc => "Where Puppet should look for facts. Multiple directories should be colon-separated, like normal PATH variables.", :call_on_define => true, # Call our hook with the default value, so we always get the value added to facter. - :type => :element, # Don't consider it a file, because it could be multiple colon-separated files + :type => :setting, # Don't consider it a file, because it could be multiple colon-separated files :hook => proc { |value| Facter.search(value) if Facter.respond_to?(:search) }}, :factdest => ["$vardir/facts/", "Where Puppet should store facts that it pulls down from the central diff --git a/lib/puppet/util/settings.rb b/lib/puppet/util/settings.rb index 0129fe705..c49fbf3cb 100644 --- a/lib/puppet/util/settings.rb +++ b/lib/puppet/util/settings.rb @@ -12,6 +12,10 @@ class Puppet::Util::Settings include Enumerable include Puppet::Util::Cacher + require 'puppet/util/settings/setting' + require 'puppet/util/settings/file_setting' + require 'puppet/util/settings/boolean_setting' + attr_accessor :file attr_reader :timer @@ -29,8 +33,8 @@ class Puppet::Util::Settings # understand, and add them to the passed option list. def addargs(options) # Add all of the config parameters as valid options. - self.each { |name, element| - element.getopt_args.each { |args| options << args } + self.each { |name, setting| + setting.getopt_args.each { |args| options << args } } return options @@ -40,8 +44,8 @@ class Puppet::Util::Settings # understand, and add them to the passed option list. def optparse_addargs(options) # Add all of the config parameters as valid options. - self.each { |name, element| - options << element.optparse_args + self.each { |name, setting| + options << setting.optparse_args } return options @@ -50,7 +54,7 @@ class Puppet::Util::Settings # Is our parameter a boolean parameter? def boolean?(param) param = param.to_sym - if @config.include?(param) and @config[param].kind_of? CBoolean + if @config.include?(param) and @config[param].kind_of? BooleanSetting return true else return false @@ -128,7 +132,7 @@ class Puppet::Util::Settings end # Return an object by name. - def element(param) + def setting(param) param = param.to_sym @config[param] end @@ -193,8 +197,8 @@ class Puppet::Util::Settings # They probably deserve their own class, but I don't want to do that until I can refactor environments # its a little better than where they were - # Prints the contents of a config file with the available config elements, or it - # prints a single value of a config element. + # Prints the contents of a config file with the available config settings, or it + # prints a single value of a config setting. def print_config_options env = value(:environment) val = value(:configprint) @@ -248,7 +252,7 @@ class Puppet::Util::Settings # Return a given object's file metadata. def metadata(param) - if obj = @config[param.to_sym] and obj.is_a?(CFile) + if obj = @config[param.to_sym] and obj.is_a?(FileSetting) return [:owner, :group, :mode].inject({}) do |meta, p| if v = obj.send(p) meta[p] = v @@ -365,39 +369,39 @@ class Puppet::Util::Settings end end - # Create a new element. The value is passed in because it's used to determine - # what kind of element we're creating, but the value itself might be either + # Create a new setting. The value is passed in because it's used to determine + # what kind of setting we're creating, but the value itself might be either # a default or a value, so we can't actually assign it. - def newelement(hash) + def newsetting(hash) klass = nil if hash[:section] hash[:section] = hash[:section].to_sym end if type = hash[:type] - unless klass = {:element => CElement, :file => CFile, :boolean => CBoolean}[type] + unless klass = {:setting => Setting, :file => FileSetting, :boolean => BooleanSetting}[type] raise ArgumentError, "Invalid setting type '%s'" % type end hash.delete(:type) else case hash[:default] when true, false, "true", "false" - klass = CBoolean + klass = BooleanSetting when /^\$\w+\//, /^\// - klass = CFile + klass = FileSetting when String, Integer, Float # nothing - klass = CElement + klass = Setting else raise Puppet::Error, "Invalid value '%s' for %s" % [value.inspect, hash[:name]] end end hash[:settings] = self - element = klass.new(hash) + setting = klass.new(hash) - return element + return setting end - # This has to be private, because it doesn't add the elements to @config - private :newelement + # This has to be private, because it doesn't add the settings to @config + private :newsetting # Iterate across all of the objects in a given section. def persection(section) @@ -463,15 +467,15 @@ class Puppet::Util::Settings def set_value(param, value, type) param = param.to_sym - unless element = @config[param] + unless setting = @config[param] raise ArgumentError, "Attempt to assign a value to unknown configuration parameter %s" % param.inspect end - if element.respond_to?(:munge) - value = element.munge(value) + if setting.respond_to?(:munge) + value = setting.munge(value) end - if element.respond_to?(:handle) - element.handle(value) + if setting.respond_to?(:handle) + setting.handle(value) end # Reset the name, so it's looked up again. if param == :name @@ -508,7 +512,7 @@ class Puppet::Util::Settings if @config.include?(name) raise ArgumentError, "Parameter %s is already defined" % name end - tryconfig = newelement(hash) + tryconfig = newsetting(hash) if short = tryconfig.short if other = @shortnames[short] raise ArgumentError, "Parameter %s is already using short name '%s'" % [other.name, short] @@ -540,7 +544,7 @@ class Puppet::Util::Settings catalog = Puppet::Resource::Catalog.new("Settings") - @config.values.find_all { |value| value.is_a?(CFile) }.each do |file| + @config.values.find_all { |value| value.is_a?(FileSetting) }.each do |file| next unless (sections.nil? or sections.include?(file.section)) next unless resource = file.to_resource next if catalog.resource(resource.ref) @@ -553,7 +557,7 @@ class Puppet::Util::Settings catalog end - # Convert our list of config elements into a configuration file. + # Convert our list of config settings into a configuration file. def to_config str = %{The configuration file for #{Puppet[:name]}. Note that this file is likely to have unused configuration parameters in it; any parameter that's @@ -753,7 +757,7 @@ Generated on #{Time.now}. raise ArgumentError, "Unknown default %s" % default end - unless obj.is_a? CFile + unless obj.is_a? FileSetting raise ArgumentError, "Default %s is not a file" % default end @@ -765,18 +769,18 @@ Generated on #{Time.now}. return unless Puppet.features.root? return unless self[:mkusers] - @config.each do |name, element| - next unless element.respond_to?(:owner) - next unless sections.nil? or sections.include?(element.section) + @config.each do |name, setting| + next unless setting.respond_to?(:owner) + next unless sections.nil? or sections.include?(setting.section) - if user = element.owner and user != "root" and catalog.resource(:user, user).nil? + if user = setting.owner and user != "root" and catalog.resource(:user, user).nil? resource = Puppet::Resource.new(:user, user, :ensure => :present) if self[:group] resource[:gid] = self[:group] end catalog.add_resource resource end - if group = element.group and ! %w{root wheel}.include?(group) and catalog.resource(:group, group).nil? + if group = setting.group and ! %w{root wheel}.include?(group) and catalog.resource(:group, group).nil? catalog.add_resource Puppet::Resource.new(:group, group, :ensure => :present) end end @@ -791,7 +795,7 @@ Generated on #{Time.now}. end end - # Return all elements that have associated hooks; this is so + # Return all settings that have associated hooks; this is so # we can call them after parsing the configuration file. def settings_with_hooks @config.values.find_all { |setting| setting.respond_to?(:handle) } @@ -916,246 +920,4 @@ Generated on #{Time.now}. end end end - - # The base element type. - class CElement - attr_accessor :name, :section, :default, :setbycli, :call_on_define - attr_reader :desc, :short - - def desc=(value) - @desc = value.gsub(/^\s*/, '') - end - - # get the arguments in getopt format - def getopt_args - if short - [["--#{name}", "-#{short}", GetoptLong::REQUIRED_ARGUMENT]] - else - [["--#{name}", GetoptLong::REQUIRED_ARGUMENT]] - end - end - - # get the arguments in OptionParser format - def optparse_args - if short - ["--#{name}", "-#{short}", desc, :REQUIRED] - else - ["--#{name}", desc, :REQUIRED] - end - end - - def hook=(block) - meta_def :handle, &block - end - - # Create the new element. Pretty much just sets the name. - def initialize(args = {}) - unless @settings = args.delete(:settings) - raise ArgumentError.new("You must refer to a settings object") - end - - args.each do |param, value| - method = param.to_s + "=" - unless self.respond_to? method - raise ArgumentError, "%s does not accept %s" % [self.class, param] - end - - self.send(method, value) - end - - unless self.desc - raise ArgumentError, "You must provide a description for the %s config option" % self.name - end - end - - def iscreated - @iscreated = true - end - - def iscreated? - if defined? @iscreated - return @iscreated - else - return false - end - end - - def set? - if defined? @value and ! @value.nil? - return true - else - return false - end - end - - # short name for the celement - def short=(value) - if value.to_s.length != 1 - raise ArgumentError, "Short names can only be one character." - end - @short = value.to_s - end - - # Convert the object to a config statement. - def to_config - str = @desc.gsub(/^/, "# ") + "\n" - - # Add in a statement about the default. - if defined? @default and @default - str += "# The default value is '%s'.\n" % @default - end - - # If the value has not been overridden, then print it out commented - # and unconverted, so it's clear that that's the default and how it - # works. - value = @settings.value(self.name) - - if value != @default - line = "%s = %s" % [@name, value] - else - line = "# %s = %s" % [@name, @default] - end - - str += line + "\n" - - str.gsub(/^/, " ") - end - - # Retrieves the value, or if it's not set, retrieves the default. - def value - @settings.value(self.name) - end - end - - # A file. - class CFile < CElement - attr_writer :owner, :group - attr_accessor :mode, :create - - # Should we create files, rather than just directories? - def create_files? - create - end - - def group - if defined? @group - return @settings.convert(@group) - else - return nil - end - end - - def owner - if defined? @owner - return @settings.convert(@owner) - else - return nil - end - end - - # Set the type appropriately. Yep, a hack. This supports either naming - # the variable 'dir', or adding a slash at the end. - def munge(value) - # If it's not a fully qualified path... - if value.is_a?(String) and value !~ /^\$/ and value !~ /^\// and value != 'false' - # Make it one - value = File.join(Dir.getwd, value) - end - if value.to_s =~ /\/$/ - @type = :directory - return value.sub(/\/$/, '') - end - return value - end - - # Return the appropriate type. - def type - value = @settings.value(self.name) - if @name.to_s =~ /dir/ - return :directory - elsif value.to_s =~ /\/$/ - return :directory - elsif value.is_a? String - return :file - else - return nil - end - end - - # Turn our setting thing into a Puppet::Resource instance. - def to_resource - return nil unless type = self.type - - path = self.value - - return nil unless path.is_a?(String) - - # Make sure the paths are fully qualified. - path = File.join(Dir.getwd, path) unless path =~ /^\// - - return nil unless type == :directory or create_files? or File.exist?(path) - return nil if path =~ /^\/dev/ - - resource = Puppet::Resource.new(:file, path) - resource[:mode] = self.mode if self.mode - - if Puppet.features.root? - resource[:owner] = self.owner if self.owner - resource[:group] = self.group if self.group - end - - resource[:ensure] = type - resource[:loglevel] = :debug - resource[:backup] = false - - resource.tag(self.section, self.name, "settings") - - resource - end - - # Make sure any provided variables look up to something. - def validate(value) - return true unless value.is_a? String - value.scan(/\$(\w+)/) { |name| - name = $1 - unless @settings.include?(name) - raise ArgumentError, - "Settings parameter '%s' is undefined" % - name - end - } - end - end - - # A simple boolean. - class CBoolean < CElement - # get the arguments in getopt format - def getopt_args - if short - [["--#{name}", "-#{short}", GetoptLong::NO_ARGUMENT], - ["--no-#{name}", GetoptLong::NO_ARGUMENT]] - else - [["--#{name}", GetoptLong::NO_ARGUMENT], - ["--no-#{name}", GetoptLong::NO_ARGUMENT]] - end - end - - def optparse_args - if short - ["--[no-]#{name}", "-#{short}", desc, :NONE ] - else - ["--[no-]#{name}", desc, :NONE] - end - end - - def munge(value) - case value - when true, "true"; return true - when false, "false"; return false - else - raise ArgumentError, "Invalid value '%s' for %s" % - [value.inspect, @name] - end - end - end end diff --git a/lib/puppet/util/settings/boolean_setting.rb b/lib/puppet/util/settings/boolean_setting.rb new file mode 100644 index 000000000..cc2704c4e --- /dev/null +++ b/lib/puppet/util/settings/boolean_setting.rb @@ -0,0 +1,33 @@ +require 'puppet/util/settings/setting' + +# A simple boolean. +class Puppet::Util::Settings::BooleanSetting < Puppet::Util::Settings::Setting + # get the arguments in getopt format + def getopt_args + if short + [["--#{name}", "-#{short}", GetoptLong::NO_ARGUMENT], + ["--no-#{name}", GetoptLong::NO_ARGUMENT]] + else + [["--#{name}", GetoptLong::NO_ARGUMENT], + ["--no-#{name}", GetoptLong::NO_ARGUMENT]] + end + end + + def optparse_args + if short + ["--[no-]#{name}", "-#{short}", desc, :NONE ] + else + ["--[no-]#{name}", desc, :NONE] + end + end + + def munge(value) + case value + when true, "true"; return true + when false, "false"; return false + else + raise ArgumentError, "Invalid value '%s' for %s" % + [value.inspect, @name] + end + end +end diff --git a/lib/puppet/util/settings/file_setting.rb b/lib/puppet/util/settings/file_setting.rb new file mode 100644 index 000000000..08d8039f4 --- /dev/null +++ b/lib/puppet/util/settings/file_setting.rb @@ -0,0 +1,102 @@ +require 'puppet/util/settings/setting' + +# A file. +class Puppet::Util::Settings::FileSetting < Puppet::Util::Settings::Setting + attr_writer :owner, :group + attr_accessor :mode, :create + + # Should we create files, rather than just directories? + def create_files? + create + end + + def group + if defined? @group + return @settings.convert(@group) + else + return nil + end + end + + def owner + if defined? @owner + return @settings.convert(@owner) + else + return nil + end + end + + # Set the type appropriately. Yep, a hack. This supports either naming + # the variable 'dir', or adding a slash at the end. + def munge(value) + # If it's not a fully qualified path... + if value.is_a?(String) and value !~ /^\$/ and value !~ /^\// and value != 'false' + # Make it one + value = File.join(Dir.getwd, value) + end + if value.to_s =~ /\/$/ + @type = :directory + return value.sub(/\/$/, '') + end + return value + end + + # Return the appropriate type. + def type + value = @settings.value(self.name) + if @name.to_s =~ /dir/ + return :directory + elsif value.to_s =~ /\/$/ + return :directory + elsif value.is_a? String + return :file + else + return nil + end + end + + # Turn our setting thing into a Puppet::Resource instance. + def to_resource + return nil unless type = self.type + + path = self.value + + return nil unless path.is_a?(String) + + # Make sure the paths are fully qualified. + path = File.join(Dir.getwd, path) unless path =~ /^\// + + return nil unless type == :directory or create_files? or File.exist?(path) + return nil if path =~ /^\/dev/ + + resource = Puppet::Resource.new(:file, path) + resource[:mode] = self.mode if self.mode + + if Puppet.features.root? + resource[:owner] = self.owner if self.owner + resource[:group] = self.group if self.group + end + + resource[:ensure] = type + resource[:loglevel] = :debug + resource[:backup] = false + + resource.tag(self.section, self.name, "settings") + + resource + end + + # Make sure any provided variables look up to something. + def validate(value) + return true unless value.is_a? String + value.scan(/\$(\w+)/) { |name| + name = $1 + unless @settings.include?(name) + raise ArgumentError, + "Settings parameter '%s' is undefined" % + name + end + } + end +end + diff --git a/lib/puppet/util/settings/setting.rb b/lib/puppet/util/settings/setting.rb new file mode 100644 index 000000000..e64cfd6c6 --- /dev/null +++ b/lib/puppet/util/settings/setting.rb @@ -0,0 +1,110 @@ +# The base element type. +class Puppet::Util::Settings::Setting + attr_accessor :name, :section, :default, :setbycli, :call_on_define + attr_reader :desc, :short + + def desc=(value) + @desc = value.gsub(/^\s*/, '') + end + + # get the arguments in getopt format + def getopt_args + if short + [["--#{name}", "-#{short}", GetoptLong::REQUIRED_ARGUMENT]] + else + [["--#{name}", GetoptLong::REQUIRED_ARGUMENT]] + end + end + + # get the arguments in OptionParser format + def optparse_args + if short + ["--#{name}", "-#{short}", desc, :REQUIRED] + else + ["--#{name}", desc, :REQUIRED] + end + end + + def hook=(block) + meta_def :handle, &block + end + + # Create the new element. Pretty much just sets the name. + def initialize(args = {}) + unless @settings = args.delete(:settings) + raise ArgumentError.new("You must refer to a settings object") + end + + args.each do |param, value| + method = param.to_s + "=" + unless self.respond_to? method + raise ArgumentError, "%s does not accept %s" % [self.class, param] + end + + self.send(method, value) + end + + unless self.desc + raise ArgumentError, "You must provide a description for the %s config option" % self.name + end + end + + def iscreated + @iscreated = true + end + + def iscreated? + if defined? @iscreated + return @iscreated + else + return false + end + end + + def set? + if defined? @value and ! @value.nil? + return true + else + return false + end + end + + # short name for the celement + def short=(value) + if value.to_s.length != 1 + raise ArgumentError, "Short names can only be one character." + end + @short = value.to_s + end + + # Convert the object to a config statement. + def to_config + str = @desc.gsub(/^/, "# ") + "\n" + + # Add in a statement about the default. + if defined? @default and @default + str += "# The default value is '%s'.\n" % @default + end + + # If the value has not been overridden, then print it out commented + # and unconverted, so it's clear that that's the default and how it + # works. + value = @settings.value(self.name) + + if value != @default + line = "%s = %s" % [@name, value] + else + line = "# %s = %s" % [@name, @default] + end + + str += line + "\n" + + str.gsub(/^/, " ") + end + + # Retrieves the value, or if it's not set, retrieves the default. + def value + @settings.value(self.name) + end +end + diff --git a/spec/integration/defaults.rb b/spec/integration/defaults.rb index 1a73521bd..b0fd7254a 100755 --- a/spec/integration/defaults.rb +++ b/spec/integration/defaults.rb @@ -39,14 +39,14 @@ describe "Puppet defaults" do # See #1232 it "should not specify a user or group for the clientyamldir" do - Puppet.settings.element(:clientyamldir).owner.should be_nil - Puppet.settings.element(:clientyamldir).group.should be_nil + Puppet.settings.setting(:clientyamldir).owner.should be_nil + Puppet.settings.setting(:clientyamldir).group.should be_nil end # See #1232 it "should not specify a user or group for the rundir" do - Puppet.settings.element(:rundir).owner.should be_nil - Puppet.settings.element(:rundir).group.should be_nil + Puppet.settings.setting(:rundir).owner.should be_nil + Puppet.settings.setting(:rundir).group.should be_nil end it "should use a bind address of ''" do @@ -61,7 +61,7 @@ describe "Puppet defaults" do [:modulepath, :factpath].each do |setting| it "should configure '#{setting}' not to be a file setting, so multi-directory settings are acceptable" do - Puppet.settings.element(setting).should be_instance_of(Puppet::Util::Settings::CElement) + Puppet.settings.setting(setting).should be_instance_of(Puppet::Util::Settings::Setting) end end diff --git a/spec/unit/util/settings.rb b/spec/unit/util/settings.rb index 5e9ab3a18..903779915 100755 --- a/spec/unit/util/settings.rb +++ b/spec/unit/util/settings.rb @@ -47,8 +47,8 @@ describe Puppet::Util::Settings do end it "should support specifying the setting type" do - @settings.setdefaults(:section, :myvalue => {:default => "w", :desc => "b", :type => :element}) - @settings.element(:myvalue).should be_instance_of(Puppet::Util::Settings::CElement) + @settings.setdefaults(:section, :myvalue => {:default => "/w", :desc => "b", :type => :setting}) + @settings.setting(:myvalue).should be_instance_of(Puppet::Util::Settings::Setting) end it "should fail if an invalid setting type is specified" do @@ -161,7 +161,7 @@ describe Puppet::Util::Settings do values.should == %w{test/yay} end - it "should munge values using the element-specific methods" do + it "should munge values using the setting-specific methods" do @settings[:bool] = "false" @settings[:bool].should == false end @@ -602,7 +602,7 @@ describe Puppet::Util::Settings do it "should ignore files whose :to_resource method returns nil" do @settings.setdefaults :main, :maindir => ["/maindir", "a"] - @settings.element(:maindir).expects(:to_resource).returns nil + @settings.setting(:maindir).expects(:to_resource).returns nil Puppet::Resource::Catalog.any_instance.expects(:add_resource).never @settings.to_catalog @@ -708,8 +708,8 @@ describe Puppet::Util::Settings do main.expects(:to_manifest).returns "maindir" second = stub 'second_resource', :ref => "File[/seconddir]" second.expects(:to_manifest).returns "seconddir" - @settings.element(:maindir).expects(:to_resource).returns main - @settings.element(:seconddir).expects(:to_resource).returns second + @settings.setting(:maindir).expects(:to_resource).returns main + @settings.setting(:seconddir).expects(:to_resource).returns second @settings.to_manifest.split("\n\n").sort.should == %w{maindir seconddir} end @@ -967,113 +967,3 @@ describe Puppet::Util::Settings do end end end - -describe Puppet::Util::Settings::CFile do - it "should be able to be converted into a resource" do - Puppet::Util::Settings::CFile.new(:settings => mock("settings"), :desc => "eh").should respond_to(:to_resource) - end - - describe "when being converted to a resource" do - before do - @settings = mock 'settings' - @file = Puppet::Util::Settings::CFile.new(:settings => @settings, :desc => "eh", :name => :mydir, :section => "mysect") - @settings.stubs(:value).with(:mydir).returns "/my/file" - end - - it "should skip files that cannot determine their types" do - @file.expects(:type).returns nil - @file.to_resource.should be_nil - end - - it "should skip non-existent files if 'create_files' is not enabled" do - @file.expects(:create_files?).returns false - @file.expects(:type).returns :file - File.expects(:exist?).with("/my/file").returns false - @file.to_resource.should be_nil - end - - it "should manage existent files even if 'create_files' is not enabled" do - @file.expects(:create_files?).returns false - @file.expects(:type).returns :file - File.expects(:exist?).with("/my/file").returns true - @file.to_resource.should be_instance_of(Puppet::Resource) - end - - it "should skip files in /dev" do - @settings.stubs(:value).with(:mydir).returns "/dev/file" - @file.to_resource.should be_nil - end - - it "should skip files whose paths are not strings" do - @settings.stubs(:value).with(:mydir).returns :foo - @file.to_resource.should be_nil - end - - it "should return a file resource with the path set appropriately" do - resource = @file.to_resource - resource.type.should == "File" - resource.title.should == "/my/file" - end - - it "should fully qualified returned files if necessary (#795)" do - @settings.stubs(:value).with(:mydir).returns "myfile" - @file.to_resource.title.should == File.join(Dir.getwd, "myfile") - end - - it "should set the mode on the file if a mode is provided" do - @file.mode = 0755 - - @file.to_resource[:mode].should == 0755 - end - - it "should set the owner if running as root and the owner is provided" do - Puppet.features.expects(:root?).returns true - @file.stubs(:owner).returns "foo" - @file.to_resource[:owner].should == "foo" - end - - it "should set the group if running as root and the group is provided" do - Puppet.features.expects(:root?).returns true - @file.stubs(:group).returns "foo" - @file.to_resource[:group].should == "foo" - end - - it "should not set owner if not running as root" do - Puppet.features.expects(:root?).returns false - @file.stubs(:owner).returns "foo" - @file.to_resource[:owner].should be_nil - end - - it "should not set group if not running as root" do - Puppet.features.expects(:root?).returns false - @file.stubs(:group).returns "foo" - @file.to_resource[:group].should be_nil - end - - it "should set :ensure to the file type" do - @file.expects(:type).returns :directory - @file.to_resource[:ensure].should == :directory - end - - it "should set the loglevel to :debug" do - @file.to_resource[:loglevel].should == :debug - end - - it "should set the backup to false" do - @file.to_resource[:backup].should be_false - end - - it "should tag the resource with the settings section" do - @file.expects(:section).returns "mysect" - @file.to_resource.should be_tagged("mysect") - end - - it "should tag the resource with the setting name" do - @file.to_resource.should be_tagged("mydir") - end - - it "should tag the resource with 'settings'" do - @file.to_resource.should be_tagged("settings") - end - end -end diff --git a/spec/unit/util/settings/file_setting.rb b/spec/unit/util/settings/file_setting.rb new file mode 100755 index 000000000..2cae56fb7 --- /dev/null +++ b/spec/unit/util/settings/file_setting.rb @@ -0,0 +1,117 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +require 'puppet/util/settings' +require 'puppet/util/settings/file_setting' + +describe Puppet::Util::Settings::FileSetting do + it "should be able to be converted into a resource" do + Puppet::Util::Settings::FileSetting.new(:settings => mock("settings"), :desc => "eh").should respond_to(:to_resource) + end + + describe "when being converted to a resource" do + before do + @settings = mock 'settings' + @file = Puppet::Util::Settings::FileSetting.new(:settings => @settings, :desc => "eh", :name => :mydir, :section => "mysect") + @settings.stubs(:value).with(:mydir).returns "/my/file" + end + + it "should skip files that cannot determine their types" do + @file.expects(:type).returns nil + @file.to_resource.should be_nil + end + + it "should skip non-existent files if 'create_files' is not enabled" do + @file.expects(:create_files?).returns false + @file.expects(:type).returns :file + File.expects(:exist?).with("/my/file").returns false + @file.to_resource.should be_nil + end + + it "should manage existent files even if 'create_files' is not enabled" do + @file.expects(:create_files?).returns false + @file.expects(:type).returns :file + File.expects(:exist?).with("/my/file").returns true + @file.to_resource.should be_instance_of(Puppet::Resource) + end + + it "should skip files in /dev" do + @settings.stubs(:value).with(:mydir).returns "/dev/file" + @file.to_resource.should be_nil + end + + it "should skip files whose paths are not strings" do + @settings.stubs(:value).with(:mydir).returns :foo + @file.to_resource.should be_nil + end + + it "should return a file resource with the path set appropriately" do + resource = @file.to_resource + resource.type.should == "File" + resource.title.should == "/my/file" + end + + it "should fully qualified returned files if necessary (#795)" do + @settings.stubs(:value).with(:mydir).returns "myfile" + @file.to_resource.title.should == File.join(Dir.getwd, "myfile") + end + + it "should set the mode on the file if a mode is provided" do + @file.mode = 0755 + + @file.to_resource[:mode].should == 0755 + end + + it "should set the owner if running as root and the owner is provided" do + Puppet.features.expects(:root?).returns true + @file.stubs(:owner).returns "foo" + @file.to_resource[:owner].should == "foo" + end + + it "should set the group if running as root and the group is provided" do + Puppet.features.expects(:root?).returns true + @file.stubs(:group).returns "foo" + @file.to_resource[:group].should == "foo" + end + + it "should not set owner if not running as root" do + Puppet.features.expects(:root?).returns false + @file.stubs(:owner).returns "foo" + @file.to_resource[:owner].should be_nil + end + + it "should not set group if not running as root" do + Puppet.features.expects(:root?).returns false + @file.stubs(:group).returns "foo" + @file.to_resource[:group].should be_nil + end + + it "should set :ensure to the file type" do + @file.expects(:type).returns :directory + @file.to_resource[:ensure].should == :directory + end + + it "should set the loglevel to :debug" do + @file.to_resource[:loglevel].should == :debug + end + + it "should set the backup to false" do + @file.to_resource[:backup].should be_false + end + + it "should tag the resource with the settings section" do + @file.expects(:section).returns "mysect" + @file.to_resource.should be_tagged("mysect") + end + + it "should tag the resource with the setting name" do + @file.to_resource.should be_tagged("mydir") + end + + it "should tag the resource with 'settings'" do + @file.to_resource.should be_tagged("settings") + end + end +end + diff --git a/test/util/settings.rb b/test/util/settings.rb index 6d5f6382a..c9e324564 100755 --- a/test/util/settings.rb +++ b/test/util/settings.rb @@ -10,8 +10,8 @@ require 'puppettest/parsertesting' class TestSettings < Test::Unit::TestCase include PuppetTest include PuppetTest::ParserTesting - CElement = Puppet::Util::Settings::CElement - CBoolean = Puppet::Util::Settings::CBoolean + Setting = Puppet::Util::Settings::Setting + BooleanSetting = Puppet::Util::Settings::BooleanSetting def setup super @@ -462,9 +462,9 @@ yay = /a/path end def test_correct_type_assumptions - file = Puppet::Util::Settings::CFile - element = Puppet::Util::Settings::CElement - bool = Puppet::Util::Settings::CBoolean + file = Puppet::Util::Settings::FileSetting + element = Puppet::Util::Settings::Setting + bool = Puppet::Util::Settings::BooleanSetting # We have to keep these ordered, unfortunately. [ @@ -669,12 +669,12 @@ yay = /a/path def test_celement_short_name element = nil assert_nothing_raised("Could not create celement") do - element = CElement.new :short => "n", :desc => "anything", :settings => Puppet::Util::Settings.new + element = Setting.new :short => "n", :desc => "anything", :settings => Puppet::Util::Settings.new end assert_equal("n", element.short, "Short value is not retained") assert_raise(ArgumentError,"Allowed multicharactered short names.") do - element = CElement.new :short => "no", :desc => "anything", :settings => Puppet::Util::Settings.new + element = Setting.new :short => "no", :desc => "anything", :settings => Puppet::Util::Settings.new end end @@ -699,13 +699,13 @@ yay = /a/path # Tell getopt which arguments are valid def test_get_getopt_args - element = CElement.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new + element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") element.short = "n" assert_equal([["--foo", "-n", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") - element = CBoolean.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new + element = BooleanSetting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new assert_equal([["--foo", GetoptLong::NO_ARGUMENT], ["--no-foo", GetoptLong::NO_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") |