~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to test/types/schedule.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
4
 
 
5
 
require 'puppet'
6
 
require 'puppet/type/schedule'
7
 
require 'puppettest'
8
 
 
9
 
class TestSchedule < Test::Unit::TestCase
10
 
    include PuppetTest
11
 
 
12
 
    def setup
13
 
        super
14
 
        @stype = Puppet::Type::Schedule
15
 
 
16
 
        # This will probably get overridden by different tests
17
 
        @now = Time.now
18
 
        Puppet[:ignoreschedules] = false
19
 
    end
20
 
 
21
 
    def mksched
22
 
        s = nil
23
 
        assert_nothing_raised {
24
 
            s = @stype.create(
25
 
                :name => "testsched"
26
 
            )
27
 
        }
28
 
 
29
 
        s
30
 
    end
31
 
 
32
 
    def diff(unit, incr, method, count)
33
 
        diff = @now.to_i.send(method, incr * count)
34
 
        t = Time.at(diff)
35
 
 
36
 
        #Puppet.notice "%s: %s %s %s = %s" %
37
 
        #    [unit, @now.send(unit), method, count, t]
38
 
        #t.strftime("%H:%M:%S")
39
 
        t
40
 
    end
41
 
 
42
 
    def month(method, count)
43
 
        diff(:hour, 3600 * 24 * 30, method, count)
44
 
    end
45
 
 
46
 
    def week(method, count)
47
 
        diff(:hour, 3600 * 24 * 7, method, count)
48
 
    end
49
 
 
50
 
    def day(method, count)
51
 
        diff(:hour, 3600 * 24, method, count)
52
 
    end
53
 
 
54
 
    def hour(method, count)
55
 
        diff(:hour, 3600, method, count)
56
 
    end
57
 
 
58
 
    def min(method, count)
59
 
        diff(:min, 60, method, count)
60
 
    end
61
 
 
62
 
    def sec(method, count)
63
 
        diff(:sec, 1, method, count)
64
 
    end
65
 
 
66
 
    def settimes
67
 
        unless defined? @@times
68
 
            @@times = [Time.now]
69
 
 
70
 
            # Make one with an edge year on each side
71
 
            ary = Time.now.to_a
72
 
            [1999, 2000, 2001].each { |y|
73
 
                ary[5] = y; @@times << Time.local(*ary)
74
 
            }
75
 
 
76
 
            # And with edge hours
77
 
            ary = Time.now.to_a
78
 
            #[23, 0].each { |h| ary[2] = h; @@times << Time.local(*ary) }
79
 
            # 23 hour
80
 
            ary[2] = 23
81
 
            @@times << Time.local(*ary)
82
 
            # 0 hour, next day
83
 
            ary[2] = 0
84
 
            @@times << addday(Time.local(*ary))
85
 
 
86
 
            # And with edge minutes
87
 
            #[59, 0].each { |m| ary[1] = m; @@times << Time.local(*ary) }
88
 
            ary = Time.now.to_a
89
 
            ary[1] = 59; @@times << Time.local(*ary)
90
 
            ary[1] = 0
91
 
            #if ary[2] == 23
92
 
                @@times << Time.local(*ary)
93
 
            #else
94
 
            #    @@times << addday(Time.local(*ary))
95
 
            #end
96
 
        end
97
 
 
98
 
        Puppet.err @@times.inspect
99
 
 
100
 
        @@times.each { |time|
101
 
            @now = time
102
 
            yield time
103
 
        }
104
 
 
105
 
        @now = Time.now
106
 
    end
107
 
 
108
 
    def test_range
109
 
        s = mksched
110
 
 
111
 
        ary = @now.to_a
112
 
        ary[2] = 12
113
 
        @now = Time.local(*ary)
114
 
        data = {
115
 
            true => [
116
 
                # An hour previous, an hour after
117
 
                [hour("-", 1), hour("+", 1)],
118
 
 
119
 
                # an hour previous but a couple minutes later, and an hour plus
120
 
                [min("-", 57), hour("+", 1)]
121
 
            ],
122
 
            false => [
123
 
                # five minutes from now, an hour from now
124
 
                [min("+", 5), hour("+", 1)],
125
 
 
126
 
                # an hour ago, 20 minutes ago
127
 
                [hour("-", 1), min("-", 20)]
128
 
            ]
129
 
        }
130
 
 
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")]
135
 
            }
136
 
            values.each { |value|
137
 
                assert_nothing_raised("Could not parse %s" % value) {
138
 
                    s[:range] = value
139
 
                }
140
 
 
141
 
                assert_equal(result, s.match?(nil, @now),
142
 
                    "%s matched %s incorrectly" % [value.inspect, @now])
143
 
            }
144
 
 
145
 
            assert_nothing_raised("Could not parse %s" % [values]) {
146
 
                s[:range] = values
147
 
            }
148
 
 
149
 
            assert_equal(result, s.match?(nil, @now),
150
 
                "%s matched %s incorrectly" % [values.inspect, @now])
151
 
        }
152
 
    end
153
 
 
154
 
    def test_period_by_distance
155
 
        previous = @now
156
 
 
157
 
        s = mksched
158
 
 
159
 
        assert_nothing_raised {
160
 
            s[:period] = :daily
161
 
        }
162
 
 
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")
167
 
 
168
 
        # Now test hourly
169
 
        assert_nothing_raised {
170
 
            s[:period] = :hourly
171
 
        }
172
 
 
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")
177
 
 
178
 
        # and weekly
179
 
        assert_nothing_raised {
180
 
            s[:period] = :weekly
181
 
        }
182
 
 
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")
189
 
 
190
 
        # and monthly
191
 
        assert_nothing_raised {
192
 
            s[:period] = :monthly
193
 
        }
194
 
 
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")
203
 
    end
204
 
 
205
 
    # A shortened test...
206
 
    def test_period_by_number
207
 
        s = mksched
208
 
        assert_nothing_raised {
209
 
            s[:periodmatch] = :number
210
 
        }
211
 
 
212
 
        assert_nothing_raised {
213
 
            s[:period] = :daily
214
 
        }
215
 
 
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")
221
 
 
222
 
        # Now test hourly
223
 
        assert_nothing_raised {
224
 
            s[:period] = :hourly
225
 
        }
226
 
 
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")
231
 
    end
232
 
 
233
 
    def test_periodmatch_default
234
 
        s = mksched
235
 
 
236
 
        match = s[:periodmatch]
237
 
        assert(match, "Could not find periodmatch")
238
 
 
239
 
        assert_equal(:distance, match, "Periodmatch was %s" % match)
240
 
    end
241
 
 
242
 
    def test_scheduled_objects
243
 
        s = mksched
244
 
        s[:period] = :hourly
245
 
 
246
 
        f = nil
247
 
        path = tempfile()
248
 
        assert_nothing_raised {
249
 
            f = Puppet.type(:file).create(
250
 
                :name => path,
251
 
                :schedule => s.name,
252
 
                :ensure => "file"
253
 
            )
254
 
        }
255
 
 
256
 
        assert(f.scheduled?, "File is not scheduled to run")
257
 
 
258
 
        assert_apply(f)
259
 
 
260
 
        assert(! f.scheduled?, "File is scheduled to run already")
261
 
        File.unlink(path)
262
 
 
263
 
        assert_apply(f)
264
 
 
265
 
        assert(! FileTest.exists?(path), "File was created when not scheduled")
266
 
    end
267
 
 
268
 
    def test_latebinding_schedules
269
 
        f = nil
270
 
        path = tempfile()
271
 
        assert_nothing_raised {
272
 
            f = Puppet.type(:file).create(
273
 
                :name => path,
274
 
                :schedule => "testsched",
275
 
                :ensure => "file"
276
 
            )
277
 
        }
278
 
 
279
 
        s = mksched
280
 
        s[:period] = :hourly
281
 
 
282
 
        assert_nothing_raised {
283
 
            f.schedule
284
 
        }
285
 
 
286
 
        assert(f.scheduled?, "File is not scheduled to run")
287
 
    end
288
 
 
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" %
294
 
                period)
295
 
        }
296
 
    end
297
 
 
298
 
    def test_period_with_repeat
299
 
        previous = @now
300
 
 
301
 
        s = mksched
302
 
        s[:period] = :hourly
303
 
 
304
 
        assert_nothing_raised("Was not able to set periodmatch") {
305
 
            s[:periodmatch] = :number
306
 
        }
307
 
        assert_raise(Puppet::Error) {
308
 
            s[:repeat] = 2
309
 
        }
310
 
        assert_nothing_raised("Was not able to reset periodmatch") {
311
 
            s[:periodmatch] = :distance
312
 
        }
313
 
 
314
 
        assert(! s.match?(min("-", 40)), "matched minus 40 minutes")
315
 
 
316
 
        assert_nothing_raised("Was not able to set period") {
317
 
            s[:repeat] = 2
318
 
        }
319
 
 
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")
322
 
 
323
 
        assert_nothing_raised("Was not able to set period") {
324
 
            s[:repeat] = 3
325
 
        }
326
 
 
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")
329
 
    end
330
 
end
331
 
 
332
 
# $Id: schedule.rb 1793 2006-10-16 22:01:40Z luke $