diff options
| author | Bruno Victal <mirai@makinata.eu> | 2023-03-26 19:41:29 +0100 |
|---|---|---|
| committer | Liliana Marie Prikler <liliana.prikler@gmail.com> | 2023-04-02 12:31:51 +0200 |
| commit | 6f48efa9b89f3c33f7b2827cae88e87ec64faa09 (patch) | |
| tree | 23a236dd58239e625aa540d68cbd833f40371af5 /tests/services | |
| parent | 2ebbe8e9df66d6607cafa38a79926e4c9ac0d151 (diff) | |
services: configuration: Add user-defined sanitizer support.
This changes the 'custom-serializer' field into a generic
'extra-args' field that can be extended to support new literals.
Within extra-args, the literals 'sanitizer' and 'serializer' allow
for user-defined sanitization and serialization procedures respectively.
The 'empty-serializer' was also added as a literal to be used as before.
To prevent confusion between the new “explicit” style of specifying
a sanitizer, and the old “implicit” style, the latter has been
deprecated, and a warning is issued if it is encountered.
* gnu/services/configuration.scm (define-configuration-helper):
Rename 'custom-serializer' to 'extra-args'. Add support for literals
'sanitizer', 'serializer' and 'empty-serializer'. Rename procedure
'field-sanitizer' to 'default-field-sanitizer' to avoid syntax clash.
Only define default field sanitizers if user-defined ones are absent.
(normalize-extra-args): New variable.
(<configuration-field>)[sanitizer]: New field.
* doc/guix.texi (Complex Configurations): Document the newly added
literals.
* tests/services/configuration.scm: Add tests for the new literals.
Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com>
Diffstat (limited to 'tests/services')
| -rw-r--r-- | tests/services/configuration.scm | 183 |
1 files changed, 181 insertions, 2 deletions
diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm index 4f8a74dc8ac..0392cce927a 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | ;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com> | 2 | ;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
| 3 | ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz> | 3 | ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz> |
| 4 | ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org> | 4 | ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org> |
| 5 | ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu> | ||
| 5 | ;;; | 6 | ;;; |
| 6 | ;;; This file is part of GNU Guix. | 7 | ;;; This file is part of GNU Guix. |
| 7 | ;;; | 8 | ;;; |
| @@ -22,6 +23,7 @@ | |||
| 22 | #:use-module (gnu services configuration) | 23 | #:use-module (gnu services configuration) |
| 23 | #:use-module (guix diagnostics) | 24 | #:use-module (guix diagnostics) |
| 24 | #:use-module (guix gexp) | 25 | #:use-module (guix gexp) |
| 26 | #:autoload (guix i18n) (G_) | ||
| 25 | #:use-module (srfi srfi-34) | 27 | #:use-module (srfi srfi-34) |
| 26 | #:use-module (srfi srfi-64)) | 28 | #:use-module (srfi srfi-64)) |
| 27 | 29 | ||
| @@ -46,14 +48,14 @@ | |||
| 46 | (port-configuration-port (port-configuration))) | 48 | (port-configuration-port (port-configuration))) |
| 47 | 49 | ||
| 48 | (test-equal "wrong type for a field" | 50 | (test-equal "wrong type for a field" |
| 49 | '("configuration.scm" 57 11) ;error location | 51 | '("configuration.scm" 59 11) ;error location |
| 50 | (guard (c ((configuration-error? c) | 52 | (guard (c ((configuration-error? c) |
| 51 | (let ((loc (error-location c))) | 53 | (let ((loc (error-location c))) |
| 52 | (list (basename (location-file loc)) | 54 | (list (basename (location-file loc)) |
| 53 | (location-line loc) | 55 | (location-line loc) |
| 54 | (location-column loc))))) | 56 | (location-column loc))))) |
| 55 | (port-configuration | 57 | (port-configuration |
| 56 | ;; This is line 56; the test relies on line/column numbers! | 58 | ;; This is line 58; the test relies on line/column numbers! |
| 57 | (port "This is not a number!")))) | 59 | (port "This is not a number!")))) |
| 58 | 60 | ||
| 59 | (define-configuration port-configuration-cs | 61 | (define-configuration port-configuration-cs |
| @@ -111,6 +113,183 @@ | |||
| 111 | 113 | ||
| 112 | 114 | ||
| 113 | ;;; | 115 | ;;; |
| 116 | ;;; define-configuration macro, extra-args literals | ||
| 117 | ;;; | ||
| 118 | |||
| 119 | (define (eval-gexp x) | ||
| 120 | "Get serialized config as string." | ||
| 121 | (eval (gexp->approximate-sexp x) | ||
| 122 | (current-module))) | ||
| 123 | |||
| 124 | (define (port? value) | ||
| 125 | (or (string? value) (number? value))) | ||
| 126 | |||
| 127 | (define (sanitize-port value) | ||
| 128 | (cond ((number? value) value) | ||
| 129 | ((string? value) (string->number value)) | ||
| 130 | (else (raise (formatted-message (G_ "Bad value: ~a") value))))) | ||
| 131 | |||
| 132 | (test-group "Basic sanitizer literal tests" | ||
| 133 | (define serialize-port serialize-number) | ||
| 134 | |||
| 135 | (define-configuration config-with-sanitizer | ||
| 136 | (port | ||
| 137 | (port 80) | ||
| 138 | "Lorem Ipsum." | ||
| 139 | (sanitizer sanitize-port))) | ||
| 140 | |||
| 141 | (test-equal "default value, sanitizer" | ||
| 142 | 80 | ||
| 143 | (config-with-sanitizer-port (config-with-sanitizer))) | ||
| 144 | |||
| 145 | (test-equal "string value, sanitized to number" | ||
| 146 | 56 | ||
| 147 | (config-with-sanitizer-port (config-with-sanitizer | ||
| 148 | (port "56")))) | ||
| 149 | |||
| 150 | (define (custom-serialize-port field-name value) | ||
| 151 | (number->string value)) | ||
| 152 | |||
| 153 | (define-configuration config-serializer | ||
| 154 | (port | ||
| 155 | (port 80) | ||
| 156 | "Lorem Ipsum." | ||
| 157 | (serializer custom-serialize-port))) | ||
| 158 | |||
| 159 | (test-equal "default value, serializer literal" | ||
| 160 | "80" | ||
| 161 | (eval-gexp | ||
| 162 | (serialize-configuration (config-serializer) | ||
| 163 | config-serializer-fields)))) | ||
| 164 | |||
| 165 | (test-group "empty-serializer as literal/procedure tests" | ||
| 166 | (define-configuration config-with-literal | ||
| 167 | (port | ||
| 168 | (port 80) | ||
| 169 | "Lorem Ipsum." | ||
| 170 | empty-serializer)) | ||
| 171 | |||
| 172 | (define-configuration config-with-proc | ||
| 173 | (port | ||
| 174 | (port 80) | ||
| 175 | "Lorem Ipsum." | ||
| 176 | (serializer empty-serializer))) | ||
| 177 | |||
| 178 | (test-equal "empty-serializer as literal" | ||
| 179 | "" | ||
| 180 | (eval-gexp | ||
| 181 | (serialize-configuration (config-with-literal) | ||
| 182 | config-with-literal-fields))) | ||
| 183 | |||
| 184 | (test-equal "empty-serializer as procedure" | ||
| 185 | "" | ||
| 186 | (eval-gexp | ||
| 187 | (serialize-configuration (config-with-proc) | ||
| 188 | config-with-proc-fields)))) | ||
| 189 | |||
| 190 | (test-group "permutation tests" | ||
| 191 | (define-configuration config-san+empty-ser | ||
| 192 | (port | ||
| 193 | (port 80) | ||
| 194 | "Lorem Ipsum." | ||
| 195 | (sanitizer sanitize-port) | ||
| 196 | empty-serializer)) | ||
| 197 | |||
| 198 | (define-configuration config-san+ser | ||
| 199 | (port | ||
| 200 | (port 80) | ||
| 201 | "Lorem Ipsum." | ||
| 202 | (sanitizer sanitize-port) | ||
| 203 | (serializer (lambda _ "foo")))) | ||
| 204 | |||
| 205 | (test-equal "default value, sanitizer, permutation" | ||
| 206 | 80 | ||
| 207 | (config-san+empty-ser-port (config-san+empty-ser))) | ||
| 208 | |||
| 209 | (test-equal "default value, serializer, permutation" | ||
| 210 | "foo" | ||
| 211 | (eval-gexp | ||
| 212 | (serialize-configuration (config-san+ser) config-san+ser-fields))) | ||
| 213 | |||
| 214 | (test-equal "string value sanitized to number, permutation" | ||
| 215 | 56 | ||
| 216 | (config-san+ser-port (config-san+ser | ||
| 217 | (port "56")))) | ||
| 218 | |||
| 219 | ;; Ordering tests. | ||
| 220 | (define-configuration config-ser+san | ||
| 221 | (port | ||
| 222 | (port 80) | ||
| 223 | "Lorem Ipsum." | ||
| 224 | (sanitizer sanitize-port) | ||
| 225 | (serializer (lambda _ "foo")))) | ||
| 226 | |||
| 227 | (define-configuration config-empty-ser+san | ||
| 228 | (port | ||
| 229 | (port 80) | ||
| 230 | "Lorem Ipsum." | ||
| 231 | empty-serializer | ||
| 232 | (sanitizer sanitize-port))) | ||
| 233 | |||
| 234 | (test-equal "default value, sanitizer, permutation 2" | ||
| 235 | 56 | ||
| 236 | (config-empty-ser+san-port (config-empty-ser+san | ||
| 237 | (port "56")))) | ||
| 238 | |||
| 239 | (test-equal "default value, serializer, permutation 2" | ||
| 240 | "foo" | ||
| 241 | (eval-gexp | ||
| 242 | (serialize-configuration (config-ser+san) config-ser+san-fields)))) | ||
| 243 | |||
| 244 | (test-group "duplicated/conflicting entries" | ||
| 245 | (test-error | ||
| 246 | "duplicate sanitizer" #t | ||
| 247 | (macroexpand '(define-configuration dupe-san | ||
| 248 | (foo | ||
| 249 | (list '()) | ||
| 250 | "Lorem Ipsum." | ||
| 251 | (sanitizer (lambda () #t)) | ||
| 252 | (sanitizer (lambda () #t)))))) | ||
| 253 | |||
| 254 | (test-error | ||
| 255 | "duplicate serializer" #t | ||
| 256 | (macroexpand '(define-configuration dupe-ser | ||
| 257 | (foo | ||
| 258 | (list '()) | ||
| 259 | "Lorem Ipsum." | ||
| 260 | (serializer (lambda _ "")) | ||
| 261 | (serializer (lambda _ "")))))) | ||
| 262 | |||
| 263 | (test-error | ||
| 264 | "conflicting use of serializer + empty-serializer" #t | ||
| 265 | (macroexpand '(define-configuration ser+empty-ser | ||
| 266 | (foo | ||
| 267 | (list '()) | ||
| 268 | "Lorem Ipsum." | ||
| 269 | (serializer (lambda _ "lorem")) | ||
| 270 | empty-serializer))))) | ||
| 271 | |||
| 272 | (test-group "Mix of deprecated and new syntax" | ||
| 273 | (test-error | ||
| 274 | "Mix of bare serializer and new syntax" #t | ||
| 275 | (macroexpand '(define-configuration mixed | ||
| 276 | (foo | ||
| 277 | (list '()) | ||
| 278 | "Lorem Ipsum." | ||
| 279 | (sanitizer (lambda () #t)) | ||
| 280 | (lambda _ "lorem"))))) | ||
| 281 | |||
| 282 | (test-error | ||
| 283 | "Mix of bare serializer and new syntax, permutation)" #t | ||
| 284 | (macroexpand '(define-configuration mixed | ||
| 285 | (foo | ||
| 286 | (list '()) | ||
| 287 | "Lorem Ipsum." | ||
| 288 | (lambda _ "lorem") | ||
| 289 | (sanitizer (lambda () #t))))))) | ||
| 290 | |||
| 291 | |||
| 292 | ;;; | ||
| 114 | ;;; define-maybe macro. | 293 | ;;; define-maybe macro. |
| 115 | ;;; | 294 | ;;; |
| 116 | (define-maybe number) | 295 | (define-maybe number) |
