diff options
164 files changed, 4158 insertions, 848 deletions
@@ -1,5 +1,6 @@ -2.6.7rc1 -======== +2.6.7 +===== +17f673d Updated CHANGELOG for 2.6.7rc1 852fb97 (#5073) Download plugins even if you're filtering on tags 4f34dbf Fix #5610: Prevent unnecessary RAL lookups 9781032 Revert "Merge branch 'ticket/2.6.x/5605' of git://github.com/stschulte/puppet into 2.6.next" diff --git a/lib/puppet/application/kick.rb b/lib/puppet/application/kick.rb index 604132818..da93c0182 100644 --- a/lib/puppet/application/kick.rb +++ b/lib/puppet/application/kick.rb @@ -245,7 +245,7 @@ License out = %x{ping -c 1 #{host}} unless $CHILD_STATUS == 0 $stderr.print "Could not contact #{host}\n" - next + exit($CHILD_STATUS) end end diff --git a/lib/puppet/application/master.rb b/lib/puppet/application/master.rb index 3bfad89f4..78499a92a 100644 --- a/lib/puppet/application/master.rb +++ b/lib/puppet/application/master.rb @@ -222,7 +222,7 @@ License exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? - Puppet.settings.use :main, :master, :ssl + Puppet.settings.use :main, :master, :ssl, :metrics # Cache our nodes in yaml. Currently not configurable. Puppet::Node.indirection.cache_class = :yaml diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index f308d4476..989ef3f35 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -474,6 +474,7 @@ module Puppet setdefaults(:metrics, :rrddir => {:default => "$vardir/rrd", + :mode => 0750, :owner => "service", :group => "service", :desc => "The directory where RRD database files are stored. diff --git a/lib/puppet/external/nagios/parser.rb b/lib/puppet/external/nagios/parser.rb index 5504f5818..17db5e307 100644 --- a/lib/puppet/external/nagios/parser.rb +++ b/lib/puppet/external/nagios/parser.rb @@ -753,7 +753,7 @@ module_eval <<'.,.,', 'grammar.ry', 40 module_eval <<'.,.,', 'grammar.ry', 42 def _reduce_10( val, _values, result ) -result = {val[0],val[1]} +result = {val[0] => val[1]} result end .,., diff --git a/lib/puppet/file_serving/fileset.rb b/lib/puppet/file_serving/fileset.rb index fdbcf93a3..c020f036d 100644 --- a/lib/puppet/file_serving/fileset.rb +++ b/lib/puppet/file_serving/fileset.rb @@ -59,6 +59,7 @@ class Puppet::FileServing::Fileset end def initialize(path, options = {}) + path = path.chomp(File::SEPARATOR) raise ArgumentError.new("Fileset paths must be fully qualified") unless File.expand_path(path) == path @path = path diff --git a/lib/puppet/indirector/exec.rb b/lib/puppet/indirector/exec.rb index fa789442b..4683eda0f 100644 --- a/lib/puppet/indirector/exec.rb +++ b/lib/puppet/indirector/exec.rb @@ -35,8 +35,7 @@ class Puppet::Indirector::Exec < Puppet::Indirector::Terminus begin output = execute(external_command) rescue Puppet::ExecutionFailure => detail - Puppet.err "Failed to find #{name} via exec: #{detail}" - return nil + raise Puppet::Error, "Failed to find #{name} via exec: #{detail}" end if output =~ /\A\s*\Z/ # all whitespace diff --git a/lib/puppet/indirector/resource_type/parser.rb b/lib/puppet/indirector/resource_type/parser.rb index 8b1bed0a9..4bcaf3f47 100644 --- a/lib/puppet/indirector/resource_type/parser.rb +++ b/lib/puppet/indirector/resource_type/parser.rb @@ -10,7 +10,10 @@ class Puppet::Indirector::ResourceType::Parser < Puppet::Indirector::Code # This is a bit ugly. [:hostclass, :definition, :node].each do |type| - if r = krt.send(type, request.key) + # We have to us 'find_<type>' here because it will + # load any missing types from disk, whereas the plain + # '<type>' method only returns from memory. + if r = krt.send("find_#{type}", [""], request.key) return r end end @@ -18,9 +21,22 @@ class Puppet::Indirector::ResourceType::Parser < Puppet::Indirector::Code end def search(request) - raise ArgumentError, "Only '*' is acceptable as a search request" unless request.key == "*" krt = request.environment.known_resource_types - result = [krt.hostclasses.values, krt.definitions.values, krt.nodes.values].flatten + # Make sure we've got all of the types loaded. + krt.loader.import_all + result = [krt.hostclasses.values, krt.definitions.values, krt.nodes.values].flatten.reject { |t| t.name == "" } + return nil if result.empty? + return result if request.key == "*" + + # Strip the regex of any wrapping slashes that might exist + key = request.key.sub(/^\//, '').sub(/\/$/, '') + begin + regex = Regexp.new(key) + rescue => detail + raise ArgumentError, "Invalid regex '#{request.key}': #{detail}" + end + + result.reject! { |t| t.name.to_s !~ regex } return nil if result.empty? result end diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb index 43266b2b5..059591ed8 100644 --- a/lib/puppet/module.rb +++ b/lib/puppet/module.rb @@ -138,7 +138,7 @@ class Puppet::Module # Find this module in the modulepath. def path - environment.modulepath.collect { |path| File.join(path, name) }.find { |d| FileTest.exist?(d) } + environment.modulepath.collect { |path| File.join(path, name) }.find { |d| FileTest.directory?(d) } end # Find all plugin directories. This is used by the Plugins fileserving mount. diff --git a/lib/puppet/node/environment.rb b/lib/puppet/node/environment.rb index 7877b7f73..d50530618 100644 --- a/lib/puppet/node/environment.rb +++ b/lib/puppet/node/environment.rb @@ -76,7 +76,7 @@ class Puppet::Node::Environment # per environment semantics with an efficient most common cases; we almost # always just return our thread's known-resource types. Only at the start # of a compilation (after our thread var has been set to nil) or when the - # environment has changed do we delve deeper. + # environment has changed do we delve deeper. Thread.current[:known_resource_types] = nil if (krt = Thread.current[:known_resource_types]) && krt.environment != self Thread.current[:known_resource_types] ||= synchronize { if @known_resource_types.nil? or @known_resource_types.stale? @@ -128,7 +128,7 @@ class Puppet::Node::Environment to_s.to_sym end - # The only thing we care about when serializing an environment is its + # The only thing we care about when serializing an environment is its # identity; everything else is ephemeral and should not be stored or # transmitted. def to_zaml(z) @@ -156,7 +156,6 @@ class Puppet::Node::Environment parser.string = code else file = Puppet.settings.value(:manifest, name.to_s) - return empty_parse_result unless File.exist?(file) parser.file = file end parser.parse diff --git a/lib/puppet/parameter.rb b/lib/puppet/parameter.rb index ff7cab22b..29d60fc66 100644 --- a/lib/puppet/parameter.rb +++ b/lib/puppet/parameter.rb @@ -300,3 +300,5 @@ class Puppet::Parameter name.to_s end end + +require 'puppet/parameter/path' diff --git a/lib/puppet/parameter/path.rb b/lib/puppet/parameter/path.rb new file mode 100644 index 000000000..44886afd0 --- /dev/null +++ b/lib/puppet/parameter/path.rb @@ -0,0 +1,42 @@ +require 'puppet/parameter' + +class Puppet::Parameter::Path < Puppet::Parameter + def self.accept_arrays(bool = true) + @accept_arrays = !!bool + end + def self.arrays? + @accept_arrays + end + + def validate_path(paths) + if paths.is_a?(Array) and ! self.class.arrays? then + fail "#{name} only accepts a single path, not an array of paths" + end + + # We *always* support Unix path separators, as Win32 does now too. + absolute = "[/#{::Regexp.quote(::File::SEPARATOR)}]" + win32 = Puppet.features.microsoft_windows? + + Array(paths).each do |path| + next if path =~ %r{^#{absolute}} + next if win32 and path =~ %r{^(?:[a-zA-Z]:)?#{absolute}} + fail("#{name} must be a fully qualified path") + end + + paths + end + + # This will be overridden if someone uses the validate option, which is why + # it just delegates to the other, useful, method. + def unsafe_validate(paths) + validate_path(paths) + end + + # Likewise, this might be overridden, but by default... + def unsafe_munge(paths) + if paths.is_a?(Array) and ! self.class.arrays? then + fail "#{name} only accepts a single path, not an array of paths" + end + paths + end +end diff --git a/lib/puppet/parser/compiler.rb b/lib/puppet/parser/compiler.rb index fdabd05c9..a891f1a11 100644 --- a/lib/puppet/parser/compiler.rb +++ b/lib/puppet/parser/compiler.rb @@ -20,7 +20,7 @@ class Puppet::Parser::Compiler puts detail.backtrace if Puppet[:trace] raise Puppet::Error, "#{detail} on node #{node.name}" ensure - # We get these from the environment and only cache them in a thread + # We get these from the environment and only cache them in a thread # variable for the duration of the compilation. Thread.current[:known_resource_types] = nil Thread.current[:env_module_directories] = nil @@ -133,12 +133,11 @@ class Puppet::Parser::Compiler end # Evaluate each specified class in turn. If there are any classes we can't - # find, just tag the catalog and move on. This method really just - # creates resource objects that point back to the classes, and then the - # resources are themselves evaluated later in the process. + # find, raise an error. This method really just creates resource objects + # that point back to the classes, and then the resources are themselves + # evaluated later in the process. def evaluate_classes(classes, scope, lazy_evaluate = true) raise Puppet::DevError, "No source for scope passed to evaluate_classes" unless scope.source - found = [] param_classes = nil # if we are a param class, save the classes hash # and transform classes to be the keys @@ -153,20 +152,17 @@ class Puppet::Parser::Compiler if param_classes resource = klass.ensure_in_catalog(scope, param_classes[name] || {}) else - found << name and next if scope.class_scope(klass) + next if scope.class_scope(klass) resource = klass.ensure_in_catalog(scope) end # If they've disabled lazy evaluation (which the :include function does), # then evaluate our resource immediately. resource.evaluate unless lazy_evaluate - found << name else - Puppet.info "Could not find class #{name} for #{node.name}" - @catalog.tag(name) + raise Puppet::Error, "Could not find class #{name} for #{node.name}" end end - found end def evaluate_relationships diff --git a/lib/puppet/parser/functions/create_resources.rb b/lib/puppet/parser/functions/create_resources.rb new file mode 100644 index 000000000..430f110b4 --- /dev/null +++ b/lib/puppet/parser/functions/create_resources.rb @@ -0,0 +1,47 @@ +Puppet::Parser::Functions::newfunction(:create_resources, :doc => ' +Converts a hash into a set of resources and adds them to the catalog. +Takes two parameters: + create_resource($type, $resources) + Creates resources of type $type from the $resources hash. Assumes that + hash is in the following form: + {title=>{parameters}} + This is currently tested for defined resources, classes, as well as native types +') do |args| + raise ArgumentError, ("create_resources(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2 + #raise ArgumentError, 'requires resource type and param hash' if args.size < 2 + # figure out what kind of resource we are + type_of_resource = nil + type_name = args[0].downcase + if type_name == 'class' + type_of_resource = :class + else + if resource = Puppet::Type.type(type_name.to_sym) + type_of_resource = :type + elsif resource = find_definition(type_name.downcase) + type_of_resource = :define + else + raise ArgumentError, "could not create resource of unknown type #{type_name}" + end + end + # iterate through the resources to create + args[1].each do |title, params| + raise ArgumentError, 'params should not contain title' if(params['title']) + case type_of_resource + when :type + res = resource.hash2resource(params.merge(:title => title)) + catalog.add_resource(res) + when :define + p_resource = Puppet::Parser::Resource.new(type_name, title, :scope => self, :source => resource) + params.merge(:name => title).each do |k,v| + p_resource.set_parameter(k,v) + end + resource.instantiate_resource(self, p_resource) + compiler.add_resource(self, p_resource) + when :class + klass = find_hostclass(title) + raise ArgumentError, "could not find hostclass #{title}" unless klass + klass.ensure_in_catalog(self, params) + compiler.catalog.add_class([title]) + end + end +end diff --git a/lib/puppet/parser/functions/fqdn_rand.rb b/lib/puppet/parser/functions/fqdn_rand.rb index 52946f2c1..91157a148 100644 --- a/lib/puppet/parser/functions/fqdn_rand.rb +++ b/lib/puppet/parser/functions/fqdn_rand.rb @@ -5,8 +5,8 @@ Puppet::Parser::Functions::newfunction(:fqdn_rand, :type => :rvalue, :doc => $random_number = fqdn_rand(30) $random_number_seed = fqdn_rand(30,30)") do |args| - require 'md5' + require 'digest/md5' max = args.shift - srand MD5.new([lookupvar('fqdn'),args].join(':')).to_s.hex + srand(Digest::MD5.hexdigest([lookupvar('fqdn'),args].join(':')).hex) rand(max).to_s end diff --git a/lib/puppet/parser/lexer.rb b/lib/puppet/parser/lexer.rb index 9a25263f6..2f416615e 100644 --- a/lib/puppet/parser/lexer.rb +++ b/lib/puppet/parser/lexer.rb @@ -312,7 +312,8 @@ class Puppet::Parser::Lexer def file=(file) @file = file @line = 1 - @scanner = StringScanner.new(File.read(file)) + contents = File.exists?(file) ? File.read(file) : "" + @scanner = StringScanner.new(contents) end def shift_token @@ -532,7 +533,7 @@ class Puppet::Parser::Lexer when 'n'; "\n" when 't'; "\t" when 's'; " " - when "\n": '' + when "\n"; '' else ch end else @@ -547,7 +548,7 @@ class Puppet::Parser::Lexer value,terminator = slurpstring('"$') token_queue << [TOKENS[token_type[terminator]],preamble+value] if terminator != '$' or @scanner.scan(/\{/) - token_queue.shift + token_queue.shift elsif var_name = @scanner.scan(%r{(\w*::)*\w+|[0-9]}) token_queue << [TOKENS[:VARIABLE],var_name] tokenize_interpolated_string(DQ_continuation_token_types) diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb index 746aa0f90..7888fe1dc 100644 --- a/lib/puppet/parser/parser_support.rb +++ b/lib/puppet/parser/parser_support.rb @@ -79,7 +79,6 @@ class Puppet::Parser::Parser unless file =~ /\.pp$/ file = file + ".pp" end - raise Puppet::Error, "Could not find file #{file}" unless FileTest.exist?(file) end raise Puppet::AlreadyImportedError, "Import loop detected" if known_resource_types.watching_file?(file) diff --git a/lib/puppet/parser/type_loader.rb b/lib/puppet/parser/type_loader.rb index 140c9f2ca..1fba73d0b 100644 --- a/lib/puppet/parser/type_loader.rb +++ b/lib/puppet/parser/type_loader.rb @@ -92,6 +92,34 @@ class Puppet::Parser::TypeLoader end end + def import_all + require 'find' + + module_names = [] + # Collect the list of all known modules + environment.modulepath.each do |path| + Dir.chdir(path) do + Dir.glob("*").each do |dir| + next unless FileTest.directory?(dir) + module_names << dir + end + end + end + + module_names.uniq! + # And then load all files from each module, but (relying on system + # behavior) only load files from the first module of a given name. E.g., + # given first/foo and second/foo, only files from first/foo will be loaded. + module_names.each do |name| + mod = Puppet::Module.new(name, environment) + Find.find(File.join(mod.path, "manifests")) do |path| + if path =~ /\.pp$/ or path =~ /\.rb$/ + import(path) + end + end + end + end + def known_resource_types environment.known_resource_types end diff --git a/lib/puppet/provider/aixobject.rb b/lib/puppet/provider/aixobject.rb new file mode 100755 index 000000000..9506c67a2 --- /dev/null +++ b/lib/puppet/provider/aixobject.rb @@ -0,0 +1,393 @@ +# +# Common code for AIX providers. This class implements basic structure for +# AIX resources. +# Author:: Hector Rivas Gandara <keymon@gmail.com> +# +class Puppet::Provider::AixObject < Puppet::Provider + desc "Generic AIX resource provider" + + # The real provider must implement these functions. + def lscmd(value=@resource[:name]) + raise Puppet::Error, "Method not defined #{@resource.class.name} #{@resource.name}: #{detail}" + end + + def lscmd(value=@resource[:name]) + raise Puppet::Error, "Method not defined #{@resource.class.name} #{@resource.name}: #{detail}" + end + + def addcmd(extra_attrs = []) + raise Puppet::Error, "Method not defined #{@resource.class.name} #{@resource.name}: #{detail}" + end + + def modifycmd(attributes_hash) + raise Puppet::Error, "Method not defined #{@resource.class.name} #{@resource.name}: #{detail}" + end + + def deletecmd + raise Puppet::Error, "Method not defined #{@resource.class.name} #{@resource.name}: #{detail}" + end + + # Valid attributes to be managed by this provider. + # It is a list of hashes + # :aix_attr AIX command attribute name + # :puppet_prop Puppet propertie name + # :to Optional. Method name that adapts puppet property to aix command value. + # :from Optional. Method to adapt aix command line value to puppet property. Optional + class << self + attr_accessor :attribute_mapping + end + + # Mapping from Puppet property to AIX attribute. + def self.attribute_mapping_to + if ! @attribute_mapping_to + @attribute_mapping_to = {} + attribute_mapping.each { |elem| + attribute_mapping_to[elem[:puppet_prop]] = { + :key => elem[:aix_attr], + :method => elem[:to] + } + } + end + @attribute_mapping_to + end + + # Mapping from AIX attribute to Puppet property. + def self.attribute_mapping_from + if ! @attribute_mapping_from + @attribute_mapping_from = {} + attribute_mapping.each { |elem| + attribute_mapping_from[elem[:aix_attr]] = { + :key => elem[:puppet_prop], + :method => elem[:from] + } + } + end + @attribute_mapping_from + end + + # This functions translates a key and value using the given mapping. + # Mapping can be nil (no translation) or a hash with this format + # {:key => new_key, :method => translate_method} + # It returns a list with the pair [key, value] + def translate_attr(key, value, mapping) + return [key, value] unless mapping + return nil unless mapping[key] + + if mapping[key][:method] + new_value = method(mapping[key][:method]).call(value) + else + new_value = value + end + [mapping[key][:key], new_value] + end + + # Loads an AIX attribute (key=value) and stores it in the given hash with + # puppet semantics. It translates the pair using the given mapping. + # + # This operation works with each property one by one, + # subclasses must reimplement this if more complex operations are needed + def load_attribute(key, value, mapping, objectinfo) + if mapping.nil? + objectinfo[key] = value + elsif mapping[key].nil? + # is not present in mapping, ignore it. + true + elsif mapping[key][:method].nil? + objectinfo[mapping[key][:key]] = value + elsif + objectinfo[mapping[key][:key]] = method(mapping[key][:method]).call(value) + end + + return objectinfo + end + + # Gets the given command line argument for the given key and value, + # using the given mapping to translate key and value. + # All the objectinfo hash (@resource or @property_hash) is passed. + # + # This operation works with each property one by one, + # and default behaviour is return the arguments as key=value pairs. + # Subclasses must reimplement this if more complex operations/arguments + # are needed + # + def get_arguments(key, value, mapping, objectinfo) + if mapping.nil? + new_key = key + new_value = value + elsif mapping[key].nil? + # is not present in mapping, ignore it. + new_key = nil + new_value = nil + elsif mapping[key][:method].nil? + new_key = mapping[key][:key] + new_value = value + elsif + new_key = mapping[key][:key] + new_value = method(mapping[key][:method]).call(value) + end + + # convert it to string + new_value = Array(new_value).join(',') + + if new_key + return [ "#{new_key}=#{new_value}" ] + else + return [] + end + end + + # Convert the provider properties (hash) to AIX command arguments + # (list of strings) + # This function will translate each value/key and generate the argument using + # the get_arguments function. + def hash2args(hash, mapping=self.class.attribute_mapping_to) + return "" unless hash + arg_list = [] + hash.each {|key, val| + arg_list += self.get_arguments(key, val, mapping, hash) + } + arg_list + end + + # Parse AIX command attributes from the output of an AIX command, that + # which format is a list of space separated of key=value pairs: + # "uid=100 groups=a,b,c". + # It returns an hash. + # + # If a mapping is provided, the keys are translated as defined in the + # mapping hash. And only values included in mapping will be added + # + # NOTE: it will ignore the items not including '=' + def parse_attr_list(str, mapping=self.class.attribute_mapping_from) + properties = {} + attrs = [] + if !str or (attrs = str.split()).empty? + return nil + end + + attrs.each { |i| + if i.include? "=" # Ignore if it does not include '=' + (key_str, val) = i.split('=') + # Check the key + if !key_str or key_str.empty? + info "Empty key in string 'i'?" + continue + end + key = key_str.to_sym + + properties = self.load_attribute(key, val, mapping, properties) + end + } + properties.empty? ? nil : properties + end + + # Parse AIX command output in a colon separated list of attributes, + # This function is useful to parse the output of commands like lsfs -c: + # #MountPoint:Device:Vfs:Nodename:Type:Size:Options:AutoMount:Acct + # /:/dev/hd4:jfs2::bootfs:557056:rw:yes:no + # /home:/dev/hd1:jfs2:::2129920:rw:yes:no + # /usr:/dev/hd2:jfs2::bootfs:9797632:rw:yes:no + # + # If a mapping is provided, the keys are translated as defined in the + # mapping hash. And only values included in mapping will be added + def parse_colon_list(str, key_list, mapping=self.class.attribute_mapping_from) + properties = {} + attrs = [] + if !str or (attrs = str.split(':')).empty? + return nil + end + + attrs.each { |val| + key = key_list.shift.downcase.to_sym + properties = self.load_attribute(key, val, mapping, properties) + } + properties.empty? ? nil : properties + + end + + # Default parsing function for AIX commands. + # It will choose the method depending of the first line. + # For the colon separated list it will: + # 1. Get keys from first line. + # 2. Parse next line. + def parse_command_output(output, mapping=self.class.attribute_mapping_from) + lines = output.split("\n") + # if it begins with #something:... is a colon separated list. + if lines[0] =~ /^#.*:/ + self.parse_colon_list(lines[1], lines[0][1..-1].split(':'), mapping) + else + self.parse_attr_list(lines[0], mapping) + end + end + + # Retrieve all the information of an existing resource. + # It will execute 'lscmd' command and parse the output, using the mapping + # 'attribute_mapping_from' to translate the keys and values. + def getinfo(refresh = false) + if @objectinfo.nil? or refresh == true + # Execute lsuser, split all attributes and add them to a dict. + begin + output = execute(self.lscmd) + @objectinfo = self.parse_command_output(execute(self.lscmd)) + # All attributtes without translation + @objectosinfo = self.parse_command_output(execute(self.lscmd), nil) + rescue Puppet::ExecutionFailure => detail + # Print error if needed. FIXME: Do not check the user here. + Puppet.debug "aix.getinfo(): Could not find #{@resource.class.name} #{@resource.name}: #{detail}" + end + end + @objectinfo + end + + # Like getinfo, but it will not use the mapping to translate the keys and values. + # It might be usefult to retrieve some raw information. + def getosinfo(refresh = false) + if @objectosinfo .nil? or refresh == true + getinfo(refresh) + end + @objectosinfo + end + + + # List all elements of given type. It works for colon separated commands and + # list commands. + # It returns a list of names. + def list_all + names = [] + begin + output = execute(self.lsallcmd()).split('\n') + (output.select{ |l| l != /^#/ }).each { |v| + name = v.split(/[ :]/) + names << name if not name.empty? + } + rescue Puppet::ExecutionFailure => detail + # Print error if needed + Puppet.debug "aix.list_all(): Could not get all resources of type #{@resource.class.name}: #{detail}" + end + names + end + + + #------------- + # Provider API + # ------------ + + # Clear out the cached values. + def flush + @property_hash.clear if @property_hash + @objectinfo.clear if @objectinfo + end + + # Check that the user exists + def exists? + !!getinfo(true) # !! => converts to bool + end + + # Return all existing instances + # The method for returning a list of provider instances. Note that it returns + # providers, preferably with values already filled in, not resources. + def self.instances + objects=[] + self.list_all().each { |entry| + objects << new(:name => entry, :ensure => :present) + } + objects + end + + #- **ensure** + # The basic state that the object should be in. Valid values are + # `present`, `absent`, `role`. + # From ensurable: exists?, create, delete + def ensure + if exists? + :present + else + :absent + end + end + + # Create a new instance of the resource + def create + if exists? + info "already exists" + # The object already exists + return nil + end + + begin + execute(self.addcmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not create #{@resource.class.name} #{@resource.name}: #{detail}" + end + end + + # Delete this instance of the resource + def delete + unless exists? + info "already absent" + # the object already doesn't exist + return nil + end + + begin + execute(self.deletecmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not delete #{@resource.class.name} #{@resource.name}: #{detail}" + end + end + + #-------------------------------- + # Call this method when the object is initialized. + # It creates getter/setter methods for each property our resource type supports. + # If setter or getter already defined it will not be overwritten + def self.mk_resource_methods + [resource_type.validproperties, resource_type.parameters].flatten.each do |prop| + next if prop == :ensure + define_method(prop) { get(prop) || :absent} unless public_method_defined?(prop) + define_method(prop.to_s + "=") { |*vals| set(prop, *vals) } unless public_method_defined?(prop.to_s + "=") + end + end + + # Define the needed getters and setters as soon as we know the resource type + def self.resource_type=(resource_type) + super + mk_resource_methods + end + + # Retrieve a specific value by name. + def get(param) + (hash = getinfo(false)) ? hash[param] : nil + end + + # Set a property. + def set(param, value) + @property_hash[symbolize(param)] = value + + if getinfo().nil? + # This is weird... + raise Puppet::Error, "Trying to update parameter '#{param}' to '#{value}' for a resource that does not exists #{@resource.class.name} #{@resource.name}: #{detail}" + end + if value == getinfo()[param.to_sym] + return + end + + #self.class.validate(param, value) + if cmd = modifycmd({param =>value}) + begin + execute(cmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}" + end + end + + # Refresh de info. + hash = getinfo(true) + end + + def initialize(resource) + super + @objectinfo = nil + @objectosinfo = nil + end + +end diff --git a/lib/puppet/provider/augeas/augeas.rb b/lib/puppet/provider/augeas/augeas.rb index 461968245..5488c6674 100644 --- a/lib/puppet/provider/augeas/augeas.rb +++ b/lib/puppet/provider/augeas/augeas.rb @@ -32,10 +32,14 @@ Puppet::Type.type(:augeas).provide(:augeas) do COMMANDS = { "set" => [ :path, :string ], + "setm" => [ :path, :string, :string ], "rm" => [ :path ], "clear" => [ :path ], + "mv" => [ :path, :path ], "insert" => [ :string, :string, :path ], "get" => [ :path, :comparator, :string ], + "defvar" => [ :string, :path ], + "defnode" => [ :string, :path, :string ], "match" => [ :path, :glob ], "size" => [:comparator, :int], "include" => [:string], @@ -46,6 +50,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do COMMANDS["ins"] = COMMANDS["insert"] COMMANDS["remove"] = COMMANDS["rm"] + COMMANDS["move"] = COMMANDS["mv"] attr_accessor :aug @@ -334,6 +339,10 @@ Puppet::Type.type(:augeas).provide(:augeas) do debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.set(cmd_array[0], cmd_array[1]) fail("Error sending command '#{command}' with params #{cmd_array.inspect}") if (!rv) + when "setm" + debug("sending command '#{command}' with params #{cmd_array.inspect}") + rv = aug.setm(cmd_array[0], cmd_array[1], cmd_array[2]) + fail("Error sending command '#{command}' with params #{cmd_array.inspect}") if (rv == -1) when "rm", "remove" debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.rm(cmd_array[0]) @@ -354,6 +363,18 @@ Puppet::Type.type(:augeas).provide(:augeas) do debug("sending command '#{command}' with params #{[label, where, path].inspect}") rv = aug.insert(path, label, before) fail("Error sending command '#{command}' with params #{cmd_array.inspect}") if (rv == -1) + when "defvar" + debug("sending command '#{command}' with params #{cmd_array.inspect}") + rv = aug.defvar(cmd_array[0], cmd_array[1]) + fail("Error sending command '#{command}' with params #{cmd_array.inspect}") if (!rv) + when "defnode" + debug("sending command '#{command}' with params #{cmd_array.inspect}") + rv = aug.defnode(cmd_array[0], cmd_array[1], cmd_array[2]) + fail("Error sending command '#{command}' with params #{cmd_array.inspect}") if (!rv) + when "mv", "move" + debug("sending command '#{command}' with params #{cmd_array.inspect}") + rv = aug.mv(cmd_array[0], cmd_array[1]) + fail("Error sending command '#{command}' with params #{cmd_array.inspect}") if (rv == -1) else fail("Command '#{command}' is not supported") end rescue SystemExit,NoMemoryError diff --git a/lib/puppet/provider/exec/posix.rb b/lib/puppet/provider/exec/posix.rb new file mode 100644 index 000000000..92dbd8c98 --- /dev/null +++ b/lib/puppet/provider/exec/posix.rb @@ -0,0 +1,112 @@ +Puppet::Type.type(:exec).provide :posix do + include Puppet::Util::Execution + + confine :feature => :posix + defaultfor :feature => :posix + + desc "Execute external binaries directly, on POSIX systems. +This does not pass through a shell, or perform any interpolation, but +only directly calls the command with the arguments given." + + def run(command, check = false) + output = nil + status = nil + dir = nil + + checkexe(command) + + if dir = resource[:cwd] + unless File.directory?(dir) + if check + dir = nil + else + self.fail "Working directory '#{dir}' does not exist" + end + end + end + + dir ||= Dir.pwd + + debug "Executing#{check ? " check": ""} '#{command}'" + begin + # Do our chdir + Dir.chdir(dir) do + environment = {} + + environment[:PATH] = resource[:path].join(":") if resource[:path] + + if envlist = resource[:environment] + envlist = [envlist] unless envlist.is_a? Array + envlist.each do |setting| + if setting =~ /^(\w+)=((.|\n)+)$/ + env_name = $1 + value = $2 + if environment.include?(env_name) || environment.include?(env_name.to_sym) + warning "Overriding environment setting '#{env_name}' with '#{value}'" + end + environment[env_name] = value + else + warning "Cannot understand environment setting #{setting.inspect}" + end + end + end + + withenv environment do + Timeout::timeout(resource[:timeout]) do + output, status = Puppet::Util::SUIDManager. + run_and_capture([command], resource[:user], resource[:group]) + end + # The shell returns 127 if the command is missing. + if status.exitstatus == 127 + raise ArgumentError, output + end + end + end + rescue Errno::ENOENT => detail + self.fail detail.to_s + end + + return output, status + end + + # Verify that we have the executable + def checkexe(command) + exe = extractexe(command) + + if resource[:path] + if Puppet.features.posix? and !File.exists?(exe) + withenv :PATH => resource[:path].join(File::PATH_SEPARATOR) do + exe = which(exe) || raise(ArgumentError,"Could not find command '#{exe}'") + end + elsif Puppet.features.microsoft_windows? and !File.exists?(exe) + resource[:path].each do |path| + [".exe", ".ps1", ".bat", ".com", ""].each do |extension| + file = File.join(path, exe+extension) + return if File.exists?(file) + end + end + end + end + + raise ArgumentError, "Could not find command '#{exe}'" unless File.exists?(exe) + unless File.executable?(exe) + raise ArgumentError, + "'#{exe}' is not executable" + end + end + + def extractexe(command) + # easy case: command was quoted + if command =~ /^"([^"]+)"/ + $1 + else + command.split(/ /)[0] + end + end + + def validatecmd(command) + exe = extractexe(command) + # if we're not fully qualified, require a path + self.fail "'#{command}' is not qualified and no path was specified. Please qualify the command or specify a path." if File.expand_path(exe) != exe and resource[:path].nil? + end +end diff --git a/lib/puppet/provider/exec/shell.rb b/lib/puppet/provider/exec/shell.rb new file mode 100644 index 000000000..98f309e8f --- /dev/null +++ b/lib/puppet/provider/exec/shell.rb @@ -0,0 +1,17 @@ +Puppet::Type.type(:exec).provide :shell, :parent => :posix do + include Puppet::Util::Execution + + confine :feature => :posix + + desc "Execute external binaries directly, on POSIX systems. +passing through a shell so that shell built ins are available." + + def run(command, check = false) + command = %Q{/bin/sh -c "#{command.gsub(/"/,'\"')}"} + super(command, check) + end + + def validatecmd(command) + true + end +end diff --git a/lib/puppet/provider/file/posix.rb b/lib/puppet/provider/file/posix.rb index f7b8c9797..7b7336b9d 100644 --- a/lib/puppet/provider/file/posix.rb +++ b/lib/puppet/provider/file/posix.rb @@ -54,7 +54,7 @@ Puppet::Type.type(:file).provide :posix do end def retrieve(resource) - unless stat = resource.stat(false) + unless stat = resource.stat return :absent end diff --git a/lib/puppet/provider/file/win32.rb b/lib/puppet/provider/file/win32.rb index 21e7ca974..9423e8f00 100644 --- a/lib/puppet/provider/file/win32.rb +++ b/lib/puppet/provider/file/win32.rb @@ -49,7 +49,7 @@ Puppet::Type.type(:file).provide :microsoft_windows do end def retrieve(resource) - unless stat = resource.stat(false) + unless stat = resource.stat return :absent end diff --git a/lib/puppet/provider/group/aix.rb b/lib/puppet/provider/group/aix.rb new file mode 100755 index 000000000..ecdef6070 --- /dev/null +++ b/lib/puppet/provider/group/aix.rb @@ -0,0 +1,141 @@ +# +# Group Puppet provider for AIX. It uses standard commands to manage groups: +# mkgroup, rmgroup, lsgroup, chgroup +# +# Author:: Hector Rivas Gandara <keymon@gmail.com> +# +require 'puppet/provider/aixobject' + +Puppet::Type.type(:group).provide :aix, :parent => Puppet::Provider::AixObject do + desc "Group management for AIX! Users are managed with mkgroup, rmgroup, lsgroup, chgroup" + + # This will the the default provider for this platform + defaultfor :operatingsystem => :aix + confine :operatingsystem => :aix + + # Provider features + has_features :manages_aix_lam + has_features :manages_members + + # Commands that manage the element + commands :list => "/usr/sbin/lsgroup" + commands :add => "/usr/bin/mkgroup" + commands :delete => "/usr/sbin/rmgroup" + commands :modify => "/usr/bin/chgroup" + + # Group attributes to ignore + def self.attribute_ignore + [] + end + + # AIX attributes to properties mapping. + # + # Valid attributes to be managed by this provider. + # It is a list with of hash + # :aix_attr AIX command attribute name + # :puppet_prop Puppet propertie name + # :to Method to adapt puppet property to aix command value. Optional. + # :from Method to adapt aix command value to puppet property. Optional + self.attribute_mapping = [ + #:name => :name, + {:aix_attr => :id, :puppet_prop => :gid }, + {:aix_attr => :users, :puppet_prop => :members, + :from => :users_from_attr}, + {:aix_attr => :attributes, :puppet_prop => :attributes}, + ] + + #-------------- + # Command definition + + # Return the IA module arguments based on the resource param ia_load_module + def get_ia_module_args + if @resource[:ia_load_module] + ["-R", @resource[:ia_load_module].to_s] + else + [] + end + end + + def lscmd(value=@resource[:name]) + [self.class.command(:list)] + + self.get_ia_module_args + + [ value] + end + + def lsallcmd() + lscmd("ALL") + end + + def addcmd(extra_attrs = []) + # Here we use the @resource.to_hash to get the list of provided parameters + # Puppet does not call to self.<parameter>= method if it does not exists. + # + # It gets an extra list of arguments to add to the user. + [self.class.command(:add) ] + + self.get_ia_module_args + + self.hash2args(@resource.to_hash) + + extra_attrs + [@resource[:name]] + end + + def modifycmd(hash = property_hash) + args = self.hash2args(hash) + return nil if args.empty? + + [self.class.command(:modify)] + + self.get_ia_module_args + + args + [@resource[:name]] + end + + def deletecmd + [self.class.command(:delete)] + + self.get_ia_module_args + + [@resource[:name]] + end + + + #-------------- + # Overwrite get_arguments to add the attributes arguments + def get_arguments(key, value, mapping, objectinfo) + # In the case of attributes, return a list of key=vlaue + if key == :attributes + raise Puppet::Error, "Attributes must be a list of pairs key=value on #{@resource.class.name}[#{@resource.name}]" \ + unless value and value.is_a? Hash + return value.select { |k,v| true }.map { |pair| pair.join("=") } + end + super(key, value, mapping, objectinfo) + end + + def filter_attributes(hash) + # Return only not managed attributtes. + hash.select { + |k,v| !self.class.attribute_mapping_from.include?(k) and + !self.class.attribute_ignore.include?(k) + }.inject({}) { + |hash, array| hash[array[0]] = array[1]; hash + } + end + + def attributes + filter_attributes(getosinfo(refresh = false)) + end + + def attributes=(attr_hash) + #self.class.validate(param, value) + param = :attributes + cmd = modifycmd({param => filter_attributes(attr_hash)}) + if cmd + begin + execute(cmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}" + end + end + end + + # Force convert users it a list. + def users_from_attr(value) + (value.is_a? String) ? value.split(',') : value + end + + +end diff --git a/lib/puppet/provider/package/pip.rb b/lib/puppet/provider/package/pip.rb new file mode 100644 index 000000000..5abbc135a --- /dev/null +++ b/lib/puppet/provider/package/pip.rb @@ -0,0 +1,109 @@ +# Puppet package provider for Python's `pip` package management frontend. +# <http://pip.openplans.org/> + +require 'puppet/provider/package' +require 'xmlrpc/client' + +Puppet::Type.type(:package).provide :pip, + :parent => ::Puppet::Provider::Package do + + desc "Python packages via `pip`." + + has_feature :installable, :uninstallable, :upgradeable, :versionable + + # Parse lines of output from `pip freeze`, which are structured as + # _package_==_version_. + def self.parse(line) + if line.chomp =~ /^([^=]+)==([^=]+)$/ + {:ensure => $2, :name => $1, :provider => name} + else + nil + end + end + + # Return an array of structured information about every installed package + # that's managed by `pip` or an empty array if `pip` is not available. + def self.instances + packages = [] + pip_cmd = which('pip') or return [] + execpipe "#{pip_cmd} freeze" do |process| + process.collect do |line| + next unless options = parse(line) + packages << new(options) + end + end + packages + end + + # Return structured information about a particular package or `nil` if + # it is not installed or `pip` itself is not available. + def query + self.class.instances.each do |provider_pip| + return provider_pip.properties if @resource[:name] == provider_pip.name + end + return nil + end + + # Ask the PyPI API for the latest version number. There is no local + # cache of PyPI's package list so this operation will always have to + # ask the web service. + def latest + client = XMLRPC::Client.new2("http://pypi.python.org/pypi") + client.http_header_extra = {"Content-Type" => "text/xml"} + result = client.call("package_releases", @resource[:name]) + result.first + end + + # Install a package. The ensure parameter may specify installed, + # latest, a version number, or, in conjunction with the source + # parameter, an SCM revision. In that case, the source parameter + # gives the fully-qualified URL to the repository. + def install + args = %w{install -q} + if @resource[:source] + args << "-e" + if String === @resource[:ensure] + args << "#{@resource[:source]}@#{@resource[:ensure]}#egg=#{ + @resource[:name]}" + else + args << "#{@resource[:source]}#egg=#{@resource[:name]}" + end + else + case @resource[:ensure] + when String + args << "#{@resource[:name]}==#{@resource[:ensure]}" + when :latest + args << "--upgrade" << @resource[:name] + else + args << @resource[:name] + end + end + lazy_pip *args + end + + # Uninstall a package. Uninstall won't work reliably on Debian/Ubuntu + # unless this issue gets fixed. + # <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=562544> + def uninstall + lazy_pip "uninstall", "-y", "-q", @resource[:name] + end + + def update + install + end + + # Execute a `pip` command. If Puppet doesn't yet know how to do so, + # try to teach it and if even that fails, raise the error. + private + def lazy_pip(*args) + pip *args + rescue NoMethodError => e + if pathname = which('pip') + self.class.commands :pip => pathname + pip *args + else + raise e + end + end + +end diff --git a/lib/puppet/provider/service/daemontools.rb b/lib/puppet/provider/service/daemontools.rb index bbb962a71..f5a073329 100644 --- a/lib/puppet/provider/service/daemontools.rb +++ b/lib/puppet/provider/service/daemontools.rb @@ -67,7 +67,7 @@ Puppet::Type.type(:service).provide :daemontools, :parent => :base do path = self.defpath unless FileTest.directory?(path) Puppet.notice "Service path #{path} does not exist" - next + return end # reject entries that aren't either a directory diff --git a/lib/puppet/provider/service/debian.rb b/lib/puppet/provider/service/debian.rb index 3d09e2849..58b808a8e 100755 --- a/lib/puppet/provider/service/debian.rb +++ b/lib/puppet/provider/service/debian.rb @@ -22,8 +22,12 @@ Puppet::Type.type(:service).provide :debian, :parent => :init do # Remove the symlinks def disable - update_rc "-f", @resource[:name], "remove" - update_rc @resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", "." + if `dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?`.to_i == 0 + update_rc @resource[:name], "disable" + else + update_rc "-f", @resource[:name], "remove" + update_rc @resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", "." + end end def enabled? diff --git a/lib/puppet/provider/service/launchd.rb b/lib/puppet/provider/service/launchd.rb index 07c549a8b..9d813bd5a 100644 --- a/lib/puppet/provider/service/launchd.rb +++ b/lib/puppet/provider/service/launchd.rb @@ -211,7 +211,7 @@ Puppet::Type.type(:service).provide :launchd, :parent => :base do job_path, job_plist = plist_from_label(resource[:name]) job_plist_disabled = job_plist["Disabled"] if job_plist.has_key?("Disabled") - if self.class.get_macosx_version_major == "10.6": + if self.class.get_macosx_version_major == "10.6" if FileTest.file?(Launchd_Overrides) and overrides = self.class.read_plist(Launchd_Overrides) if overrides.has_key?(resource[:name]) overrides_disabled = overrides[resource[:name]]["Disabled"] if overrides[resource[:name]].has_key?("Disabled") diff --git a/lib/puppet/provider/service/smf.rb b/lib/puppet/provider/service/smf.rb index 3efb2eb37..f6f221a7c 100755 --- a/lib/puppet/provider/service/smf.rb +++ b/lib/puppet/provider/service/smf.rb @@ -27,7 +27,7 @@ Puppet::Type.type(:service).provide :smf, :parent => :base do end end rescue Puppet::ExecutionFailure => detail - raise Puppet::Error.new( "Cannot config #{self.service} to enable it: #{detail}" ) + raise Puppet::Error.new( "Cannot config #{self.name} to enable it: #{detail}" ) end def enable @@ -54,10 +54,10 @@ Puppet::Type.type(:service).provide :smf, :parent => :base do def startcmd self.setupservice case self.status - when :stopped - [command(:adm), :enable, @resource[:name]] when :maintenance [command(:adm), :clear, @resource[:name]] + else + [command(:adm), :enable, @resource[:name]] end end diff --git a/lib/puppet/provider/user/aix.rb b/lib/puppet/provider/user/aix.rb new file mode 100755 index 000000000..032d2b536 --- /dev/null +++ b/lib/puppet/provider/user/aix.rb @@ -0,0 +1,353 @@ +# +# User Puppet provider for AIX. It uses standard commands to manage users: +# mkuser, rmuser, lsuser, chuser +# +# Notes: +# - AIX users can have expiry date defined with minute granularity, +# but puppet does not allow it. There is a ticket open for that (#5431) +# - AIX maximum password age is in WEEKs, not days +# +# See http://projects.puppetlabs.com/projects/puppet/wiki/Development_Provider_Development +# for more information +# +# Author:: Hector Rivas Gandara <keymon@gmail.com> +# +require 'puppet/provider/aixobject' +require 'tempfile' +require 'date' + +Puppet::Type.type(:user).provide :aix, :parent => Puppet::Provider::AixObject do + desc "User management for AIX! Users are managed with mkuser, rmuser, chuser, lsuser" + + # This will the the default provider for this platform + defaultfor :operatingsystem => :aix + confine :operatingsystem => :aix + + # Commands that manage the element + commands :list => "/usr/sbin/lsuser" + commands :add => "/usr/bin/mkuser" + commands :delete => "/usr/sbin/rmuser" + commands :modify => "/usr/bin/chuser" + + commands :lsgroup => "/usr/sbin/lsgroup" + commands :chpasswd => "/bin/chpasswd" + + # Provider features + has_features :manages_aix_lam + has_features :manages_homedir, :manages_passwords + has_features :manages_expiry, :manages_password_age + + # Attribute verification (TODO) + #verify :gid, "GID must be an string or int of a valid group" do |value| + # value.is_a? String || value.is_a? Integer + #end + # + #verify :groups, "Groups must be comma-separated" do |value| + # value !~ /\s/ + #end + + # User attributes to ignore from AIX output. + def self.attribute_ignore + [] + end + + # AIX attributes to properties mapping. + # + # Valid attributes to be managed by this provider. + # It is a list with of hash + # :aix_attr AIX command attribute name + # :puppet_prop Puppet propertie name + # :to Method to adapt puppet property to aix command value. Optional. + # :from Method to adapt aix command value to puppet property. Optional + self.attribute_mapping = [ + #:name => :name, + {:aix_attr => :pgrp, :puppet_prop => :gid, + :to => :gid_to_attr, :from => :gid_from_attr}, + {:aix_attr => :id, :puppet_prop => :uid}, + {:aix_attr => :groups, :puppet_prop => :groups}, + {:aix_attr => :home, :puppet_prop => :home}, + {:aix_attr => :shell, :puppet_prop => :shell}, + {:aix_attr => :expires, :puppet_prop => :expiry, + :to => :expiry_to_attr, :from => :expiry_from_attr}, + {:aix_attr => :maxage, :puppet_prop => :password_max_age}, + {:aix_attr => :minage, :puppet_prop => :password_min_age}, + {:aix_attr => :attributes, :puppet_prop => :attributes}, + ] + + #-------------- + # Command definition + + # Return the IA module arguments based on the resource param ia_load_module + def get_ia_module_args + if @resource[:ia_load_module] + ["-R", @resource[:ia_load_module].to_s] + else + [] + end + end + + # List groups and Ids + def lsgroupscmd(value=@resource[:name]) + [command(:lsgroup)] + + self.get_ia_module_args + + ["-a", "id", value] + end + + def lscmd(value=@resource[:name]) + [self.class.command(:list)] + self.get_ia_module_args + [ value] + end + + def lsallcmd() + lscmd("ALL") + end + + def addcmd(extra_attrs = []) + # Here we use the @resource.to_hash to get the list of provided parameters + # Puppet does not call to self.<parameter>= method if it does not exists. + # + # It gets an extra list of arguments to add to the user. + [self.class.command(:add)] + self.get_ia_module_args + + self.hash2args(@resource.to_hash) + + extra_attrs + [@resource[:name]] + end + + # Get modify command. Set translate=false if no mapping must be used. + # Needed for special properties like "attributes" + def modifycmd(hash = property_hash) + args = self.hash2args(hash) + return nil if args.empty? + + [self.class.command(:modify)] + self.get_ia_module_args + + args + [@resource[:name]] + end + + def deletecmd + [self.class.command(:delete)] + self.get_ia_module_args + [@resource[:name]] + end + + #-------------- + # We overwrite the create function to change the password after creation. + def create + super + # Reset the password if needed + self.password = @resource[:password] if @resource[:password] + end + + + def get_arguments(key, value, mapping, objectinfo) + # In the case of attributes, return a list of key=vlaue + if key == :attributes + raise Puppet::Error, "Attributes must be a list of pairs key=value on #{@resource.class.name}[#{@resource.name}]" \ + unless value and value.is_a? Hash + return value.select { |k,v| true }.map { |pair| pair.join("=") } + end + + super(key, value, mapping, objectinfo) + end + + # Get the groupname from its id + def self.groupname_by_id(gid) + groupname=nil + execute(lsgroupscmd("ALL")).each { |entry| + attrs = self.parse_attr_list(entry, nil) + if attrs and attrs.include? :id and gid == attrs[:id].to_i + groupname = entry.split(" ")[0] + end + } + groupname + end + + # Get the groupname from its id + def groupid_by_name(groupname) + attrs = self.parse_attr_list(execute(lsgroupscmd(groupname)).split("\n")[0], nil) + attrs ? attrs[:id].to_i : nil + end + + # Check that a group exists and is valid + def verify_group(value) + if value.is_a? Integer or value.is_a? Fixnum + groupname = self.groupname_by_id(value) + raise ArgumentError, "AIX group must be a valid existing group" unless groupname + else + raise ArgumentError, "AIX group must be a valid existing group" unless groupid_by_name(value) + groupname = value + end + groupname + end + + # The user's primary group. Can be specified numerically or by name. + def gid_to_attr(value) + verify_group(value) + end + + # Get the group gid from its name + def gid_from_attr(value) + groupid_by_name(value) + end + + # The expiry date for this user. Must be provided in + # a zero padded YYYY-MM-DD HH:MM format + def expiry_to_attr(value) + # For chuser the expires parameter is a 10-character string in the MMDDhhmmyy format + # that is,"%m%d%H%M%y" + newdate = '0' + if value.is_a? String and value!="0000-00-00" + d = DateTime.parse(value, "%Y-%m-%d %H:%M") + newdate = d.strftime("%m%d%H%M%y") + end + newdate + end + + def expiry_from_attr(value) + if value =~ /(..)(..)(..)(..)(..)/ + #d= DateTime.parse("20#{$5}-#{$1}-#{$2} #{$3}:#{$4}") + #expiry_date = d.strftime("%Y-%m-%d %H:%M") + #expiry_date = d.strftime("%Y-%m-%d") + expiry_date = "20#{$5}-#{$1}-#{$2}" + else + Puppet.warn("Could not convert AIX expires date '#{value}' on #{@resource.class.name}[#{@resource.name}]") \ + unless value == '0' + expiry_date = :absent + end + expiry_date + end + + #-------------------------------- + # Getter and Setter + # When the provider is initialized, create getter/setter methods for each + # property our resource type supports. + # If setter or getter already defined it will not be overwritten + + #- **password** + # The user's password, in whatever encrypted format the local machine + # requires. Be sure to enclose any value that includes a dollar sign ($) + # in single quotes ('). Requires features manages_passwords. + # + # Retrieve the password parsing directly the /etc/security/passwd + def password + password = :absent + user = @resource[:name] + f = File.open("/etc/security/passwd", 'r') + # Skip to the user + f.each { |l| break if l =~ /^#{user}:\s*$/ } + if ! f.eof? + f.each { |l| + # If there is a new user stanza, stop + break if l =~ /^\S*:\s*$/ + # If the password= entry is found, return it + if l =~ /^\s*password\s*=\s*(.*)$/ + password = $1; break; + end + } + end + f.close() + return password + end + + def password=(value) + user = @resource[:name] + + # Puppet execute does not support strings as input, only files. + tmpfile = Tempfile.new('puppet_#{user}_pw') + tmpfile << "#{user}:#{value}\n" + tmpfile.close() + + # Options '-e', '-c', use encrypted password and clear flags + # Must receibe "user:enc_password" as input + # command, arguments = {:failonfail => true, :combine => true} + cmd = [self.class.command(:chpasswd),"-R", self.class.ia_module, + '-e', '-c', user] + begin + execute(cmd, {:failonfail => true, :combine => true, :stdinfile => tmpfile.path }) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}" + ensure + tmpfile.delete() + end + end + + def filter_attributes(hash) + # Return only not managed attributtes. + hash.select { + |k,v| !self.class.attribute_mapping_from.include?(k) and + !self.class.attribute_ignore.include?(k) + }.inject({}) { + |hash, array| hash[array[0]] = array[1]; hash + } + end + + def attributes + filter_attributes(getosinfo(refresh = false)) + end + + def attributes=(attr_hash) + #self.class.validate(param, value) + param = :attributes + cmd = modifycmd({param => filter_attributes(attr_hash)}) + if cmd + begin + execute(cmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}" + end + end + end + + #- **comment** + # A description of the user. Generally is a user's full name. + #def comment=(value) + #end + # + #def comment + #end + # UNSUPPORTED + #- **profile_membership** + # Whether specified roles should be treated as the only roles + # of which the user is a member or whether they should merely + # be treated as the minimum membership list. Valid values are + # `inclusive`, `minimum`. + # UNSUPPORTED + #- **profiles** + # The profiles the user has. Multiple profiles should be + # specified as an array. Requires features manages_solaris_rbac. + # UNSUPPORTED + #- **project** + # The name of the project associated with a user Requires features + # manages_solaris_rbac. + # UNSUPPORTED + #- **role_membership** + # Whether specified roles should be treated as the only roles + # of which the user is a member or whether they should merely + # be treated as the minimum membership list. Valid values are + # `inclusive`, `minimum`. + # UNSUPPORTED + #- **roles** + # The roles the user has. Multiple roles should be + # specified as an array. Requires features manages_solaris_rbac. + # UNSUPPORTED + #- **key_membership** + # Whether specified key value pairs should be treated as the only + # attributes + # of the user or whether they should merely + # be treated as the minimum list. Valid values are `inclusive`, + # `minimum`. + # UNSUPPORTED + #- **keys** + # Specify user attributes in an array of keyvalue pairs Requires features + # manages_solaris_rbac. + # UNSUPPORTED + #- **allowdupe** + # Whether to allow duplicate UIDs. Valid values are `true`, `false`. + # UNSUPPORTED + #- **auths** + # The auths the user has. Multiple auths should be + # specified as an array. Requires features manages_solaris_rbac. + # UNSUPPORTED + #- **auth_membership** + # Whether specified auths should be treated as the only auths + # of which the user is a member or whether they should merely + # be treated as the minimum membership list. Valid values are + # `inclusive`, `minimum`. + # UNSUPPORTED + +end diff --git a/lib/puppet/provider/zfs/solaris.rb b/lib/puppet/provider/zfs/solaris.rb index 9aec9d801..b783f9e01 100644 --- a/lib/puppet/provider/zfs/solaris.rb +++ b/lib/puppet/provider/zfs/solaris.rb @@ -31,7 +31,7 @@ Puppet::Type.type(:zfs).provide(:solaris) do end end - [:mountpoint, :recordsize, :aclmode, :aclinherit, :primarycache, :secondarycache, :compression, :copies, :quota, :reservation, :sharenfs, :snapdir].each do |field| + [:aclinherit, :aclmode, :atime, :canmount, :checksum, :compression, :copies, :devices, :exec, :logbias, :mountpoint, :nbmand, :primarycache, :quota, :readonly, :recordsize, :refquota, :refreservation, :reservation, :secondarycache, :setuid, :shareiscsi, :sharenfs, :sharesmb, :snapdir, :version, :volsize, :vscan, :xattr, :zoned, :vscan].each do |field| define_method(field) do zfs(:get, "-H", "-o", "value", field, @resource[:name]).strip end diff --git a/lib/puppet/rails.rb b/lib/puppet/rails.rb index 74805bb6f..f74e63f20 100644 --- a/lib/puppet/rails.rb +++ b/lib/puppet/rails.rb @@ -58,7 +58,7 @@ module Puppet::Rails socket = Puppet[:dbsocket] args[:socket] = socket unless socket.to_s.empty? - when "oracle_enhanced": + when "oracle_enhanced" args[:database] = Puppet[:dbname] unless Puppet[:dbname].to_s.empty? args[:username] = Puppet[:dbuser] unless Puppet[:dbuser].to_s.empty? args[:password] = Puppet[:dbpassword] unless Puppet[:dbpassword].to_s.empty? diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb index b9cf6991a..48d8c1f48 100644 --- a/lib/puppet/resource/type.rb +++ b/lib/puppet/resource/type.rb @@ -34,13 +34,13 @@ class Puppet::Resource::Type end def to_pson_data_hash - data = [:code, :doc, :line, :file, :parent].inject({}) do |hash, param| - next hash unless value = self.send(param) + data = [:doc, :line, :file, :parent].inject({}) do |hash, param| + next hash unless (value = self.send(param)) and (value != "") hash[param.to_s] = value hash end - data['arguments'] = arguments.dup + data['arguments'] = arguments.dup unless arguments.empty? data['name'] = name data['type'] = type diff --git a/lib/puppet/simple_graph.rb b/lib/puppet/simple_graph.rb index e39aa8770..27e068e30 100644 --- a/lib/puppet/simple_graph.rb +++ b/lib/puppet/simple_graph.rb @@ -367,7 +367,7 @@ class Puppet::SimpleGraph return [] unless ns = (options[:direction] == :in) ? @in_to[v] : @out_from[v] (options[:type] == :edges) ? ns.values.flatten : ns.keys end - + # Take container information from another graph and use it # to replace any container vertices with their respective leaves. # This creates direct relationships where there were previously @@ -387,7 +387,7 @@ class Puppet::SimpleGraph children = other.adjacent(container, :direction => :out) # MQR TODO: Luke suggests that it should be possible to refactor the system so that - # container nodes are retained, thus obviating the need for the whit. + # container nodes are retained, thus obviating the need for the whit. children = [whit_class.new(:name => container.name, :catalog => other)] if children.empty? # First create new edges for each of the :in edges @@ -570,7 +570,10 @@ class Puppet::SimpleGraph end def to_yaml_properties - other_vars = instance_variables.reject { |v| %w{@in_to @out_from @upstream_from @downstream_from}.include?(v) } + other_vars = instance_variables. + map {|v| v.to_s}. + reject { |v| %w{@in_to @out_from @upstream_from @downstream_from}.include?(v) } + (other_vars + %w{@vertices @edges}).sort.uniq end diff --git a/lib/puppet/transaction/event.rb b/lib/puppet/transaction/event.rb index cd695cff8..d3f25b71c 100644 --- a/lib/puppet/transaction/event.rb +++ b/lib/puppet/transaction/event.rb @@ -48,7 +48,7 @@ class Puppet::Transaction::Event end def to_yaml_properties - (YAML_ATTRIBUTES & instance_variables).sort + (YAML_ATTRIBUTES.map {|ya| ya.to_s} & instance_variables.map{|iv| iv.to_s}).sort end private diff --git a/lib/puppet/type/exec.rb b/lib/puppet/type/exec.rb index ae579502a..3ba488f19 100755 --- a/lib/puppet/type/exec.rb +++ b/lib/puppet/type/exec.rb @@ -23,17 +23,15 @@ module Puppet you are doing a lot of work with `exec`, please at least notify us at Puppet Labs what you are doing, and hopefully we can work with you to get a native resource type for the work you are doing. - - **Autorequires:** If Puppet is managing an exec's cwd or the executable file used in an exec's command, the exec resource will autorequire those files. If Puppet is managing the user that an exec should run as, the exec resource will autorequire that user." - require 'open3' + **Autorequires:** If Puppet is managing an exec's cwd or the executable file used in an exec's command, the exec resource will autorequire those files. If Puppet is managing the user that an exec should run as, the exec resource will autorequire that user." # Create a new check mechanism. It's basically just a parameter that # provides one extra 'check' method. - def self.newcheck(name, &block) + def self.newcheck(name, options = {}, &block) @checks ||= {} - check = newparam(name, &block) + check = newparam(name, options, &block) @checks[name] = check end @@ -65,9 +63,11 @@ module Puppet # First verify that all of our checks pass. def retrieve - # Default to somethinng - - if @resource.check + # We need to return :notrun to trigger evaluation; when that isn't + # true, we *LIE* about what happened and return a "success" for the + # value, which causes us to be treated as in_sync?, which means we + # don't actually execute anything. I think. --daniel 2011-03-10 + if @resource.check_all_attributes return :notrun else return self.should @@ -89,7 +89,7 @@ module Puppet tries.times do |try| # Only add debug messages for tries > 1 to reduce log spam. debug("Exec try #{try+1}/#{tries}") if tries > 1 - @output, @status = @resource.run(self.resource[:command]) + @output, @status = provider.run(self.resource[:command]) break if self.should.include?(@status.exitstatus.to_s) if try_sleep > 0 and tries > 1 debug("Sleeping for #{try_sleep} seconds between tries") @@ -139,7 +139,7 @@ module Puppet newparam(:path) do desc "The search path used for command execution. Commands must be fully qualified if no path is specified. Paths - can be specified as an array or as a colon-separated list." + can be specified as an array or as a colon separated list." # Support both arrays and colon-separated fields. def value=(*values) @@ -176,21 +176,9 @@ module Puppet # Validation is handled by the SUIDManager class. end - newparam(:cwd) do + newparam(:cwd, :parent => Puppet::Parameter::Path) do desc "The directory from which to run the command. If this directory does not exist, the command will fail." - - validate do |dir| - unless dir =~ /^#{File::SEPARATOR}/ - self.fail("CWD must be a fully qualified path") - end - end - - munge do |dir| - dir = dir[0] if dir.is_a?(Array) - - dir - end end newparam(:logoutput) do @@ -209,7 +197,7 @@ module Puppet for refreshing." validate do |command| - @resource.validatecmd(command) + provider.validatecmd(command) end end @@ -232,19 +220,17 @@ module Puppet newparam(:timeout) do desc "The maximum time the command should take. If the command takes longer than the timeout, the command is considered to have failed - and will be stopped. Use any negative number to disable the timeout. + and will be stopped. Use 0 to disable the timeout. The time is specified in seconds." munge do |value| value = value.shift if value.is_a?(Array) - if value.is_a?(String) - unless value =~ /^[-\d.]+$/ - raise ArgumentError, "The timeout must be a number." - end - Float(value) - else - value + begin + value = Float(value) + rescue ArgumentError => e + raise ArgumentError, "The timeout must be a number." end + [value, 0.0].max end defaultto 300 @@ -324,7 +310,7 @@ module Puppet end end - newcheck(:creates) do + newcheck(:creates, :parent => Puppet::Parameter::Path) do desc "A file that this command creates. If this parameter is provided, then the command will only be run if the specified file does not exist: @@ -337,19 +323,7 @@ module Puppet " - # FIXME if they try to set this and fail, then we should probably - # fail the entire exec, right? - validate do |files| - files = [files] unless files.is_a? Array - - files.each do |file| - self.fail("'creates' must be set to a fully qualified path") unless file - - unless file =~ %r{^#{File::SEPARATOR}} - self.fail "'creates' files must be fully qualified." - end - end - end + accept_arrays # If the file exists, return false (i.e., don't run the command), # else return true @@ -377,15 +351,15 @@ module Puppet validate do |cmds| cmds = [cmds] unless cmds.is_a? Array - cmds.each do |cmd| - @resource.validatecmd(cmd) + cmds.each do |command| + provider.validatecmd(command) end end # Return true if the command does not return 0. def check(value) begin - output, status = @resource.run(value, true) + output, status = provider.run(value, true) rescue Timeout::Error err "Check #{value.inspect} exceeded timeout" return false @@ -419,15 +393,15 @@ module Puppet validate do |cmds| cmds = [cmds] unless cmds.is_a? Array - cmds.each do |cmd| - @resource.validatecmd(cmd) + cmds.each do |command| + provider.validatecmd(command) end end # Return true if the command returns 0. def check(value) begin - output, status = @resource.run(value, true) + output, status = provider.run(value, true) rescue Timeout::Error err "Check #{value.inspect} exceeded timeout" return false @@ -441,7 +415,7 @@ module Puppet @isomorphic = false validate do - validatecmd(self[:command]) + provider.validatecmd(self[:command]) end # FIXME exec should autorequire any exec that 'creates' our cwd @@ -494,7 +468,7 @@ module Puppet # Verify that we pass all of the checks. The argument determines whether # we skip the :refreshonly check, which is necessary because we now check # within refresh - def check(refreshing = false) + def check_all_attributes(refreshing = false) self.class.checks.each { |check| next if refreshing and check == :refreshonly if @parameters.include?(check) @@ -509,32 +483,6 @@ module Puppet true end - # Verify that we have the executable - def checkexe(cmd) - exe = extractexe(cmd) - - if self[:path] - if Puppet.features.posix? and !File.exists?(exe) - withenv :PATH => self[:path].join(File::PATH_SEPARATOR) do - exe = which(exe) || raise(ArgumentError,"Could not find command '#{exe}'") - end - elsif Puppet.features.microsoft_windows? and !File.exists?(exe) - self[:path].each do |path| - [".exe", ".ps1", ".bat", ".com", ""].each do |extension| - file = File.join(path, exe+extension) - return if File.exists?(file) - end - end - end - end - - raise ArgumentError, "Could not find executable '#{exe}'" unless FileTest.exists?(exe) - unless FileTest.executable?(exe) - raise ArgumentError, - "'#{exe}' is not executable" - end - end - def output if self.property(:returns).nil? return nil @@ -545,98 +493,13 @@ module Puppet # Run the command, or optionally run a separately-specified command. def refresh - if self.check(true) + if self.check_all_attributes(true) if cmd = self[:refresh] - self.run(cmd) + provider.run(cmd) else self.property(:returns).sync end end end - - # Run a command. - def run(command, check = false) - output = nil - status = nil - - dir = nil - - checkexe(command) - - if dir = self[:cwd] - unless File.directory?(dir) - if check - dir = nil - else - self.fail "Working directory '#{dir}' does not exist" - end - end - end - - dir ||= Dir.pwd - - if check - debug "Executing check '#{command}'" - else - debug "Executing '#{command}'" - end - begin - # Do our chdir - Dir.chdir(dir) do - environment = {} - - environment[:PATH] = self[:path].join(":") if self[:path] - - if envlist = self[:environment] - envlist = [envlist] unless envlist.is_a? Array - envlist.each do |setting| - if setting =~ /^(\w+)=((.|\n)+)$/ - name = $1 - value = $2 - if environment.include? name - warning( - "Overriding environment setting '#{name}' with '#{value}'" - ) - end - environment[name] = value - else - warning "Cannot understand environment setting #{setting.inspect}" - end - end - end - - withenv environment do - Timeout::timeout(self[:timeout]) do - output, status = Puppet::Util::SUIDManager.run_and_capture( - [command], self[:user], self[:group] - ) - end - # The shell returns 127 if the command is missing. - if status.exitstatus == 127 - raise ArgumentError, output - end - end - end - rescue Errno::ENOENT => detail - self.fail detail.to_s - end - - return output, status - end - - def validatecmd(cmd) - exe = extractexe(cmd) - # if we're not fully qualified, require a path - self.fail "'#{cmd}' is not qualified and no path was specified. Please qualify the command or specify a path." if File.expand_path(exe) != exe and self[:path].nil? - end - - def extractexe(cmd) - # easy case: command was quoted - if cmd =~ /^"([^"]+)"/ - $1 - else - cmd.split(/ /)[0] - end - end end end diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index 16b1f962d..a73ada57e 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -44,7 +44,7 @@ Puppet::Type.newtype(:file) do # convert the current path in an index into the collection and the last # path name. The aim is to use less storage for all common paths in a hierarchy munge do |value| - path, name = File.split(value.gsub(/\/+/,'/')) + path, name = ::File.split(value.gsub(/\/+/,'/')) { :index => Puppet::FileCollection.collection.index(path), :name => name } end @@ -55,7 +55,7 @@ Puppet::Type.newtype(:file) do if value[:name] == '/' basedir else - File.join( basedir, value[:name] ) + ::File.join( basedir, value[:name] ) end end end @@ -122,7 +122,17 @@ Puppet::Type.newtype(:file) do newparam(:recurse) do desc "Whether and how deeply to do recursive - management." + management. Options are: + inf,true => Regular style recursion on both remote and local + directory structure. + remote => Descends recursively into the remote directory + but not the local directory. Allows copying of + a few files into a directory containing many + unmanaged files without scanning all the local files. + false => Default of no-recursion. + [0-9]+ => Both, but limit recursion. Warning: this syntax + is deprecated and has moved to recurselimit. + " newvalues(:true, :false, :inf, :remote, /^[0-9]+$/) @@ -248,7 +258,7 @@ Puppet::Type.newtype(:file) do # Autorequire any parent directories. autorequire(:file) do - basedir = File.dirname(self[:path]) + basedir = ::File.dirname(self[:path]) if basedir != self[:path] basedir else @@ -309,7 +319,7 @@ Puppet::Type.newtype(:file) do def asuser if self.should(:owner) and ! self.should(:owner).is_a?(Symbol) writeable = Puppet::Util::SUIDManager.asuser(self.should(:owner)) { - FileTest.writable?(File.dirname(self[:path])) + FileTest.writable?(::File.dirname(self[:path])) } # If the parent directory is writeable, then we execute @@ -412,7 +422,7 @@ Puppet::Type.newtype(:file) do # Create a new file or directory object as a child to the current # object. def newchild(path) - full_path = File.join(self[:path], path) + full_path = ::File.join(self[:path], path) # Add some new values to our original arguments -- these are the ones # set at initialization. We specifically want to exclude any param @@ -486,16 +496,16 @@ Puppet::Type.newtype(:file) do # not likely to have many actual conflicts, which is good, because # this is a pretty inefficient implementation. def remove_less_specific_files(files) - mypath = self[:path].split(File::Separator) + mypath = self[:path].split(::File::Separator) other_paths = catalog.vertices. select { |r| r.is_a?(self.class) and r[:path] != self[:path] }. - collect { |r| r[:path].split(File::Separator) }. + collect { |r| r[:path].split(::File::Separator) }. select { |p| p[0,mypath.length] == mypath } return files if other_paths.empty? files.reject { |file| - path = file[:path].split(File::Separator) + path = file[:path].split(::File::Separator) other_paths.any? { |p| path[0,p.length] == p } } end @@ -612,7 +622,7 @@ Puppet::Type.newtype(:file) do end when "link", "file" debug "Removing existing #{s.ftype} for replacement with #{should}" - File.unlink(self[:path]) + ::File.unlink(self[:path]) else self.fail "Could not back up files of type #{s.ftype}" end @@ -677,7 +687,7 @@ Puppet::Type.newtype(:file) do path = self[:path] begin - File.send(method, self[:path]) + ::File.send(method, self[:path]) rescue Errno::ENOENT => error return nil rescue Errno::EACCES => error @@ -703,7 +713,7 @@ Puppet::Type.newtype(:file) do use_temporary_file = write_temporary_file? if use_temporary_file path = "#{self[:path]}.puppettmp_#{rand(10000)}" - path = "#{self[:path]}.puppettmp_#{rand(10000)}" while File.exists?(path) or File.symlink?(path) + path = "#{self[:path]}.puppettmp_#{rand(10000)}" while ::File.exists?(path) or ::File.symlink?(path) else path = self[:path] end @@ -712,18 +722,18 @@ Puppet::Type.newtype(:file) do umask = mode ? 000 : 022 mode_int = mode ? mode.to_i(8) : nil - content_checksum = Puppet::Util.withumask(umask) { File.open(path, 'w', mode_int ) { |f| write_content(f) } } + content_checksum = Puppet::Util.withumask(umask) { ::File.open(path, 'w', mode_int ) { |f| write_content(f) } } # And put our new file in place if use_temporary_file # This is only not true when our file is empty. begin fail_if_checksum_is_wrong(path, content_checksum) if validate_checksum? - File.rename(path, self[:path]) + ::File.rename(path, self[:path]) rescue => detail fail "Could not rename temporary file #{path} to #{self[:path]}: #{detail}" ensure # Make sure the created file gets removed - File.unlink(path) if FileTest.exists?(path) + ::File.unlink(path) if FileTest.exists?(path) end end diff --git a/lib/puppet/type/file/ctime.rb b/lib/puppet/type/file/ctime.rb index 24b098703..90d95da64 100644 --- a/lib/puppet/type/file/ctime.rb +++ b/lib/puppet/type/file/ctime.rb @@ -4,7 +4,7 @@ module Puppet def retrieve current_value = :absent - if stat = @resource.stat(false) + if stat = @resource.stat current_value = stat.ctime end current_value diff --git a/lib/puppet/type/file/ensure.rb b/lib/puppet/type/file/ensure.rb index 99652ecc6..0f065da14 100755 --- a/lib/puppet/type/file/ensure.rb +++ b/lib/puppet/type/file/ensure.rb @@ -138,7 +138,7 @@ module Puppet end def retrieve - if stat = @resource.stat(false) + if stat = @resource.stat return stat.ftype.intern else if self.should == :false diff --git a/lib/puppet/type/file/group.rb b/lib/puppet/type/file/group.rb index 5ed5166bc..4d1f2f4e6 100755 --- a/lib/puppet/type/file/group.rb +++ b/lib/puppet/type/file/group.rb @@ -62,7 +62,7 @@ module Puppet end def retrieve - return :absent unless stat = resource.stat(false) + return :absent unless stat = resource.stat currentvalue = stat.gid diff --git a/lib/puppet/type/file/mode.rb b/lib/puppet/type/file/mode.rb index 2acd8b359..9f58e6fb0 100755 --- a/lib/puppet/type/file/mode.rb +++ b/lib/puppet/type/file/mode.rb @@ -63,7 +63,7 @@ module Puppet # If we're not following links and we're a link, then we just turn # off mode management entirely. - if stat = @resource.stat(false) + if stat = @resource.stat unless defined?(@fixed) @should &&= @should.collect { |s| self.dirmask(s) } end diff --git a/lib/puppet/type/file/mtime.rb b/lib/puppet/type/file/mtime.rb index 8ca7ed0d6..5952b4b84 100644 --- a/lib/puppet/type/file/mtime.rb +++ b/lib/puppet/type/file/mtime.rb @@ -4,7 +4,7 @@ module Puppet def retrieve current_value = :absent - if stat = @resource.stat(false) + if stat = @resource.stat current_value = stat.mtime end current_value diff --git a/lib/puppet/type/file/selcontext.rb b/lib/puppet/type/file/selcontext.rb index ea385eec0..1b1a77245 100644 --- a/lib/puppet/type/file/selcontext.rb +++ b/lib/puppet/type/file/selcontext.rb @@ -26,7 +26,7 @@ module Puppet include Puppet::Util::SELinux def retrieve - return :absent unless @resource.stat(false) + return :absent unless @resource.stat context = self.get_selinux_current_context(@resource[:path]) parse_selinux_context(name, context) end diff --git a/lib/puppet/type/file/type.rb b/lib/puppet/type/file/type.rb index 4da54e2cb..864d3b1a4 100755 --- a/lib/puppet/type/file/type.rb +++ b/lib/puppet/type/file/type.rb @@ -5,7 +5,7 @@ module Puppet def retrieve current_value = :absent - if stat = @resource.stat(false) + if stat = @resource.stat current_value = stat.ftype end current_value diff --git a/lib/puppet/type/group.rb b/lib/puppet/type/group.rb index cde1cfd65..aa96bd9c3 100755 --- a/lib/puppet/type/group.rb +++ b/lib/puppet/type/group.rb @@ -15,6 +15,9 @@ module Puppet feature :manages_members, "For directories where membership is an attribute of groups not users." + feature :manages_aix_lam, + "The provider can manage AIX Loadable Authentication Module (LAM) system." + ensurable do desc "Create or remove the group." @@ -95,5 +98,38 @@ module Puppet defaultto false end + + newparam(:ia_load_module, :required_features => :manages_aix_lam) do + desc "The name of the I&A module to use to manage this user" + + defaultto "compat" + end + + newproperty(:attributes, :parent => Puppet::Property::KeyValue, :required_features => :manages_aix_lam) do + desc "Specify group AIX attributes in an array of keyvalue pairs" + + def membership + :attribute_membership + end + + def delimiter + " " + end + + validate do |value| + raise ArgumentError, "Attributes value pairs must be seperated by an =" unless value.include?("=") + end + end + + newparam(:attribute_membership) do + desc "Whether specified attribute value pairs should be treated as the only attributes + of the user or whether they should merely + be treated as the minimum list." + + newvalues(:inclusive, :minimum) + + defaultto :minimum + end + end end diff --git a/lib/puppet/type/macauthorization.rb b/lib/puppet/type/macauthorization.rb index e89aa7c89..b16ab6dde 100644 --- a/lib/puppet/type/macauthorization.rb +++ b/lib/puppet/type/macauthorization.rb @@ -2,7 +2,7 @@ Puppet::Type.newtype(:macauthorization) do @doc = "Manage the Mac OS X authorization database. See the [Apple developer site](http://developer.apple.com/documentation/Security/Conceptual/Security_Overview/Security_Services/chapter_4_section_5.html) for more information. - + **Autorequires:** If Puppet is managing the `/etc/authorization` file, each macauthorization resource will autorequire it." @@ -33,8 +33,8 @@ Puppet::Type.newtype(:macauthorization) do desc "The name of the right or rule to be managed. Corresponds to 'key' in Authorization Services. The key is the name of a rule. A key uses the same naming conventions as a right. The - Security Server uses a rule’s key to match the rule with a right. - Wildcard keys end with a ‘.’. The generic rule has an empty key value. + Security Server uses a rule's key to match the rule with a right. + Wildcard keys end with a '.'. The generic rule has an empty key value. Any rights that do not match a specific rule use the generic rule." isnamevar diff --git a/lib/puppet/type/tidy.rb b/lib/puppet/type/tidy.rb index 93a7e96cf..146481fed 100755 --- a/lib/puppet/type/tidy.rb +++ b/lib/puppet/type/tidy.rb @@ -254,7 +254,7 @@ Puppet::Type.newtype(:tidy) do if parameter files = Puppet::FileServing::Fileset.new(self[:path], parameter).files.collect do |f| - f == "." ? self[:path] : File.join(self[:path], f) + f == "." ? self[:path] : ::File.join(self[:path], f) end else files = [self[:path]] @@ -270,7 +270,7 @@ Puppet::Type.newtype(:tidy) do files_by_name = result.inject({}) { |hash, file| hash[file[:path]] = file; hash } files_by_name.keys.sort { |a,b| b <=> b }.each do |path| - dir = File.dirname(path) + dir = ::File.dirname(path) next unless resource = files_by_name[dir] if resource[:require] resource[:require] << Puppet::Resource.new(:file, path) @@ -321,7 +321,7 @@ Puppet::Type.newtype(:tidy) do def stat(path) begin - File.lstat(path) + ::File.lstat(path) rescue Errno::ENOENT => error info "File does not exist" return nil diff --git a/lib/puppet/type/user.rb b/lib/puppet/type/user.rb index f74e4266f..767959308 100755 --- a/lib/puppet/type/user.rb +++ b/lib/puppet/type/user.rb @@ -13,7 +13,7 @@ module Puppet This resource type uses the prescribed native tools for creating groups and generally uses POSIX APIs for retrieving information about them. It does not directly modify `/etc/passwd` or anything. - + **Autorequires:** If Puppet is managing the user's primary group (as provided in the `gid` attribute), the user resource will autorequire that group. If Puppet is managing any role accounts corresponding to the user's roles, the user resource will autorequire those role accounts." feature :allows_duplicates, @@ -39,6 +39,9 @@ module Puppet feature :system_users, "The provider allows you to create system users with lower UIDs." + feature :manages_aix_lam, + "The provider can manage AIX Loadable Authentication Module (LAM) system." + newproperty(:ensure, :parent => Puppet::Property::Ensure) do newvalue(:present, :event => :user_created) do provider.create @@ -445,5 +448,39 @@ module Puppet newproperty(:project, :required_features => :manages_solaris_rbac) do desc "The name of the project associated with a user" end + + newparam(:ia_load_module, :required_features => :manages_aix_lam) do + desc "The name of the I&A module to use to manage this user" + + defaultto "compat" + end + + newproperty(:attributes, :parent => Puppet::Property::KeyValue, :required_features => :manages_aix_lam) do + desc "Specify user AIX attributes in an array of keyvalue pairs" + + def membership + :attribute_membership + end + + def delimiter + " " + end + + validate do |value| + raise ArgumentError, "Attributes value pairs must be seperated by an =" unless value.include?("=") + end + end + + newparam(:attribute_membership) do + desc "Whether specified attribute value pairs should be treated as the only attributes + of the user or whether they should merely + be treated as the minimum list." + + newvalues(:inclusive, :minimum) + + defaultto :minimum + end + + end end diff --git a/lib/puppet/type/zfs.rb b/lib/puppet/type/zfs.rb index 7123f8ac9..75f821787 100755..100644 --- a/lib/puppet/type/zfs.rb +++ b/lib/puppet/type/zfs.rb @@ -10,52 +10,124 @@ module Puppet desc "The full name for this filesystem. (including the zpool)" end - newproperty(:mountpoint) do - desc "The mountpoint property." - end - - newproperty(:recordsize) do - desc "The recordsize property." + newproperty(:aclinherit) do + desc "The aclinherit property. Values: discard | noallow | restricted | passthrough | passthrough-x" end newproperty(:aclmode) do - desc "The aclmode property." + desc "The aclmode property. Values: discard | groupmask | passthrough" end - newproperty(:aclinherit) do - desc "The aclinherit property." + newproperty(:atime) do + desc "The atime property. Values: on | off" end - newproperty(:primarycache) do - desc "The primarycache property." + newproperty(:canmount) do + desc "The canmount property. Values: on | off | noauto" end - newproperty(:secondarycache) do - desc "The secondarycache property." + newproperty(:checksum) do + desc "The checksum property. Values: on | off | fletcher2 | fletcher4 | sha256" end newproperty(:compression) do - desc "The compression property." + desc "The compression property. Values: on | off | lzjb | gzip | gzip-[1-9] | zle" end newproperty(:copies) do - desc "The copies property." + desc "The copies property. Values: 1 | 2 | 3" + end + + newproperty(:devices) do + desc "The devices property. Values: on | off" + end + + newproperty(:exec) do + desc "The exec property. Values: on | off" + end + + newproperty(:logbias) do + desc "The logbias property. Values: latency | throughput" + end + + newproperty(:mountpoint) do + desc "The mountpoint property. Values: <path> | legacy | none" + end + + newproperty(:nbmand) do + desc "The nbmand property. Values: on | off" + end + + newproperty(:primarycache) do + desc "The primarycache property. Values: all | none | metadata" end newproperty(:quota) do - desc "The quota property." + desc "The quota property. Values: <size> | none" + end + + newproperty(:readonly) do + desc "The readonly property. Values: on | off" + end + + newproperty(:recordsize) do + desc "The recordsize property. Values: 512 to 128k, power of 2" + end + + newproperty(:refquota) do + desc "The refquota property. Values: <size> | none" + end + + newproperty(:refreservation) do + desc "The refreservation property. Values: <size> | none" end newproperty(:reservation) do - desc "The reservation property." + desc "The reservation property. Values: <size> | none" + end + + newproperty(:secondarycache) do + desc "The secondarycache property. Values: all | none | metadata" + end + + newproperty(:setuid) do + desc "The setuid property. Values: on | off" + end + + newproperty(:shareiscsi) do + desc "The shareiscsi property. Values: on | off | type=<type>" end newproperty(:sharenfs) do - desc "The sharenfs property." + desc "The sharenfs property. Values: on | off | share(1M) options" + end + + newproperty(:sharesmb) do + desc "The sharesmb property. Values: on | off | sharemgr(1M) options" end newproperty(:snapdir) do - desc "The snapdir property." + desc "The snapdir property. Values: hidden | visible" + end + + newproperty(:version) do + desc "The version property. Values: 1 | 2 | 3 | 4 | current" + end + + newproperty(:volsize) do + desc "The volsize property. Values: <size>" + end + + newproperty(:vscan) do + desc "The vscan property. Values: on | off" + end + + newproperty(:xattr) do + desc "The xattr property. Values: on | off" + end + + newproperty(:zoned) do + desc "The zoned property. Values: on | off" end autorequire(:zpool) do diff --git a/lib/puppet/type/zone.rb b/lib/puppet/type/zone.rb index 1bae93120..0fc702ccf 100644 --- a/lib/puppet/type/zone.rb +++ b/lib/puppet/type/zone.rb @@ -409,7 +409,7 @@ Puppet::Type.newtype(:zone) do # both as prerequisites. autorequire(:file) do if @parameters.include? :path - [@parameters[:path].value, File.dirname(@parameters[:path].value)] + [@parameters[:path].value, ::File.dirname(@parameters[:path].value)] else nil end diff --git a/lib/puppet/type/zpool.rb b/lib/puppet/type/zpool.rb index 40ee8f286..2da713c2b 100755 --- a/lib/puppet/type/zpool.rb +++ b/lib/puppet/type/zpool.rb @@ -4,6 +4,7 @@ module Puppet class VDev < Property def flatten_and_sort(array) + array = [array] unless array.is_a? Array array.collect { |a| a.split(' ') }.flatten.sort end diff --git a/lib/puppet/util/classgen.rb b/lib/puppet/util/classgen.rb index ed69c5878..1e99aa873 100644 --- a/lib/puppet/util/classgen.rb +++ b/lib/puppet/util/classgen.rb @@ -124,11 +124,23 @@ module Puppet::Util::ClassGen klass end + # const_defined? in Ruby 1.9 behaves differently in terms + # of which class hierarchy it polls for nested namespaces + # + # See http://redmine.ruby-lang.org/issues/show/1915 + def is_constant_defined?(const) + if ::RUBY_VERSION =~ /1.9/ + const_defined?(const, false) + else + const_defined?(const) + end + end + # Handle the setting and/or removing of the associated constant. def handleclassconst(klass, name, options) const = genconst_string(name, options) - if const_defined?(const) + if is_constant_defined?(const) if options[:overwrite] Puppet.info "Redefining #{name} in #{self}" remove_const(const) diff --git a/lib/puppet/util/loadedfile.rb b/lib/puppet/util/loadedfile.rb index 735dba459..d2f5d0923 100755 --- a/lib/puppet/util/loadedfile.rb +++ b/lib/puppet/util/loadedfile.rb @@ -34,10 +34,6 @@ module Puppet # Create the file. Must be passed the file path. def initialize(file) @file = file - unless FileTest.exists?(@file) - raise Puppet::NoSuchFile, - "Can not use a non-existent file for parsing" - end @statted = 0 @stamp = nil @tstamp = stamp @@ -50,7 +46,7 @@ module Puppet @statted = Time.now.to_i begin @stamp = File.stat(@file).ctime - rescue Errno::ENOENT + rescue Errno::ENOENT, Errno::ENOTDIR @stamp = Time.now end end diff --git a/lib/puppet/util/log/destinations.rb b/lib/puppet/util/log/destinations.rb index dd0d996bf..9550e2c3b 100644 --- a/lib/puppet/util/log/destinations.rb +++ b/lib/puppet/util/log/destinations.rb @@ -96,7 +96,7 @@ Puppet::Util::Log.newdesttype :console do HWHITE = {:console => "[1;37m", :html => "FFFFFF"} RESET = {:console => "[0m", :html => "" } - @@colormap = { + Colormap = { :debug => WHITE, :info => GREEN, :notice => CYAN, @@ -117,11 +117,11 @@ Puppet::Util::Log.newdesttype :console do end def console_color(level, str) - @@colormap[level][:console] + str + RESET[:console] + Colormap[level][:console] + str + RESET[:console] end def html_color(level, str) - %{<span style="color: %s">%s</span>} % [@@colormap[level][:html], str] + %{<span style="color: %s">%s</span>} % [Colormap[level][:html], str] end def initialize diff --git a/lib/puppet/util/rdoc/parser.rb b/lib/puppet/util/rdoc/parser.rb index 0f746e2ea..762ce25f0 100644 --- a/lib/puppet/util/rdoc/parser.rb +++ b/lib/puppet/util/rdoc/parser.rb @@ -7,13 +7,19 @@ require "rdoc/code_objects" require "puppet/util/rdoc/code_objects" require "rdoc/tokenstream" -require "rdoc/markup/simple_markup/preprocess" -require "rdoc/parsers/parserfactory" + +if ::RUBY_VERSION =~ /1.9/ + require "rdoc/markup/preprocess" + require "rdoc/parser" +else + require "rdoc/markup/simple_markup/preprocess" + require "rdoc/parsers/parserfactory" +end module RDoc class Parser - extend ParserFactory + extend ParserFactory unless ::RUBY_VERSION =~ /1.9/ SITE = "__site__" diff --git a/spec/integration/indirector/catalog/compiler_spec.rb b/spec/integration/indirector/catalog/compiler_spec.rb index dcb7eb425..1146c20b0 100755 --- a/spec/integration/indirector/catalog/compiler_spec.rb +++ b/spec/integration/indirector/catalog/compiler_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/resource/catalog' diff --git a/spec/integration/indirector/catalog/queue_spec.rb b/spec/integration/indirector/catalog/queue_spec.rb index 4581e3062..237e489f9 100755 --- a/spec/integration/indirector/catalog/queue_spec.rb +++ b/spec/integration/indirector/catalog/queue_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/resource/catalog' diff --git a/spec/integration/provider/package_spec.rb b/spec/integration/provider/package_spec.rb index 472662d6b..cc03c57a7 100755 --- a/spec/integration/provider/package_spec.rb +++ b/spec/integration/provider/package_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe "Package Provider" do Puppet::Type.type(:package).providers.each do |name| diff --git a/spec/integration/provider/service/init_spec.rb b/spec/integration/provider/service/init_spec.rb index 2e2505bd4..c74ce29fe 100755 --- a/spec/integration/provider/service/init_spec.rb +++ b/spec/integration/provider/service/init_spec.rb @@ -1,7 +1,6 @@ #!/usr/bin/env ruby -# Find and load the spec file. -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') provider = Puppet::Type.type(:service).provider(:init) diff --git a/spec/integration/reference/providers_spec.rb b/spec/integration/reference/providers_spec.rb index c2b1e17c5..d6e19cb59 100755 --- a/spec/integration/reference/providers_spec.rb +++ b/spec/integration/reference/providers_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/reference' diff --git a/spec/integration/transaction_spec.rb b/spec/integration/transaction_spec.rb index b5080b5c6..ff15e597d 100755 --- a/spec/integration/transaction_spec.rb +++ b/spec/integration/transaction_spec.rb @@ -107,29 +107,23 @@ describe Puppet::Transaction do file1 = tmpfile("file1") file2 = tmpfile("file2") - file = Puppet::Type.type(:file).new( - - :path => path, - + file = Puppet::Type.type(:file).new( + :path => path, :ensure => "file" ) - exec1 = Puppet::Type.type(:exec).new( - - :path => ENV["PATH"], + exec1 = Puppet::Type.type(:exec).new( + :path => ENV["PATH"], :command => "touch #{file1}", :refreshonly => true, - - :subscribe => Puppet::Resource.new(:file, path) + :subscribe => Puppet::Resource.new(:file, path) ) - exec2 = Puppet::Type.type(:exec).new( - - :path => ENV["PATH"], - :command => "touch #{file2}", + exec2 = Puppet::Type.type(:exec).new( + :path => ENV["PATH"], + :command => "touch #{file2}", :refreshonly => true, - - :subscribe => Puppet::Resource.new(:file, path) + :subscribe => Puppet::Resource.new(:file, path) ) catalog = mk_catalog(file, exec1, exec2) diff --git a/spec/integration/util/autoload_spec.rb b/spec/integration/util/autoload_spec.rb index 8a5d66232..f9b913502 100755 --- a/spec/integration/util/autoload_spec.rb +++ b/spec/integration/util/autoload_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/autoload' require 'fileutils' diff --git a/spec/integration/util/feature_spec.rb b/spec/integration/util/feature_spec.rb index f1ada9057..b038cf308 100755 --- a/spec/integration/util/feature_spec.rb +++ b/spec/integration/util/feature_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/feature' require 'puppet_spec/files' diff --git a/spec/integration/util/file_locking_spec.rb b/spec/integration/util/file_locking_spec.rb index 50613448b..1914fadf7 100755 --- a/spec/integration/util/file_locking_spec.rb +++ b/spec/integration/util/file_locking_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/file_locking' diff --git a/spec/integration/util/rdoc/parser_spec.rb b/spec/integration/util/rdoc/parser_spec.rb index 7d3b0ec3e..9cb0c9e4a 100755 --- a/spec/integration/util/rdoc/parser_spec.rb +++ b/spec/integration/util/rdoc/parser_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/resource/type_collection' require 'puppet/util/rdoc/parser' diff --git a/spec/lib/puppet_spec/verbose.rb b/spec/lib/puppet_spec/verbose.rb new file mode 100644 index 000000000..d9834f2d7 --- /dev/null +++ b/spec/lib/puppet_spec/verbose.rb @@ -0,0 +1,9 @@ +# Support code for running stuff with warnings disabled. +module Kernel + def with_verbose_disabled + verbose, $VERBOSE = $VERBOSE, nil + result = yield + $VERBOSE = verbose + return result + end +end diff --git a/spec/shared_behaviours/path_parameters.rb b/spec/shared_behaviours/path_parameters.rb new file mode 100644 index 000000000..b5a907900 --- /dev/null +++ b/spec/shared_behaviours/path_parameters.rb @@ -0,0 +1,185 @@ +# In order to use this correctly you must define a method to get an instance +# of the type being tested, so that this code can remain generic: +# +# it_should_behave_like "all path parameters", :path do +# def instance(path) +# Puppet::Type.type(:example).new( +# :name => 'foo', :require => 'bar', :path_param => path +# ) +# end +# +# That method will be invoked for each test to create the instance that we +# subsequently test through the system; you should ensure that the minimum of +# possible attributes are set to keep the tests clean. +# +# You must also pass the symbolic name of the parameter being tested to the +# block, and optionally can pass a hash of additional options to the block. +# +# The known options are: +# :array :: boolean, does this support arrays of paths, default true. + +shared_examples_for "all pathname parameters with arrays" do |win32| + path_types = { + "unix absolute" => "/foo/bar", + "unix relative" => "foo/bar", + "win32 absolute" => %q{\foo\bar}, + "win32 relative" => %q{foo\bar}, + "drive absolute" => %q{c:\foo\bar}, + "drive relative" => %q{c:foo\bar} + } + + describe "when given an array of paths" do + (1..path_types.length).each do |n| + path_types.keys.combination(n) do |set| + data = path_types.collect { |k, v| set.member?(k) ? v : nil } .compact + reject = true + only_absolute = set.find { |k| k =~ /relative/ } .nil? + only_unix = set.reject { |k| k =~ /unix/ } .length == 0 + + if only_absolute and (only_unix or win32) then + reject = false + end + + it "should #{reject ? 'reject' : 'accept'} #{set.join(", ")}" do + if reject then + expect { instance(data) }. + should raise_error Puppet::Error, /fully qualified/ + else + instance = instance(data) + instance[@param].should == data + end + end + + it "should #{reject ? 'reject' : 'accept'} #{set.join(", ")} doubled" do + if reject then + expect { instance(data + data) }. + should raise_error Puppet::Error, /fully qualified/ + else + instance = instance(data + data) + instance[@param].should == (data + data) + end + end + end + end + end +end + + +shared_examples_for "all path parameters" do |param, options| + # Extract and process options to the block. + options ||= {} + array = options[:array].nil? ? true : options.delete(:array) + if options.keys.length > 0 then + fail "unknown options for 'all path parameters': " + + options.keys.sort.join(', ') + end + + def instance(path) + fail "we didn't implement the 'instance(path)' method in the it_should_behave_like block" + end + + ######################################################################## + # The actual testing code... + before :all do + @param = param + end + + before :each do + @file_separator = File::SEPARATOR + end + after :each do + with_verbose_disabled do + verbose, $VERBOSE = $VERBOSE, nil + File::SEPARATOR = @file_separator + $VERBOSE = verbose + end + end + + describe "on a Unix-like platform it" do + before :each do + with_verbose_disabled do + File::SEPARATOR = '/' + end + Puppet.features.stubs(:microsoft_windows?).returns(false) + Puppet.features.stubs(:posix?).returns(true) + end + + if array then + it_should_behave_like "all pathname parameters with arrays", false + end + + it "should accept a fully qualified path" do + path = File.join('', 'foo') + instance = instance(path) + instance[@param].should == path + end + + it "should give a useful error when the path is not absolute" do + path = 'foo' + expect { instance(path) }. + should raise_error Puppet::Error, /fully qualified/ + end + + { "Unix" => '/', "Win32" => '\\' }.each do |style, slash| + %w{q Q a A z Z c C}.sort.each do |drive| + it "should reject drive letter '#{drive}' with #{style} path separators" do + path = "#{drive}:#{slash}Program Files" + expect { instance(path) }. + should raise_error Puppet::Error, /fully qualified/ + end + end + end + end + + describe "on a Windows-like platform it" do + before :each do + with_verbose_disabled do + File::SEPARATOR = '\\' + end + Puppet.features.stubs(:microsoft_windows?).returns(true) + Puppet.features.stubs(:posix?).returns(false) + end + + if array then + it_should_behave_like "all pathname parameters with arrays", true + end + + it "should accept a fully qualified path" do + path = File.join('', 'foo') + instance = instance(path) + instance[@param].should == path + end + + it "should give a useful error when the path is not absolute" do + path = 'foo' + expect { instance(path) }. + should raise_error Puppet::Error, /fully qualified/ + end + + it "also accepts Unix style path separators" do + path = '/Program Files' + instance = instance(path) + instance[@param].should == path + end + + { "Unix" => '/', "Win32" => '\\' }.each do |style, slash| + %w{q Q a A z Z c C}.sort.each do |drive| + it "should accept drive letter '#{drive}' with #{style} path separators " do + path = "#{drive}:#{slash}Program Files" + instance = instance(path) + instance[@param].should == path + end + end + end + + { "UNC paths" => %q{\\foo\bar}, + "unparsed local paths" => %q{\\?\c:\foo}, + "unparsed UNC paths" => %q{\\?\foo\bar} + }.each do |name, path| + it "should accept #{name} as absolute" do + instance = instance(path) + instance[@param].should == path + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d7f546b0b..13470428e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,11 +19,17 @@ module PuppetSpec FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR) end +require 'pathname' +require 'lib/puppet_spec/verbose' require 'lib/puppet_spec/files' require 'lib/puppet_spec/fixtures' require 'monkey_patches/alias_should_to_must' require 'monkey_patches/publicize_methods' +Pathname.glob("#{dir}/shared_behaviours/**/*.rb") do |behaviour| + require behaviour.relative_path_from(Pathname.new(dir)) +end + RSpec.configure do |config| include PuppetSpec::Fixtures diff --git a/spec/unit/application/agent_spec.rb b/spec/unit/application/agent_spec.rb index cc745d1fc..804057868 100755 --- a/spec/unit/application/agent_spec.rb +++ b/spec/unit/application/agent_spec.rb @@ -268,8 +268,9 @@ describe Puppet::Application::Agent do end it "should exit after printing puppet config if asked to in Puppet config" do - Puppet[:configprint] = "pluginsync" - + Puppet[:modulepath] = '/my/path' + Puppet[:configprint] = "modulepath" + Puppet::Util::Settings.any_instance.expects(:puts).with('/my/path') lambda { @puppetd.setup }.should raise_error(SystemExit) end diff --git a/spec/unit/application/master_spec.rb b/spec/unit/application/master_spec.rb index 1173752d9..14478a61a 100644 --- a/spec/unit/application/master_spec.rb +++ b/spec/unit/application/master_spec.rb @@ -177,8 +177,8 @@ describe Puppet::Application::Master do lambda { @master.setup }.should raise_error(SystemExit) end - it "should tell Puppet.settings to use :main,:ssl and :master category" do - Puppet.settings.expects(:use).with(:main,:master,:ssl) + it "should tell Puppet.settings to use :main,:ssl,:master and :metrics category" do + Puppet.settings.expects(:use).with(:main,:master,:ssl,:metrics) @master.setup end diff --git a/spec/unit/file_serving/fileset_spec.rb b/spec/unit/file_serving/fileset_spec.rb index 1ef9cdc50..3b12c953e 100755 --- a/spec/unit/file_serving/fileset_spec.rb +++ b/spec/unit/file_serving/fileset_spec.rb @@ -13,6 +13,14 @@ describe Puppet::FileServing::Fileset, " when initializing" do proc { Puppet::FileServing::Fileset.new("some/file") }.should raise_error(ArgumentError) end + it "should not fail if the path is fully qualified, with a trailing separator" do + path = "/some/path/with/trailing/separator" + path_with_separator = "#{path}#{File::SEPARATOR}" + File.stubs(:lstat).with(path).returns stub('stat') + fileset = Puppet::FileServing::Fileset.new(path_with_separator) + fileset.path.should == path + end + it "should fail if its path does not exist" do File.expects(:lstat).with("/some/file").returns nil proc { Puppet::FileServing::Fileset.new("/some/file") }.should raise_error(ArgumentError) diff --git a/spec/unit/indirector/active_record_spec.rb b/spec/unit/indirector/active_record_spec.rb index 4765cc5ea..4fab17da2 100755 --- a/spec/unit/indirector/active_record_spec.rb +++ b/spec/unit/indirector/active_record_spec.rb @@ -14,10 +14,9 @@ describe Puppet::Indirector::ActiveRecord do @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @active_record_class = Class.new(Puppet::Indirector::ActiveRecord) do - def self.to_s - "Mystuff::Testing" - end + module Testing; end + @active_record_class = class Testing::MyActiveRecord < Puppet::Indirector::ActiveRecord + self end @ar_model = mock 'ar_model' diff --git a/spec/unit/indirector/code_spec.rb b/spec/unit/indirector/code_spec.rb index 452d009f8..1c9e4d2f1 100755 --- a/spec/unit/indirector/code_spec.rb +++ b/spec/unit/indirector/code_spec.rb @@ -4,16 +4,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/indirector/code' describe Puppet::Indirector::Code do - before do + before :all do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @code_class = Class.new(Puppet::Indirector::Code) do - def self.to_s - "Mystuff::Testing" - end + module Testing; end + @code_class = class Testing::MyCode < Puppet::Indirector::Code + self end @searcher = @code_class.new diff --git a/spec/unit/indirector/direct_file_server_spec.rb b/spec/unit/indirector/direct_file_server_spec.rb index 8eab4ad76..5d9af626f 100755 --- a/spec/unit/indirector/direct_file_server_spec.rb +++ b/spec/unit/indirector/direct_file_server_spec.rb @@ -8,16 +8,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/indirector/direct_file_server' describe Puppet::Indirector::DirectFileServer do - before :each do + before :all do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @direct_file_class = Class.new(Puppet::Indirector::DirectFileServer) do - def self.to_s - "Testing::Mytype" - end + module Testing; end + @direct_file_class = class Testing::Mytype < Puppet::Indirector::DirectFileServer + self end @server = @direct_file_class.new diff --git a/spec/unit/indirector/exec_spec.rb b/spec/unit/indirector/exec_spec.rb index c6726d4b9..5abb00ae9 100755 --- a/spec/unit/indirector/exec_spec.rb +++ b/spec/unit/indirector/exec_spec.rb @@ -5,17 +5,17 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/indirector/exec' describe Puppet::Indirector::Exec do - before do + before :all do @indirection = stub 'indirection', :name => :testing Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection) - @exec_class = Class.new(Puppet::Indirector::Exec) do - def self.to_s - "Testing::Mytype" - end - + module Testing; end + @exec_class = class Testing::MyTesting < Puppet::Indirector::Exec attr_accessor :command + self end + end + before :each do @searcher = @exec_class.new @searcher.command = ["/echo"] @@ -47,10 +47,9 @@ describe Puppet::Indirector::Exec do @searcher.find(@request).should be_nil end - it "should return nil and log an error if there's an execution failure" do + it "should raise an exception if there's an execution failure" do @searcher.expects(:execute).with(%w{/echo foo}).raises(Puppet::ExecutionFailure.new("message")) - Puppet.expects(:err) - @searcher.find(@request).should be_nil + lambda {@searcher.find(@request)}.should raise_exception(Puppet::Error, 'Failed to find foo via exec: message') end end diff --git a/spec/unit/indirector/file_bucket_file/rest_spec.rb b/spec/unit/indirector/file_bucket_file/rest_spec.rb index 960f60ae7..d0f714751 100755 --- a/spec/unit/indirector/file_bucket_file/rest_spec.rb +++ b/spec/unit/indirector/file_bucket_file/rest_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/indirector/file_bucket_file/rest' diff --git a/spec/unit/indirector/file_server_spec.rb b/spec/unit/indirector/file_server_spec.rb index a81d50487..079eba0ae 100755 --- a/spec/unit/indirector/file_server_spec.rb +++ b/spec/unit/indirector/file_server_spec.rb @@ -10,18 +10,19 @@ require 'puppet/file_serving/configuration' describe Puppet::Indirector::FileServer do - before :each do + before :all do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @file_server_class = Class.new(Puppet::Indirector::FileServer) do - def self.to_s - "Testing::Mytype" - end + module Testing; end + @file_server_class = class Testing::MyFileServer < Puppet::Indirector::FileServer + self end + end + before :each do @file_server = @file_server_class.new @uri = "puppet://host/my/local/file" diff --git a/spec/unit/indirector/file_spec.rb b/spec/unit/indirector/file_spec.rb index 8fd197eac..96d5b2ae7 100755 --- a/spec/unit/indirector/file_spec.rb +++ b/spec/unit/indirector/file_spec.rb @@ -5,16 +5,15 @@ require 'puppet/indirector/file' describe Puppet::Indirector::File do - before :each do + before :all do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @file_class = Class.new(Puppet::Indirector::File) do - def self.to_s - "Testing::Mytype" - end + module Testing; end + @file_class = class Testing::MyFile < Puppet::Indirector::File + self end @searcher = @file_class.new diff --git a/spec/unit/indirector/ldap_spec.rb b/spec/unit/indirector/ldap_spec.rb index 2178a8709..ab5dab9ce 100755 --- a/spec/unit/indirector/ldap_spec.rb +++ b/spec/unit/indirector/ldap_spec.rb @@ -8,10 +8,9 @@ describe Puppet::Indirector::Ldap do before do @indirection = stub 'indirection', :name => :testing Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @ldap_class = Class.new(Puppet::Indirector::Ldap) do - def self.to_s - "Testing::Mytype" - end + module Testing; end + @ldap_class = class Testing::MyLdap < Puppet::Indirector::Ldap + self end @connection = mock 'ldap' diff --git a/spec/unit/indirector/memory_spec.rb b/spec/unit/indirector/memory_spec.rb index f9010326f..751adb1b6 100755 --- a/spec/unit/indirector/memory_spec.rb +++ b/spec/unit/indirector/memory_spec.rb @@ -14,10 +14,9 @@ describe Puppet::Indirector::Memory do @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @memory_class = Class.new(Puppet::Indirector::Memory) do - def self.to_s - "Mystuff::Testing" - end + module Testing; end + @memory_class = class Testing::MyMemory < Puppet::Indirector::Memory + self end @searcher = @memory_class.new diff --git a/spec/unit/indirector/plain_spec.rb b/spec/unit/indirector/plain_spec.rb index 54c0233f8..dfaa701bd 100755 --- a/spec/unit/indirector/plain_spec.rb +++ b/spec/unit/indirector/plain_spec.rb @@ -10,10 +10,9 @@ describe Puppet::Indirector::Plain do @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @plain_class = Class.new(Puppet::Indirector::Plain) do - def self.to_s - "Mystuff::Testing" - end + module Testing; end + @plain_class = class Testing::MyPlain < Puppet::Indirector::Plain + self end @searcher = @plain_class.new diff --git a/spec/unit/indirector/queue_spec.rb b/spec/unit/indirector/queue_spec.rb index 49e5e1015..6f5b44b4c 100755 --- a/spec/unit/indirector/queue_spec.rb +++ b/spec/unit/indirector/queue_spec.rb @@ -31,10 +31,9 @@ describe Puppet::Indirector::Queue, :if => Puppet.features.pson? do @model = mock 'model' @indirection = stub 'indirection', :name => :my_queue, :register_terminus_type => nil, :model => @model Puppet::Indirector::Indirection.stubs(:instance).with(:my_queue).returns(@indirection) - @store_class = Class.new(Puppet::Indirector::Queue) do - def self.to_s - 'MyQueue::MyType' - end + module MyQueue; end + @store_class = class MyQueue::MyType < Puppet::Indirector::Queue + self end @store = @store_class.new diff --git a/spec/unit/indirector/resource/rest_spec.rb b/spec/unit/indirector/resource/rest_spec.rb index 351aee33b..1285d338a 100755 --- a/spec/unit/indirector/resource/rest_spec.rb +++ b/spec/unit/indirector/resource/rest_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/indirector/resource/rest' diff --git a/spec/unit/indirector/resource_type/parser_spec.rb b/spec/unit/indirector/resource_type/parser_spec.rb index 739e58b35..f86b319f9 100755 --- a/spec/unit/indirector/resource_type/parser_spec.rb +++ b/spec/unit/indirector/resource_type/parser_spec.rb @@ -3,13 +3,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/indirector/resource_type/parser' +require 'puppet_spec/files' describe Puppet::Indirector::ResourceType::Parser do + include PuppetSpec::Files + before do @terminus = Puppet::Indirector::ResourceType::Parser.new @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo") - @krt = Puppet::Resource::TypeCollection.new(@request.environment) - @request.environment.stubs(:known_resource_types).returns @krt + @krt = @request.environment.known_resource_types end it "should be registered with the resource_type indirection" do @@ -17,16 +19,29 @@ describe Puppet::Indirector::ResourceType::Parser do end describe "when finding" do - it "should use the request's environment's list of known resource types" do - @request.environment.known_resource_types.expects(:hostclass).returns nil + it "should return any found type from the request's environment" do + type = Puppet::Resource::Type.new(:hostclass, "foo") + @request.environment.known_resource_types.add(type) - @terminus.find(@request) + @terminus.find(@request).should == type end - it "should return any found type" do - type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + it "should attempt to load the type if none is found in memory" do + dir = tmpdir("find_a_type") + FileUtils.mkdir_p(dir) + Puppet[:modulepath] = dir - @terminus.find(@request).should == type + # Make a new request, since we've reset the env + @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo::bar") + + manifest_path = File.join(dir, "foo", "manifests") + FileUtils.mkdir_p(manifest_path) + + File.open(File.join(manifest_path, "bar.pp"), "w") { |f| f.puts "class foo::bar {}" } + + result = @terminus.find(@request) + result.should be_instance_of(Puppet::Resource::Type) + result.name.should == "foo::bar" end it "should return nil if no type can be found" do @@ -52,8 +67,35 @@ describe Puppet::Indirector::ResourceType::Parser do @terminus.search(@request) end - it "should fail if anyther other than '*' was provided as the search key" do - @request.key = "foo*" + it "should return all results if '*' is provided as the search string" do + @request.key = "*" + type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + node = @krt.add(Puppet::Resource::Type.new(:node, "bar")) + define = @krt.add(Puppet::Resource::Type.new(:definition, "baz")) + + result = @terminus.search(@request) + result.should be_include(type) + result.should be_include(node) + result.should be_include(define) + end + + it "should treat any search string not '*' as a regex" do + @request.key = "a" + foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + bar = @krt.add(Puppet::Resource::Type.new(:hostclass, "bar")) + baz = @krt.add(Puppet::Resource::Type.new(:hostclass, "baz")) + + result = @terminus.search(@request) + result.should be_include(bar) + result.should be_include(baz) + result.should_not be_include(foo) + end + + it "should fail if a provided search string is not '*' and is not a valid regex" do + @request.key = "*foo*" + + # Add one instance so we don't just get an empty array" + @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) lambda { @terminus.search(@request) }.should raise_error(ArgumentError) end @@ -68,8 +110,41 @@ describe Puppet::Indirector::ResourceType::Parser do result.should be_include(define) end + it "should not return the 'main' class" do + main = @krt.add(Puppet::Resource::Type.new(:hostclass, "")) + + # So there is a return value + foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + + @terminus.search(@request).should_not be_include(main) + end + it "should return nil if no types can be found" do @terminus.search(@request).should be_nil end + + it "should load all resource types from all search paths" do + dir = tmpdir("searching_in_all") + first = File.join(dir, "first") + second = File.join(dir, "second") + FileUtils.mkdir_p(first) + FileUtils.mkdir_p(second) + Puppet[:modulepath] = "#{first}:#{second}" + + # Make a new request, since we've reset the env + @request = Puppet::Indirector::Request.new(:resource_type, :search, "*") + + onepath = File.join(first, "one", "manifests") + FileUtils.mkdir_p(onepath) + twopath = File.join(first, "two", "manifests") + FileUtils.mkdir_p(twopath) + + File.open(File.join(onepath, "oneklass.pp"), "w") { |f| f.puts "class one::oneklass {}" } + File.open(File.join(twopath, "twoklass.pp"), "w") { |f| f.puts "class two::twoklass {}" } + + result = @terminus.search(@request) + result.find { |t| t.name == "one::oneklass" }.should be_instance_of(Puppet::Resource::Type) + result.find { |t| t.name == "two::twoklass" }.should be_instance_of(Puppet::Resource::Type) + end end end diff --git a/spec/unit/indirector/rest_spec.rb b/spec/unit/indirector/rest_spec.rb index 547e68dd3..326d85f9d 100755 --- a/spec/unit/indirector/rest_spec.rb +++ b/spec/unit/indirector/rest_spec.rb @@ -26,19 +26,27 @@ shared_examples_for "a REST http call" do end describe Puppet::Indirector::REST do - before do + before :all do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = stub('model', :supported_formats => %w{}, :convert_from => nil) @instance = stub('model instance', :name= => nil) @indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model) - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + Puppet::Indirector::Indirection.expects(:instance).returns(@indirection) - @rest_class = Class.new(Puppet::Indirector::REST) do - def self.to_s - "This::Is::A::Test::Class" + module This + module Is + module A + module Test + end + end end end + @rest_class = class This::Is::A::Test::Class < Puppet::Indirector::REST + self + end + end + before :each do @response = stub('mock response', :body => 'result', :code => "200") @response.stubs(:[]).with('content-type').returns "text/plain" @response.stubs(:[]).with('content-encoding').returns nil diff --git a/spec/unit/indirector/ssl_file_spec.rb b/spec/unit/indirector/ssl_file_spec.rb index 4549127f9..ca97cf49e 100755 --- a/spec/unit/indirector/ssl_file_spec.rb +++ b/spec/unit/indirector/ssl_file_spec.rb @@ -8,15 +8,16 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/indirector/ssl_file' describe Puppet::Indirector::SslFile do - before do - @model = mock 'model' + before :all do @indirection = stub 'indirection', :name => :testing, :model => @model Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection) - @file_class = Class.new(Puppet::Indirector::SslFile) do - def self.to_s - "Testing::Mytype" - end + module Testing; end + @file_class = class Testing::MyType < Puppet::Indirector::SslFile + self end + end + before :each do + @model = mock 'model' @setting = :certdir @file_class.store_in @setting diff --git a/spec/unit/indirector/status/rest_spec.rb b/spec/unit/indirector/status/rest_spec.rb index 5eed5fc93..436c86881 100755 --- a/spec/unit/indirector/status/rest_spec.rb +++ b/spec/unit/indirector/status/rest_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/indirector/status/rest' diff --git a/spec/unit/indirector/yaml_spec.rb b/spec/unit/indirector/yaml_spec.rb index 35017991b..188e300d6 100755 --- a/spec/unit/indirector/yaml_spec.rb +++ b/spec/unit/indirector/yaml_spec.rb @@ -5,14 +5,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/indirector/yaml' describe Puppet::Indirector::Yaml, " when choosing file location" do - before :each do + before :all do @indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil - Puppet::Indirector::Indirection.stubs(:instance).with(:my_yaml).returns(@indirection) - @store_class = Class.new(Puppet::Indirector::Yaml) do - def self.to_s - "MyYaml::MyType" - end + Puppet::Indirector::Indirection.expects(:instance).with(:my_yaml).returns(@indirection) + module MyYaml; end + @store_class = class MyYaml::MyType < Puppet::Indirector::Yaml + self end + end + before :each do @store = @store_class.new @subject = Object.new diff --git a/spec/unit/module_spec.rb b/spec/unit/module_spec.rb index 54f5444ee..f3120e16b 100755 --- a/spec/unit/module_spec.rb +++ b/spec/unit/module_spec.rb @@ -1,8 +1,11 @@ #!/usr/bin/env ruby require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'puppet_spec/files' describe Puppet::Module do + include PuppetSpec::Files + before do # This is necessary because of the extra checks we have for the deprecated # 'plugins' directory @@ -267,17 +270,39 @@ describe Puppet::Module do end it "should return the path to the first found instance in its environment's module paths as its path" do + dir = tmpdir("deep_path") + first = File.join(dir, "first") + second = File.join(dir, "second") + + FileUtils.mkdir_p(first) + FileUtils.mkdir_p(second) + Puppet[:modulepath] = "#{first}:#{second}" + + modpath = File.join(first, "foo") + FileUtils.mkdir_p(modpath) + + # Make a second one, which we shouldn't find + FileUtils.mkdir_p(File.join(second, "foo")) + mod = Puppet::Module.new("foo") - env = mock 'environment' - mod.stubs(:environment).returns env + mod.path.should == modpath + end + + it "should be able to find itself in a directory other than the first directory in the module path" do + dir = tmpdir("deep_path") + first = File.join(dir, "first") + second = File.join(dir, "second") - env.expects(:modulepath).returns %w{/a /b /c} + FileUtils.mkdir_p(first) + FileUtils.mkdir_p(second) + Puppet[:modulepath] = "#{first}:#{second}" - FileTest.expects(:exist?).with("/a/foo").returns false - FileTest.expects(:exist?).with("/b/foo").returns true - FileTest.expects(:exist?).with("/c/foo").never + modpath = File.join(second, "foo") + FileUtils.mkdir_p(modpath) - mod.path.should == "/b/foo" + mod = Puppet::Module.new("foo") + mod.should be_exist + mod.path.should == modpath end it "should be considered existent if it exists in at least one module path" do diff --git a/spec/unit/network/authconfig_spec.rb b/spec/unit/network/authconfig_spec.rb index 4367e25e6..9d69e99ac 100755 --- a/spec/unit/network/authconfig_spec.rb +++ b/spec/unit/network/authconfig_spec.rb @@ -12,7 +12,7 @@ describe Puppet::Network::AuthConfig do FileTest.stubs(:exists?).returns(true) File.stubs(:stat).returns(stub('stat', :ctime => :now)) - Time.stubs(:now).returns :now + Time.stubs(:now).returns Time.now @authconfig = Puppet::Network::AuthConfig.new("dummy", false) end diff --git a/spec/unit/network/authstore_spec.rb b/spec/unit/network/authstore_spec.rb index 0b4dd21de..d477ee301 100644 --- a/spec/unit/network/authstore_spec.rb +++ b/spec/unit/network/authstore_spec.rb @@ -86,7 +86,7 @@ describe Puppet::Network::AuthStore::Declaration do [ "02001:0000:1234:0000:0000:C1C0:ABCD:0876", "2001:0000:1234:0000:00001:C1C0:ABCD:0876", - " 2001:0000:1234:0000:0000:C1C0:ABCD:0876 0", + " 2001:0000:1234:0000:0000:C1C0:ABCD:0876 0", "2001:0000:1234: 0000:0000:C1C0:ABCD:0876", "3ffe:0b00:0000:0001:0000:0000:000a", "FF02:0000:0000:0000:0000:0000:0000:0000:0001", diff --git a/spec/unit/network/http/api/v1_spec.rb b/spec/unit/network/http/api/v1_spec.rb index 25f6d8fe2..257eec5de 100644 --- a/spec/unit/network/http/api/v1_spec.rb +++ b/spec/unit/network/http/api/v1_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../../spec_helper') require 'puppet/network/http/api/v1' diff --git a/spec/unit/network/rest_authconfig_spec.rb b/spec/unit/network/rest_authconfig_spec.rb index 270d1d094..e0bcb5af0 100755 --- a/spec/unit/network/rest_authconfig_spec.rb +++ b/spec/unit/network/rest_authconfig_spec.rb @@ -22,7 +22,7 @@ describe Puppet::Network::RestAuthConfig do before :each do FileTest.stubs(:exists?).returns(true) File.stubs(:stat).returns(stub('stat', :ctime => :now)) - Time.stubs(:now).returns :now + Time.stubs(:now).returns Time.now @authconfig = Puppet::Network::RestAuthConfig.new("dummy", false) @authconfig.stubs(:read) diff --git a/spec/unit/network/xmlrpc/client_spec.rb b/spec/unit/network/xmlrpc/client_spec.rb index 8440d39fa..7498ca4cf 100755 --- a/spec/unit/network/xmlrpc/client_spec.rb +++ b/spec/unit/network/xmlrpc/client_spec.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby require 'puppet/network/client' -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe Puppet::Network::XMLRPCClient do describe "when performing the rpc call" do diff --git a/spec/unit/node/environment_spec.rb b/spec/unit/node/environment_spec.rb index 153be5f60..05527e70f 100755 --- a/spec/unit/node/environment_spec.rb +++ b/spec/unit/node/environment_spec.rb @@ -6,6 +6,7 @@ require 'puppet/node/environment' require 'puppet/util/execution' describe Puppet::Node::Environment do + include PuppetSpec::Files after do Puppet::Node::Environment.clear end @@ -276,16 +277,11 @@ describe Puppet::Node::Environment do describe "when performing initial import" do before do - @parser = stub 'parser', :file= => nil, :string => nil, :parse => nil + @parser = stub 'parser' Puppet::Parser::Parser.stubs(:new).returns @parser @env = Puppet::Node::Environment.new("env") end - it "should create a new parser instance" do - Puppet::Parser::Parser.expects(:new).returns @parser - @env.instance_eval { perform_initial_import } - end - it "should set the parser's string to the 'code' setting and parse if code is available" do Puppet.settings[:code] = "my code" @parser.expects(:string=).with "my code" @@ -294,25 +290,26 @@ describe Puppet::Node::Environment do end it "should set the parser's file to the 'manifest' setting and parse if no code is available and the manifest is available" do - File.stubs(:expand_path).with("/my/file").returns "/my/file" - File.expects(:exist?).with("/my/file").returns true - Puppet.settings[:manifest] = "/my/file" - @parser.expects(:file=).with "/my/file" + filename = tmpfile('myfile') + File.open(filename, 'w'){|f| } + Puppet.settings[:manifest] = filename + @parser.expects(:file=).with filename @parser.expects(:parse) @env.instance_eval { perform_initial_import } end - it "should not attempt to load a manifest if none is present" do - File.stubs(:expand_path).with("/my/file").returns "/my/file" - File.expects(:exist?).with("/my/file").returns false - Puppet.settings[:manifest] = "/my/file" - @parser.expects(:file=).never - @parser.expects(:parse).never + it "should pass the manifest file to the parser even if it does not exist on disk" do + filename = tmpfile('myfile') + Puppet.settings[:code] = "" + Puppet.settings[:manifest] = filename + @parser.expects(:file=).with(filename).once + @parser.expects(:parse).once @env.instance_eval { perform_initial_import } end it "should fail helpfully if there is an error importing" do File.stubs(:exist?).returns true + @parser.expects(:file=).once @parser.expects(:parse).raises ArgumentError lambda { @env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error) end diff --git a/spec/unit/parameter/path_spec.rb b/spec/unit/parameter/path_spec.rb new file mode 100644 index 000000000..08a26de33 --- /dev/null +++ b/spec/unit/parameter/path_spec.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby +require File.expand_path(File.join(File.dirname(__FILE__), '../../spec_helper')) + +require 'puppet/parameter/path' + +[false, true].each do |arrays| + describe "Puppet::Parameter::Path with arrays #{arrays}" do + it_should_behave_like "all path parameters", :path, :array => arrays do + # The new type allows us a test that is guaranteed to go direct to our + # validation code, without passing through any "real type" overrides or + # whatever on the way. + Puppet::newtype(:test_puppet_parameter_path) do + newparam(:path, :parent => Puppet::Parameter::Path, :arrays => arrays) do + isnamevar + accept_arrays arrays + end + end + + def instance(path) + Puppet::Type.type(:test_puppet_parameter_path).new(:path => path) + end + end + end +end diff --git a/spec/unit/parser/ast/casestatement_spec.rb b/spec/unit/parser/ast/casestatement_spec.rb index a77c04c43..bce3ad801 100755 --- a/spec/unit/parser/ast/casestatement_spec.rb +++ b/spec/unit/parser/ast/casestatement_spec.rb @@ -13,11 +13,14 @@ describe Puppet::Parser::AST::CaseStatement do @test = stub 'test' @test.stubs(:safeevaluate).with(@scope).returns("value") - @option1 = stub 'option1', :eachopt => nil, :default? => false - @option2 = stub 'option2', :eachopt => nil, :default? => false + @option1 = Puppet::Parser::AST::CaseOpt.new({}) + @option1.stubs(:eachopt) + @option1.stubs(:default?).returns false + @option2 = Puppet::Parser::AST::CaseOpt.new({}) + @option2.stubs(:eachopt) + @option2.stubs(:default?).returns false - @options = stub 'options' - @options.stubs(:each).multiple_yields(@option1, @option2) + @options = Puppet::Parser::AST::ASTArray.new(:children => [@option1, @option2]) @casestmt = Puppet::Parser::AST::CaseStatement.new :test => @test, :options => @options end @@ -29,8 +32,6 @@ describe Puppet::Parser::AST::CaseStatement do end it "should scan each option" do - @options.expects(:each).multiple_yields(@option1, @option2) - @casestmt.evaluate(@scope) end @@ -137,12 +138,15 @@ describe Puppet::Parser::AST::CaseStatement do options = tests.collect do |result, values| values = values.collect { |v| AST::Leaf.new :value => v } - AST::CaseOpt.new( - :value => AST::ASTArray.new(:children => values), - - :statements => AST::Leaf.new(:value => result)) + AST::CaseOpt.new( + :value => AST::ASTArray.new(:children => values), + :statements => AST::Leaf.new(:value => result) + ) end - options << AST::CaseOpt.new(:value => AST::Default.new(:value => "default"), :statements => AST::Leaf.new(:value => "default")) + options << AST::CaseOpt.new( + :value => AST::Default.new(:value => "default"), + :statements => AST::Leaf.new(:value => "default") + ) ast = nil param = AST::Variable.new(:value => "testparam") diff --git a/spec/unit/parser/compiler_spec.rb b/spec/unit/parser/compiler_spec.rb index 18f9a93b6..e4b18e14b 100755 --- a/spec/unit/parser/compiler_spec.rb +++ b/spec/unit/parser/compiler_spec.rb @@ -576,18 +576,16 @@ describe Puppet::Parser::Compiler do proc { @compiler.evaluate_classes(%w{one two}, scope) }.should raise_error(Puppet::DevError) end - it "should tag the catalog with the name of each not-found class" do - @compiler.catalog.expects(:tag).with("notfound") + it "should raise an error if a class is not found" do @scope.expects(:find_hostclass).with("notfound").returns(nil) - @compiler.evaluate_classes(%w{notfound}, @scope) + lambda{ @compiler.evaluate_classes(%w{notfound}, @scope) }.should raise_error(Puppet::Error, /Could not find class/) end - # I wish it would fail - it "should log when it can't find class" do + + it "should raise an error when it can't find class" do klasses = {'foo'=>nil} @node.classes = klasses @compiler.topscope.stubs(:find_hostclass).with('foo').returns(nil) - Puppet.expects(:info).with('Could not find class foo for testnode') - @compiler.compile + lambda{ @compiler.compile }.should raise_error(Puppet::Error, /Could not find class foo for testnode/) end end @@ -714,18 +712,6 @@ describe Puppet::Parser::Compiler do Puppet::Parser::Resource.expects(:new).never @compiler.evaluate_classes(%w{MyClass}, @scope, false) end - - it "should return the list of found classes" do - @compiler.catalog.stubs(:tag) - - @compiler.stubs(:add_resource) - @scope.stubs(:find_hostclass).with("notfound").returns(nil) - @scope.stubs(:class_scope).with(@class) - - Puppet::Parser::Resource.stubs(:new).returns(@resource) - @class.stubs :ensure_in_catalog - @compiler.evaluate_classes(%w{myclass notfound}, @scope).should == %w{myclass} - end end describe "when evaluating AST nodes with no AST nodes present" do diff --git a/spec/unit/parser/functions/create_resources_spec.rb b/spec/unit/parser/functions/create_resources_spec.rb new file mode 100755 index 000000000..d4095b777 --- /dev/null +++ b/spec/unit/parser/functions/create_resources_spec.rb @@ -0,0 +1,135 @@ +require 'puppet' +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe 'function for dynamically creating resources' do + + def get_scope + @topscope = Puppet::Parser::Scope.new + # This is necessary so we don't try to use the compiler to discover our parent. + @topscope.parent = nil + @scope = Puppet::Parser::Scope.new + @scope.compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production')) + @scope.parent = @topscope + @compiler = @scope.compiler + end + before :each do + get_scope + Puppet::Parser::Functions.function(:create_resources) + end + + it "should exist" do + Puppet::Parser::Functions.function(:create_resources).should == "function_create_resources" + end + it 'should require two arguments' do + lambda { @scope.function_create_resources(['foo']) }.should raise_error(ArgumentError, 'create_resources(): wrong number of arguments (1; must be 2)') + end + describe 'when creating native types' do + before :each do + Puppet[:code]='notify{test:}' + get_scope + @scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope) + end + it 'empty hash should not cause resources to be added' do + @scope.function_create_resources(['file', {}]) + @compiler.catalog.resources.size == 1 + end + it 'should be able to add' do + @scope.function_create_resources(['file', {'/etc/foo'=>{'ensure'=>'present'}}]) + @compiler.catalog.resource(:file, "/etc/foo")['ensure'].should == 'present' + end + it 'should accept multiple types' do + type_hash = {} + type_hash['foo'] = {'message' => 'one'} + type_hash['bar'] = {'message' => 'two'} + @scope.function_create_resources(['notify', type_hash]) + @compiler.catalog.resource(:notify, "foo")['message'].should == 'one' + @compiler.catalog.resource(:notify, "bar")['message'].should == 'two' + end + it 'should fail to add non-existing type' do + lambda { @scope.function_create_resources(['foo', {}]) }.should raise_error(ArgumentError, 'could not create resource of unknown type foo') + end + it 'should be able to add edges' do + @scope.function_create_resources(['notify', {'foo'=>{'require' => 'Notify[test]'}}]) + @scope.compiler.compile + edge = @scope.compiler.catalog.to_ral.relationship_graph.edges.detect do |edge| + edge.source.title == 'test' + end + edge.source.title.should == 'test' + edge.target.title.should == 'foo' + end + end + describe 'when dynamically creating resource types' do + before :each do + Puppet[:code]= +'define foo($one){notify{$name: message => $one}} +notify{test:} +' + get_scope + @scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope) + Puppet::Parser::Functions.function(:create_resources) + end + it 'should be able to create defined resoure types' do + @scope.function_create_resources(['foo', {'blah'=>{'one'=>'two'}}]) + # still have to compile for this to work... + # I am not sure if this constraint ruins the tests + @scope.compiler.compile + @compiler.catalog.resource(:notify, "blah")['message'].should == 'two' + end + it 'should fail if defines are missing params' do + @scope.function_create_resources(['foo', {'blah'=>{}}]) + lambda { @scope.compiler.compile }.should raise_error(Puppet::ParseError, 'Must pass one to Foo[blah] at line 1') + end + it 'should be able to add multiple defines' do + hash = {} + hash['blah'] = {'one' => 'two'} + hash['blaz'] = {'one' => 'three'} + @scope.function_create_resources(['foo', hash]) + # still have to compile for this to work... + # I am not sure if this constraint ruins the tests + @scope.compiler.compile + @compiler.catalog.resource(:notify, "blah")['message'].should == 'two' + @compiler.catalog.resource(:notify, "blaz")['message'].should == 'three' + end + it 'should be able to add edges' do + @scope.function_create_resources(['foo', {'blah'=>{'one'=>'two', 'require' => 'Notify[test]'}}]) + @scope.compiler.compile + edge = @scope.compiler.catalog.to_ral.relationship_graph.edges.detect do |edge| + edge.source.title == 'test' + end + edge.source.title.should == 'test' + edge.target.title.should == 'blah' + @compiler.catalog.resource(:notify, "blah")['message'].should == 'two' + end + end + describe 'when creating classes' do + before :each do + Puppet[:code]= +'class bar($one){notify{test: message => $one}} +notify{tester:} +' + get_scope + @scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope) + Puppet::Parser::Functions.function(:create_resources) + end + it 'should be able to create classes' do + @scope.function_create_resources(['class', {'bar'=>{'one'=>'two'}}]) + @scope.compiler.compile + @compiler.catalog.resource(:notify, "test")['message'].should == 'two' + @compiler.catalog.resource(:class, "bar").should_not be_nil#['message'].should == 'two' + end + it 'should fail to create non-existing classes' do + lambda { @scope.function_create_resources(['class', {'blah'=>{'one'=>'two'}}]) }.should raise_error(ArgumentError ,'could not find hostclass blah') + end + it 'should be able to add edges' do + @scope.function_create_resources(['class', {'bar'=>{'one'=>'two', 'require' => 'Notify[tester]'}}]) + @scope.compiler.compile + edge = @scope.compiler.catalog.to_ral.relationship_graph.edges.detect do |e| + e.source.title == 'tester' + end + edge.source.title.should == 'tester' + edge.target.title.should == 'test' + #@compiler.catalog.resource(:notify, "blah")['message'].should == 'two' + end + + end +end diff --git a/spec/unit/parser/lexer_spec.rb b/spec/unit/parser/lexer_spec.rb index 96df61348..bc9e22e48 100755 --- a/spec/unit/parser/lexer_spec.rb +++ b/spec/unit/parser/lexer_spec.rb @@ -676,3 +676,15 @@ describe "Puppet::Parser::Lexer in the old tests when lexing example files" do end end end + +describe "when trying to lex an non-existent file" do + include PuppetSpec::Files + + it "should return an empty list of tokens" do + lexer = Puppet::Parser::Lexer.new + lexer.file = nofile = tmpfile('lexer') + File.exists?(nofile).should == false + + lexer.fullscan.should == [[false,false]] + end +end diff --git a/spec/unit/parser/type_loader_spec.rb b/spec/unit/parser/type_loader_spec.rb index bd41adfb6..12bc1ccd6 100644 --- a/spec/unit/parser/type_loader_spec.rb +++ b/spec/unit/parser/type_loader_spec.rb @@ -93,6 +93,103 @@ describe Puppet::Parser::TypeLoader do end end + describe "when importing all" do + before do + @base = tmpdir("base") + + # Create two module path directories + @modulebase1 = File.join(@base, "first") + FileUtils.mkdir_p(@modulebase1) + @modulebase2 = File.join(@base, "second") + FileUtils.mkdir_p(@modulebase2) + + Puppet[:modulepath] = "#{@modulebase1}:#{@modulebase2}" + end + + def mk_module(basedir, name) + module_dir = File.join(basedir, name) + + # Go ahead and make our manifest directory + FileUtils.mkdir_p(File.join(module_dir, "manifests")) + + return Puppet::Module.new(name) + end + + # We have to pass the base path so that we can + # write to modules that are in the second search path + def mk_manifests(base, mod, type, files) + exts = {"ruby" => ".rb", "puppet" => ".pp"} + files.collect do |file| + name = mod.name + "::" + file.gsub("/", "::") + path = File.join(base, mod.name, "manifests", file + exts[type]) + FileUtils.mkdir_p(File.split(path)[0]) + + # write out the class + if type == "ruby" + File.open(path, "w") { |f| f.print "hostclass '#{name}' do\nend" } + else + File.open(path, "w") { |f| f.print "class #{name} {}" } + end + name + end + end + + it "should load all puppet manifests from all modules in the specified environment" do + @module1 = mk_module(@modulebase1, "one") + @module2 = mk_module(@modulebase2, "two") + + mk_manifests(@modulebase1, @module1, "puppet", %w{a b}) + mk_manifests(@modulebase2, @module2, "puppet", %w{c d}) + + @loader.import_all + + @loader.environment.known_resource_types.hostclass("one::a").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("one::b").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("two::c").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("two::d").should be_instance_of(Puppet::Resource::Type) + end + + it "should load all ruby manifests from all modules in the specified environment" do + @module1 = mk_module(@modulebase1, "one") + @module2 = mk_module(@modulebase2, "two") + + mk_manifests(@modulebase1, @module1, "ruby", %w{a b}) + mk_manifests(@modulebase2, @module2, "ruby", %w{c d}) + + @loader.import_all + + @loader.environment.known_resource_types.hostclass("one::a").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("one::b").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("two::c").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("two::d").should be_instance_of(Puppet::Resource::Type) + end + + it "should not load manifests from duplicate modules later in the module path" do + @module1 = mk_module(@modulebase1, "one") + + # duplicate + @module2 = mk_module(@modulebase2, "one") + + mk_manifests(@modulebase1, @module1, "puppet", %w{a}) + mk_manifests(@modulebase2, @module2, "puppet", %w{c}) + + @loader.import_all + + @loader.environment.known_resource_types.hostclass("one::c").should be_nil + end + + it "should load manifests from subdirectories" do + @module1 = mk_module(@modulebase1, "one") + + mk_manifests(@modulebase1, @module1, "puppet", %w{a a/b a/b/c}) + + @loader.import_all + + @loader.environment.known_resource_types.hostclass("one::a::b").should be_instance_of(Puppet::Resource::Type) + @loader.environment.known_resource_types.hostclass("one::a::b::c").should be_instance_of(Puppet::Resource::Type) + end + end + describe "when parsing a file" do before do @parser = Puppet::Parser::Parser.new(@loader.environment) diff --git a/spec/unit/property/ensure_spec.rb b/spec/unit/property/ensure_spec.rb index f2722de89..d3029c658 100644 --- a/spec/unit/property/ensure_spec.rb +++ b/spec/unit/property/ensure_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/property/ensure' diff --git a/spec/unit/property/keyvalue_spec.rb b/spec/unit/property/keyvalue_spec.rb index a44d891d7..a0175cfa3 100644 --- a/spec/unit/property/keyvalue_spec.rb +++ b/spec/unit/property/keyvalue_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/property/keyvalue' diff --git a/spec/unit/property/list_spec.rb b/spec/unit/property/list_spec.rb index c6c5db10e..704fbe3d9 100644 --- a/spec/unit/property/list_spec.rb +++ b/spec/unit/property/list_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/property/list' diff --git a/spec/unit/property/ordered_list_spec.rb b/spec/unit/property/ordered_list_spec.rb index 7c8eceb0d..460bec79d 100644 --- a/spec/unit/property/ordered_list_spec.rb +++ b/spec/unit/property/ordered_list_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/property/ordered_list' diff --git a/spec/unit/provider/augeas/augeas_spec.rb b/spec/unit/provider/augeas/augeas_spec.rb index 9fcc85660..c65f39097 100644 --- a/spec/unit/provider/augeas/augeas_spec.rb +++ b/spec/unit/provider/augeas/augeas_spec.rb @@ -433,5 +433,59 @@ describe provider_class do @augeas.expects(:close) @provider.execute_changes.should == :executed end + + it "should handle defvar commands" do + command = "defvar myjar Jar/Jar" + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:defvar).with("myjar", "/foo/Jar/Jar").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should pass through augeas variables without context" do + command = ["defvar myjar Jar/Jar","set $myjar/Binks 1"] + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:defvar).with("myjar", "/foo/Jar/Jar").returns(true) + # this is the important bit, shouldn't be /foo/$myjar/Binks + @augeas.expects(:set).with("$myjar/Binks", "1").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle defnode commands" do + command = "defnode newjar Jar/Jar[last()+1] Binks" + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:defnode).with("newjar", "/foo/Jar/Jar[last()+1]", "Binks").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle mv commands" do + command = "mv Jar/Jar Binks" + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:mv).with("/foo/Jar/Jar", "/foo/Binks").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end + + it "should handle setm commands" do + command = ["set test[1]/Jar/Jar Foo","set test[2]/Jar/Jar Bar","setm test Jar/Jar Binks"] + context = "/foo/" + @resource.expects(:[]).times(2).returns(command).then.returns(context) + @augeas.expects(:set).with("/foo/test[1]/Jar/Jar", "Foo").returns(true) + @augeas.expects(:set).with("/foo/test[2]/Jar/Jar", "Bar").returns(true) + @augeas.expects(:setm).with("/foo/test", "Jar/Jar", "Binks").returns(true) + @augeas.expects(:save).returns(true) + @augeas.expects(:close) + @provider.execute_changes.should == :executed + end end end diff --git a/spec/unit/provider/exec/posix_spec.rb b/spec/unit/provider/exec/posix_spec.rb new file mode 100755 index 000000000..d02099250 --- /dev/null +++ b/spec/unit/provider/exec/posix_spec.rb @@ -0,0 +1,120 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__) + '/../../../spec_helper' + +provider_class = Puppet::Type.type(:exec).provider(:posix) + +describe provider_class do + before :each do + @resource = Puppet::Resource.new(:exec, 'foo') + @provider = provider_class.new(@resource) + end + + ["posix", "microsoft_windows"].each do |feature| + describe "when in #{feature} environment" do + before :each do + if feature == "microsoft_windows" + Puppet.features.stubs(:microsoft_windows?).returns(true) + Puppet.features.stubs(:posix?).returns(false) + else + Puppet.features.stubs(:posix?).returns(true) + Puppet.features.stubs(:microsoft_windows?).returns(false) + end + end + + describe "#validatecmd" do + it "should fail if no path is specified and the command is not fully qualified" do + lambda { @provider.validatecmd("foo") }.should raise_error( + Puppet::Error, + "'foo' is not qualified and no path was specified. Please qualify the command or specify a path." + ) + end + + it "should pass if a path is given" do + @provider.resource[:path] = ['/bogus/bin'] + @provider.validatecmd("../foo") + end + + it "should pass if command is fully qualifed" do + @provider.resource[:path] = ['/bogus/bin'] + @provider.validatecmd("/bin/blah/foo") + end + end + + describe "#run" do + it "should fail if no path is specified and command does not exist" do + lambda { @provider.run("foo") }.should raise_error(ArgumentError, "Could not find command 'foo'") + end + + it "should fail if the command isn't in the path" do + @provider.resource[:path] = ['/bogus/bin'] + lambda { @provider.run("foo") }.should raise_error(ArgumentError, "Could not find command 'foo'") + end + + it "should fail if the command isn't executable" do + @provider.resource[:path] = ['/bogus/bin'] + File.stubs(:exists?).with("foo").returns(true) + + lambda { @provider.run("foo") }.should raise_error(ArgumentError, "'foo' is not executable") + end + + it "should not be able to execute shell builtins" do + @provider.resource[:path] = ['/bin'] + lambda { @provider.run("cd ..") }.should raise_error(ArgumentError, "Could not find command 'cd'") + end + + it "should execute the command if the command given includes arguments or subcommands" do + @provider.resource[:path] = ['/bogus/bin'] + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with("foo").returns(true) + File.stubs(:executable?).with("foo").returns(true) + + Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo bar --sillyarg=true --blah']) && (arguments.is_a? Hash) } + @provider.run("foo bar --sillyarg=true --blah") + end + + it "should fail if quoted command doesn't exist" do + @provider.resource[:path] = ['/bogus/bin'] + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with("foo").returns(true) + File.stubs(:executable?).with("foo").returns(true) + + lambda { @provider.run('"foo bar --sillyarg=true --blah"') }.should raise_error(ArgumentError, "Could not find command 'foo bar --sillyarg=true --blah'") + end + + it "should execute the command if it finds it in the path and is executable" do + @provider.resource[:path] = ['/bogus/bin'] + File.stubs(:exists?).with("foo").returns(true) + File.stubs(:executable?).with("foo").returns(true) + Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo']) && (arguments.is_a? Hash) } + + @provider.run("foo") + end + + if feature == "microsoft_windows" + [".exe", ".ps1", ".bat", ".com", ""].each do |extension| + it "should check file extension #{extension} when it can't find the executable" do + @provider.resource[:path] = ['/bogus/bin'] + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with("/bogus/bin/foo#{extension}").returns(true) + File.stubs(:executable?).with("foo").returns(true) + Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo']) && (arguments.is_a? Hash) } + + @provider.run("foo") + end + end + end + + it "should warn if you're overriding something in environment" do + @provider.resource[:environment] = ['WHATEVER=/something/else', 'WHATEVER=/foo'] + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with("foo").returns(true) + File.stubs(:executable?).with("foo").returns(true) + + Puppet::Util.expects(:execute).with() { |command, arguments| (command == ['foo']) && (arguments.is_a? Hash) } + @provider.run("foo") + @logs.map {|l| "#{l.level}: #{l.message}" }.should == ["warning: Overriding environment setting 'WHATEVER' with '/foo'"] + end + end + end + end +end diff --git a/spec/unit/provider/exec/shell_spec.rb b/spec/unit/provider/exec/shell_spec.rb new file mode 100644 index 000000000..4bae354c9 --- /dev/null +++ b/spec/unit/provider/exec/shell_spec.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__) + '/../../../spec_helper' + +provider_class = Puppet::Type.type(:exec).provider(:shell) + +describe provider_class do + before :each do + @resource = Puppet::Resource.new(:exec, 'foo') + @provider = provider_class.new(@resource) + end + + describe "#run" do + it "should be able to run builtin shell commands" do + output, status = @provider.run("if [ 1 = 1 ]; then echo 'blah'; fi") + status.exitstatus.should == 0 + output.should == "blah\n" + end + + it "should be able to run commands with single quotes in them" do + output, status = @provider.run("echo 'foo bar'") + status.exitstatus.should == 0 + output.should == "foo bar\n" + end + + it "should be able to run commands with double quotes in them" do + output, status = @provider.run('echo "foo bar"') + status.exitstatus.should == 0 + output.should == "foo bar\n" + end + + it "should be able to run multiple commands separated by a semicolon" do + output, status = @provider.run("echo 'foo' ; echo 'bar'") + status.exitstatus.should == 0 + output.should == "foo\nbar\n" + end + + it "should be able to read values from the environment parameter" do + @resource[:environment] = "FOO=bar" + output, status = @provider.run("echo $FOO") + status.exitstatus.should == 0 + output.should == "bar\n" + end + end + + describe "#validatecmd" do + it "should always return true because builtins don't need path or to be fully qualified" do + @provider.validatecmd('whateverdoesntmatter').should == true + end + end +end diff --git a/spec/unit/provider/package/apt_spec.rb b/spec/unit/provider/package/apt_spec.rb index 3c6bf62af..b0ce4f799 100755 --- a/spec/unit/provider/package/apt_spec.rb +++ b/spec/unit/provider/package/apt_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') provider = Puppet::Type.type(:package).provider(:apt) diff --git a/spec/unit/provider/package/dpkg_spec.rb b/spec/unit/provider/package/dpkg_spec.rb index 47ac9766e..444fb31c1 100755 --- a/spec/unit/provider/package/dpkg_spec.rb +++ b/spec/unit/provider/package/dpkg_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') provider = Puppet::Type.type(:package).provider(:dpkg) diff --git a/spec/unit/provider/package/pip_spec.rb b/spec/unit/provider/package/pip_spec.rb new file mode 100644 index 000000000..8953b4b2c --- /dev/null +++ b/spec/unit/provider/package/pip_spec.rb @@ -0,0 +1,177 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +provider_class = Puppet::Type.type(:package).provider(:pip) + +describe provider_class do + + before do + @resource = Puppet::Resource.new(:package, "sdsfdssdhdfyjymdgfcjdfjxdrssf") + @provider = provider_class.new(@resource) + end + + describe "parse" do + + it "should return a hash on valid input" do + provider_class.parse("Django==1.2.5").should == { + :ensure => "1.2.5", + :name => "Django", + :provider => :pip, + } + end + + it "should return nil on invalid input" do + provider_class.parse("foo").should == nil + end + + end + + describe "instances" do + + it "should return an array when pip is present" do + provider_class.expects(:which).with('pip').returns("/fake/bin/pip") + p = stub("process") + p.expects(:collect).yields("Django==1.2.5") + provider_class.expects(:execpipe).with("/fake/bin/pip freeze").yields(p) + provider_class.instances + end + + it "should return an empty array when pip is missing" do + provider_class.expects(:which).with('pip').returns nil + provider_class.instances.should == [] + end + + end + + describe "query" do + + before do + @resource[:name] = "Django" + end + + it "should return a hash when pip and the package are present" do + provider_class.expects(:instances).returns [provider_class.new({ + :ensure => "1.2.5", + :name => "Django", + :provider => :pip, + })] + + @provider.query.should == { + :ensure => "1.2.5", + :name => "Django", + :provider => :pip, + } + end + + it "should return nil when the package is missing" do + provider_class.expects(:instances).returns [] + @provider.query.should == nil + end + + end + + describe "latest" do + + it "should find a version number for Django" do + @resource[:name] = "Django" + @provider.latest.should_not == nil + end + + it "should not find a version number for sdsfdssdhdfyjymdgfcjdfjxdrssf" do + @resource[:name] = "sdsfdssdhdfyjymdgfcjdfjxdrssf" + @provider.latest.should == nil + end + + end + + describe "install" do + + before do + @resource[:name] = "sdsfdssdhdfyjymdgfcjdfjxdrssf" + @url = "git+https://example.com/sdsfdssdhdfyjymdgfcjdfjxdrssf.git" + end + + it "should install" do + @resource[:ensure] = :installed + @resource[:source] = nil + @provider.expects(:lazy_pip). + with("install", '-q', "sdsfdssdhdfyjymdgfcjdfjxdrssf") + @provider.install + end + + it "should install from SCM" do + @resource[:ensure] = :installed + @resource[:source] = @url + @provider.expects(:lazy_pip). + with("install", '-q', '-e', "#{@url}#egg=sdsfdssdhdfyjymdgfcjdfjxdrssf") + @provider.install + end + + it "should install a particular SCM revision" do + @resource[:ensure] = "0123456" + @resource[:source] = @url + @provider.expects(:lazy_pip). + with("install", "-q", "-e", "#{@url}@0123456#egg=sdsfdssdhdfyjymdgfcjdfjxdrssf") + @provider.install + end + + it "should install a particular version" do + @resource[:ensure] = "0.0.0" + @resource[:source] = nil + @provider.expects(:lazy_pip).with("install", "-q", "sdsfdssdhdfyjymdgfcjdfjxdrssf==0.0.0") + @provider.install + end + + it "should upgrade" do + @resource[:ensure] = :latest + @resource[:source] = nil + @provider.expects(:lazy_pip). + with("install", "-q", "--upgrade", "sdsfdssdhdfyjymdgfcjdfjxdrssf") + @provider.install + end + + end + + describe "uninstall" do + + it "should uninstall" do + @resource[:name] = "sdsfdssdhdfyjymdgfcjdfjxdrssf" + @provider.expects(:lazy_pip). + with('uninstall', '-y', '-q', 'sdsfdssdhdfyjymdgfcjdfjxdrssf') + @provider.uninstall + end + + end + + describe "update" do + + it "should just call install" do + @provider.expects(:install).returns(nil) + @provider.update + end + + end + + describe "lazy_pip" do + + it "should succeed if pip is present" do + @provider.stubs(:pip).returns(nil) + @provider.method(:lazy_pip).call "freeze" + end + + it "should retry if pip has not yet been found" do + @provider.expects(:pip).twice.with('freeze').raises(NoMethodError).then.returns(nil) + @provider.expects(:which).with('pip').returns("/fake/bin/pip") + @provider.method(:lazy_pip).call "freeze" + end + + it "should fail if pip is missing" do + @provider.expects(:pip).with('freeze').raises(NoMethodError) + @provider.expects(:which).with('pip').returns(nil) + expect { @provider.method(:lazy_pip).call("freeze") }.to raise_error(NoMethodError) + end + + end + +end diff --git a/spec/unit/provider/package/pkg_spec.rb b/spec/unit/provider/package/pkg_spec.rb index 1544b8b7e..3455c4c40 100644 --- a/spec/unit/provider/package/pkg_spec.rb +++ b/spec/unit/provider/package/pkg_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') provider = Puppet::Type.type(:package).provider(:pkg) diff --git a/spec/unit/provider/package/pkgdmg_spec.rb b/spec/unit/provider/package/pkgdmg_spec.rb index 1fd5b4ac4..86631e596 100755 --- a/spec/unit/provider/package/pkgdmg_spec.rb +++ b/spec/unit/provider/package/pkgdmg_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') provider = Puppet::Type.type(:package).provider(:pkgdmg) diff --git a/spec/unit/provider/selboolean_spec.rb b/spec/unit/provider/selboolean_spec.rb index b37b44b45..02a39f2a7 100755 --- a/spec/unit/provider/selboolean_spec.rb +++ b/spec/unit/provider/selboolean_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') provider_class = Puppet::Type.type(:selboolean).provider(:getsetsebool) diff --git a/spec/unit/provider/selmodule_spec.rb b/spec/unit/provider/selmodule_spec.rb index fda6d0d78..cb143e993 100755 --- a/spec/unit/provider/selmodule_spec.rb +++ b/spec/unit/provider/selmodule_spec.rb @@ -5,7 +5,7 @@ # with version 1.5.0. The provided selmodule-example.pp is the first # 256 bytes taken from /usr/share/selinux/targeted/nagios.pp on Fedora 9 -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') provider_class = Puppet::Type.type(:selmodule).provider(:semodule) diff --git a/spec/unit/provider/service/debian_spec.rb b/spec/unit/provider/service/debian_spec.rb index 8c9522deb..b5edf6882 100755 --- a/spec/unit/provider/service/debian_spec.rb +++ b/spec/unit/provider/service/debian_spec.rb @@ -52,8 +52,20 @@ describe provider_class do end describe "when disabling" do - it "should call update-rc.d twice" do - @provider.expects(:update_rc).twice + it "should be able to disable services with newer sysv-rc versions" do + @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "0" + + @provider.expects(:update_rc).with(@resource[:name], "disable") + + @provider.disable + end + + it "should be able to enable services with older sysv-rc versions" do + @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "1" + + @provider.expects(:update_rc).with("-f", @resource[:name], "remove") + @provider.expects(:update_rc).with(@resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", ".") + @provider.disable end end diff --git a/spec/unit/provider/service/smf_spec.rb b/spec/unit/provider/service/smf_spec.rb new file mode 100755 index 000000000..40e96ab23 --- /dev/null +++ b/spec/unit/provider/service/smf_spec.rb @@ -0,0 +1,137 @@ +#!/usr/bin/env ruby +# +# Unit testing for the SMF service Provider +# +# author Dominic Cleal +# +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +provider_class = Puppet::Type.type(:service).provider(:smf) + +describe provider_class do + + before(:each) do + # Create a mock resource + @resource = Puppet::Type.type(:service).new( + :name => "/system/myservice", :ensure => :running, :enable => :true) + @provider = provider_class.new(@resource) + + FileTest.stubs(:file?).with('/usr/sbin/svcadm').returns true + FileTest.stubs(:executable?).with('/usr/sbin/svcadm').returns true + FileTest.stubs(:file?).with('/usr/bin/svcs').returns true + FileTest.stubs(:executable?).with('/usr/bin/svcs').returns true + end + + it "should have a restart method" do + @provider.should respond_to(:restart) + end + + it "should have a restartcmd method" do + @provider.should respond_to(:restartcmd) + end + + it "should have a start method" do + @provider.should respond_to(:start) + end + + it "should have a stop method" do + @provider.should respond_to(:stop) + end + + it "should have an enabled? method" do + @provider.should respond_to(:enabled?) + end + + it "should have an enable method" do + @provider.should respond_to(:enable) + end + + it "should have a disable method" do + @provider.should respond_to(:disable) + end + + describe "when checking status" do + it "should call the external command 'svcs /system/myservice' once" do + @provider.expects(:svcs).with('-H', '-o', 'state,nstate', "/system/myservice").returns("online\t-") + @provider.status + end + it "should return stopped if svcs can't find the service" do + @provider.stubs(:svcs).raises(Puppet::ExecutionFailure.new("no svc found")) + @provider.status.should == :stopped + end + it "should return running if online in svcs output" do + @provider.stubs(:svcs).returns("online\t-") + @provider.status.should == :running + end + it "should return stopped if disabled in svcs output" do + @provider.stubs(:svcs).returns("disabled\t-") + @provider.status.should == :stopped + end + it "should return maintenance if in maintenance in svcs output" do + @provider.stubs(:svcs).returns("maintenance\t-") + @provider.status.should == :maintenance + end + it "should return target state if transitioning in svcs output" do + @provider.stubs(:svcs).returns("online\tdisabled") + @provider.status.should == :stopped + end + it "should throw error if it's a legacy service in svcs output" do + @provider.stubs(:svcs).returns("legacy_run\t-") + lambda { @provider.status }.should raise_error(Puppet::Error, "Cannot manage legacy services through SMF") + end + end + + describe "when starting" do + it "should enable the service if it is not enabled" do + @provider.expects(:status).returns :stopped + @provider.expects(:texecute) + @provider.start + end + + it "should always execute external command 'svcadm enable /system/myservice'" do + @provider.stubs(:status).returns :running + @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :enable, "/system/myservice"], true) + @provider.start + end + + it "should execute external command 'svcadm clear /system/myservice' if in maintenance" do + @provider.stubs(:status).returns :maintenance + @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :clear, "/system/myservice"], true) + @provider.start + end + end + + describe "when starting a service with a manifest" do + before(:each) do + @resource = Puppet::Type.type(:service).new(:name => "/system/myservice", :ensure => :running, :enable => :true, :manifest => "/tmp/myservice.xml") + @provider = provider_class.new(@resource) + $CHILD_STATUS.stubs(:exitstatus).returns(1) + end + + it "should import the manifest if service is missing" do + @provider.expects(:svccfg).with(:import, "/tmp/myservice.xml") + @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :enable, "/system/myservice"], true) + @provider.start + end + + it "should handle failures if importing a manifest" do + @provider.expects(:svccfg).raises(Puppet::ExecutionFailure.new("can't svccfg import")) + lambda { @provider.start }.should raise_error(Puppet::Error, "Cannot config /system/myservice to enable it: can't svccfg import") + end + end + + describe "when stopping" do + it "should execute external command 'svcadm disable /system/myservice'" do + @provider.expects(:texecute).with(:stop, ["/usr/sbin/svcadm", :disable, "/system/myservice"], true) + @provider.stop + end + end + + describe "when restarting" do + it "should call 'svcadm restart /system/myservice'" do + @provider.expects(:texecute).with(:restart, ["/usr/sbin/svcadm", :restart, "/system/myservice"], true) + @provider.restart + end + end + +end diff --git a/spec/unit/provider/zfs/solaris_spec.rb b/spec/unit/provider/zfs/solaris_spec.rb index 4998ba6fe..84a2be9f6 100755 --- a/spec/unit/provider/zfs/solaris_spec.rb +++ b/spec/unit/provider/zfs/solaris_spec.rb @@ -27,6 +27,7 @@ describe provider_class do describe "when calling add_properties" do it "should add -o and the key=value for each properties with a value" do @resource.stubs(:[]).with(:quota).returns "" + @resource.stubs(:[]).with(:refquota).returns "" @resource.stubs(:[]).with(:mountpoint).returns "/foo" properties = @provider.add_properties properties.include?("-o").should == true diff --git a/spec/unit/reports/http_spec.rb b/spec/unit/reports/http_spec.rb index 70742f7dc..a62793ed5 100644 --- a/spec/unit/reports/http_spec.rb +++ b/spec/unit/reports/http_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/reports' diff --git a/spec/unit/reports/rrdgraph_spec.rb b/spec/unit/reports/rrdgraph_spec.rb index ce2cf7905..5215f1dcc 100644 --- a/spec/unit/reports/rrdgraph_spec.rb +++ b/spec/unit/reports/rrdgraph_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/reports' diff --git a/spec/unit/reports/store_spec.rb b/spec/unit/reports/store_spec.rb index 9d9042386..d48f6a846 100644 --- a/spec/unit/reports/store_spec.rb +++ b/spec/unit/reports/store_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/reports' require 'time' diff --git a/spec/unit/reports/tagmail_spec.rb b/spec/unit/reports/tagmail_spec.rb index fa8990ebb..716bcbc04 100755 --- a/spec/unit/reports/tagmail_spec.rb +++ b/spec/unit/reports/tagmail_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/reports' diff --git a/spec/unit/resource/type_spec.rb b/spec/unit/resource/type_spec.rb index e9c203526..41b5554d9 100755 --- a/spec/unit/resource/type_spec.rb +++ b/spec/unit/resource/type_spec.rb @@ -55,12 +55,24 @@ describe Puppet::Resource::Type do double_convert.arguments.should == {"one" => nil, "two" => "foo"} end - it "should include any extra attributes" do - @type.file = "/my/file" - @type.line = 50 + it "should not include arguments if none are present" do + @type.to_pson["arguments"].should be_nil + end + + [:line, :doc, :file, :parent].each do |attr| + it "should include #{attr} when set" do + @type.send(attr.to_s + "=", "value") + double_convert.send(attr).should == "value" + end + + it "should not include #{attr} when not set" do + @type.to_pson[attr.to_s].should be_nil + end + end - double_convert.file.should == "/my/file" - double_convert.line.should == 50 + it "should not include docs if they are empty" do + @type.doc = "" + @type.to_pson["doc"].should be_nil end end diff --git a/spec/unit/type/cron_spec.rb b/spec/unit/type/cron_spec.rb index 03817d20e..5466b4d58 100755 --- a/spec/unit/type/cron_spec.rb +++ b/spec/unit/type/cron_spec.rb @@ -1,33 +1,481 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe Puppet::Type.type(:cron) do before do - @cron = Puppet::Type.type(:cron).new( :name => "foo" ) - end + @class = Puppet::Type.type(:cron) + + # Init a fake provider + @provider_class = stub 'provider_class', :ancestors => [], :name => 'fake', :suitable? => true, :supports_parameter? => true + @class.stubs(:defaultprovider).returns @provider_class + @class.stubs(:provider).returns @provider_class + + @provider = stub 'provider', :class => @provider_class, :clean => nil + @provider.stubs(:is_a?).returns false + @provider_class.stubs(:new).returns @provider - it "it should accept an :environment that looks like a path" do - lambda do - @cron[:environment] = 'PATH=/bin:/usr/bin:/usr/sbin' - end.should_not raise_error + @cron = @class.new( :name => "foo" ) end - it "should not accept environment variables that do not contain '='" do - lambda do - @cron[:environment] = "INVALID" - end.should raise_error(Puppet::Error) + it "should have :name be its namevar" do + @class.key_attributes.should == [:name] end - it "should accept empty environment variables that do not contain '='" do - lambda do - @cron[:environment] = "MAILTO=" - end.should_not raise_error(Puppet::Error) + describe "when validating attributes" do + + [:name, :provider].each do |param| + it "should have a #{param} parameter" do + @class.attrtype(param).should == :param + end + end + + [:command, :special, :minute, :hour, :weekday, :month, :monthday, :environment, :user, :target].each do |property| + it "should have a #{property} property" do + @class.attrtype(property).should == :property + end + end + + [:command, :minute, :hour, :weekday, :month, :monthday].each do |cronparam| + it "should have #{cronparam} of type CronParam" do + @class.attrclass(cronparam).ancestors.should include CronParam + end + end + end - it "should accept 'absent'" do - lambda do - @cron[:environment] = 'absent' - end.should_not raise_error(Puppet::Error) + + describe "when validating attribute" do + + describe "ensure" do + it "should support present as a value for ensure" do + proc { @class.new(:name => 'foo', :ensure => :present) }.should_not raise_error + end + + it "should support absent as a value for ensure" do + proc { @class.new(:name => 'foo', :ensure => :present) }.should_not raise_error + end + end + + describe "minute" do + + it "should support absent" do + proc { @class.new(:name => 'foo', :minute => 'absent') }.should_not raise_error + end + + it "should support *" do + proc { @class.new(:name => 'foo', :minute => '*') }.should_not raise_error + end + + it "should translate absent to :absent" do + @class.new(:name => 'foo', :minute => 'absent')[:minute].should == :absent + end + + it "should translate * to :absent" do + @class.new(:name => 'foo', :minute => '*')[:minute].should == :absent + end + + it "should support valid single values" do + proc { @class.new(:name => 'foo', :minute => '0') }.should_not raise_error + proc { @class.new(:name => 'foo', :minute => '1') }.should_not raise_error + proc { @class.new(:name => 'foo', :minute => '59') }.should_not raise_error + end + + it "should not support non numeric characters" do + proc { @class.new(:name => 'foo', :minute => 'z59') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => '5z9') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => '59z') }.should raise_error(Puppet::Error) + end + + it "should not support single values out of range" do + + proc { @class.new(:name => 'foo', :minute => '-1') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => '60') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => '61') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => '120') }.should raise_error(Puppet::Error) + end + + it "should support valid multiple values" do + proc { @class.new(:name => 'foo', :minute => ['0','1','59'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :minute => ['40','30','20'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :minute => ['10','30','20'] ) }.should_not raise_error + end + + it "should not support multiple values if at least one is invalid" do + # one invalid + proc { @class.new(:name => 'foo', :minute => ['0','1','60'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => ['0','120','59'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => ['-1','1','59'] ) }.should raise_error(Puppet::Error) + # two invalid + proc { @class.new(:name => 'foo', :minute => ['0','61','62'] ) }.should raise_error(Puppet::Error) + # all invalid + proc { @class.new(:name => 'foo', :minute => ['-1','61','62'] ) }.should raise_error(Puppet::Error) + end + + it "should support valid step syntax" do + proc { @class.new(:name => 'foo', :minute => '*/2' ) }.should_not raise_error + proc { @class.new(:name => 'foo', :minute => '10-16/2' ) }.should_not raise_error + end + + it "should not support invalid steps" do + proc { @class.new(:name => 'foo', :minute => '*/A' ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :minute => '*/2A' ) }.should raise_error(Puppet::Error) + # As it turns out cron does not complaining about steps that exceed the valid range + # proc { @class.new(:name => 'foo', :minute => '*/120' ) }.should raise_error(Puppet::Error) + end + + end + + describe "hour" do + + it "should support absent" do + proc { @class.new(:name => 'foo', :hour => 'absent') }.should_not raise_error + end + + it "should support *" do + proc { @class.new(:name => 'foo', :hour => '*') }.should_not raise_error + end + + it "should translate absent to :absent" do + @class.new(:name => 'foo', :hour => 'absent')[:hour].should == :absent + end + + it "should translate * to :absent" do + @class.new(:name => 'foo', :hour => '*')[:hour].should == :absent + end + + it "should support valid single values" do + proc { @class.new(:name => 'foo', :hour => '0') }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => '11') }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => '12') }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => '13') }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => '23') }.should_not raise_error + end + + it "should not support non numeric characters" do + proc { @class.new(:name => 'foo', :hour => 'z15') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => '1z5') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => '15z') }.should raise_error(Puppet::Error) + end + + it "should not support single values out of range" do + proc { @class.new(:name => 'foo', :hour => '-1') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => '24') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => '120') }.should raise_error(Puppet::Error) + end + + it "should support valid multiple values" do + proc { @class.new(:name => 'foo', :hour => ['0','1','23'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => ['5','16','14'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => ['16','13','9'] ) }.should_not raise_error + end + + it "should not support multiple values if at least one is invalid" do + # one invalid + proc { @class.new(:name => 'foo', :hour => ['0','1','24'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => ['0','-1','5'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => ['-1','1','23'] ) }.should raise_error(Puppet::Error) + # two invalid + proc { @class.new(:name => 'foo', :hour => ['0','25','26'] ) }.should raise_error(Puppet::Error) + # all invalid + proc { @class.new(:name => 'foo', :hour => ['-1','24','120'] ) }.should raise_error(Puppet::Error) + end + + it "should support valid step syntax" do + proc { @class.new(:name => 'foo', :hour => '*/2' ) }.should_not raise_error + proc { @class.new(:name => 'foo', :hour => '10-18/4' ) }.should_not raise_error + end + + it "should not support invalid steps" do + proc { @class.new(:name => 'foo', :hour => '*/A' ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :hour => '*/2A' ) }.should raise_error(Puppet::Error) + # As it turns out cron does not complaining about steps that exceed the valid range + # proc { @class.new(:name => 'foo', :hour => '*/26' ) }.should raise_error(Puppet::Error) + end + + end + + describe "weekday" do + + it "should support absent" do + proc { @class.new(:name => 'foo', :weekday => 'absent') }.should_not raise_error + end + + it "should support *" do + proc { @class.new(:name => 'foo', :weekday => '*') }.should_not raise_error + end + + it "should translate absent to :absent" do + @class.new(:name => 'foo', :weekday => 'absent')[:weekday].should == :absent + end + + it "should translate * to :absent" do + @class.new(:name => 'foo', :weekday => '*')[:weekday].should == :absent + end + + it "should support valid numeric weekdays" do + proc { @class.new(:name => 'foo', :weekday => '0') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => '1') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => '6') }.should_not raise_error + # According to http://www.manpagez.com/man/5/crontab 7 is also valid (Sunday) + proc { @class.new(:name => 'foo', :weekday => '7') }.should_not raise_error + end + + it "should support valid weekdays as words (3 character version)" do + proc { @class.new(:name => 'foo', :weekday => 'Monday') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Tuesday') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Wednesday') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Thursday') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Friday') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Saturday') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Sunday') }.should_not raise_error + end + + it "should support valid weekdays as words (3 character version)" do + proc { @class.new(:name => 'foo', :weekday => 'Mon') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Tue') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Wed') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Thu') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Fri') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Sat') }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => 'Sun') }.should_not raise_error + end + + it "should not support numeric values out of range" do + proc { @class.new(:name => 'foo', :weekday => '-1') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :weekday => '8') }.should raise_error(Puppet::Error) + end + + it "should not support invalid weekday names" do + proc { @class.new(:name => 'foo', :weekday => 'Sar') }.should raise_error(Puppet::Error) + end + + it "should support valid multiple values" do + proc { @class.new(:name => 'foo', :weekday => ['0','1','6'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => ['Mon','Wed','Friday'] ) }.should_not raise_error + end + + it "should not support multiple values if at least one is invalid" do + # one invalid + proc { @class.new(:name => 'foo', :weekday => ['0','1','8'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :weekday => ['Mon','Fii','Sat'] ) }.should raise_error(Puppet::Error) + # two invalid + proc { @class.new(:name => 'foo', :weekday => ['Mos','Fii','Sat'] ) }.should raise_error(Puppet::Error) + # all invalid + proc { @class.new(:name => 'foo', :weekday => ['Mos','Fii','Saa'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :weekday => ['-1','8','11'] ) }.should raise_error(Puppet::Error) + end + + it "should support valid step syntax" do + proc { @class.new(:name => 'foo', :weekday => '*/2' ) }.should_not raise_error + proc { @class.new(:name => 'foo', :weekday => '0-4/2' ) }.should_not raise_error + end + + it "should not support invalid steps" do + proc { @class.new(:name => 'foo', :weekday => '*/A' ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :weekday => '*/2A' ) }.should raise_error(Puppet::Error) + # As it turns out cron does not complaining about steps that exceed the valid range + # proc { @class.new(:name => 'foo', :weekday => '*/9' ) }.should raise_error(Puppet::Error) + end + + end + + describe "month" do + + it "should support absent" do + proc { @class.new(:name => 'foo', :month => 'absent') }.should_not raise_error + end + + it "should support *" do + proc { @class.new(:name => 'foo', :month => '*') }.should_not raise_error + end + + it "should translate absent to :absent" do + @class.new(:name => 'foo', :month => 'absent')[:month].should == :absent + end + + it "should translate * to :absent" do + @class.new(:name => 'foo', :month => '*')[:month].should == :absent + end + + it "should support valid numeric values" do + proc { @class.new(:name => 'foo', :month => '1') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => '12') }.should_not raise_error + end + + it "should support valid months as words" do + proc { @class.new(:name => 'foo', :month => 'January') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'February') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'March') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'April') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'May') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'June') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'July') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'August') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'September') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'October') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'November') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'December') }.should_not raise_error + end + + it "should support valid months as words (3 character short version)" do + proc { @class.new(:name => 'foo', :month => 'Jan') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Feb') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Mar') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Apr') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'May') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Jun') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Jul') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Aug') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Sep') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Oct') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Nov') }.should_not raise_error + proc { @class.new(:name => 'foo', :month => 'Dec') }.should_not raise_error + end + + it "should not support numeric values out of range" do + proc { @class.new(:name => 'foo', :month => '-1') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => '0') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => '13') }.should raise_error(Puppet::Error) + end + + it "should not support words that are not valid months" do + proc { @class.new(:name => 'foo', :month => 'Jal') }.should raise_error(Puppet::Error) + end + + it "should not support single values out of range" do + + proc { @class.new(:name => 'foo', :month => '-1') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => '60') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => '61') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => '120') }.should raise_error(Puppet::Error) + end + + it "should support valid multiple values" do + proc { @class.new(:name => 'foo', :month => ['1','9','12'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :month => ['Jan','March','Jul'] ) }.should_not raise_error + end + + it "should not support multiple values if at least one is invalid" do + # one invalid + proc { @class.new(:name => 'foo', :month => ['0','1','12'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => ['1','13','10'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => ['Jan','Feb','Jxx'] ) }.should raise_error(Puppet::Error) + # two invalid + proc { @class.new(:name => 'foo', :month => ['Jan','Fex','Jux'] ) }.should raise_error(Puppet::Error) + # all invalid + proc { @class.new(:name => 'foo', :month => ['-1','0','13'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => ['Jax','Fex','Aux'] ) }.should raise_error(Puppet::Error) + end + + it "should support valid step syntax" do + proc { @class.new(:name => 'foo', :month => '*/2' ) }.should_not raise_error + proc { @class.new(:name => 'foo', :month => '1-12/3' ) }.should_not raise_error + end + + it "should not support invalid steps" do + proc { @class.new(:name => 'foo', :month => '*/A' ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :month => '*/2A' ) }.should raise_error(Puppet::Error) + # As it turns out cron does not complaining about steps that exceed the valid range + # proc { @class.new(:name => 'foo', :month => '*/13' ) }.should raise_error(Puppet::Error) + end + + end + + describe "monthday" do + + it "should support absent" do + proc { @class.new(:name => 'foo', :monthday => 'absent') }.should_not raise_error + end + + it "should support *" do + proc { @class.new(:name => 'foo', :monthday => '*') }.should_not raise_error + end + + it "should translate absent to :absent" do + @class.new(:name => 'foo', :monthday => 'absent')[:monthday].should == :absent + end + + it "should translate * to :absent" do + @class.new(:name => 'foo', :monthday => '*')[:monthday].should == :absent + end + + it "should support valid single values" do + proc { @class.new(:name => 'foo', :monthday => '1') }.should_not raise_error + proc { @class.new(:name => 'foo', :monthday => '30') }.should_not raise_error + proc { @class.new(:name => 'foo', :monthday => '31') }.should_not raise_error + end + + it "should not support non numeric characters" do + proc { @class.new(:name => 'foo', :monthday => 'z23') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => '2z3') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => '23z') }.should raise_error(Puppet::Error) + end + + it "should not support single values out of range" do + proc { @class.new(:name => 'foo', :monthday => '-1') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => '0') }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => '32') }.should raise_error(Puppet::Error) + end + + it "should support valid multiple values" do + proc { @class.new(:name => 'foo', :monthday => ['1','23','31'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :monthday => ['31','23','1'] ) }.should_not raise_error + proc { @class.new(:name => 'foo', :monthday => ['1','31','23'] ) }.should_not raise_error + end + + it "should not support multiple values if at least one is invalid" do + # one invalid + proc { @class.new(:name => 'foo', :monthday => ['1','23','32'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => ['-1','12','23'] ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => ['13','32','30'] ) }.should raise_error(Puppet::Error) + # two invalid + proc { @class.new(:name => 'foo', :monthday => ['-1','0','23'] ) }.should raise_error(Puppet::Error) + # all invalid + proc { @class.new(:name => 'foo', :monthday => ['-1','0','32'] ) }.should raise_error(Puppet::Error) + end + + it "should support valid step syntax" do + proc { @class.new(:name => 'foo', :monthday => '*/2' ) }.should_not raise_error + proc { @class.new(:name => 'foo', :monthday => '10-16/2' ) }.should_not raise_error + end + + it "should not support invalid steps" do + proc { @class.new(:name => 'foo', :monthday => '*/A' ) }.should raise_error(Puppet::Error) + proc { @class.new(:name => 'foo', :monthday => '*/2A' ) }.should raise_error(Puppet::Error) + # As it turns out cron does not complaining about steps that exceed the valid range + # proc { @class.new(:name => 'foo', :monthday => '*/32' ) }.should raise_error(Puppet::Error) + end + + end + + describe "environment" do + + it "it should accept an :environment that looks like a path" do + lambda do + @cron[:environment] = 'PATH=/bin:/usr/bin:/usr/sbin' + end.should_not raise_error + end + + it "should not accept environment variables that do not contain '='" do + lambda do + @cron[:environment] = "INVALID" + end.should raise_error(Puppet::Error) + end + + it "should accept empty environment variables that do not contain '='" do + lambda do + @cron[:environment] = "MAILTO=" + end.should_not raise_error(Puppet::Error) + end + + it "should accept 'absent'" do + lambda do + @cron[:environment] = 'absent' + end.should_not raise_error(Puppet::Error) + end + + end + end end diff --git a/spec/unit/type/exec_spec.rb b/spec/unit/type/exec_spec.rb index e980ec542..86b824423 100755 --- a/spec/unit/type/exec_spec.rb +++ b/spec/unit/type/exec_spec.rb @@ -3,160 +3,658 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe Puppet::Type.type(:exec) do - - def create_resource(command, output, exitstatus, returns = 0) - @user_name = 'some_user_name' + def exec_tester(command, exitstatus = 0, rest = {}) + @user_name = 'some_user_name' @group_name = 'some_group_name' Puppet.features.stubs(:root?).returns(true) - @execer = Puppet::Type.type(:exec).new(:name => command, :path => @example_path, :user => @user_name, :group => @group_name, :returns => returns) - status = stub "process" - status.stubs(:exitstatus).returns(exitstatus) + output = rest.delete(:output) || '' + tries = rest[:tries] || 1 + + args = { + :name => command, + :path => @example_path, + :user => @user_name, + :group => @group_name, + :logoutput => false, + :loglevel => :err, + :returns => 0 + }.merge(rest) + + exec = Puppet::Type.type(:exec).new(args) + + status = stub "process", :exitstatus => exitstatus + Puppet::Util::SUIDManager.expects(:run_and_capture).times(tries). + with([command], @user_name, @group_name).returns([output, status]) - Puppet::Util::SUIDManager.expects(:run_and_capture).with([command], @user_name, @group_name).returns([output, status]) + return exec end - def create_logging_resource(command, output, exitstatus, logoutput, loglevel, returns = 0) - create_resource(command, output, exitstatus, returns) - @execer[:logoutput] = logoutput - @execer[:loglevel] = loglevel + before do + @command = Puppet.features.posix? ? '/bin/true whatever' : '"C:/Program Files/something.exe" whatever' end - def expect_output(output, loglevel) - output.split(/\n/).each do |line| - @execer.property(:returns).expects(loglevel).with(line) + describe "when not stubbing the provider" do + before do + @executable = Puppet.features.posix? ? '/bin/true' : 'C:/Program Files/something.exe' + File.stubs(:exists?).returns false + File.stubs(:exists?).with(@executable).returns true + File.stubs(:exists?).with('/bin/false').returns true + @example_path = Puppet.features.posix? ? %w{/usr/bin /bin} : [ "C:/Program Files/something/bin", "C:/Ruby/bin" ] + File.stubs(:exists?).with(File.join(@example_path[0],"true")).returns true + File.stubs(:exists?).with(File.join(@example_path[0],"false")).returns true + end + + it "should return :executed_command as its event" do + resource = Puppet::Type.type(:exec).new :command => @command + resource.parameter(:returns).event.name.should == :executed_command + end + + describe "when execing" do + it "should use the 'run_and_capture' method to exec" do + exec_tester("true").refresh.should == :executed_command + end + + it "should report a failure" do + proc { exec_tester('false', 1).refresh }. + should raise_error(Puppet::Error, /^false returned 1 instead of/) + end + + it "should not report a failure if the exit status is specified in a returns array" do + proc { exec_tester("false", 1, :returns => [0, 1]).refresh }.should_not raise_error + end + + it "should report a failure if the exit status is not specified in a returns array" do + proc { exec_tester('false', 1, :returns => [0, 100]).refresh }. + should raise_error(Puppet::Error, /^false returned 1 instead of/) + end + + it "should log the output on success" do + output = "output1\noutput2\n" + exec_tester('false', 0, :output => output, :logoutput => true).refresh + output.split("\n").each do |line| + log = @logs.shift + log.level.should == :err + log.message.should == line + end + end + + it "should log the output on failure" do + output = "output1\noutput2\n" + proc { exec_tester('false', 1, :output => output, :logoutput => true).refresh }. + should raise_error(Puppet::Error) + + output.split("\n").each do |line| + log = @logs.shift + log.level.should == :err + log.message.should == line + end + end + end + + describe "when logoutput=>on_failure is set" do + it "should log the output on failure" do + output = "output1\noutput2\n" + proc { exec_tester('false', 1, :output => output, :logoutput => :on_failure).refresh }. + should raise_error(Puppet::Error, /^false returned 1 instead of/) + + output.split("\n").each do |line| + log = @logs.shift + log.level.should == :err + log.message.should == line + end + end + + it "should log the output on failure when returns is specified as an array" do + output = "output1\noutput2\n" + + proc { + exec_tester('false', 1, :output => output, :returns => [0, 100], + :logoutput => :on_failure).refresh + }.should raise_error(Puppet::Error, /^false returned 1 instead of/) + + output.split("\n").each do |line| + log = @logs.shift + log.level.should == :err + log.message.should == line + end + end + + it "shouldn't log the output on success" do + exec_tester('true', 0, :output => "a\nb\nc\n", :logoutput => :on_failure).refresh + @logs.should == [] + end + end + + it "shouldn't log the output on success when non-zero exit status is in a returns array" do + exec_tester("true", 100, :output => "a\n", :logoutput => :on_failure, :returns => [1, 100]).refresh + @logs.should == [] + end + + describe " when multiple tries are set," do + it "should repeat the command attempt 'tries' times on failure and produce an error" do + tries = 5 + resource = exec_tester("false", 1, :tries => tries, :try_sleep => 0) + proc { resource.refresh }.should raise_error(Puppet::Error) + end end end - before do - @executable = Puppet.features.posix? ? '/bin/true' : 'C:/Program Files/something.exe' - @command = Puppet.features.posix? ? '/bin/true whatever' : '"C:/Program Files/something.exe" whatever' - File.stubs(:exists?).returns false - File.stubs(:exists?).with(@executable).returns true - @example_path = Puppet.features.posix? ? %w{/usr/bin /bin} : [ "C:/Program Files/something/bin", "C:/Ruby/bin" ] - File.stubs(:exists?).with(File.join(@example_path[0],"true")).returns true - File.stubs(:exists?).with(File.join(@example_path[0],"false")).returns true + it "should be able to autorequire files mentioned in the command" do + catalog = Puppet::Resource::Catalog.new + tmp = Puppet::Type.type(:file).new(:name => "/bin/foo") + catalog.add_resource tmp + execer = Puppet::Type.type(:exec).new(:name => "/bin/foo") + catalog.add_resource execer + + catalog.relationship_graph.dependencies(execer).should == [tmp] + end + + describe "when handling the path parameter" do + expect = %w{one two three four} + { "an array" => expect, + "a colon separated list" => "one:two:three:four", + "a semi-colon separated list" => "one;two;three;four", + "both array and colon lists" => ["one", "two:three", "four"], + "both array and semi-colon lists" => ["one", "two;three", "four"], + "colon and semi-colon lists" => ["one:two", "three;four"] + }.each do |test, input| + it "should accept #{test}" do + type = Puppet::Type.type(:exec).new(:name => @command, :path => input) + type[:path].should == expect + end + end + end + + describe "when setting user" do + it "should fail if we are not root" do + Puppet.features.stubs(:root?).returns(false) + expect { Puppet::Type.type(:exec).new(:name => @command, :user => 'input') }. + should raise_error Puppet::Error, /Parameter user failed/ + end + + ['one', 2, 'root', 4294967295, 4294967296].each do |value| + it "should accept '#{value}' as user if we are root" do + Puppet.features.stubs(:root?).returns(true) + type = Puppet::Type.type(:exec).new(:name => @command, :user => value) + type[:user].should == value + end + end + end + + describe "when setting group" do + shared_examples_for "exec[:group]" do + ['one', 2, 'wheel', 4294967295, 4294967296].each do |value| + it "should accept '#{value}' without error or judgement" do + type = Puppet::Type.type(:exec).new(:name => @command, :group => value) + type[:group].should == value + end + end + end + + describe "when running as root" do + before :each do Puppet.features.stubs(:root?).returns(true) end + it_behaves_like "exec[:group]" + end + + describe "when not running as root" do + before :each do Puppet.features.stubs(:root?).returns(false) end + it_behaves_like "exec[:group]" + end + end + + describe "when setting cwd" do + it_should_behave_like "all path parameters", :cwd, :array => false do + def instance(path) + Puppet::Type.type(:exec).new(:name => '/bin/true', :cwd => path) + end + end end - it "should return :executed_command as its event" do - resource = Puppet::Type.type(:exec).new :command => @command - resource.parameter(:returns).event.name.should == :executed_command + shared_examples_for "all exec command parameters" do |param| + { "relative" => "example", "absolute" => "/bin/example" }.sort.each do |name, command| + describe "if command is #{name}" do + before :each do + @param = param + end + + def test(command, valid) + if @param == :name then + instance = Puppet::Type.type(:exec).new() + else + instance = Puppet::Type.type(:exec).new(:name => "/bin/true") + end + if valid then + instance.provider.expects(:validatecmd).returns(true) + else + instance.provider.expects(:validatecmd).raises(Puppet::Error, "from a stub") + end + instance[@param] = command + end + + it "should work if the provider calls the command valid" do + expect { test(command, true) }.should_not raise_error + end + + it "should fail if the provider calls the command invalid" do + expect { test(command, false) }. + should raise_error Puppet::Error, /Parameter #{@param} failed: from a stub/ + end + end + end end - describe "when execing" do + shared_examples_for "all exec command parameters that take arrays" do |param| + describe "when given an array of inputs" do + before :each do + @test = Puppet::Type.type(:exec).new(:name => "/bin/true") + end + + it "should accept the array when all commands return valid" do + input = %w{one two three} + @test.provider.expects(:validatecmd).times(input.length).returns(true) + @test[param] = input + @test[param].should == input + end + + it "should reject the array when any commands return invalid" do + input = %w{one two three} + @test.provider.expects(:validatecmd).with(input.first).returns(false) + input[1..-1].each do |cmd| + @test.provider.expects(:validatecmd).with(cmd).returns(true) + end + @test[param] = input + @test[param].should == input + end - it "should use the 'run_and_capture' method to exec" do - command = "true" - create_resource(command, "", 0) + it "should reject the array when all commands return invalid" do + input = %w{one two three} + @test.provider.expects(:validatecmd).times(input.length).returns(false) + @test[param] = input + @test[param].should == input + end + end + end + + describe "when setting refresh" do + it_should_behave_like "all exec command parameters", :refresh + end - @execer.refresh.should == :executed_command + describe "for simple parameters" do + before :each do + @exec = Puppet::Type.type(:exec).new(:name => '/bin/true') end - it "should report a failure" do - command = "false" - create_resource(command, "", 1) + describe "when setting environment" do + { "single values" => "foo=bar", + "multiple values" => ["foo=bar", "baz=quux"], + }.each do |name, data| + it "should accept #{name}" do + @exec[:environment] = data + @exec[:environment].should == data + end + end - proc { @execer.refresh }.should raise_error(Puppet::Error) + { "single values" => "foo", + "only values" => ["foo", "bar"], + "any values" => ["foo=bar", "baz"] + }.each do |name, data| + it "should reject #{name} without assignment" do + expect { @exec[:environment] = data }. + should raise_error Puppet::Error, /Invalid environment setting/ + end + end end - it "should not report a failure if the exit status is specified in a returns array" do - command = "false" - create_resource(command, "", 1, [0,1]) - proc { @execer.refresh }.should_not raise_error(Puppet::Error) + describe "when setting timeout" do + [0, 0.1, 1, 10, 4294967295].each do |valid| + it "should accept '#{valid}' as valid" do + @exec[:timeout] = valid + @exec[:timeout].should == valid + end + + it "should accept '#{valid}' in an array as valid" do + @exec[:timeout] = [valid] + @exec[:timeout].should == valid + end + end + + ['1/2', '', 'foo', '5foo'].each do |invalid| + it "should reject '#{invalid}' as invalid" do + expect { @exec[:timeout] = invalid }. + should raise_error Puppet::Error, /The timeout must be a number/ + end + + it "should reject '#{invalid}' in an array as invalid" do + expect { @exec[:timeout] = [invalid] }. + should raise_error Puppet::Error, /The timeout must be a number/ + end + end + + it "should fail if timeout is exceeded" do + File.stubs(:exists?).with('/bin/sleep').returns(true) + File.stubs(:exists?).with('sleep').returns(false) + sleep_exec = Puppet::Type.type(:exec).new(:name => 'sleep 1', :path => ['/bin'], :timeout => '0.2') + lambda { sleep_exec.refresh }.should raise_error Puppet::Error, "Command exceeded timeout" + end + + it "should convert timeout to a float" do + resource = Puppet::Type.type(:exec).new :command => "/bin/false", :timeout => "12" + resource[:timeout].should be_a(Float) + resource[:timeout].should == 12.0 + end + + it "should munge negative timeouts to 0.0" do + resource = Puppet::Type.type(:exec).new :command => "/bin/false", :timeout => "-12.0" + resource.parameter(:timeout).value.should be_a(Float) + resource.parameter(:timeout).value.should == 0.0 + end end - it "should report a failure if the exit status is not specified in a returns array" do - command = "false" - create_resource(command, "", 1, [0,100]) - proc { @execer.refresh }.should raise_error(Puppet::Error) + describe "when setting tries" do + [1, 10, 4294967295].each do |valid| + it "should accept '#{valid}' as valid" do + @exec[:tries] = valid + @exec[:tries].should == valid + end + + if "REVISIT: too much test log spam" == "a good thing" then + it "should accept '#{valid}' in an array as valid" do + pending "inconsistent, but this is not supporting arrays, unlike timeout" + @exec[:tries] = [valid] + @exec[:tries].should == valid + end + end + end + + [-3.5, -1, 0, 0.2, '1/2', '1_000_000', '+12', '', 'foo'].each do |invalid| + it "should reject '#{invalid}' as invalid" do + expect { @exec[:tries] = invalid }. + should raise_error Puppet::Error, /Tries must be an integer/ + end + + if "REVISIT: too much test log spam" == "a good thing" then + it "should reject '#{invalid}' in an array as invalid" do + pending "inconsistent, but this is not supporting arrays, unlike timeout" + expect { @exec[:tries] = [invalid] }. + should raise_error Puppet::Error, /Tries must be an integer/ + end + end + end end - it "should log the output on success" do - #Puppet::Util::Log.newdestination :console - command = "false" - output = "output1\noutput2\n" - create_logging_resource(command, output, 0, true, :err) - expect_output(output, :err) - @execer.refresh + describe "when setting try_sleep" do + [0, 0.2, 1, 10, 4294967295].each do |valid| + it "should accept '#{valid}' as valid" do + @exec[:try_sleep] = valid + @exec[:try_sleep].should == valid + end + + if "REVISIT: too much test log spam" == "a good thing" then + it "should accept '#{valid}' in an array as valid" do + pending "inconsistent, but this is not supporting arrays, unlike timeout" + @exec[:try_sleep] = [valid] + @exec[:try_sleep].should == valid + end + end + end + + { -3.5 => "cannot be a negative number", + -1 => "cannot be a negative number", + '1/2' => 'must be a number', + '1_000_000' => 'must be a number', + '+12' => 'must be a number', + '' => 'must be a number', + 'foo' => 'must be a number', + }.each do |invalid, error| + it "should reject '#{invalid}' as invalid" do + expect { @exec[:try_sleep] = invalid }. + should raise_error Puppet::Error, /try_sleep #{error}/ + end + + if "REVISIT: too much test log spam" == "a good thing" then + it "should reject '#{invalid}' in an array as invalid" do + pending "inconsistent, but this is not supporting arrays, unlike timeout" + expect { @exec[:try_sleep] = [invalid] }. + should raise_error Puppet::Error, /try_sleep #{error}/ + end + end + end end - it "should log the output on failure" do - #Puppet::Util::Log.newdestination :console - command = "false" - output = "output1\noutput2\n" - create_logging_resource(command, output, 1, true, :err) - expect_output(output, :err) + describe "when setting refreshonly" do + [:true, :false].each do |value| + it "should accept '#{value}'" do + @exec[:refreshonly] = value + @exec[:refreshonly].should == value + end + end - proc { @execer.refresh }.should raise_error(Puppet::Error) + [1, 0, "1", "0", "yes", "y", "no", "n"].each do |value| + it "should reject '#{value}'" do + expect { @exec[:refreshonly] = value }. + should raise_error(Puppet::Error, + /Invalid value #{value.inspect}\. Valid values are true, false/ + ) + end + end end + describe "when setting creates" do + it_should_behave_like "all path parameters", :creates, :array => true do + def instance(path) + Puppet::Type.type(:exec).new(:name => '/bin/true', :creates => path) + end + end + end end - describe "when logoutput=>on_failure is set" do + describe "when setting unless" do + it_should_behave_like "all exec command parameters", :unless + it_should_behave_like "all exec command parameters that take arrays", :unless + end - it "should log the output on failure" do - #Puppet::Util::Log.newdestination :console - command = "false" - output = "output1\noutput2\n" - create_logging_resource(command, output, 1, :on_failure, :err) - expect_output(output, :err) + describe "when setting onlyif" do + it_should_behave_like "all exec command parameters", :onlyif + it_should_behave_like "all exec command parameters that take arrays", :onlyif + end + + describe "#check" do + before :each do + @test = Puppet::Type.type(:exec).new(:name => "/bin/true") + end - proc { @execer.refresh }.should raise_error(Puppet::Error) + describe ":refreshonly" do + { :true => false, :false => true }.each do |input, result| + it "should return '#{result}' when given '#{input}'" do + @test[:refreshonly] = input + @test.check_all_attributes.should == result + end + end end - it "should log the output on failure when returns is specified as an array" do - #Puppet::Util::Log.newdestination :console - command = "false" - output = "output1\noutput2\n" - create_logging_resource(command, output, 1, :on_failure, :err, [0, 100]) - expect_output(output, :err) + describe ":creates" do + before :all do + @exist = "/" + @unexist = "/this/path/should/never/exist" + while FileTest.exist?(@unexist) do @unexist += "/foo" end + end - proc { @execer.refresh }.should raise_error(Puppet::Error) + context "with a single item" do + it "should run when the item does not exist" do + @test[:creates] = @unexist + @test.check_all_attributes.should == true + end + + it "should not run when the item exists" do + @test[:creates] = @exist + @test.check_all_attributes.should == false + end + end + + context "with an array with one item" do + it "should run when the item does not exist" do + @test[:creates] = [@unexist] + @test.check_all_attributes.should == true + end + + it "should not run when the item exists" do + @test[:creates] = [@exist] + @test.check_all_attributes.should == false + end + end + + context "with an array with multiple items" do + it "should run when all items do not exist" do + @test[:creates] = [@unexist] * 3 + @test.check_all_attributes.should == true + end + + it "should not run when one item exists" do + @test[:creates] = [@unexist, @exist, @unexist] + @test.check_all_attributes.should == false + end + + it "should not run when all items exist" do + @test[:creates] = [@exist] * 3 + end + end end - it "shouldn't log the output on success" do - #Puppet::Util::Log.newdestination :console - command = "true" - output = "output1\noutput2\n" - create_logging_resource(command, output, 0, :on_failure, :err) - @execer.property(:returns).expects(:err).never - @execer.refresh + { :onlyif => { :pass => false, :fail => true }, + :unless => { :pass => true, :fail => false }, + }.each do |param, sense| + describe ":#{param}" do + before :each do + @pass = "/magic/pass" + @fail = "/magic/fail" + + @pass_status = stub('status', :exitstatus => sense[:pass] ? 0 : 1) + @fail_status = stub('status', :exitstatus => sense[:fail] ? 0 : 1) + + @test.provider.stubs(:checkexe).returns(true) + [true, false].each do |check| + @test.provider.stubs(:run).with(@pass, check). + returns(['test output', @pass_status]) + @test.provider.stubs(:run).with(@fail, check). + returns(['test output', @fail_status]) + end + end + + context "with a single item" do + it "should run if the command exits non-zero" do + @test[param] = @fail + @test.check_all_attributes.should == true + end + + it "should not run if the command exits zero" do + @test[param] = @pass + @test.check_all_attributes.should == false + end + end + + context "with an array with a single item" do + it "should run if the command exits non-zero" do + @test[param] = [@fail] + @test.check_all_attributes.should == true + end + + it "should not run if the command exits zero" do + @test[param] = [@pass] + @test.check_all_attributes.should == false + end + end + + context "with an array with multiple items" do + it "should run if all the commands exits non-zero" do + @test[param] = [@fail] * 3 + @test.check_all_attributes.should == true + end + + it "should not run if one command exits zero" do + @test[param] = [@pass, @fail, @pass] + @test.check_all_attributes.should == false + end + + it "should not run if all command exits zero" do + @test[param] = [@pass] * 3 + @test.check_all_attributes.should == false + end + end + end end end - it "shouldn't log the output on success when non-zero exit status is in a returns array" do - #Puppet::Util::Log.newdestination :console - command = "true" - output = "output1\noutput2\n" - create_logging_resource(command, output, 100, :on_failure, :err, [1,100]) - @execer.property(:returns).expects(:err).never - @execer.refresh + describe "#retrieve" do + before :each do + @exec_resource = Puppet::Type.type(:exec).new(:name => "/bogus/cmd") + end + + it "should return :notrun when check_all_attributes returns true" do + @exec_resource.stubs(:check_all_attributes).returns true + @exec_resource.retrieve[:returns].should == :notrun + end + + it "should return default exit code 0 when check_all_attributes returns false" do + @exec_resource.stubs(:check_all_attributes).returns false + @exec_resource.retrieve[:returns].should == ['0'] + end + + it "should return the specified exit code when check_all_attributes returns false" do + @exec_resource.stubs(:check_all_attributes).returns false + @exec_resource[:returns] = 42 + @exec_resource.retrieve[:returns].should == ["42"] + end end - describe " when multiple tries are set," do + describe "#output" do + before :each do + @exec_resource = Puppet::Type.type(:exec).new(:name => "/bogus/cmd") + end + + it "should return the provider's run output" do + provider = stub 'provider' + status = stubs "process_status" + status.stubs(:exitstatus).returns("0") + provider.expects(:run).returns(["silly output", status]) + @exec_resource.stubs(:provider).returns(provider) - it "should repeat the command attempt 'tries' times on failure and produce an error" do - Puppet.features.stubs(:root?).returns(true) - command = "false" - user = "user" - group = "group" - tries = 5 - retry_exec = Puppet::Type.type(:exec).new(:name => command, :path => %w{/usr/bin /bin}, :user => user, :group => group, :returns => 0, :tries => tries, :try_sleep => 0) - status = stub "process" - status.stubs(:exitstatus).returns(1) - Puppet::Util::SUIDManager.expects(:run_and_capture).with([command], user, group).times(tries).returns(["", status]) - proc { retry_exec.refresh }.should raise_error(Puppet::Error) + @exec_resource.refresh + @exec_resource.output.should == 'silly output' end end - it "should be able to autorequire files mentioned in the command" do - catalog = Puppet::Resource::Catalog.new - catalog.add_resource Puppet::Type.type(:file).new(:name => @executable) - @execer = Puppet::Type.type(:exec).new(:name => @command) - catalog.add_resource @execer + describe "#refresh" do + before :each do + @exec_resource = Puppet::Type.type(:exec).new(:name => "/bogus/cmd") + end + + it "should call provider run with the refresh parameter if it is set" do + provider = stub 'provider' + @exec_resource.stubs(:provider).returns(provider) + @exec_resource.stubs(:[]).with(:refresh).returns('/myother/bogus/cmd') + provider.expects(:run).with('/myother/bogus/cmd') - rels = @execer.autorequire - rels[0].should be_instance_of(Puppet::Relationship) - rels[0].target.should equal(@execer) + @exec_resource.refresh + end + + it "should call provider run with the specified command if the refresh parameter is not set" do + provider = stub 'provider' + status = stubs "process_status" + status.stubs(:exitstatus).returns("0") + provider.expects(:run).with('/bogus/cmd').returns(["silly output", status]) + @exec_resource.stubs(:provider).returns(provider) + + @exec_resource.refresh + end + + it "should not run the provider if check_all_attributes is false" do + @exec_resource.stubs(:check_all_attributes).returns false + provider = stub 'provider' + provider.expects(:run).never + @exec_resource.stubs(:provider).returns(provider) + + @exec_resource.refresh + end end end diff --git a/spec/unit/type/file/checksum_spec.rb b/spec/unit/type/file/checksum_spec.rb index 8b9138b78..16e8e99e1 100644 --- a/spec/unit/type/file/checksum_spec.rb +++ b/spec/unit/type/file/checksum_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') checksum = Puppet::Type.type(:file).attrclass(:checksum) describe checksum do diff --git a/spec/unit/type/file/content_spec.rb b/spec/unit/type/file/content_spec.rb index bd2b2adaf..7abc7c433 100755 --- a/spec/unit/type/file/content_spec.rb +++ b/spec/unit/type/file/content_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') content = Puppet::Type.type(:file).attrclass(:content) describe content do diff --git a/spec/unit/type/file/ctime.rb b/spec/unit/type/file/ctime.rb index 6145cbfdc..9fb892aed 100644 --- a/spec/unit/type/file/ctime.rb +++ b/spec/unit/type/file/ctime.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe Puppet::Type.type(:file).attrclass(:ctime) do require 'puppet_spec/files' diff --git a/spec/unit/type/file/ensure_spec.rb b/spec/unit/type/file/ensure_spec.rb index dbb3a1053..c5351309e 100755 --- a/spec/unit/type/file/ensure_spec.rb +++ b/spec/unit/type/file/ensure_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') property = Puppet::Type.type(:file).attrclass(:ensure) diff --git a/spec/unit/type/file/group_spec.rb b/spec/unit/type/file/group_spec.rb index 956cd57e7..37a6872bd 100755 --- a/spec/unit/type/file/group_spec.rb +++ b/spec/unit/type/file/group_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') property = Puppet::Type.type(:file).attrclass(:group) diff --git a/spec/unit/type/file/mtime.rb b/spec/unit/type/file/mtime.rb index 043156ceb..fa61bc343 100644 --- a/spec/unit/type/file/mtime.rb +++ b/spec/unit/type/file/mtime.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe Puppet::Type.type(:file).attrclass(:mtime) do require 'puppet_spec/files' diff --git a/spec/unit/type/file/owner_spec.rb b/spec/unit/type/file/owner_spec.rb index bcb8e07d6..375faa237 100755 --- a/spec/unit/type/file/owner_spec.rb +++ b/spec/unit/type/file/owner_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') property = Puppet::Type.type(:file).attrclass(:owner) diff --git a/spec/unit/type/file/selinux_spec.rb b/spec/unit/type/file/selinux_spec.rb index a2444acd9..45e8b3b14 100644 --- a/spec/unit/type/file/selinux_spec.rb +++ b/spec/unit/type/file/selinux_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') [:seluser, :selrole, :seltype, :selrange].each do |param| diff --git a/spec/unit/type/file/source_spec.rb b/spec/unit/type/file/source_spec.rb index c07963a50..bb83e7531 100755 --- a/spec/unit/type/file/source_spec.rb +++ b/spec/unit/type/file/source_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') source = Puppet::Type.type(:file).attrclass(:source) describe Puppet::Type.type(:file).attrclass(:source) do diff --git a/spec/unit/type/file/type.rb b/spec/unit/type/file/type.rb index e46f0e0b0..0d38b64cd 100644 --- a/spec/unit/type/file/type.rb +++ b/spec/unit/type/file/type.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe Puppet::Type.type(:file).attrclass(:type) do require 'puppet_spec/files' diff --git a/spec/unit/type/maillist_spec.rb b/spec/unit/type/maillist_spec.rb index 04acea7dd..7e96b4760 100755 --- a/spec/unit/type/maillist_spec.rb +++ b/spec/unit/type/maillist_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') maillist = Puppet::Type.type(:maillist) diff --git a/spec/unit/type/selboolean_spec.rb b/spec/unit/type/selboolean_spec.rb index 3b31f4306..41b254f25 100755 --- a/spec/unit/type/selboolean_spec.rb +++ b/spec/unit/type/selboolean_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe Puppet::Type.type(:selboolean), "when validating attributes" do [:name, :persistent].each do |param| diff --git a/spec/unit/type/selmodule_spec.rb b/spec/unit/type/selmodule_spec.rb index 3d126256c..37453d8ed 100755 --- a/spec/unit/type/selmodule_spec.rb +++ b/spec/unit/type/selmodule_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe Puppet::Type.type(:selmodule), "when validating attributes" do [:name, :selmoduledir, :selmodulepath].each do |param| diff --git a/spec/unit/type/zfs_spec.rb b/spec/unit/type/zfs_spec.rb index 6b0b17f3a..88193fca3 100755 --- a/spec/unit/type/zfs_spec.rb +++ b/spec/unit/type/zfs_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') zfs = Puppet::Type.type(:zfs) diff --git a/spec/unit/type/zone_spec.rb b/spec/unit/type/zone_spec.rb index a3e748bde..e479fb1e9 100755 --- a/spec/unit/type/zone_spec.rb +++ b/spec/unit/type/zone_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') zone = Puppet::Type.type(:zone) diff --git a/spec/unit/type/zpool_spec.rb b/spec/unit/type/zpool_spec.rb index be8cb12ba..96e7d548c 100755 --- a/spec/unit/type/zpool_spec.rb +++ b/spec/unit/type/zpool_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') zpool = Puppet::Type.type(:zpool) diff --git a/spec/unit/util/autoload_spec.rb b/spec/unit/util/autoload_spec.rb index eb0b705f4..808885dd7 100755 --- a/spec/unit/util/autoload_spec.rb +++ b/spec/unit/util/autoload_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/autoload' diff --git a/spec/unit/util/command_line_spec.rb b/spec/unit/util/command_line_spec.rb index 98ddb92f6..a7d261dcf 100755 --- a/spec/unit/util/command_line_spec.rb +++ b/spec/unit/util/command_line_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/command_line' diff --git a/spec/unit/util/errors_spec.rb b/spec/unit/util/errors_spec.rb index 2500403d0..da1b8b0bd 100755 --- a/spec/unit/util/errors_spec.rb +++ b/spec/unit/util/errors_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/errors' diff --git a/spec/unit/util/feature_spec.rb b/spec/unit/util/feature_spec.rb index 8cedade7d..365428752 100755 --- a/spec/unit/util/feature_spec.rb +++ b/spec/unit/util/feature_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/feature' diff --git a/spec/unit/util/file_locking_spec.rb b/spec/unit/util/file_locking_spec.rb index 8fafb1d52..e9dbe9d7b 100755 --- a/spec/unit/util/file_locking_spec.rb +++ b/spec/unit/util/file_locking_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/file_locking' diff --git a/spec/unit/util/filetype_spec.rb b/spec/unit/util/filetype_spec.rb index 68ef9d6eb..012631b91 100644 --- a/spec/unit/util/filetype_spec.rb +++ b/spec/unit/util/filetype_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/filetype' diff --git a/spec/unit/util/inline_docs_spec.rb b/spec/unit/util/inline_docs_spec.rb index edfa405a1..75afb57cb 100755 --- a/spec/unit/util/inline_docs_spec.rb +++ b/spec/unit/util/inline_docs_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/inline_docs' diff --git a/spec/unit/util/loadedfile_spec.rb b/spec/unit/util/loadedfile_spec.rb index 23d2766d7..92daeb953 100755 --- a/spec/unit/util/loadedfile_spec.rb +++ b/spec/unit/util/loadedfile_spec.rb @@ -6,6 +6,7 @@ require 'tempfile' require 'puppet/util/loadedfile' describe Puppet::Util::LoadedFile do + include PuppetSpec::Files before(:each) do @f = Tempfile.new('loadedfile_test') @f.puts "yayness" @@ -18,6 +19,12 @@ describe Puppet::Util::LoadedFile do @fake_now = Time.now + (2 * Puppet[:filetimeout]) end + it "should accept files that don't exist" do + nofile = tmpfile('testfile') + File.exists?(nofile).should == false + lambda{ Puppet::Util::LoadedFile.new(nofile) }.should_not raise_error + end + it "should recognize when the file has not changed" do # Use fake "now" so that we can be sure changed? actually checks, without sleeping # for Puppet[:filetimeout] seconds. diff --git a/spec/unit/util/log_spec.rb b/spec/unit/util/log_spec.rb index 29df0787a..3c8577493 100755 --- a/spec/unit/util/log_spec.rb +++ b/spec/unit/util/log_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/log' diff --git a/spec/unit/util/logging_spec.rb b/spec/unit/util/logging_spec.rb index 411cd17a9..97ffc0b25 100755 --- a/spec/unit/util/logging_spec.rb +++ b/spec/unit/util/logging_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/logging' diff --git a/spec/unit/util/metric_spec.rb b/spec/unit/util/metric_spec.rb index 600b88f85..425812297 100755 --- a/spec/unit/util/metric_spec.rb +++ b/spec/unit/util/metric_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/metric' diff --git a/spec/unit/util/monkey_patches_spec.rb b/spec/unit/util/monkey_patches_spec.rb index 049ed1044..8bfcd900e 100644 --- a/spec/unit/util/monkey_patches_spec.rb +++ b/spec/unit/util/monkey_patches_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/monkey_patches' diff --git a/spec/unit/util/posix_spec.rb b/spec/unit/util/posix_spec.rb index dbc90d9d0..6d7351220 100755 --- a/spec/unit/util/posix_spec.rb +++ b/spec/unit/util/posix_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/posix' diff --git a/spec/unit/util/pson_spec.rb b/spec/unit/util/pson_spec.rb index 474ddafa4..08758ee38 100755 --- a/spec/unit/util/pson_spec.rb +++ b/spec/unit/util/pson_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/pson' diff --git a/spec/unit/util/rdoc/parser_spec.rb b/spec/unit/util/rdoc/parser_spec.rb index 8545def54..f118dc99b 100755 --- a/spec/unit/util/rdoc/parser_spec.rb +++ b/spec/unit/util/rdoc/parser_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') require 'puppet/resource/type_collection' require 'puppet/util/rdoc/parser' @@ -20,8 +20,9 @@ describe RDoc::Parser do @parser.stubs(:scan_top_level) parser = stub 'parser' Puppet::Parser::Parser.stubs(:new).returns(parser) - parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')) + parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once parser.expects(:file=).with("module/manifests/init.pp") + parser.expects(:file=).with("/dev/null/manifests/site.pp") @parser.scan end @@ -29,7 +30,7 @@ describe RDoc::Parser do it "should scan the ast for Puppet files" do parser = stub_everything 'parser' Puppet::Parser::Parser.stubs(:new).returns(parser) - parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')) + parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once @parser.expects(:scan_top_level) @@ -39,7 +40,7 @@ describe RDoc::Parser do it "should return a PuppetTopLevel to RDoc" do parser = stub_everything 'parser' Puppet::Parser::Parser.stubs(:new).returns(parser) - parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')) + parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once @parser.expects(:scan_top_level) diff --git a/spec/unit/util/rdoc_spec.rb b/spec/unit/util/rdoc_spec.rb index 93c4f9bfa..3b5248528 100755 --- a/spec/unit/util/rdoc_spec.rb +++ b/spec/unit/util/rdoc_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/rdoc' require 'rdoc/rdoc' diff --git a/spec/unit/util/user_attr_spec.rb b/spec/unit/util/user_attr_spec.rb index 61312ef19..5acdaee58 100644 --- a/spec/unit/util/user_attr_spec.rb +++ b/spec/unit/util/user_attr_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/user_attr' diff --git a/spec/unit/util/zaml_spec.rb b/spec/unit/util/zaml_spec.rb index 358c6aa11..804aa8e58 100755 --- a/spec/unit/util/zaml_spec.rb +++ b/spec/unit/util/zaml_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'puppet/util/monkey_patches' diff --git a/tasks/rake/gem.rake b/tasks/rake/gem.rake index d654886ae..efea73882 100644 --- a/tasks/rake/gem.rake +++ b/tasks/rake/gem.rake @@ -1,4 +1,4 @@ -require 'ftools' +require 'fileutils' GEM_FILES = FileList[ '[A-Z]*', @@ -44,7 +44,7 @@ end desc "Prepare binaries for gem creation" task :prepare_gem do SBIN.each do |f| - File.copy(f,"bin") + FileUtils.copy(f,"bin") end end @@ -52,8 +52,9 @@ desc "Create the gem" task :create_gem => :prepare_gem do Dir.mkdir("pkg") rescue nil Gem::Builder.new(spec).build - File.move("puppet-#{Puppet::PUPPETVERSION}.gem", "pkg") + FileUtils.move("puppet-#{Puppet::PUPPETVERSION}.gem", "pkg") SBIN.each do |f| - File.unlink("bin/" + f.gsub(/sbin\//, '')) + fn = f.gsub(/sbin\/(.*)/, '\1') + FileUtils.rm_r "bin/" + fn end end diff --git a/tasks/rake/git_workflow.rake b/tasks/rake/git_workflow.rake index 1ba57c1aa..56a414568 100644 --- a/tasks/rake/git_workflow.rake +++ b/tasks/rake/git_workflow.rake @@ -5,82 +5,82 @@ def find_start(start) # This is a case statement, as we might want to map certain # git tags to starting points that are not currently in git. - case start - when nil?: - when @next_release: return "master" - else return start - end + case start + when nil?; + when @next_release; return "master" + else return start + end end desc "Set up git for working with Puppet" task :git_setup do - # This should be changed as new versions get released - @next_release = '0.26.x' - @remote = {} - default_remote = {} - default_remote[:url] = 'git://github.com/reductivelabs/puppet' - default_remote[:name] = 'origin' - @remote[:name] = %x{git config puppet.defaultremote}.chomp - @remote[:name] = @remote[:name].empty? ? default_remote[:name] : @remote[:name] - @remote[:url] = default_remote[:url] if @remote[:name] == default_remote[:name] - default_fetch = '+refs/heads/*:refs/remotes/puppet/*' - @remote[:fetch] = %x{git config puppet.#{@remote[:name]}.fetch}.chomp - @remote[:fetch] = @remote[:fetch].empty? ? default_fetch : @remote[:fetch] + # This should be changed as new versions get released + @next_release = '0.26.x' + @remote = {} + default_remote = {} + default_remote[:url] = 'git://github.com/reductivelabs/puppet' + default_remote[:name] = 'origin' + @remote[:name] = %x{git config puppet.defaultremote}.chomp + @remote[:name] = @remote[:name].empty? ? default_remote[:name] : @remote[:name] + @remote[:url] = default_remote[:url] if @remote[:name] == default_remote[:name] + default_fetch = '+refs/heads/*:refs/remotes/puppet/*' + @remote[:fetch] = %x{git config puppet.#{@remote[:name]}.fetch}.chomp + @remote[:fetch] = @remote[:fetch].empty? ? default_fetch : @remote[:fetch] end desc "Start work on a feature" task :start_feature, [:feature,:remote,:branch] => :git_setup do |t, args| - args.with_defaults(:remote => @remote[:name]) - args.with_defaults(:branch => @next_release) - start_at = find_start(args.branch) - branch = "feature/#{start_at}/#{args.feature}" - sh "git checkout -b #{branch} #{start_at}" do |ok, res| - if ! ok - raise <<EOS + args.with_defaults(:remote => @remote[:name]) + args.with_defaults(:branch => @next_release) + start_at = find_start(args.branch) + branch = "feature/#{start_at}/#{args.feature}" + sh "git checkout -b #{branch} #{start_at}" do |ok, res| + if ! ok + raise <<EOS Was not able to create branch for #{args.feature} on branch #{args.branch}, starting at #{start_at}: error code was: #{res.exitstatus} EOS - end - end - sh "git config branch.#{branch}.remote #{args.remote}" do |ok, res| - raise "Could not set remote: #{$?}" unless ok - end - - sh "git config branch.#{branch}.merge refs/heads/#{branch}" do |ok, res| - raise "Could not configure merge: #{$?}" unless ok - end + end + end + sh "git config branch.#{branch}.remote #{args.remote}" do |ok, res| + raise "Could not set remote: #{$?}" unless ok + end + + sh "git config branch.#{branch}.merge refs/heads/#{branch}" do |ok, res| + raise "Could not configure merge: #{$?}" unless ok + end end desc "Do git prep to start work on a Redmine ticket" task :start_ticket, [:ticket, :remote, :branch] => :git_setup do |t, args| - args.with_defaults(:remote => @remote[:name]) - args.with_defaults(:branch => @next_release) - start_at = find_start(args.branch) - branch = "tickets/#{start_at}/#{args.ticket}" - sh "git checkout -b #{branch} #{start_at}" do |ok, res| - unless ok - raise <<EOS + args.with_defaults(:remote => @remote[:name]) + args.with_defaults(:branch => @next_release) + start_at = find_start(args.branch) + branch = "tickets/#{start_at}/#{args.ticket}" + sh "git checkout -b #{branch} #{start_at}" do |ok, res| + unless ok + raise <<EOS Was not able to create branch for ticket #{args.ticket} on branch #{args.branch}, starting at #{start_at}: error code was: #{$?} Git command used was: #{command} EOS - end - end + end + end sh "git config branch.#{branch}.remote #{args.remote}" do |ok, res| - raise "Could not set remote: #{$?}" unless ok - end + raise "Could not set remote: #{$?}" unless ok + end sh "git config branch.#{branch}.merge refs/heads/#{branch}" do |ok, res| - raise "Could not configure merge: #{$?}" unless ok - end + raise "Could not configure merge: #{$?}" unless ok + end end # This isn't very useful by itself, but we might enhance it later, or use it # in a dependency for a more complex task. desc "Push out changes" task :push_changes, [:remote] do |t, arg| - branch = %x{git branch | grep "^" | awk '{print $2}'} - sh "git push #{arg.remote} #{branch}" do |ok, res| - raise "Unable to push to #{arg.remote}" unless ok - end + branch = %x{git branch | grep "^" | awk '{print $2}'} + sh "git push #{arg.remote} #{branch}" do |ok, res| + raise "Unable to push to #{arg.remote}" unless ok + end end desc "Send patch information to the puppet-dev list" @@ -119,12 +119,14 @@ task :mail_patches do # If we've got more than one patch, add --compose if files.length > 1 compose = "--compose" + subject = "--subject \"#{type} #{name} against #{parent}\"" else compose = "" + subject = "" end # Now send the mail. - sh "git send-email #{compose} --no-signed-off-by-cc --suppress-from --to puppet-dev@googlegroups.com 00*.patch" + sh "git send-email #{compose} #{subject} --no-signed-off-by-cc --suppress-from --to puppet-dev@googlegroups.com 00*.patch" # Finally, clean up the patches sh "rm 00*.patch" diff --git a/test/language/functions.rb b/test/language/functions.rb index 3c27f2b2b..e882b68f3 100755 --- a/test/language/functions.rb +++ b/test/language/functions.rb @@ -447,7 +447,7 @@ class TestLangFunctions < Test::Unit::TestCase include = Puppet::Parser::Functions.function(:include) - assert_raise(Puppet::ParseError, "did not throw error on missing class") do + assert_raise(Puppet::Error, "did not throw error on missing class") do scope.function_include("nosuchclass") end diff --git a/test/ral/type/exec.rb b/test/ral/type/exec.rb index 829b1a068..0831d466d 100755 --- a/test/ral/type/exec.rb +++ b/test/ral/type/exec.rb @@ -34,10 +34,8 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { command = Puppet::Type.type(:exec).new( - :command => "echo", - - :path => "/usr/bin:/bin:/usr/sbin:/sbin" + :path => "/usr/bin:/bin:/usr/sbin:/sbin" ) } assert_nothing_raised { @@ -48,10 +46,8 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { command = Puppet::Type.type(:exec).new( - :command => "/bin/echo", - - :path => "/usr/bin:/bin:/usr/sbin:/sbin" + :path => "/usr/bin:/bin:/usr/sbin:/sbin" ) } end @@ -60,30 +56,24 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { command = Puppet::Type.type(:exec).new( - :command => "mkdir /this/directory/does/not/exist", - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - + :path => "/usr/bin:/bin:/usr/sbin:/sbin", :returns => 1 ) } assert_nothing_raised { command = Puppet::Type.type(:exec).new( - :command => "touch /etc", - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - + :path => "/usr/bin:/bin:/usr/sbin:/sbin", :returns => 1 ) } assert_nothing_raised { command = Puppet::Type.type(:exec).new( - :command => "thiscommanddoesnotexist", - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - + :path => "/usr/bin:/bin:/usr/sbin:/sbin", :returns => 127 ) } @@ -98,11 +88,9 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { command = Puppet::Type.type(:exec).new( - :command => "pwd", - :cwd => dir, - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - + :cwd => dir, + :path => "/usr/bin:/bin:/usr/sbin:/sbin", :returns => 0 ) } @@ -117,11 +105,9 @@ class TestExec < Test::Unit::TestCase @@tmpfiles.push tmpfile trans = nil - file = Puppet::Type.type(:file).new( - - :path => tmpfile, - - :content => "yay" + file = Puppet::Type.type(:file).new( + :path => tmpfile, + :content => "yay" ) # Get the file in sync assert_apply(file) @@ -129,13 +115,10 @@ class TestExec < Test::Unit::TestCase # Now make an exec maker = tempfile assert_nothing_raised { - cmd = Puppet::Type.type(:exec).new( - - :command => "touch #{maker}", - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - :subscribe => file, - + :command => "touch #{maker}", + :path => "/usr/bin:/bin:/usr/sbin:/sbin", + :subscribe => file, :refreshonly => true ) } @@ -143,7 +126,7 @@ class TestExec < Test::Unit::TestCase assert(cmd, "did not make exec") assert_nothing_raised do - assert(! cmd.check, "Check passed when refreshonly is set") + assert(! cmd.check_all_attributes, "Check passed when refreshonly is set") end assert_events([], file, cmd) @@ -158,25 +141,22 @@ class TestExec < Test::Unit::TestCase def test_refreshonly cmd = true assert_nothing_raised { - cmd = Puppet::Type.type(:exec).new( - - :command => "pwd", - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - + :command => "pwd", + :path => "/usr/bin:/bin:/usr/sbin:/sbin", :refreshonly => true ) } # Checks should always fail when refreshonly is enabled - assert(!cmd.check, "Check passed with refreshonly true") + assert(!cmd.check_all_attributes, "Check passed with refreshonly true") # Now make sure it passes if we pass in "true" - assert(cmd.check(true), "Check failed with refreshonly true while refreshing") + assert(cmd.check_all_attributes(true), "Check failed with refreshonly true while refreshing") # Now set it to false cmd[:refreshonly] = false - assert(cmd.check, "Check failed with refreshonly false") + assert(cmd.check_all_attributes, "Check failed with refreshonly false") end def test_creates @@ -186,10 +166,8 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { exec = Puppet::Type.type(:exec).new( - :command => "touch #{file}", - :path => "/usr/bin:/bin:/usr/sbin:/sbin", - + :path => "/usr/bin:/bin:/usr/sbin:/sbin", :creates => file ) } @@ -206,21 +184,15 @@ class TestExec < Test::Unit::TestCase sh = %x{which sh} File.open(exe, "w") { |f| f.puts "#!#{sh}\necho yup" } - - file = Puppet::Type.type(:file).new( - - :path => oexe, - :source => exe, - - :mode => 0755 + file = Puppet::Type.type(:file).new( + :path => oexe, + :source => exe, + :mode => 0755 ) - - exec = Puppet::Type.type(:exec).new( - - :command => oexe, - - :require => Puppet::Resource.new(:file, oexe) + exec = Puppet::Type.type(:exec).new( + :command => oexe, + :require => Puppet::Resource.new(:file, oexe) ) comp = mk_catalog("Testing", file, exec) @@ -236,47 +208,36 @@ class TestExec < Test::Unit::TestCase File.open(exe, "w") { |f| f.puts "#!#{sh}\necho yup" } - file = Puppet::Type.type(:file).new( - - :path => oexe, - :source => exe, - - :mode => 755 + file = Puppet::Type.type(:file).new( + :path => oexe, + :source => exe, + :mode => 755 ) basedir = File.dirname(oexe) - baseobj = Puppet::Type.type(:file).new( - - :path => basedir, - :source => exe, - - :mode => 755 + baseobj = Puppet::Type.type(:file).new( + :path => basedir, + :source => exe, + :mode => 755 ) - ofile = Puppet::Type.type(:file).new( - - :path => exe, - - :mode => 755 + ofile = Puppet::Type.type(:file).new( + :path => exe, + :mode => 755 ) - exec = Puppet::Type.type(:exec).new( - - :command => oexe, - :path => ENV["PATH"], - - :cwd => basedir + exec = Puppet::Type.type(:exec).new( + :command => oexe, + :path => ENV["PATH"], + :cwd => basedir ) - - cat = Puppet::Type.type(:exec).new( - - :command => "cat #{exe} #{oexe}", - - :path => ENV["PATH"] + cat = Puppet::Type.type(:exec).new( + :command => "cat #{exe} #{oexe}", + :path => ENV["PATH"] ) catalog = mk_catalog(file, baseobj, ofile, exec, cat) @@ -311,11 +272,9 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { exec = Puppet::Type.type(:exec).new( - :command => "touch #{bfile}", - :onlyif => "test -f #{afile}", - - :path => ENV['PATH'] + :onlyif => "test -f #{afile}", + :path => ENV['PATH'] ) } @@ -333,12 +292,9 @@ class TestExec < Test::Unit::TestCase exec = nil assert_nothing_raised { - exec = Puppet::Type.type(:exec).new( - :command => "touch #{bfile}", :unless => "test -f #{afile}", - :path => ENV['PATH'] ) } @@ -417,13 +373,10 @@ class TestExec < Test::Unit::TestCase def test_logoutput exec = nil assert_nothing_raised { - exec = Puppet::Type.type(:exec).new( - - :title => "logoutputesting", - :path => "/usr/bin:/bin", - :command => "echo logoutput is false", - + :title => "logoutputesting", + :path => "/usr/bin:/bin", + :command => "echo logoutput is false", :logoutput => false ) } @@ -453,24 +406,19 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised { exec = Puppet::Type.type(:exec).new( - - :title => "mkdir", - :path => "/usr/bin:/bin", + :title => "mkdir", + :path => "/usr/bin:/bin", :creates => basedir, - :command => "mkdir #{basedir}; touch #{path}" - ) } assert_nothing_raised { file = Puppet::Type.type(:file).new( - - :path => basedir, + :path => basedir, :recurse => true, - :mode => "755", - + :mode => "755", :require => Puppet::Resource.new("exec", "mkdir") ) } @@ -507,12 +455,9 @@ class TestExec < Test::Unit::TestCase file = tempfile assert_nothing_raised { - exec1 = Puppet::Type.type(:exec).new( - - :title => "one", - :path => ENV["PATH"], - + :title => "one", + :path => ENV["PATH"], :command => "mkdir #{dir}" ) } @@ -520,12 +465,10 @@ class TestExec < Test::Unit::TestCase assert_nothing_raised("Could not create exec w/out existing cwd") { exec2 = Puppet::Type.type(:exec).new( - - :title => "two", - :path => ENV["PATH"], + :title => "two", + :path => ENV["PATH"], :command => "touch #{file}", - - :cwd => dir + :cwd => dir ) } @@ -555,11 +498,8 @@ class TestExec < Test::Unit::TestCase test = "test -f #{file}" assert_nothing_raised { - exec = Puppet::Type.type(:exec).new( - - :path => ENV["PATH"], - + :path => ENV["PATH"], :command => "touch #{file}" ) } @@ -569,7 +509,7 @@ class TestExec < Test::Unit::TestCase } assert_nothing_raised { - assert(exec.check, "Check did not pass") + assert(exec.check_all_attributes, "Check did not pass") } assert_nothing_raised { @@ -582,13 +522,13 @@ class TestExec < Test::Unit::TestCase } assert_nothing_raised { - assert(exec.check, "Check did not pass") + assert(exec.check_all_attributes, "Check did not pass") } assert_apply(exec) assert_nothing_raised { - assert(! exec.check, "Check passed") + assert(! exec.check_all_attributes, "Check passed") } end @@ -597,25 +537,21 @@ class TestExec < Test::Unit::TestCase return if Facter.value(:operatingsystem) == "Solaris" exec = Puppet::Type.type(:exec).new( - :command => "echo true", - :path => ENV["PATH"], - - :onlyif => "/bin/nosuchthingexists" - ) + :path => ENV["PATH"], + :onlyif => "/bin/nosuchthingexists" + ) assert_raise(ArgumentError, "Missing command did not raise error") { - exec.run("/bin/nosuchthingexists") + exec.provider.run("/bin/nosuchthingexists") } end def test_environmentparam exec = Puppet::Type.newexec( - - :command => "echo $environmenttest", - :path => ENV["PATH"], - + :command => "echo $environmenttest", + :path => ENV["PATH"], :environment => "environmenttest=yayness" ) @@ -623,7 +559,7 @@ class TestExec < Test::Unit::TestCase output = status = nil assert_nothing_raised { - output, status = exec.run("echo $environmenttest") + output, status = exec.provider.run("echo $environmenttest") } assert_equal("yayness\n", output) @@ -636,7 +572,7 @@ and stuff" output = status = nil assert_nothing_raised { - output, status = exec.run('echo "$environmenttest"') + output, status = exec.provider.run('echo "$environmenttest"') } assert_equal("a list of things\nand stuff\n", output) @@ -647,36 +583,20 @@ and stuff" output = status = nil assert_nothing_raised { - output, status = exec.run('echo "$funtest" "$yaytest"') + output, status = exec.provider.run('echo "$funtest" "$yaytest"') } assert_equal("A B\n", output) end - def test_timeout - exec = Puppet::Type.type(:exec).new(:command => "sleep 1", :path => ENV["PATH"], :timeout => "0.2") - time = Time.now - - assert_raise(Timeout::Error) { - exec.run("sleep 1") - } - Puppet.info "#{Time.now.to_f - time.to_f} seconds, vs a timeout of #{exec[:timeout]}" - - - assert_apply(exec) - end - # Testing #470 def test_run_as_created_user exec = nil if Process.uid == 0 user = "nosuchuser" assert_nothing_raised("Could not create exec with non-existent user") do - exec = Puppet::Type.type(:exec).new( - :command => "/bin/echo yay", - - :user => user + :user => user ) end end @@ -686,10 +606,8 @@ and stuff" assert_nothing_raised("Could not create exec with non-existent user") do exec = Puppet::Type.type(:exec).new( - :command => "/bin/echo yay", - - :group => group + :group => group ) end end @@ -716,12 +634,10 @@ and stuff" file = tempfile maker = tempfile - exec = Puppet::Type.type(:exec).new( - - :title => "maker", - :command => "touch #{maker}", - - :path => ENV["PATH"] + exec = Puppet::Type.type(:exec).new( + :title => "maker", + :command => "touch #{maker}", + :path => ENV["PATH"] ) # Make sure it runs normally @@ -758,12 +674,10 @@ and stuff" refresher = tempfile maker = tempfile - exec = Puppet::Type.type(:exec).new( - - :title => "maker", - :command => "touch #{maker}", - - :path => ENV["PATH"] + exec = Puppet::Type.type(:exec).new( + :title => "maker", + :command => "touch #{maker}", + :path => ENV["PATH"] ) # Call refresh normally @@ -801,4 +715,3 @@ and stuff" end end end - |