diff --git a/spec/unit/ral/types/schedule.rb b/spec/unit/ral/types/schedule.rb index 73b3a0bd1..4e9840c34 100755 --- a/spec/unit/ral/types/schedule.rb +++ b/spec/unit/ral/types/schedule.rb @@ -1,374 +1,341 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/type/schedule' module ScheduleTesting - def setup - Puppet.settings.stubs(:value).with(:ignoreschedules).returns(false) - - @schedule = Puppet::Type::Schedule.create(:name => "testing") - end def format(time) time.strftime("%H:%M:%S") end def diff(unit, incr, method, count) diff = Time.now.to_i.send(method, incr * count) Time.at(diff) end def month(method, count) diff(:hour, 3600 * 24 * 30, method, count) end def week(method, count) diff(:hour, 3600 * 24 * 7, method, count) end def day(method, count) diff(:hour, 3600 * 24, method, count) end def hour(method, count) diff(:hour, 3600, method, count) end def min(method, count) diff(:min, 60, method, count) end def sec(method, count) diff(:sec, 1, method, count) end - def teardown - Puppet::Type::Schedule.clear - end end describe Puppet::Type::Schedule do - include ScheduleTesting - - it "should default to :distance for period-matching" do - @schedule[:periodmatch].should == :distance - end - it "should default to a :repeat of 1" do - @schedule[:repeat].should == 1 - end - - it "should never match when the period is :never" do - @schedule[:period] = :never - @schedule.match?.should be_false - end -end - -describe Puppet::Type::Schedule, "when producing default schedules" do - include ScheduleTesting + before :each do + Puppet.settings.stubs(:value).with(:ignoreschedules).returns(false) - %w{hourly daily weekly monthly never}.each do |period| - period = period.to_sym - it "should produce a #{period} schedule with the period set appropriately" do - schedules = Puppet::Type::Schedule.mkdefaultschedules - schedules.find { |s| s[:name] == period.to_s and s[:period] == period }.should be_instance_of(Puppet::Type::Schedule) - end + @schedule = Puppet::Type::Schedule.create(:name => "testing") end - it "should produce a schedule named puppet with a period of hourly and a repeat of 2" do - schedules = Puppet::Type::Schedule.mkdefaultschedules - schedules.find { |s| - s[:name] == "puppet" and s[:period] == :hourly and s[:repeat] == 2 - }.should be_instance_of(Puppet::Type::Schedule) + after :each do + Puppet::Type::Schedule.clear end -end -describe Puppet::Type::Schedule, "when matching ranges" do - include ScheduleTesting - it "should match when the start time is before the current time and the end time is after the current time" do - @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now + 10)] - @schedule.match?.should be_true - end - - it "should not match when the start time is after the current time" do - @schedule[:range] = "%s - %s" % [format(Time.now + 5), format(Time.now + 10)] - @schedule.match?.should be_false - end + describe Puppet::Type::Schedule do + include ScheduleTesting - it "should not match when the end time is previous to the current time" do - @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now - 5)] - @schedule.match?.should be_false - end -end + it "should default to :distance for period-matching" do + @schedule[:periodmatch].should == :distance + end -describe Puppet::Type::Schedule, "when matching hourly by distance" do - include ScheduleTesting + it "should default to a :repeat of 1" do + @schedule[:repeat].should == 1 + end - before do - @schedule[:period] = :hourly - @schedule[:periodmatch] = :distance + it "should never match when the period is :never" do + @schedule[:period] = :never + @schedule.match?.should be_false + end end - it "should match an hour ago" do - @schedule.match?(hour("-", 1)).should be_true - end + describe Puppet::Type::Schedule, "when producing default schedules" do + include ScheduleTesting - it "should not match now" do - @schedule.match?(Time.now).should be_false - end + %w{hourly daily weekly monthly never}.each do |period| + period = period.to_sym + it "should produce a #{period} schedule with the period set appropriately" do + schedules = Puppet::Type::Schedule.mkdefaultschedules + schedules.find { |s| s[:name] == period.to_s and s[:period] == period }.should be_instance_of(Puppet::Type::Schedule) + end + end - it "should not match 59 minutes ago" do - @schedule.match?(min("-", 59)).should be_false + it "should produce a schedule named puppet with a period of hourly and a repeat of 2" do + schedules = Puppet::Type::Schedule.mkdefaultschedules + schedules.find { |s| + s[:name] == "puppet" and s[:period] == :hourly and s[:repeat] == 2 + }.should be_instance_of(Puppet::Type::Schedule) + end end -end - -describe Puppet::Type::Schedule, "when matching daily by distance" do - include ScheduleTesting - before do - @schedule[:period] = :daily - @schedule[:periodmatch] = :distance - end + describe Puppet::Type::Schedule, "when matching ranges" do + include ScheduleTesting - it "should match when the previous time was one day ago" do - @schedule.match?(day("-", 1)).should be_true - end + it "should match when the start time is before the current time and the end time is after the current time" do + @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now + 10)] + @schedule.match?.should be_true + end - it "should not match when the previous time is now" do - @schedule.match?(Time.now).should be_false - end + it "should not match when the start time is after the current time" do + @schedule[:range] = "%s - %s" % [format(Time.now + 5), format(Time.now + 10)] + @schedule.match?.should be_false + end - it "should not match when the previous time was 23 hours ago" do - @schedule.match?(hour("-", 23)).should be_false + it "should not match when the end time is previous to the current time" do + @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now - 5)] + @schedule.match?.should be_false + end end -end -describe Puppet::Type::Schedule, "when matching weekly by distance" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching hourly by distance" do + include ScheduleTesting - before do - @schedule[:period] = :weekly - @schedule[:periodmatch] = :distance - end + before do + @schedule[:period] = :hourly + @schedule[:periodmatch] = :distance + end - it "should match seven days ago" do - @schedule.match?(day("-", 7)).should be_true - end + it "should match an hour ago" do + @schedule.match?(hour("-", 1)).should be_true + end - it "should not match now" do - @schedule.match?(Time.now).should be_false - end + it "should not match now" do + @schedule.match?(Time.now).should be_false + end - it "should not match six days ago" do - @schedule.match?(day("-", 6)).should be_false + it "should not match 59 minutes ago" do + @schedule.match?(min("-", 59)).should be_false + end end -end -describe Puppet::Type::Schedule, "when matching monthly by distance" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching daily by distance" do + include ScheduleTesting - before do - @schedule[:period] = :monthly - @schedule[:periodmatch] = :distance - end + before do + @schedule[:period] = :daily + @schedule[:periodmatch] = :distance + end - it "should match 32 days ago" do - @schedule.match?(day("-", 32)).should be_true - end + it "should match when the previous time was one day ago" do + @schedule.match?(day("-", 1)).should be_true + end - it "should not match now" do - @schedule.match?(Time.now).should be_false - end + it "should not match when the previous time is now" do + @schedule.match?(Time.now).should be_false + end - it "should not match 27 days ago" do - @schedule.match?(day("-", 27)).should be_false + it "should not match when the previous time was 23 hours ago" do + @schedule.match?(hour("-", 23)).should be_false + end end -end -describe Puppet::Type::Schedule, "when matching hourly by number" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching weekly by distance" do + include ScheduleTesting - before do - @schedule[:period] = :hourly - @schedule[:periodmatch] = :number - end + before do + @schedule[:period] = :weekly + @schedule[:periodmatch] = :distance + end - it "should match if the times are one minute apart and the current minute is 0" do - current = Time.now + it "should match seven days ago" do + @schedule.match?(day("-", 7)).should be_true + end - # Subtract an hour, reset the minute to zero, then add 59 minutes, so we're the previous hour plus 59 minutes. - previous = (current - 3600 - (current.min * 60) + (59 * 60)) + it "should not match now" do + @schedule.match?(Time.now).should be_false + end - # Now set the "current" time to the zero minute of the current hour. - now = (current - (current.min * 60)) - Time.stubs(:now).returns(now) - @schedule.match?(previous).should be_true + it "should not match six days ago" do + @schedule.match?(day("-", 6)).should be_false + end end - it "should not match if the times are 58 minutes apart and the current minute is 59" do - current = Time.now + describe Puppet::Type::Schedule, "when matching monthly by distance" do + include ScheduleTesting - # reset the minute to zero - previous = current - (current.min * 60) + before do + @schedule[:period] = :monthly + @schedule[:periodmatch] = :distance + end - # Now set the "current" time to the 59th minute of the current hour. - now = (current - (current.min * 60) + (59 * 60)) - Time.stubs(:now).returns(now) - @schedule.match?(previous).should be_false - end -end + it "should match 32 days ago" do + @schedule.match?(day("-", 32)).should be_true + end -describe Puppet::Type::Schedule, "when matching daily by number" do - include ScheduleTesting + it "should not match now" do + @schedule.match?(Time.now).should be_false + end - before do - @schedule[:period] = :daily - @schedule[:periodmatch] = :number + it "should not match 27 days ago" do + @schedule.match?(day("-", 27)).should be_false + end end - it "should match if the times are one minute apart and the current minute and hour are 0" do - zero = Time.now + describe Puppet::Type::Schedule, "when matching hourly by number" do + include ScheduleTesting - # Reset the current time to X:00:00 - current = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec + before do + @schedule[:period] = :hourly + @schedule[:periodmatch] = :number + end - # Now set the previous time to one minute before that - previous = current - 60 + it "should match if the times are one minute apart and the current minute is 0" do + current = Time.now - Time.stubs(:now).returns(current) - @schedule.match?(previous).should be_true - end + # Subtract an hour, reset the minute to zero, then add 59 minutes, so we're the previous hour plus 59 minutes. + previous = (current - 3600 - (current.min * 60) + (59 * 60)) - it "should not match if the times are 23 hours and 58 minutes apart and the current hour is 23 and the current minute is 59" do - zero = Time.now + # Now set the "current" time to the zero minute of the current hour. + now = (current - (current.min * 60)) + Time.stubs(:now).returns(now) + @schedule.match?(previous).should be_true + end - # Reset the previous time to 00:00:00 - previous = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec + it "should not match if the times are 58 minutes apart and the current minute is 59" do + current = Time.now - # Set the current time to 23:59 - now = previous + (23 * 3600) + (59 * 60) + # reset the minute to zero + previous = current - (current.min * 60) - Time.stubs(:now).returns(now) - @schedule.match?(previous).should be_false + # Now set the "current" time to the 59th minute of the current hour. + now = (current - (current.min * 60) + (59 * 60)) + Time.stubs(:now).returns(now) + @schedule.match?(previous).should be_false + end end -end -describe Puppet::Type::Schedule, "when matching weekly by number" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching daily by number" do + include ScheduleTesting - before do - @schedule[:period] = :weekly - @schedule[:periodmatch] = :number - end + before do + @schedule[:period] = :daily + @schedule[:periodmatch] = :number + end - it "should match if the previous time is prior to the most recent Sunday" do - now = Time.now + it "should match if the times are one minute apart and the current minute and hour are 0" do + zero = Time.now - # Subtract the number days we've progressed into the week, plus one because we're zero-indexed. - previous = now - (3600 * 24 * (now.wday + 1)) + # Reset the current time to X:00:00 + current = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec - @schedule.match?(previous).should be_true - end + # Now set the previous time to one minute before that + previous = current - 60 - it "should not match if the previous time is after the most recent Saturday" do - now = Time.now + Time.stubs(:now).returns(current) + @schedule.match?(previous).should be_true + end - # Subtract the number days we've progressed into the week - previous = now - (3600 * 24 * now.wday) + it "should not match if the times are 23 hours and 58 minutes apart and the current hour is 23 and the current minute is 59" do + zero = Time.now - @schedule.match?(previous).should be_false - end -end + # Reset the previous time to 00:00:00 + previous = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec -describe Puppet::Type::Schedule, "when matching monthly by number" do - include ScheduleTesting + # Set the current time to 23:59 + now = previous + (23 * 3600) + (59 * 60) - before do - @schedule[:period] = :monthly - @schedule[:periodmatch] = :number + Time.stubs(:now).returns(now) + @schedule.match?(previous).should be_false + end end - it "should match when the previous time is prior to the first day of this month" do - now = Time.now + describe Puppet::Type::Schedule, "when matching weekly by number" do + include ScheduleTesting - # Subtract the number days we've progressed into the month - previous = now - (3600 * 24 * now.day) + before do + @schedule[:period] = :weekly + @schedule[:periodmatch] = :number + end - @schedule.match?(previous).should be_true - end + it "should match if the previous time is prior to the most recent Sunday" do + now = Time.now - it "should not match when the previous time is after the last day of last month" do - now = Time.now + # Subtract the number days we've progressed into the week, plus one because we're zero-indexed. + previous = now - (3600 * 24 * (now.wday + 1)) - # Subtract the number days we've progressed into the month, minus one - previous = now - (3600 * 24 * (now.day - 1)) + @schedule.match?(previous).should be_true + end - @schedule.match?(previous).should be_false - end -end + it "should not match if the previous time is after the most recent Saturday" do + now = Time.now -describe Puppet::Type::Schedule, "when matching with a repeat greater than one" do - include ScheduleTesting + # Subtract the number days we've progressed into the week + previous = now - (3600 * 24 * now.wday) - before do - @schedule[:period] = :daily - @schedule[:repeat] = 2 + @schedule.match?(previous).should be_false + end end - it "should fail if the periodmatch is 'number'" do - @schedule[:periodmatch] = :number - proc { @schedule[:repeat] = 2 }.should raise_error(Puppet::Error) - end + describe Puppet::Type::Schedule, "when matching monthly by number" do + include ScheduleTesting - it "should match if the previous run was further away than the distance divided by the repeat" do - previous = Time.now - (3600 * 13) - @schedule.match?(previous).should be_true - end + before do + @schedule[:period] = :monthly + @schedule[:periodmatch] = :number + end - it "should not match if the previous run was closer than the distance divided by the repeat" do - previous = Time.now - (3600 * 11) - @schedule.match?(previous).should be_false - end -end + it "should match when the previous time is prior to the first day of this month" do + now = Time.now -module OldTesting - def mksched - @stype.create(:name => "testsched") - end + # Subtract the number days we've progressed into the month + previous = now - (3600 * 24 * now.day) + + @schedule.match?(previous).should be_true + end - def test_period_with_repeat - previous = @now + it "should not match when the previous time is after the last day of last month" do + now = Time.now - s = mksched - s[:period] = :hourly + # Subtract the number days we've progressed into the month, minus one + previous = now - (3600 * 24 * (now.day - 1)) - assert_nothing_raised("Was not able to set periodmatch") { - s[:periodmatch] = :number - } - assert_raise(Puppet::Error) { - s[:repeat] = 2 - } - assert_nothing_raised("Was not able to reset periodmatch") { - s[:periodmatch] = :distance - } + @schedule.match?(previous).should be_false + end + end - assert(! s.match?(min("-", 40)), "matched minus 40 minutes") + describe Puppet::Type::Schedule, "when matching with a repeat greater than one" do + include ScheduleTesting - assert_nothing_raised("Was not able to set period") { - s[:repeat] = 2 - } + before do + @schedule[:period] = :daily + @schedule[:repeat] = 2 + end - assert(! s.match?(min("-", 20)), "matched minus 20 minutes with half-hourly") - assert(s.match?(min("-", 40)), "Did not match minus 40 with half-hourly") + it "should fail if the periodmatch is 'number'" do + @schedule[:periodmatch] = :number + proc { @schedule[:repeat] = 2 }.should raise_error(Puppet::Error) + end - assert_nothing_raised("Was not able to set period") { - s[:repeat] = 3 - } + it "should match if the previous run was further away than the distance divided by the repeat" do + previous = Time.now - (3600 * 13) + @schedule.match?(previous).should be_true + end - assert(! s.match?(min("-", 15)), "matched minus 15 minutes with half-hourly") - assert(s.match?(min("-", 25)), "Did not match minus 25 with half-hourly") + it "should not match if the previous run was closer than the distance divided by the repeat" do + previous = Time.now - (3600 * 11) + @schedule.match?(previous).should be_false + end end end