AboutSummaryRefsLogTreeCommitDiffStats
path: root/tests/job-specifier.scm
blob: 70dd5189b1a5faae62dd469e57352821e74bda17 (about) (plain) (blame)
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
;;;; job-specifier.scm -- tests for (mcron job-specifier) module
;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
;;;
;;; This file is part of GNU Mcron.
;;;
;;; GNU Mcron is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; GNU Mcron is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Mcron.  If not, see <http://www.gnu.org/licenses/>.

(use-modules (ice-9 match)
             (srfi srfi-64)
             (srfi srfi-111)
             (mcron job-specifier))

(test-begin "job-specifier")

(test-equal "range: basic"
  '(0 1 2 3 4 5 6 7 8 9)
  (range 0 10))

(test-equal "range: positive step"
  '(0 2 4 6 8)
  (range 0 10 2))

(test-assert "range: zero step"
  ;; Since this behavior is undefined, only check if range doesn't crash.
  (range 0 5 0))

(test-assert "range: negative step"
  ;; Since this behavior is undefined, only check if range doesn't crash.
  (range 0 5 -2))

(test-assert "range: reverse boundaries"
  (range 10 3))

(define %find-best-next (@@ (mcron job-specifier) %find-best-next))

(test-assert "%find-best-next: exact"
  ;; Ensure that '%find-best-next' preserves the exactness of the numbers
  ;; inside the NEXT-LIST argument.
  (match (pk 'match (%find-best-next 1 '(0 2)))
    ((a . b) (and (exact? a) (exact? b)))))

;;;
;;; Check 'next-...' procedures.
;;;

;;; TODO: Find more meaningful date examples.

(setenv "TZ" ":UTC")

(test-equal "next-year"
  (list 1893456000 1546300800)
  (list (next-year '(130))   ;; This is the year 2030.
        (next-year-from 1522095469)))

(test-equal "next-month"
  5097600
  (next-month-from 101 '(0 2 4)))

(test-equal "next-day"
  345600
  (next-day-from 4337 '(0 5 10)))

(test-equal "next-hour"
  3600
  (next-hour-from 3 '(0 1 2 3 4)))

(test-equal "next-minute"
  60
  (next-minute-from 8))

(test-equal "next-second"
  15
  (next-second-from 14))

;;;
;;; Check 'configuration-user' manipulation
;;;

(define configuration-user (@@ (mcron job-specifier) configuration-user))

;;; Call 'set-configuration-user' with a valid uid.
(let ((uid (getuid)))
  (test-equal "set-configuration-user: uid"
    uid
    (begin
      (set-configuration-user uid)
      (passwd:uid (unbox configuration-user)))))

(define entry
  ;; Random user entry.
  (getpw))

;;; Call 'set-configuration-user' with a valid user name.
(let ((name (passwd:name entry)))
  (test-equal "set-configuration-user: name"
    name
    (begin
      (set-configuration-user name)
      (passwd:name (unbox configuration-user)))))

(define root-entry (getpw 0))

;;; Call 'set-configuration-user' with a passwd entry.
(test-equal "set-configuration-user: passwd entry"
  root-entry
  (begin
    (set-configuration-user root-entry)
    (unbox configuration-user)))

;;; Call 'set-configuration-user' with an invalid uid.
(test-error "set-configuration-user: invalid uid"
   #t
   (set-configuration-user -20000))

;;; Call 'set-configuration-user' with an invalid spec.
(test-error "set-configuration-user: invalid spec"
   #t
   (set-configuration-user 'wrong))

;;;
;;; Check the 'job' procedure
;;;

(test-assert "job: procedure timeproc"
  (job 1+ "dummy action"))

(test-assert "job: list timeproc"
  (job '(next-hour '(0)) "dummy action"))

(test-assert "job: string timeproc"
  (job "30 4 1,15 * 5" "dummy action"))

(test-error "job: invalid string timeproc"
  'mcron-error
  (job "30 4 1,15 * WRONG" "dummy action"))

(test-error "job: invalid timeproc"
  'mcron-error
  (job 42 "dummy action"))

(test-assert "job: procedure action"
  (job 1+ (λ () (display "hello\n"))))

(test-assert "job: list action"
  (job 1+ '(display "hello\n")))

(test-assert "job: string action"
  (job 1+ "echo hello"))

(test-error "job: string action"
  'mcron-error
  (job 1+ 42))

(test-assert "job: user name"
  (job 1+ "dummy action" #:user (getuid)))

(test-end)