SummaryRefsLogTreeCommitDiffStats
path: root/tests/vixie-specification.scm
blob: 78c1dad77a9959f67c9b000b5b9360e8de1821f9 (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
;;;; vixie-specification.scm -- tests for (mcron vixie-specificaion) module
;;; Copyright © 2020 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 (srfi srfi-1)
             (srfi srfi-64)
             (mcron vixie-specification))

(setenv "TZ" "UTC0")

;;; Do not send mail
(setenv "MAILTO" "")

(define (create-file! content)
  "Construct a temporary file port containing CONTENT which must be a string."
  (let ((port (mkstemp! (string-copy "file-XXXXXX"))))
    (display content port)
    (force-output port)
    port))

(define (clean-temp port)
  "Close and Delete a temporary file port"
  (let ((fname (port-filename port)))
    (close port)
    (delete-file fname)))

(define schedule (@@ (mcron base) %global-schedule))
(define schedule-user (@@ (mcron base) schedule-user))
(define set-schedule-user! (@@ (mcron base) set-schedule-user!))
(define job:environment (@@ (mcron base) job:environment))
(define job:displayable (@@ (mcron base) job:displayable))
(define job:user (@@ (mcron base) job:user))

(test-begin "vixie-specification")

;;; Parse user crontab file

(define user-crontab-example
  "# Example crontab
FOO=x
BAR=y

# Example of job definitions:
17 *	* * *	cd / && run baz
47 6	* * 7	foo -x /tmp/example || bar
")

(define user-crontab (create-file! user-crontab-example))

(dynamic-wind
  (const #t)
  (lambda ()
    (set-schedule-user! schedule '())
    (read-vixie-file (port-filename user-crontab))

    (test-assert "User schedule has exactly 2 matching jobs"
      (lset= string=?
             '("cd / && run baz"
               "foo -x /tmp/example || bar")
             (map job:displayable (schedule-user schedule))))

    (test-assert "Job environment matches configuration"
      (every (lambda (j)
               (lset= equal?
                      '(("FOO" . "x") ("BAR" . "y"))
                      (job:environment j)))
             (schedule-user schedule))))

  (lambda ()
    (clean-temp user-crontab)))

;;; Parse system crontab file

;;; Get two existing users from the test environment.
(setpwent)
(define user0 (getpwent))
(define user1 (or (getpwent) user0))
(define system-crontab-example
  (string-append
   "# Example crontab
BAZ=z

17 *	* * * " (passwd:name user0) " cd / && run baz
47 6	* * 7 "	(passwd:name user1) "   foo -x /tmp/example || bar"))

(define sys-crontab (create-file! system-crontab-example))

(dynamic-wind
  (const #t)
  (lambda ()
    (set-schedule-user! schedule '())
    (read-vixie-file (port-filename sys-crontab) parse-system-vixie-line)

    (test-assert "System schedule has exactly 2 matching jobs"
      (lset= equal?
             `((,user0 . "cd / && run baz")
               (,user1 . "foo -x /tmp/example || bar"))
             (map (lambda (j)
                    (cons (job:user j) (job:displayable j)))
                  (schedule-user schedule))))

    (test-assert "Job environment matches configuration"
    (every (lambda (j)
             (lset= equal? '(("BAZ" . "z")) (job:environment j)))
           (schedule-user schedule))))

  (lambda ()
    (clean-temp sys-crontab)))

;;; Try to parse a user crontab in a system context

(define wrong-system-crontab-example
  "
# Example of job definitions:
17 *	* * *	ls")

(define wrong-sys-crontab (create-file! wrong-system-crontab-example))

(dynamic-wind
  (const #t)
  (lambda ()
    (test-error "missing user"
      'mcron-error
      (read-vixie-file (port-filename wrong-sys-crontab)
                       parse-system-vixie-line)))

  (lambda ()
    (clean-temp wrong-sys-crontab)))

(test-end)