Several schedule spec tests made assumptions about the system clock,
which resulted in intermittent failures.

Also found some issues with schedule ranges, filed it as bug #7369 and
created a pending schedule test for it.

Reviewed-by: Jacob Helwig <[email protected]>
Signed-off-by: Josh Cooper <[email protected]>
---
 spec/unit/type/schedule_spec.rb |  115 +++++++++++++++++++++++++--------------
 1 files changed, 73 insertions(+), 42 deletions(-)

diff --git a/spec/unit/type/schedule_spec.rb b/spec/unit/type/schedule_spec.rb
index 08ec70c..b302d95 100755
--- a/spec/unit/type/schedule_spec.rb
+++ b/spec/unit/type/schedule_spec.rb
@@ -3,10 +3,6 @@ require 'spec_helper'
 
 module ScheduleTesting
 
-  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)
@@ -54,7 +50,7 @@ describe Puppet::Type.type(:schedule) do
 
     it "should never match when the period is :never" do
       @schedule[:period] = :never
-      @schedule.match?.should be_false
+      @schedule.should_not be_match
     end
   end
 
@@ -80,19 +76,44 @@ describe Puppet::Type.type(:schedule) do
   describe Puppet::Type.type(:schedule), "when matching ranges" do
     include ScheduleTesting
 
+    before do
+      Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
+    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] = "#{format(Time.now - 10)} - #{format(Time.now + 10)}"
-      @schedule.match?.should be_true
+      @schedule[:range] = "10:59:50 - 11:00:10"
+      @schedule.should be_match
     end
 
     it "should not match when the start time is after the current time" do
-      @schedule[:range] = "#{format(Time.now + 5)} - #{format(Time.now + 10)}"
-      @schedule.match?.should be_false
+      @schedule[:range] = "11:00:05 - 11:00:10"
+      @schedule.should_not be_match
     end
 
     it "should not match when the end time is previous to the current time" do
-      @schedule[:range] = "#{format(Time.now - 10)} - #{format(Time.now - 5)}"
-      @schedule.match?.should be_false
+      @schedule[:range] = "10:59:50 - 10:59:55"
+      @schedule.should be_match
+    end
+
+    it "should throw an error if the upper limit is less than the lower limit" 
do
+      pending "bug #7639"
+      @schedule[:range] = "01:02:03 - 01:00:00"
+      @schedule.should_throw Puppet::Error
+    end
+
+    it "should not match the current time fails between an array of ranges" do
+      @schedule[:range] = ["4-6", "20-23"]
+      @schedule.should_not be_match
+    end
+
+    it "should match the lower array of ranges" do
+      @schedule[:range] = ["9-11", "14-16"]
+      @schedule.should be_match
+    end
+
+    it "should match the upper array of ranges" do
+      @schedule[:range] = ["4-6", "11-12"]
+      @schedule.should be_match
     end
   end
 
@@ -102,18 +123,20 @@ describe Puppet::Type.type(:schedule) do
     before do
       @schedule[:period] = :hourly
       @schedule[:periodmatch] = :distance
+
+      Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
     end
 
-    it "should match an hour ago" do
-      @schedule.match?(hour("-", 1)).should be_true
+    it "should match when the previous time was an hour ago" do
+      @schedule.should be_match(hour("-", 1))
     end
 
-    it "should not match now" do
-      @schedule.match?(Time.now).should be_false
+    it "should not match when the previous time was now" do
+      @schedule.should_not be_match(Time.now)
     end
 
-    it "should not match 59 minutes ago" do
-      @schedule.match?(min("-", 59)).should be_false
+    it "should not match when the previous time was 59 minutes ago" do
+      @schedule.should_not be_match(min("-", 59))
     end
   end
 
@@ -123,18 +146,20 @@ describe Puppet::Type.type(:schedule) do
     before do
       @schedule[:period] = :daily
       @schedule[:periodmatch] = :distance
+
+      Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
     end
 
     it "should match when the previous time was one day ago" do
-      @schedule.match?(day("-", 1)).should be_true
+      @schedule.should be_match(day("-", 1))
     end
 
     it "should not match when the previous time is now" do
-      @schedule.match?(Time.now).should be_false
+      @schedule.should_not be_match(Time.now)
     end
 
     it "should not match when the previous time was 23 hours ago" do
-      @schedule.match?(hour("-", 23)).should be_false
+      @schedule.should_not be_match(hour("-", 23))
     end
   end
 
@@ -144,18 +169,20 @@ describe Puppet::Type.type(:schedule) do
     before do
       @schedule[:period] = :weekly
       @schedule[:periodmatch] = :distance
+
+      Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
     end
 
-    it "should match seven days ago" do
-      @schedule.match?(day("-", 7)).should be_true
+    it "should match when the previous time was seven days ago" do
+      @schedule.should be_match(day("-", 7))
     end
 
-    it "should not match now" do
-      @schedule.match?(Time.now).should be_false
+    it "should not match when the previous time was now" do
+      @schedule.should be_match(Time.now)
     end
 
-    it "should not match six days ago" do
-      @schedule.match?(day("-", 6)).should be_false
+    it "should not match when the previous time was six days ago" do
+      @schedule.should_not be_match(day("-", 6))
     end
   end
 
@@ -165,18 +192,20 @@ describe Puppet::Type.type(:schedule) do
     before do
       @schedule[:period] = :monthly
       @schedule[:periodmatch] = :distance
+
+      Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
     end
 
-    it "should match 32 days ago" do
-      @schedule.match?(day("-", 32)).should be_true
+    it "should match when the previous time was 32 days ago" do
+      @schedule.should be_match(day("-", 32))
     end
 
-    it "should not match now" do
-      @schedule.match?(Time.now).should be_false
+    it "should not match when the previous time was now" do
+      @schedule.should_not be_match(Time.now)
     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 27 days ago" do
+      @schedule.should_not be_match(day("-", 27))
     end
   end
 
@@ -193,7 +222,7 @@ describe Puppet::Type.type(:schedule) do
       previous = Time.utc(2007, 12, 31, 23, 59, 0)
 
       Time.stubs(:now).returns(current)
-      @schedule.match?(previous).should be_true
+      @schedule.should be_match(previous)
     end
 
     it "should not match if the times are 59 minutes apart and the current 
minute is 59" do
@@ -201,7 +230,7 @@ describe Puppet::Type.type(:schedule) do
       previous = Time.utc(2009, 2, 1, 12, 0, 0)
 
       Time.stubs(:now).returns(current)
-      @schedule.match?(previous).should be_false
+      @schedule.should_not be_match(previous)
     end
   end
 
@@ -220,7 +249,7 @@ describe Puppet::Type.type(:schedule) do
       previous = current - 60
 
       Time.stubs(:now).returns(current)
-      @schedule.match?(previous).should be_true
+      @schedule.should be_match(previous)
     end
 
     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
@@ -232,7 +261,7 @@ describe Puppet::Type.type(:schedule) do
       now = previous + (23 * 3600) + (59 * 60)
 
       Time.stubs(:now).returns(now)
-      @schedule.match?(previous).should be_false
+      @schedule.should_not be_match(previous)
     end
   end
 
@@ -249,7 +278,7 @@ describe Puppet::Type.type(:schedule) do
       Time.stubs(:now).returns(now)
       previous = Time.utc(2010, "nov", 6, 23, 59, 59) # Sat
 
-      @schedule.match?(previous).should be_true
+      @schedule.should be_match(previous)
     end
 
     it "should not match if the previous time is after the most recent 
Saturday" do
@@ -257,7 +286,7 @@ describe Puppet::Type.type(:schedule) do
       Time.stubs(:now).returns(now)
       previous = Time.utc(2010, "nov", 7, 0, 0, 0) # Sunday
 
-      @schedule.match?(previous).should be_false
+      @schedule.should_not be_match(previous)
     end
   end
 
@@ -274,7 +303,7 @@ describe Puppet::Type.type(:schedule) do
       Time.stubs(:now).returns(now)
       previous = Time.utc(2010, "oct", 31, 23, 59, 59)
 
-      @schedule.match?(previous).should be_true
+      @schedule.should be_match(previous)
     end
 
     it "should not match when the previous time is after the last day of last 
month" do
@@ -282,7 +311,7 @@ describe Puppet::Type.type(:schedule) do
       Time.stubs(:now).returns(now)
       previous = Time.utc(2010, "nov", 1, 0, 0, 0)
 
-      @schedule.match?(previous).should be_false
+      @schedule.should_not be_match(previous)
     end
   end
 
@@ -292,6 +321,8 @@ describe Puppet::Type.type(:schedule) do
     before do
       @schedule[:period] = :daily
       @schedule[:repeat] = 2
+
+      Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
     end
 
     it "should fail if the periodmatch is 'number'" do
@@ -301,12 +332,12 @@ describe Puppet::Type.type(:schedule) do
 
     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
+      @schedule.should be_match(previous)
     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
+      @schedule.should_not be_match(previous)
     end
   end
 end
-- 
1.7.4.4

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to