diff --git a/lib/puppet/type/cron.rb b/lib/puppet/type/cron.rb index 1a2584a3f..8e53bf77f 100644 --- a/lib/puppet/type/cron.rb +++ b/lib/puppet/type/cron.rb @@ -1,488 +1,492 @@ require 'etc' require 'facter' require 'puppet/util/filetype' Puppet::Type.newtype(:cron) do @doc = <<-'EOT' Installs and manages cron jobs. Every cron resource created by Puppet requires a command and at least one periodic attribute (hour, minute, month, monthday, weekday, or special). While the name of the cron job is not part of the actual job, the name is stored in a comment beginning with `# Puppet Name: `. These comments are used to match crontab entries created by Puppet with cron resources. If an existing crontab entry happens to match the scheduling and command of a cron resource that has never been synched, Puppet will defer to the existing crontab entry and will not create a new entry tagged with the `# Puppet Name: ` comment. Example: cron { logrotate: command => "/usr/sbin/logrotate", user => root, hour => 2, minute => 0 } Note that all periodic attributes can be specified as an array of values: cron { logrotate: command => "/usr/sbin/logrotate", user => root, hour => [2, 4] } ...or using ranges or the step syntax `*/2` (although there's no guarantee that your `cron` daemon supports these): cron { logrotate: command => "/usr/sbin/logrotate", user => root, hour => ['2-4'], minute => '*/10' } An important note: _the Cron type will not reset parameters that are removed from a manifest_. For example, removing a `minute => 10` parameter will not reset the minute component of the associated cronjob to `*`. These changes must be expressed by setting the parameter to `minute => absent` because Puppet only manages parameters that are out of sync with manifest entries. **Autorequires:** If Puppet is managing the user account specified by the `user` property of a cron resource, then the cron resource will autorequire that user. EOT ensurable # A base class for all of the Cron parameters, since they all have # similar argument checking going on. class CronParam < Puppet::Property class << self attr_accessor :boundaries, :default end # We have to override the parent method, because we consume the entire # "should" array def insync?(is) self.is_to_s(is) == self.should_to_s end # A method used to do parameter input handling. Converts integers # in string form to actual integers, and returns the value if it's # an integer or false if it's just a normal string. def numfix(num) if num =~ /^\d+$/ return num.to_i elsif num.is_a?(Integer) return num else return false end end # Verify that a number is within the specified limits. Return the # number if it is, or false if it is not. def limitcheck(num, lower, upper) (num >= lower and num <= upper) && num end # Verify that a value falls within the specified array. Does case # insensitive matching, and supports matching either the entire word # or the first three letters of the word. def alphacheck(value, ary) tmp = value.downcase # If they specified a shortened version of the name, then see # if we can lengthen it (e.g., mon => monday). if tmp.length == 3 ary.each_with_index { |name, index| if tmp.upcase == name[0..2].upcase return index end } else return ary.index(tmp) if ary.include?(tmp) end false end def should_to_s(newvalue = @should) if newvalue newvalue = [newvalue] unless newvalue.is_a?(Array) if self.name == :command or newvalue[0].is_a? Symbol newvalue[0] else newvalue.join(",") end else nil end end def is_to_s(currentvalue = @is) if currentvalue return currentvalue unless currentvalue.is_a?(Array) if self.name == :command or currentvalue[0].is_a? Symbol currentvalue[0] else currentvalue.join(",") end else nil end end def should if @should and @should[0] == :absent :absent else @should end end def should=(ary) super @should.flatten! end # The method that does all of the actual parameter value # checking; called by all of the +param=+ methods. # Requires the value, type, and bounds, and optionally supports # a boolean of whether to do alpha checking, and if so requires # the ary against which to do the checking. munge do |value| # Support 'absent' as a value, so that they can remove # a value if value == "absent" or value == :absent return :absent end # Allow the */2 syntax if value =~ /^\*\/[0-9]+$/ return value end # Allow ranges if value =~ /^[0-9]+-[0-9]+$/ return value end # Allow ranges + */2 if value =~ /^[0-9]+-[0-9]+\/[0-9]+$/ return value end if value == "*" return :absent end return value unless self.class.boundaries lower, upper = self.class.boundaries retval = nil if num = numfix(value) retval = limitcheck(num, lower, upper) elsif respond_to?(:alpha) # If it has an alpha method defined, then we check # to see if our value is in that list and if so we turn # it into a number retval = alphacheck(value, alpha) end if retval return retval.to_s else self.fail "#{value} is not a valid #{self.class.name}" end end end # Somewhat uniquely, this property does not actually change anything -- it # just calls +@resource.sync+, which writes out the whole cron tab for # the user in question. There is no real way to change individual cron # jobs without rewriting the entire cron file. # # Note that this means that managing many cron jobs for a given user # could currently result in multiple write sessions for that user. newproperty(:command, :parent => CronParam) do desc "The command to execute in the cron job. The environment provided to the command varies by local system rules, and it is best to always provide a fully qualified command. The user's profile is not sourced when the command is run, so if the user's environment is desired it should be sourced manually. All cron parameters support `absent` as a value; this will remove any existing values for that field." def retrieve return_value = super return_value = return_value[0] if return_value && return_value.is_a?(Array) return_value end def should if @should if @should.is_a? Array @should[0] else devfail "command is not an array" end else nil end end def munge(value) value.strip end end newproperty(:special) do desc "A special value such as 'reboot' or 'annually'. Only available on supported systems such as Vixie Cron. Overrides more specific time of day/week settings. Set to 'absent' to make puppet revert to a plain numeric schedule." def specials %w{reboot yearly annually monthly weekly daily midnight hourly absent} + [ :absent ] end validate do |value| raise ArgumentError, "Invalid special schedule #{value.inspect}" unless specials.include?(value) end def munge(value) # Support value absent so that a schedule can be # forced to change to numeric. if value == "absent" or value == :absent return :absent end value end end newproperty(:minute, :parent => CronParam) do self.boundaries = [0, 59] desc "The minute at which to run the cron job. Optional; if specified, must be between 0 and 59, inclusive." end newproperty(:hour, :parent => CronParam) do self.boundaries = [0, 23] desc "The hour at which to run the cron job. Optional; if specified, must be between 0 and 23, inclusive." end newproperty(:weekday, :parent => CronParam) do def alpha %w{sunday monday tuesday wednesday thursday friday saturday} end self.boundaries = [0, 7] desc "The weekday on which to run the command. Optional; if specified, must be between 0 and 7, inclusive, with 0 (or 7) being Sunday, or must be the name of the day (e.g., Tuesday)." end newproperty(:month, :parent => CronParam) do def alpha - %w{january february march april may june july + # The ___placeholder accounts for the fact that month is unique among + # "nameable" crontab entries in that it does not use 0-based indexing. + # Padding the array with a placeholder introduces the appropriate shift + # in indices. + %w{___placeholder january february march april may june july august september october november december} end self.boundaries = [1, 12] desc "The month of the year. Optional; if specified must be between 1 and 12 or the month name (e.g., December)." end newproperty(:monthday, :parent => CronParam) do self.boundaries = [1, 31] desc "The day of the month on which to run the command. Optional; if specified, must be between 1 and 31." end newproperty(:environment) do desc "Any environment settings associated with this cron job. They will be stored between the header and the job in the crontab. There can be no guarantees that other, earlier settings will not also affect a given cron job. Also, Puppet cannot automatically determine whether an existing, unmanaged environment setting is associated with a given cron job. If you already have cron jobs with environment settings, then Puppet will keep those settings in the same place in the file, but will not associate them with a specific job. Settings should be specified exactly as they should appear in the crontab, e.g., `PATH=/bin:/usr/bin:/usr/sbin`." validate do |value| unless value =~ /^\s*(\w+)\s*=\s*(.*)\s*$/ or value == :absent or value == "absent" raise ArgumentError, "Invalid environment setting #{value.inspect}" end end def insync?(is) if is.is_a? Array return is.sort == @should.sort else return is == @should end end def is_to_s(newvalue) if newvalue if newvalue.is_a?(Array) newvalue.join(",") else newvalue end else nil end end def should @should end def should_to_s(newvalue = @should) if newvalue newvalue.join(",") else nil end end end newparam(:name) do desc "The symbolic name of the cron job. This name is used for human reference only and is generated automatically for cron jobs found on the system. This generally won't matter, as Puppet will do its best to match existing cron jobs against specified jobs (and Puppet adds a comment to cron jobs it adds), but it is at least possible that converting from unmanaged jobs to managed jobs might require manual intervention." isnamevar end newproperty(:user) do desc "The user who owns the cron job. This user must be allowed to run cron jobs, which is not currently checked by Puppet. This property defaults to the user running Puppet or `root`. The default crontab provider executes the system `crontab` using the user account specified by this property." defaultto { if not provider.is_a?(@resource.class.provider(:crontab)) struct = Etc.getpwuid(Process.uid) struct.respond_to?(:name) && struct.name or 'root' end } end # Autorequire the owner of the crontab entry. autorequire(:user) do self[:user] end newproperty(:target) do desc "The name of the crontab file in which the cron job should be stored. This property defaults to the value of the `user` property if set, the user running Puppet or `root`. For the default crontab provider, this property is functionally equivalent to the `user` property and should be avoided. In particular, setting both `user` and `target` to different values will result in undefined behavior." defaultto { if provider.is_a?(@resource.class.provider(:crontab)) if val = @resource.should(:user) val else struct = Etc.getpwuid(Process.uid) struct.respond_to?(:name) && struct.name or 'root' end elsif provider.class.ancestors.include?(Puppet::Provider::ParsedFile) provider.class.default_target else nil end } end validate do return true unless self[:special] return true if self[:special] == :absent # there is a special schedule in @should, so we don't want to see # any numeric should values [ :minute, :hour, :weekday, :monthday, :month ].each do |field| next unless self[field] next if self[field] == :absent raise ArgumentError, "#{self.ref} cannot specify both a special schedule and a value for #{field}" end end # We have to reorder things so that :provide is before :target attr_accessor :uid # Marks the resource as "being purged". # # @api public # # @note This overrides the Puppet::Type method in order to handle # an edge case that has so far been observed during testig only. # Without forcing the should-value for the user property to be # identical to the original cron file, purging from a fixture # will not work, because the user property defaults to the user # running the test. It is not clear whether this scenario can apply # during normal operation. # # @note Also, when not forcing the should-value for the target # property, unpurged file content (such as comments) can end up # being written to the default target (i.e. the current login name). def purging self[:target] = provider.target self[:user] = provider.target super end def value(name) name = name.intern ret = nil if obj = @parameters[name] ret = obj.should ret ||= obj.retrieve if ret == :absent ret = nil end end unless ret case name when :command when :special # nothing else #ret = (self.class.validproperty?(name).default || "*").to_s ret = "*" end end ret end end diff --git a/spec/unit/type/cron_spec.rb b/spec/unit/type/cron_spec.rb index 82f646290..37be5cbcc 100755 --- a/spec/unit/type/cron_spec.rb +++ b/spec/unit/type/cron_spec.rb @@ -1,543 +1,543 @@ #! /usr/bin/env ruby require 'spec_helper' describe Puppet::Type.type(:cron), :unless => Puppet.features.microsoft_windows? do let(:simple_provider) do @provider_class = described_class.provide(:simple) { mk_resource_methods } @provider_class.stubs(:suitable?).returns true @provider_class end before :each do described_class.stubs(:defaultprovider).returns @provider_class end after :each do described_class.unprovide(:simple) end it "should have :name be its namevar" do described_class.key_attributes.should == [:name] end describe "when validating attributes" do [:name, :provider].each do |param| it "should have a #{param} parameter" do described_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 described_class.attrtype(property).should == :property end end [:command, :minute, :hour, :weekday, :month, :monthday].each do |cronparam| it "should have #{cronparam} of type CronParam" do described_class.attrclass(cronparam).ancestors.should include CronParam end end end describe "when validating values" do describe "ensure" do it "should support present as a value for ensure" do expect { described_class.new(:name => 'foo', :ensure => :present) }.to_not raise_error end it "should support absent as a value for ensure" do expect { described_class.new(:name => 'foo', :ensure => :present) }.to_not raise_error end it "should not support other values" do expect { described_class.new(:name => 'foo', :ensure => :foo) }.to raise_error(Puppet::Error, /Invalid value/) end end describe "command" do it "should discard leading spaces" do described_class.new(:name => 'foo', :command => " /bin/true")[:command].should_not match Regexp.new(" ") end it "should discard trailing spaces" do described_class.new(:name => 'foo', :command => "/bin/true ")[:command].should_not match Regexp.new(" ") end end describe "minute" do it "should support absent" do expect { described_class.new(:name => 'foo', :minute => 'absent') }.to_not raise_error end it "should support *" do expect { described_class.new(:name => 'foo', :minute => '*') }.to_not raise_error end it "should translate absent to :absent" do described_class.new(:name => 'foo', :minute => 'absent')[:minute].should == :absent end it "should translate * to :absent" do described_class.new(:name => 'foo', :minute => '*')[:minute].should == :absent end it "should support valid single values" do expect { described_class.new(:name => 'foo', :minute => '0') }.to_not raise_error expect { described_class.new(:name => 'foo', :minute => '1') }.to_not raise_error expect { described_class.new(:name => 'foo', :minute => '59') }.to_not raise_error end it "should not support non numeric characters" do expect { described_class.new(:name => 'foo', :minute => 'z59') }.to raise_error(Puppet::Error, /z59 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => '5z9') }.to raise_error(Puppet::Error, /5z9 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => '59z') }.to raise_error(Puppet::Error, /59z is not a valid minute/) end it "should not support single values out of range" do expect { described_class.new(:name => 'foo', :minute => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => '60') }.to raise_error(Puppet::Error, /60 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => '61') }.to raise_error(Puppet::Error, /61 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => '120') }.to raise_error(Puppet::Error, /120 is not a valid minute/) end it "should support valid multiple values" do expect { described_class.new(:name => 'foo', :minute => ['0','1','59'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :minute => ['40','30','20'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :minute => ['10','30','20'] ) }.to_not raise_error end it "should not support multiple values if at least one is invalid" do # one invalid expect { described_class.new(:name => 'foo', :minute => ['0','1','60'] ) }.to raise_error(Puppet::Error, /60 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => ['0','120','59'] ) }.to raise_error(Puppet::Error, /120 is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => ['-1','1','59'] ) }.to raise_error(Puppet::Error, /-1 is not a valid minute/) # two invalid expect { described_class.new(:name => 'foo', :minute => ['0','61','62'] ) }.to raise_error(Puppet::Error, /(61|62) is not a valid minute/) # all invalid expect { described_class.new(:name => 'foo', :minute => ['-1','61','62'] ) }.to raise_error(Puppet::Error, /(-1|61|62) is not a valid minute/) end it "should support valid step syntax" do expect { described_class.new(:name => 'foo', :minute => '*/2' ) }.to_not raise_error expect { described_class.new(:name => 'foo', :minute => '10-16/2' ) }.to_not raise_error end it "should not support invalid steps" do expect { described_class.new(:name => 'foo', :minute => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid minute/) expect { described_class.new(:name => 'foo', :minute => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/2A is not a valid minute/) # As it turns out cron does not complaining about steps that exceed the valid range # expect { described_class.new(:name => 'foo', :minute => '*/120' ) }.to raise_error(Puppet::Error, /is not a valid minute/) end end describe "hour" do it "should support absent" do expect { described_class.new(:name => 'foo', :hour => 'absent') }.to_not raise_error end it "should support *" do expect { described_class.new(:name => 'foo', :hour => '*') }.to_not raise_error end it "should translate absent to :absent" do described_class.new(:name => 'foo', :hour => 'absent')[:hour].should == :absent end it "should translate * to :absent" do described_class.new(:name => 'foo', :hour => '*')[:hour].should == :absent end it "should support valid single values" do expect { described_class.new(:name => 'foo', :hour => '0') }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => '11') }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => '12') }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => '13') }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => '23') }.to_not raise_error end it "should not support non numeric characters" do expect { described_class.new(:name => 'foo', :hour => 'z15') }.to raise_error(Puppet::Error, /z15 is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => '1z5') }.to raise_error(Puppet::Error, /1z5 is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => '15z') }.to raise_error(Puppet::Error, /15z is not a valid hour/) end it "should not support single values out of range" do expect { described_class.new(:name => 'foo', :hour => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => '24') }.to raise_error(Puppet::Error, /24 is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => '120') }.to raise_error(Puppet::Error, /120 is not a valid hour/) end it "should support valid multiple values" do expect { described_class.new(:name => 'foo', :hour => ['0','1','23'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => ['5','16','14'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => ['16','13','9'] ) }.to_not raise_error end it "should not support multiple values if at least one is invalid" do # one invalid expect { described_class.new(:name => 'foo', :hour => ['0','1','24'] ) }.to raise_error(Puppet::Error, /24 is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => ['0','-1','5'] ) }.to raise_error(Puppet::Error, /-1 is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => ['-1','1','23'] ) }.to raise_error(Puppet::Error, /-1 is not a valid hour/) # two invalid expect { described_class.new(:name => 'foo', :hour => ['0','25','26'] ) }.to raise_error(Puppet::Error, /(25|26) is not a valid hour/) # all invalid expect { described_class.new(:name => 'foo', :hour => ['-1','24','120'] ) }.to raise_error(Puppet::Error, /(-1|24|120) is not a valid hour/) end it "should support valid step syntax" do expect { described_class.new(:name => 'foo', :hour => '*/2' ) }.to_not raise_error expect { described_class.new(:name => 'foo', :hour => '10-18/4' ) }.to_not raise_error end it "should not support invalid steps" do expect { described_class.new(:name => 'foo', :hour => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid hour/) expect { described_class.new(:name => 'foo', :hour => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/2A is not a valid hour/) # As it turns out cron does not complaining about steps that exceed the valid range # expect { described_class.new(:name => 'foo', :hour => '*/26' ) }.to raise_error(Puppet::Error, /is not a valid hour/) end end describe "weekday" do it "should support absent" do expect { described_class.new(:name => 'foo', :weekday => 'absent') }.to_not raise_error end it "should support *" do expect { described_class.new(:name => 'foo', :weekday => '*') }.to_not raise_error end it "should translate absent to :absent" do described_class.new(:name => 'foo', :weekday => 'absent')[:weekday].should == :absent end it "should translate * to :absent" do described_class.new(:name => 'foo', :weekday => '*')[:weekday].should == :absent end it "should support valid numeric weekdays" do expect { described_class.new(:name => 'foo', :weekday => '0') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => '1') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => '6') }.to_not raise_error # According to http://www.manpagez.com/man/5/crontab 7 is also valid (Sunday) expect { described_class.new(:name => 'foo', :weekday => '7') }.to_not raise_error end it "should support valid weekdays as words (long version)" do expect { described_class.new(:name => 'foo', :weekday => 'Monday') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Tuesday') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Wednesday') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Thursday') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Friday') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Saturday') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Sunday') }.to_not raise_error end it "should support valid weekdays as words (3 character version)" do expect { described_class.new(:name => 'foo', :weekday => 'Mon') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Tue') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Wed') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Thu') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Fri') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Sat') }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => 'Sun') }.to_not raise_error end it "should not support numeric values out of range" do expect { described_class.new(:name => 'foo', :weekday => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid weekday/) expect { described_class.new(:name => 'foo', :weekday => '8') }.to raise_error(Puppet::Error, /8 is not a valid weekday/) end it "should not support invalid weekday names" do expect { described_class.new(:name => 'foo', :weekday => 'Sar') }.to raise_error(Puppet::Error, /Sar is not a valid weekday/) end it "should support valid multiple values" do expect { described_class.new(:name => 'foo', :weekday => ['0','1','6'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => ['Mon','Wed','Friday'] ) }.to_not raise_error end it "should not support multiple values if at least one is invalid" do # one invalid expect { described_class.new(:name => 'foo', :weekday => ['0','1','8'] ) }.to raise_error(Puppet::Error, /8 is not a valid weekday/) expect { described_class.new(:name => 'foo', :weekday => ['Mon','Fii','Sat'] ) }.to raise_error(Puppet::Error, /Fii is not a valid weekday/) # two invalid expect { described_class.new(:name => 'foo', :weekday => ['Mos','Fii','Sat'] ) }.to raise_error(Puppet::Error, /(Mos|Fii) is not a valid weekday/) # all invalid expect { described_class.new(:name => 'foo', :weekday => ['Mos','Fii','Saa'] ) }.to raise_error(Puppet::Error, /(Mos|Fii|Saa) is not a valid weekday/) expect { described_class.new(:name => 'foo', :weekday => ['-1','8','11'] ) }.to raise_error(Puppet::Error, /(-1|8|11) is not a valid weekday/) end it "should support valid step syntax" do expect { described_class.new(:name => 'foo', :weekday => '*/2' ) }.to_not raise_error expect { described_class.new(:name => 'foo', :weekday => '0-4/2' ) }.to_not raise_error end it "should not support invalid steps" do expect { described_class.new(:name => 'foo', :weekday => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid weekday/) expect { described_class.new(:name => 'foo', :weekday => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/2A is not a valid weekday/) # As it turns out cron does not complaining about steps that exceed the valid range # expect { described_class.new(:name => 'foo', :weekday => '*/9' ) }.to raise_error(Puppet::Error, /is not a valid weekday/) end end describe "month" do it "should support absent" do expect { described_class.new(:name => 'foo', :month => 'absent') }.to_not raise_error end it "should support *" do expect { described_class.new(:name => 'foo', :month => '*') }.to_not raise_error end it "should translate absent to :absent" do described_class.new(:name => 'foo', :month => 'absent')[:month].should == :absent end it "should translate * to :absent" do described_class.new(:name => 'foo', :month => '*')[:month].should == :absent end it "should support valid numeric values" do expect { described_class.new(:name => 'foo', :month => '1') }.to_not raise_error expect { described_class.new(:name => 'foo', :month => '12') }.to_not raise_error end it "should support valid months as words" do - expect { described_class.new(:name => 'foo', :month => 'January') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'February') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'March') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'April') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'May') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'June') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'July') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'August') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'September') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'October') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'November') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'December') }.to_not raise_error + expect( described_class.new(:name => 'foo', :month => 'January')[:month] ).to eq(['1']) + expect( described_class.new(:name => 'foo', :month => 'February')[:month] ).to eq(['2']) + expect( described_class.new(:name => 'foo', :month => 'March')[:month] ).to eq(['3']) + expect( described_class.new(:name => 'foo', :month => 'April')[:month] ).to eq(['4']) + expect( described_class.new(:name => 'foo', :month => 'May')[:month] ).to eq(['5']) + expect( described_class.new(:name => 'foo', :month => 'June')[:month] ).to eq(['6']) + expect( described_class.new(:name => 'foo', :month => 'July')[:month] ).to eq(['7']) + expect( described_class.new(:name => 'foo', :month => 'August')[:month] ).to eq(['8']) + expect( described_class.new(:name => 'foo', :month => 'September')[:month] ).to eq(['9']) + expect( described_class.new(:name => 'foo', :month => 'October')[:month] ).to eq(['10']) + expect( described_class.new(:name => 'foo', :month => 'November')[:month] ).to eq(['11']) + expect( described_class.new(:name => 'foo', :month => 'December')[:month] ).to eq(['12']) end it "should support valid months as words (3 character short version)" do - expect { described_class.new(:name => 'foo', :month => 'Jan') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Feb') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Mar') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Apr') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'May') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Jun') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Jul') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Aug') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Sep') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Oct') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Nov') }.to_not raise_error - expect { described_class.new(:name => 'foo', :month => 'Dec') }.to_not raise_error + expect( described_class.new(:name => 'foo', :month => 'Jan')[:month] ).to eq(['1']) + expect( described_class.new(:name => 'foo', :month => 'Feb')[:month] ).to eq(['2']) + expect( described_class.new(:name => 'foo', :month => 'Mar')[:month] ).to eq(['3']) + expect( described_class.new(:name => 'foo', :month => 'Apr')[:month] ).to eq(['4']) + expect( described_class.new(:name => 'foo', :month => 'May')[:month] ).to eq(['5']) + expect( described_class.new(:name => 'foo', :month => 'Jun')[:month] ).to eq(['6']) + expect( described_class.new(:name => 'foo', :month => 'Jul')[:month] ).to eq(['7']) + expect( described_class.new(:name => 'foo', :month => 'Aug')[:month] ).to eq(['8']) + expect( described_class.new(:name => 'foo', :month => 'Sep')[:month] ).to eq(['9']) + expect( described_class.new(:name => 'foo', :month => 'Oct')[:month] ).to eq(['10']) + expect( described_class.new(:name => 'foo', :month => 'Nov')[:month] ).to eq(['11']) + expect( described_class.new(:name => 'foo', :month => 'Dec')[:month] ).to eq(['12']) end it "should not support numeric values out of range" do expect { described_class.new(:name => 'foo', :month => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid month/) expect { described_class.new(:name => 'foo', :month => '0') }.to raise_error(Puppet::Error, /0 is not a valid month/) expect { described_class.new(:name => 'foo', :month => '13') }.to raise_error(Puppet::Error, /13 is not a valid month/) end it "should not support words that are not valid months" do expect { described_class.new(:name => 'foo', :month => 'Jal') }.to raise_error(Puppet::Error, /Jal is not a valid month/) end it "should not support single values out of range" do expect { described_class.new(:name => 'foo', :month => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid month/) expect { described_class.new(:name => 'foo', :month => '60') }.to raise_error(Puppet::Error, /60 is not a valid month/) expect { described_class.new(:name => 'foo', :month => '61') }.to raise_error(Puppet::Error, /61 is not a valid month/) expect { described_class.new(:name => 'foo', :month => '120') }.to raise_error(Puppet::Error, /120 is not a valid month/) end it "should support valid multiple values" do expect { described_class.new(:name => 'foo', :month => ['1','9','12'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :month => ['Jan','March','Jul'] ) }.to_not raise_error end it "should not support multiple values if at least one is invalid" do # one invalid expect { described_class.new(:name => 'foo', :month => ['0','1','12'] ) }.to raise_error(Puppet::Error, /0 is not a valid month/) expect { described_class.new(:name => 'foo', :month => ['1','13','10'] ) }.to raise_error(Puppet::Error, /13 is not a valid month/) expect { described_class.new(:name => 'foo', :month => ['Jan','Feb','Jxx'] ) }.to raise_error(Puppet::Error, /Jxx is not a valid month/) # two invalid expect { described_class.new(:name => 'foo', :month => ['Jan','Fex','Jux'] ) }.to raise_error(Puppet::Error, /(Fex|Jux) is not a valid month/) # all invalid expect { described_class.new(:name => 'foo', :month => ['-1','0','13'] ) }.to raise_error(Puppet::Error, /(-1|0|13) is not a valid month/) expect { described_class.new(:name => 'foo', :month => ['Jax','Fex','Aux'] ) }.to raise_error(Puppet::Error, /(Jax|Fex|Aux) is not a valid month/) end it "should support valid step syntax" do expect { described_class.new(:name => 'foo', :month => '*/2' ) }.to_not raise_error expect { described_class.new(:name => 'foo', :month => '1-12/3' ) }.to_not raise_error end it "should not support invalid steps" do expect { described_class.new(:name => 'foo', :month => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid month/) expect { described_class.new(:name => 'foo', :month => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/2A is not a valid month/) # As it turns out cron does not complaining about steps that exceed the valid range # expect { described_class.new(:name => 'foo', :month => '*/13' ) }.to raise_error(Puppet::Error, /is not a valid month/) end end describe "monthday" do it "should support absent" do expect { described_class.new(:name => 'foo', :monthday => 'absent') }.to_not raise_error end it "should support *" do expect { described_class.new(:name => 'foo', :monthday => '*') }.to_not raise_error end it "should translate absent to :absent" do described_class.new(:name => 'foo', :monthday => 'absent')[:monthday].should == :absent end it "should translate * to :absent" do described_class.new(:name => 'foo', :monthday => '*')[:monthday].should == :absent end it "should support valid single values" do expect { described_class.new(:name => 'foo', :monthday => '1') }.to_not raise_error expect { described_class.new(:name => 'foo', :monthday => '30') }.to_not raise_error expect { described_class.new(:name => 'foo', :monthday => '31') }.to_not raise_error end it "should not support non numeric characters" do expect { described_class.new(:name => 'foo', :monthday => 'z23') }.to raise_error(Puppet::Error, /z23 is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => '2z3') }.to raise_error(Puppet::Error, /2z3 is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => '23z') }.to raise_error(Puppet::Error, /23z is not a valid monthday/) end it "should not support single values out of range" do expect { described_class.new(:name => 'foo', :monthday => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => '0') }.to raise_error(Puppet::Error, /0 is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => '32') }.to raise_error(Puppet::Error, /32 is not a valid monthday/) end it "should support valid multiple values" do expect { described_class.new(:name => 'foo', :monthday => ['1','23','31'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :monthday => ['31','23','1'] ) }.to_not raise_error expect { described_class.new(:name => 'foo', :monthday => ['1','31','23'] ) }.to_not raise_error end it "should not support multiple values if at least one is invalid" do # one invalid expect { described_class.new(:name => 'foo', :monthday => ['1','23','32'] ) }.to raise_error(Puppet::Error, /32 is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => ['-1','12','23'] ) }.to raise_error(Puppet::Error, /-1 is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => ['13','32','30'] ) }.to raise_error(Puppet::Error, /32 is not a valid monthday/) # two invalid expect { described_class.new(:name => 'foo', :monthday => ['-1','0','23'] ) }.to raise_error(Puppet::Error, /(-1|0) is not a valid monthday/) # all invalid expect { described_class.new(:name => 'foo', :monthday => ['-1','0','32'] ) }.to raise_error(Puppet::Error, /(-1|0|32) is not a valid monthday/) end it "should support valid step syntax" do expect { described_class.new(:name => 'foo', :monthday => '*/2' ) }.to_not raise_error expect { described_class.new(:name => 'foo', :monthday => '10-16/2' ) }.to_not raise_error end it "should not support invalid steps" do expect { described_class.new(:name => 'foo', :monthday => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid monthday/) expect { described_class.new(:name => 'foo', :monthday => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/2A is not a valid monthday/) # As it turns out cron does not complaining about steps that exceed the valid range # expect { described_class.new(:name => 'foo', :monthday => '*/32' ) }.to raise_error(Puppet::Error, /is not a valid monthday/) end end describe "special" do %w(reboot yearly annually monthly weekly daily midnight hourly).each do |value| it "should support the value '#{value}'" do expect { described_class.new(:name => 'foo', :special => value ) }.to_not raise_error end end context "when combined with numeric schedule fields" do context "which are 'absent'" do [ %w(reboot yearly annually monthly weekly daily midnight hourly), :absent ].flatten.each { |value| it "should accept the value '#{value}' for special" do expect { described_class.new(:name => 'foo', :minute => :absent, :special => value ) }.to_not raise_error end } end context "which are not absent" do %w(reboot yearly annually monthly weekly daily midnight hourly).each { |value| it "should not accept the value '#{value}' for special" do expect { described_class.new(:name => 'foo', :minute => "1", :special => value ) }.to raise_error(Puppet::Error, /cannot specify both a special schedule and a value/) end } it "should accept the 'absent' value for special" do expect { described_class.new(:name => 'foo', :minute => "1", :special => :absent ) }.to_not raise_error end end end end describe "environment" do it "it should accept an :environment that looks like a path" do expect do described_class.new(:name => 'foo',:environment => 'PATH=/bin:/usr/bin:/usr/sbin') end.to_not raise_error end it "should not accept environment variables that do not contain '='" do expect do described_class.new(:name => 'foo',:environment => 'INVALID') end.to raise_error(Puppet::Error, /Invalid environment setting "INVALID"/) end it "should accept empty environment variables that do not contain '='" do expect do described_class.new(:name => 'foo',:environment => 'MAILTO=') end.to_not raise_error end it "should accept 'absent'" do expect do described_class.new(:name => 'foo',:environment => 'absent') end.to_not raise_error end end end describe "when autorequiring resources" do before :each do @user_bob = Puppet::Type.type(:user).new(:name => 'bob', :ensure => :present) @user_alice = Puppet::Type.type(:user).new(:name => 'alice', :ensure => :present) @catalog = Puppet::Resource::Catalog.new @catalog.add_resource @user_bob, @user_alice end it "should autorequire the user" do @resource = described_class.new(:name => 'dummy', :command => '/usr/bin/uptime', :user => 'alice') @catalog.add_resource @resource req = @resource.autorequire req.size.should == 1 req[0].target.must == @resource req[0].source.must == @user_alice end end it "should not require a command when removing an entry" do entry = described_class.new(:name => "test_entry", :ensure => :absent) entry.value(:command).should == nil end it "should default to user => root if Etc.getpwuid(Process.uid) returns nil (#12357)" do Etc.expects(:getpwuid).returns(nil) entry = described_class.new(:name => "test_entry", :ensure => :present) entry.value(:user).should eql "root" end end