guix/tests/services/configuration.scm

151 lines
4.6 KiB
Scheme
Raw Normal View History

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
services: configuration: Allow specifying prefix for serializer names. Sometimes two configurations might have the same types for their field values, but the values might be serialized in two completely different ways (e.g. because the two programs have different configuration languages). An example of this would be the ‘serialize-boolean’ procedure in (gnu services mail) and (gnu services getmail). They both serialize a boolean value, but because the Dovecot’s configuration language has a different syntax to the configuration language for Getmail, two different procedures have to be defined. One way to workaround this would be to specify custom serializers for many fields in order to separate the serialization of the values that have the same type but serialize in different ways. This could get very tedious, especially if there are many configurations in the same module. Another way would be to move one of the configurations to its own module, like what was done with (gnu services getmail). However, this would mean that there would be multiple modules containing configurations for related programs, e.g. we have (gnu services mail) and (gnu services getmail), it doesn’t make much sense to keep the Getmail configuration in its own module. This patch will allow one to write something like this: (define-configuration foo-configuration (bar (string "bob") "Option bar.") (prefix bar-)) and the value of the ‘bar’ field would be serialized using a procedure named ‘bar-serialize-string’ instead of just ‘serialize-string’. * gnu/services/configuration.scm (define-maybe-helper): Accept ‘prefix’ argument for using serializer with custom prefix. (define-maybe): Pattern match on ‘prefix’ literal. (define-configuration-helper): Accept ‘prefix’ argument for using serializer with custom prefix. (define-configuration): Pattern match on ‘prefix’ literal. * tests/services/configuration.scm ("serialize-configuration with prefix"): New test. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2021-06-12 15:17:08 -04:00
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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 Guix 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 Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (tests services configuration)
#:use-module (gnu services configuration)
#:use-module (guix gexp)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-64))
;;; Tests for the (gnu services configuration) module.
(test-begin "services-configuration")
(define (serialize-number field value)
(format #f "~a=~a" field value))
;;;
;;; define-configuration macro.
;;;
(define-configuration port-configuration
(port (number 80) "The port number.")
(no-serialization))
(test-equal "default value, no serialization"
80
(port-configuration-port (port-configuration)))
(define-configuration port-configuration-cs
(port (number 80) "The port number." empty-serializer))
(test-equal "default value, custom serializer"
80
(port-configuration-cs-port (port-configuration-cs)))
(define-configuration port-configuration-ndv
(port (number) "The port number."))
(test-equal "no default value, provided"
55
(port-configuration-ndv-port (port-configuration-ndv
(port 55))))
(test-assert "no default value, not provided"
(guard (c ((configuration-error? c)
#t))
(port-configuration-ndv-port (port-configuration-ndv))))
(define (custom-number-serializer name value)
(format #f "~a = ~a;" name value))
(define-configuration serializable-configuration
(port (number 80) "The port number." custom-number-serializer))
(test-assert "serialize-configuration"
(gexp?
(let ((config (serializable-configuration)))
(serialize-configuration config serializable-configuration-fields))))
(define-configuration serializable-configuration
(port (number 80) "The port number." custom-number-serializer)
(no-serialization))
(test-assert "serialize-configuration with no-serialization"
;; When serialization is disabled, the serializer is set to #f, so
;; attempting to use it fails with a 'wrong-type-arg' error.
(not (false-if-exception
(let ((config (serializable-configuration)))
(serialize-configuration config serializable-configuration-fields)))))
services: configuration: Allow specifying prefix for serializer names. Sometimes two configurations might have the same types for their field values, but the values might be serialized in two completely different ways (e.g. because the two programs have different configuration languages). An example of this would be the ‘serialize-boolean’ procedure in (gnu services mail) and (gnu services getmail). They both serialize a boolean value, but because the Dovecot’s configuration language has a different syntax to the configuration language for Getmail, two different procedures have to be defined. One way to workaround this would be to specify custom serializers for many fields in order to separate the serialization of the values that have the same type but serialize in different ways. This could get very tedious, especially if there are many configurations in the same module. Another way would be to move one of the configurations to its own module, like what was done with (gnu services getmail). However, this would mean that there would be multiple modules containing configurations for related programs, e.g. we have (gnu services mail) and (gnu services getmail), it doesn’t make much sense to keep the Getmail configuration in its own module. This patch will allow one to write something like this: (define-configuration foo-configuration (bar (string "bob") "Option bar.") (prefix bar-)) and the value of the ‘bar’ field would be serialized using a procedure named ‘bar-serialize-string’ instead of just ‘serialize-string’. * gnu/services/configuration.scm (define-maybe-helper): Accept ‘prefix’ argument for using serializer with custom prefix. (define-maybe): Pattern match on ‘prefix’ literal. (define-configuration-helper): Accept ‘prefix’ argument for using serializer with custom prefix. (define-configuration): Pattern match on ‘prefix’ literal. * tests/services/configuration.scm ("serialize-configuration with prefix"): New test. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2021-06-12 15:17:08 -04:00
(define (custom-prefix-serialize-integer field-name name) name)
(define-configuration configuration-with-prefix
(port (integer 10) "The port number.")
(prefix custom-prefix-))
(test-assert "serialize-configuration with prefix"
(gexp?
(let ((config (configuration-with-prefix)))
(serialize-configuration config configuration-with-prefix-fields))))
;;;
;;; define-maybe macro.
;;;
(define-maybe number)
(define-configuration config-with-maybe-number
(port (maybe-number 80) "")
(count maybe-number ""))
(test-equal "maybe value serialization"
"port=80"
(serialize-maybe-number "port" 80))
(define (config-with-maybe-number->string x)
(eval (gexp->approximate-sexp
(serialize-configuration x config-with-maybe-number-fields))
(current-module)))
(test-equal "maybe value serialization of the instance"
"port=42count=43"
(config-with-maybe-number->string
(config-with-maybe-number
(port 42)
(count 43))))
(test-equal "maybe value serialization of the instance, unspecified"
"port=42"
(config-with-maybe-number->string
(config-with-maybe-number
(port 42))))
(define-maybe/no-serialization string)
(define-configuration config-with-maybe-string/no-serialization
(name (maybe-string) "The name of the item.")
(no-serialization))
(test-assert "maybe value without serialization no procedure bound"
(not (defined? 'serialize-maybe-string)))
services: configuration: Use *unspecified* instead of 'disabled. Use *unspecified* as a marker for field values that have not been set. Rationale: 'disabled may easily clash with user values for boolean fields, is confusing (i.e. its meaning is *not* boolean false, but unspecified) and it also passes silently through the symbol? predicate of a field of type symbol. * gnu/services/configuration.scm (configuration-missing-default-value): Renamed from configuration-no-default-value. (define-maybe-helper): Use *unspecified* instead of 'disabled, and make the default value optional. * gnu/home/services/desktop.scm (home-redshift-configuration): Change (maybe-xyz 'disabled) to maybe-xyz. * gnu/services/authentication.scm (nslcd-configuration): Likewise. * gnu/services/cgit.scm (repository-cgit-configuration): Likewise. * gnu/services/file-sharing.scm (serialize-maybe-string) (serialize-maybe-file-object): Use 'unspecified?' instead of (eq? val 'disabled). * gnu/services/messaging.scm (raw-content?): Likewise. (ssl-configuration): Change (maybe-xyz 'disabled) to maybe-xyz. (prosody-configuration): Likewise. * gnu/services/file-sharing.scm (transmission-daemon-configuration): Likewise. * gnu/services/messaging.scm (define-all-configurations): Use *unspecified* instead of 'disabled'. * gnu/services/networking.scm (opendht-configuration): Likewise. * gnu/services/pm.scm (tlp-configuration): Likewise. * gnu/services/telephony.scm (jami-account): Likewise. (jami-configuration): Likewise. * gnu/services/vpn.scm (openvpn-client-configuration): Likewise. * tests/services/configuration.scm ("maybe type, no default") ("maybe type, with default"): New tests. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2022-05-17 07:39:28 -04:00
(test-assert "maybe type, no default"
(unspecified?
(config-with-maybe-string/no-serialization-name
(config-with-maybe-string/no-serialization))))
(test-assert "maybe type, with default"
(equal?
"foo"
(config-with-maybe-string/no-serialization-name
(config-with-maybe-string/no-serialization
(name "foo")))))