3
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
6
require 'puppet/type/schedule'
9
class TestSchedule < Test::Unit::TestCase
14
@stype = Puppet::Type::Schedule
16
# This will probably get overridden by different tests
18
Puppet[:ignoreschedules] = false
23
assert_nothing_raised {
32
def diff(unit, incr, method, count)
33
diff = @now.to_i.send(method, incr * count)
36
#Puppet.notice "%s: %s %s %s = %s" %
37
# [unit, @now.send(unit), method, count, t]
38
#t.strftime("%H:%M:%S")
42
def month(method, count)
43
diff(:hour, 3600 * 24 * 30, method, count)
46
def week(method, count)
47
diff(:hour, 3600 * 24 * 7, method, count)
50
def day(method, count)
51
diff(:hour, 3600 * 24, method, count)
54
def hour(method, count)
55
diff(:hour, 3600, method, count)
58
def min(method, count)
59
diff(:min, 60, method, count)
62
def sec(method, count)
63
diff(:sec, 1, method, count)
67
unless defined? @@times
70
# Make one with an edge year on each side
72
[1999, 2000, 2001].each { |y|
73
ary[5] = y; @@times << Time.local(*ary)
78
#[23, 0].each { |h| ary[2] = h; @@times << Time.local(*ary) }
81
@@times << Time.local(*ary)
84
@@times << addday(Time.local(*ary))
86
# And with edge minutes
87
#[59, 0].each { |m| ary[1] = m; @@times << Time.local(*ary) }
89
ary[1] = 59; @@times << Time.local(*ary)
92
@@times << Time.local(*ary)
94
# @@times << addday(Time.local(*ary))
98
Puppet.err @@times.inspect
100
@@times.each { |time|
113
@now = Time.local(*ary)
116
# An hour previous, an hour after
117
[hour("-", 1), hour("+", 1)],
119
# an hour previous but a couple minutes later, and an hour plus
120
[min("-", 57), hour("+", 1)]
123
# five minutes from now, an hour from now
124
[min("+", 5), hour("+", 1)],
126
# an hour ago, 20 minutes ago
127
[hour("-", 1), min("-", 20)]
131
data.each { |result, values|
132
values = values.collect { |value|
133
"%s - %s" % [value[0].strftime("%H:%M:%S"),
134
value[1].strftime("%H:%M:%S")]
136
values.each { |value|
137
assert_nothing_raised("Could not parse %s" % value) {
141
assert_equal(result, s.match?(nil, @now),
142
"%s matched %s incorrectly" % [value.inspect, @now])
145
assert_nothing_raised("Could not parse %s" % [values]) {
149
assert_equal(result, s.match?(nil, @now),
150
"%s matched %s incorrectly" % [values.inspect, @now])
154
def test_period_by_distance
159
assert_nothing_raised {
163
assert(s.match?(day("-", 1)), "did not match minus a day")
164
assert(s.match?(day("-", 2)), "did not match two days")
165
assert(! s.match?(@now), "matched today")
166
assert(! s.match?(hour("-", 11)), "matched minus 11 hours")
169
assert_nothing_raised {
173
assert(s.match?(hour("-", 1)), "did not match minus an hour")
174
assert(s.match?(hour("-", 2)), "did not match two hours")
175
assert(! s.match?(@now), "matched now")
176
assert(! s.match?(min("-", 59)), "matched minus 11 hours")
179
assert_nothing_raised {
183
assert(s.match?(week("-", 1)), "did not match minus a week")
184
assert(s.match?(day("-", 7)), "did not match minus 7 days")
185
assert(s.match?(day("-", 8)), "did not match minus 8 days")
186
assert(s.match?(week("-", 2)), "did not match two weeks")
187
assert(! s.match?(@now), "matched now")
188
assert(! s.match?(day("-", 6)), "matched minus 6 days")
191
assert_nothing_raised {
192
s[:period] = :monthly
195
assert(s.match?(month("-", 1)), "did not match minus a month")
196
assert(s.match?(week("-", 5)), "did not match minus 5 weeks")
197
assert(s.match?(week("-", 7)), "did not match minus 7 weeks")
198
assert(s.match?(day("-", 50)), "did not match minus 50 days")
199
assert(s.match?(month("-", 2)), "did not match two months")
200
assert(! s.match?(@now), "matched now")
201
assert(! s.match?(week("-", 3)), "matched minus 3 weeks")
202
assert(! s.match?(day("-", 20)), "matched minus 20 days")
205
# A shortened test...
206
def test_period_by_number
208
assert_nothing_raised {
209
s[:periodmatch] = :number
212
assert_nothing_raised {
216
assert(s.match?(day("+", 1)), "didn't match plus a day")
217
assert(s.match?(week("+", 1)), "didn't match plus a week")
218
assert(! s.match?(@now), "matched today")
219
assert(! s.match?(hour("-", 1)), "matched minus an hour")
220
assert(! s.match?(hour("-", 2)), "matched plus two hours")
223
assert_nothing_raised {
227
assert(s.match?(hour("+", 1)), "did not match plus an hour")
228
assert(s.match?(hour("+", 2)), "did not match plus two hours")
229
assert(! s.match?(@now), "matched now")
230
assert(! s.match?(sec("+", 20)), "matched plus 20 seconds")
233
def test_periodmatch_default
236
match = s[:periodmatch]
237
assert(match, "Could not find periodmatch")
239
assert_equal(:distance, match, "Periodmatch was %s" % match)
242
def test_scheduled_objects
248
assert_nothing_raised {
249
f = Puppet.type(:file).create(
256
assert(f.scheduled?, "File is not scheduled to run")
260
assert(! f.scheduled?, "File is scheduled to run already")
265
assert(! FileTest.exists?(path), "File was created when not scheduled")
268
def test_latebinding_schedules
271
assert_nothing_raised {
272
f = Puppet.type(:file).create(
274
:schedule => "testsched",
282
assert_nothing_raised {
286
assert(f.scheduled?, "File is not scheduled to run")
289
# Verify that each of our default schedules exist
290
def test_defaultschedules
291
Puppet.type(:schedule).mkdefaultschedules
292
%w{puppet hourly daily weekly monthly}.each { |period|
293
assert(Puppet.type(:schedule)[period], "Could not find %s schedule" %
298
def test_period_with_repeat
304
assert_nothing_raised("Was not able to set periodmatch") {
305
s[:periodmatch] = :number
307
assert_raise(Puppet::Error) {
310
assert_nothing_raised("Was not able to reset periodmatch") {
311
s[:periodmatch] = :distance
314
assert(! s.match?(min("-", 40)), "matched minus 40 minutes")
316
assert_nothing_raised("Was not able to set period") {
320
assert(! s.match?(min("-", 20)), "matched minus 20 minutes with half-hourly")
321
assert(s.match?(min("-", 40)), "Did not match minus 40 with half-hourly")
323
assert_nothing_raised("Was not able to set period") {
327
assert(! s.match?(min("-", 15)), "matched minus 15 minutes with half-hourly")
328
assert(s.match?(min("-", 25)), "Did not match minus 25 with half-hourly")
332
# $Id: schedule.rb 1793 2006-10-16 22:01:40Z luke $