forked from puppetlabs/puppet
/
schedule_spec.rb
621 lines (495 loc) · 20.2 KB
/
schedule_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#! /usr/bin/env ruby
require 'spec_helper'
module ScheduleTesting
def diff(unit, incr, method, count)
diff = Time.now.to_i.send(method, incr * count)
Time.at(diff)
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
end
describe Puppet::Type.type(:schedule) do
include ScheduleTesting
before :each do
Puppet[:ignoreschedules] = false
@schedule = Puppet::Type.type(:schedule).new(:name => "testing")
end
describe Puppet::Type.type(:schedule) do
it "should apply to device" do
expect(@schedule).to be_appliable_to_device
end
it "should apply to host" do
expect(@schedule).to be_appliable_to_host
end
it "should default to :distance for period-matching" do
expect(@schedule[:periodmatch]).to eq(:distance)
end
it "should default to a :repeat of 1" do
expect(@schedule[:repeat]).to eq(1)
end
it "should never match when the period is :never" do
@schedule[:period] = :never
expect(@schedule).to_not be_match
end
end
describe Puppet::Type.type(:schedule), "when producing default schedules" do
%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.type(:schedule).mkdefaultschedules
expect(schedules.find { |s| s[:name] == period.to_s and s[:period] == period }).to be_instance_of(Puppet::Type.type(:schedule))
end
end
it "should not produce default schedules when default_schedules is false" do
Puppet[:default_schedules] = false
schedules = Puppet::Type.type(:schedule).mkdefaultschedules
expect(schedules).to have_exactly(0).items
end
it "should produce a schedule named puppet with a period of hourly and a repeat of 2" do
schedules = Puppet::Type.type(:schedule).mkdefaultschedules
expect(schedules.find { |s|
s[:name] == "puppet" and s[:period] == :hourly and s[:repeat] == 2
}).to be_instance_of(Puppet::Type.type(:schedule))
end
end
describe Puppet::Type.type(:schedule), "when matching ranges" do
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] = "10:59:50 - 11:00:10"
expect(@schedule).to be_match
end
it "should not match when the start time is after the current time" do
@schedule[:range] = "11:00:05 - 11:00:10"
expect(@schedule).to_not be_match
end
it "should not match when the end time is previous to the current time" do
@schedule[:range] = "10:59:50 - 10:59:55"
expect(@schedule).to_not be_match
end
it "should not match the current time fails between an array of ranges" do
@schedule[:range] = ["4-6", "20-23"]
expect(@schedule).to_not be_match
end
it "should match the lower array of ranges" do
@schedule[:range] = ["9-11", "14-16"]
expect(@schedule).to be_match
end
it "should match the upper array of ranges" do
@schedule[:range] = ["11:30 - 6", "11-12"]
expect(@schedule).to be_match
end
end
describe Puppet::Type.type(:schedule), "when matching ranges with abbreviated time specifications" do
before do
Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 45, 59))
end
it "should match when just an hour is specified" do
@schedule[:range] = "11-12"
expect(@schedule).to be_match
end
it "should not match when the ending hour is the current hour" do
@schedule[:range] = "10-11"
expect(@schedule).to_not be_match
end
it "should not match when the ending minute is the current minute" do
@schedule[:range] = "10:00 - 11:45"
expect(@schedule).to_not be_match
end
end
describe Puppet::Type.type(:schedule), "when matching ranges with abbreviated time specifications, edge cases part 1" do
before do
Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 00, 00))
end
it "should match when the current time is the start of the range using hours" do
@schedule[:range] = "11 - 12"
expect(@schedule).to be_match
end
it "should match when the current time is the end of the range using hours" do
@schedule[:range] = "10 - 11"
expect(@schedule).to be_match
end
it "should match when the current time is the start of the range using hours and minutes" do
@schedule[:range] = "11:00 - 12:00"
expect(@schedule).to be_match
end
it "should match when the current time is the end of the range using hours and minutes" do
@schedule[:range] = "10:00 - 11:00"
expect(@schedule).to be_match
end
end
describe Puppet::Type.type(:schedule), "when matching ranges with abbreviated time specifications, edge cases part 2" do
before do
Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 00, 01))
end
it "should match when the current time is just past the start of the range using hours" do
@schedule[:range] = "11 - 12"
expect(@schedule).to be_match
end
it "should not match when the current time is just past the end of the range using hours" do
@schedule[:range] = "10 - 11"
expect(@schedule).to_not be_match
end
it "should match when the current time is just past the start of the range using hours and minutes" do
@schedule[:range] = "11:00 - 12:00"
expect(@schedule).to be_match
end
it "should not match when the current time is just past the end of the range using hours and minutes" do
@schedule[:range] = "10:00 - 11:00"
expect(@schedule).to_not be_match
end
end
describe Puppet::Type.type(:schedule), "when matching ranges with abbreviated time specifications, edge cases part 3" do
before do
Time.stubs(:now).returns(Time.local(2011, "may", 23, 10, 59, 59))
end
it "should not match when the current time is just before the start of the range using hours" do
@schedule[:range] = "11 - 12"
expect(@schedule).to_not be_match
end
it "should match when the current time is just before the end of the range using hours" do
@schedule[:range] = "10 - 11"
expect(@schedule).to be_match
end
it "should not match when the current time is just before the start of the range using hours and minutes" do
@schedule[:range] = "11:00 - 12:00"
expect(@schedule).to_not be_match
end
it "should match when the current time is just before the end of the range using hours and minutes" do
@schedule[:range] = "10:00 - 11:00"
expect(@schedule).to be_match
end
end
describe Puppet::Type.type(:schedule), "when matching ranges spanning days, day 1" do
before do
# Test with the current time at a month's end boundary to ensure we are
# advancing the day properly when we push the ending limit out a day.
# For example, adding 1 to 31 would throw an error instead of advancing
# the date.
Time.stubs(:now).returns(Time.local(2011, "mar", 31, 22, 30, 0))
end
it "should match when the start time is before current time and the end time is the following day" do
@schedule[:range] = "22:00:00 - 02:00:00"
expect(@schedule).to be_match
end
it "should not match when the current time is outside the range" do
@schedule[:range] = "23:30:00 - 21:00:00"
expect(@schedule).to_not be_match
end
end
describe Puppet::Type.type(:schedule), "when matching ranges spanning days, day 2" do
before do
# Test with the current time at a month's end boundary to ensure we are
# advancing the day properly when we push the ending limit out a day.
# For example, adding 1 to 31 would throw an error instead of advancing
# the date.
Time.stubs(:now).returns(Time.local(2011, "mar", 31, 1, 30, 0))
end
it "should match when the start time is the day before the current time and the end time is after the current time" do
@schedule[:range] = "22:00:00 - 02:00:00"
expect(@schedule).to be_match
end
it "should not match when the start time is after the current time" do
@schedule[:range] = "02:00:00 - 00:30:00"
expect(@schedule).to_not be_match
end
it "should not match when the end time is before the current time" do
@schedule[:range] = "22:00:00 - 01:00:00"
expect(@schedule).to_not be_match
end
end
describe Puppet::Type.type(:schedule), "when matching hourly by distance" 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 when the previous time was an hour ago" do
expect(@schedule).to be_match(hour("-", 1))
end
it "should not match when the previous time was now" do
expect(@schedule).to_not be_match(Time.now)
end
it "should not match when the previous time was 59 minutes ago" do
expect(@schedule).to_not be_match(min("-", 59))
end
end
describe Puppet::Type.type(:schedule), "when matching daily by distance" 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
expect(@schedule).to be_match(day("-", 1))
end
it "should not match when the previous time is now" do
expect(@schedule).to_not be_match(Time.now)
end
it "should not match when the previous time was 23 hours ago" do
expect(@schedule).to_not be_match(hour("-", 23))
end
end
describe Puppet::Type.type(:schedule), "when matching weekly by distance" 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 when the previous time was seven days ago" do
expect(@schedule).to be_match(day("-", 7))
end
it "should not match when the previous time was now" do
expect(@schedule).to_not be_match(Time.now)
end
it "should not match when the previous time was six days ago" do
expect(@schedule).to_not be_match(day("-", 6))
end
end
describe Puppet::Type.type(:schedule), "when matching monthly by distance" 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 when the previous time was 32 days ago" do
expect(@schedule).to be_match(day("-", 32))
end
it "should not match when the previous time was now" do
expect(@schedule).to_not be_match(Time.now)
end
it "should not match when the previous time was 27 days ago" do
expect(@schedule).to_not be_match(day("-", 27))
end
end
describe Puppet::Type.type(:schedule), "when matching hourly by number" do
before do
@schedule[:period] = :hourly
@schedule[:periodmatch] = :number
end
it "should match if the times are one minute apart and the current minute is 0" do
current = Time.utc(2008, 1, 1, 0, 0, 0)
previous = Time.utc(2007, 12, 31, 23, 59, 0)
Time.stubs(:now).returns(current)
expect(@schedule).to be_match(previous)
end
it "should not match if the times are 59 minutes apart and the current minute is 59" do
current = Time.utc(2009, 2, 1, 12, 59, 0)
previous = Time.utc(2009, 2, 1, 12, 0, 0)
Time.stubs(:now).returns(current)
expect(@schedule).to_not be_match(previous)
end
end
describe Puppet::Type.type(:schedule), "when matching daily by number" do
before do
@schedule[:period] = :daily
@schedule[:periodmatch] = :number
end
it "should match if the times are one minute apart and the current minute and hour are 0" do
current = Time.utc(2010, "nov", 7, 0, 0, 0)
# Now set the previous time to one minute before that
previous = current - 60
Time.stubs(:now).returns(current)
expect(@schedule).to 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
# Reset the previous time to 00:00:00
previous = Time.utc(2010, "nov", 7, 0, 0, 0)
# Set the current time to 23:59
now = previous + (23 * 3600) + (59 * 60)
Time.stubs(:now).returns(now)
expect(@schedule).to_not be_match(previous)
end
end
describe Puppet::Type.type(:schedule), "when matching weekly by number" do
before do
@schedule[:period] = :weekly
@schedule[:periodmatch] = :number
end
it "should match if the previous time is prior to the most recent Sunday" do
now = Time.utc(2010, "nov", 11, 0, 0, 0) # Thursday
Time.stubs(:now).returns(now)
previous = Time.utc(2010, "nov", 6, 23, 59, 59) # Sat
expect(@schedule).to be_match(previous)
end
it "should not match if the previous time is after the most recent Saturday" do
now = Time.utc(2010, "nov", 11, 0, 0, 0) # Thursday
Time.stubs(:now).returns(now)
previous = Time.utc(2010, "nov", 7, 0, 0, 0) # Sunday
expect(@schedule).to_not be_match(previous)
end
end
describe Puppet::Type.type(:schedule), "when matching monthly by number" do
before do
@schedule[:period] = :monthly
@schedule[:periodmatch] = :number
end
it "should match when the previous time is prior to the first day of this month" do
now = Time.utc(2010, "nov", 8, 00, 59, 59)
Time.stubs(:now).returns(now)
previous = Time.utc(2010, "oct", 31, 23, 59, 59)
expect(@schedule).to be_match(previous)
end
it "should not match when the previous time is after the last day of last month" do
now = Time.utc(2010, "nov", 8, 00, 59, 59)
Time.stubs(:now).returns(now)
previous = Time.utc(2010, "nov", 1, 0, 0, 0)
expect(@schedule).to_not be_match(previous)
end
end
describe Puppet::Type.type(:schedule), "when matching with a repeat greater than one" 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
@schedule[:periodmatch] = :number
expect(proc { @schedule[:repeat] = 2 }).to raise_error(Puppet::Error)
end
it "should match if the previous run was further away than the distance divided by the repeat" do
previous = Time.now - (3600 * 13)
expect(@schedule).to 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)
expect(@schedule).to_not be_match(previous)
end
end
describe Puppet::Type.type(:schedule), "when matching days of the week" do
before do
# 2011-05-23 is a Monday
Time.stubs(:now).returns(Time.local(2011, "may", 23, 11, 0, 0))
end
it "should raise an error if the weekday is 'Someday'" do
expect { @schedule[:weekday] = "Someday" }.to raise_error(Puppet::Error)
end
it "should raise an error if the weekday is '7'" do
expect { @schedule[:weekday] = "7" }.to raise_error(Puppet::Error)
end
it "should accept all full weekday names as valid values" do
expect { @schedule[:weekday] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday'] }.not_to raise_error
end
it "should accept all short weekday names as valid values" do
expect { @schedule[:weekday] = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat'] }.not_to raise_error
end
it "should accept all integers 0-6 as valid values" do
expect {@schedule[:weekday] = [0, 1, 2, 3, 4,
5, 6] }.not_to raise_error
end
it "should match if the weekday is 'Monday'" do
@schedule[:weekday] = "Monday"
expect(@schedule.match?).to be_truthy
end
it "should match if the weekday is 'Mon'" do
@schedule[:weekday] = "Mon"
expect(@schedule.match?).to be_truthy
end
it "should match if the weekday is '1'" do
@schedule[:weekday] = "1"
expect(@schedule.match?).to be_truthy
end
it "should match if weekday is 1" do
@schedule[:weekday] = 1
expect(@schedule).to be_match
end
it "should not match if the weekday is Tuesday" do
@schedule[:weekday] = "Tuesday"
expect(@schedule).not_to be_match
end
it "should match if weekday is ['Sun', 'Mon']" do
@schedule[:weekday] = ["Sun", "Mon"]
expect(@schedule.match?).to be_truthy
end
it "should not match if weekday is ['Sun', 'Tue']" do
@schedule[:weekday] = ["Sun", "Tue"]
expect(@schedule).not_to be_match
end
it "should match if the weekday is 'Monday'" do
@schedule[:weekday] = "Monday"
expect(@schedule.match?).to be_truthy
end
it "should match if the weekday is 'Mon'" do
@schedule[:weekday] = "Mon"
expect(@schedule.match?).to be_truthy
end
it "should match if the weekday is '1'" do
@schedule[:weekday] = "1"
expect(@schedule.match?).to be_truthy
end
it "should not match if the weekday is Tuesday" do
@schedule[:weekday] = "Tuesday"
expect(@schedule).not_to be_match
end
it "should match if weekday is ['Sun', 'Mon']" do
@schedule[:weekday] = ["Sun", "Mon"]
expect(@schedule.match?).to be_truthy
end
end
it "should raise an error if the weekday is an int higher than 6" do
expect { @schedule[:weekday] = 7 }.to raise_error(Puppet::ResourceError, /7 is not a valid day of the week/)
end
describe Puppet::Type.type(:schedule), "when matching days of week and ranges spanning days, day 1" do
before do
# Test with ranges and days-of-week both set. 2011-03-31 was a Thursday.
Time.stubs(:now).returns(Time.local(2011, "mar", 31, 22, 30, 0))
end
it "should match when the range and day of week matches" do
@schedule[:range] = "22:00:00 - 02:00:00"
@schedule[:weekday] = "Thursday"
expect(@schedule).to be_match
end
it "should not match when the range doesn't match even if the day-of-week matches" do
@schedule[:range] = "23:30:00 - 21:00:00"
@schedule[:weekday] = "Thursday"
expect(@schedule).to_not be_match
end
it "should not match when day-of-week doesn't match even if the range matches (1 day later)" do
@schedule[:range] = "22:00:00 - 01:00:00"
@schedule[:weekday] = "Friday"
expect(@schedule).to_not be_match
end
it "should not match when day-of-week doesn't match even if the range matches (1 day earlier)" do
@schedule[:range] = "22:00:00 - 01:00:00"
@schedule[:weekday] = "Wednesday"
expect(@schedule).to_not be_match
end
end
describe Puppet::Type.type(:schedule), "when matching days of week and ranges spanning days, day 2" do
before do
# 2011-03-31 was a Thursday. As the end-time of a day spanning match, that means
# we need to match on Wednesday.
Time.stubs(:now).returns(Time.local(2011, "mar", 31, 1, 30, 0))
end
it "should match when the range matches and the day of week should match" do
@schedule[:range] = "22:00:00 - 02:00:00"
@schedule[:weekday] = "Wednesday"
expect(@schedule).to be_match
end
it "should not match when the range does not match and the day of week should match" do
@schedule[:range] = "22:00:00 - 01:00:00"
@schedule[:weekday] = "Thursday"
expect(@schedule).to_not be_match
end
it "should not match when the range matches but the day-of-week does not (1 day later)" do
@schedule[:range] = "22:00:00 - 02:00:00"
@schedule[:weekday] = "Thursday"
expect(@schedule).to_not be_match
end
it "should not match when the range matches but the day-of-week does not (1 day later)" do
@schedule[:range] = "22:00:00 - 02:00:00"
@schedule[:weekday] = "Tuesday"
expect(@schedule).to_not be_match
end
end
end