aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
;;;
;;; 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 diagnostics)
  #:use-module (guix gexp)
  #:autoload (guix i18n) (G_)
  #: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)))

(test-equal "wrong type for a field"
  '("configuration.scm" 59 11)                    ;error location
  (guard (c ((configuration-error? c)
             (let ((loc (error-location c)))
               (list (basename (location-file loc))
                     (location-line loc)
                     (location-column loc)))))
    (port-configuration
     ;; This is line 58; the test relies on line/column numbers!
     (port "This is not a number!"))))

(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." (serializer custom-number-serializer)))

(define-configuration serializable-configuration-deprecated
  (port (number 80) "The port number." custom-number-serializer))

(test-assert "serialize-configuration"
  (gexp?
   (let ((config (serializable-configuration)))
     (serialize-configuration config serializable-configuration-fields))))

(test-assert "serialize-configuration [deprecated]"
  (gexp?
   (let ((config (serializable-configuration-deprecated)))
     (serialize-configuration
      config serializable-configuration-deprecated-fields))))

(define-configuration serializable-configuration
  (port (number 80) "The port number." (serializer 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)))))

(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-configuration macro, extra-args literals
;;;

(define (eval-gexp x)
  "Get serialized config as string."
  (eval (gexp->approximate-sexp x)
        (current-module)))

(define (port? value)
  (or (string? value) (number? value)))

(define (sanitize-port value)
  (cond ((number? value) value)
        ((string? value) (string->number value))
        (else (raise (formatted-message (G_ "Bad value: ~a") value)))))

(test-group "Basic sanitizer literal tests"
  (define serialize-port serialize-number)

  (define-configuration config-with-sanitizer
    (port
     (port 80)
     "Lorem Ipsum."
     (sanitizer sanitize-port)))

  (test-equal "default value, sanitizer"
    80
    (config-with-sanitizer-port (config-with-sanitizer)))

  (test-equal "string value, sanitized to number"
    56
    (config-with-sanitizer-port (config-with-sanitizer
                                 (port "56"))))

  (define (custom-serialize-port field-name value)
    (number->string value))

  (define-configuration config-serializer
    (port
     (port 80)
     "Lorem Ipsum."
     (serializer custom-serialize-port)))

  (test-equal "default value, serializer literal"
    "80"
    (eval-gexp
     (serialize-configuration (config-serializer)
                              config-serializer-fields))))

(test-group "empty-serializer as literal/procedure tests"
  (define-configuration config-with-literal
    (port
     (port 80)
     "Lorem Ipsum."
     empty-serializer))

  (define-configuration config-with-proc
    (port
     (port 80)
     "Lorem Ipsum."
     (serializer empty-serializer)))

  (test-equal "empty-serializer as literal"
    ""
    (eval-gexp
     (serialize-configuration (config-with-literal)
                              config-with-literal-fields)))

  (test-equal "empty-serializer as procedure"
    ""
    (eval-gexp
     (serialize-configuration (config-with-proc)
                              config-with-proc-fields))))

(test-group "permutation tests"
  (define-configuration config-san+empty-ser
    (port
     (port 80)
     "Lorem Ipsum."
     (sanitizer sanitize-port)
     empty-serializer))

  (define-configuration config-san+ser
    (port
     (port 80)
     "Lorem Ipsum."
     (sanitizer sanitize-port)
     (serializer (lambda _ "foo"))))

  (test-equal "default value, sanitizer, permutation"
    80
    (config-san+empty-ser-port (config-san+empty-ser)))

  (test-equal "default value, serializer, permutation"
    "foo"
    (eval-gexp
     (serialize-configuration (config-san+ser) config-san+ser-fields)))

  (test-equal "string value sanitized to number, permutation"
    56
    (config-san+ser-port (config-san+ser
                          (port "56"))))

  ;; Ordering tests.
  (define-configuration config-ser+san
    (port
     (port 80)
     "Lorem Ipsum."
     (sanitizer sanitize-port)
     (serializer (lambda _ "foo"))))

  (define-configuration config-empty-ser+san
    (port
     (port 80)
     "Lorem Ipsum."
     empty-serializer
     (sanitizer sanitize-port)))

  (test-equal "default value, sanitizer, permutation 2"
    56
    (config-empty-ser+san-port (config-empty-ser+san
                                (port "56"))))

  (test-equal "default value, serializer, permutation 2"
    "foo"
    (eval-gexp
     (serialize-configuration (config-ser+san) config-ser+san-fields))))

(test-group "duplicated/conflicting entries"
  (test-error
   "duplicate sanitizer" #t
   (macroexpand '(define-configuration dupe-san
                   (foo
                    (list '())
                    "Lorem Ipsum."
                    (sanitizer (lambda () #t))
                    (sanitizer (lambda () #t))))))

  (test-error
   "duplicate serializer" #t
   (macroexpand '(define-configuration dupe-ser
                   (foo
                    (list '())
                    "Lorem Ipsum."
                    (serializer (lambda _ ""))
                    (serializer (lambda _ ""))))))

  (test-error
   "conflicting use of serializer + empty-serializer" #t
   (macroexpand '(define-configuration ser+empty-ser
                   (foo
                    (list '())
                    "Lorem Ipsum."
                    (serializer (lambda _ "lorem"))
                    empty-serializer)))))

(test-group "Mix of deprecated and new syntax"
  (test-error
   "Mix of bare serializer and new syntax" #t
   (macroexpand '(define-configuration mixed
                   (foo
                    (list '())
                    "Lorem Ipsum."
                    (sanitizer (lambda () #t))
                    (lambda _ "lorem")))))

  (test-error
   "Mix of bare serializer and new syntax, permutation)" #t
   (macroexpand '(define-configuration mixed
                   (foo
                    (list '())
                    "Lorem Ipsum."
                    (lambda _ "lorem")
                    (sanitizer (lambda () #t)))))))


;;;
;;; 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 (serialize-symbol name value)
  (format #f "~a=~a~%" name value))

(define-maybe symbol)

(define-configuration config-with-maybe-symbol
  (protocol maybe-symbol ""))

(test-equal "symbol maybe value serialization, unspecified"
  ""
  (eval-gexp
   (serialize-configuration (config-with-maybe-symbol)
                            config-with-maybe-symbol-fields)))

(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)))

(test-assert "maybe type, no default"
  (eq? %unset-value
       (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")))))
* guix/lint.scm (check-patch-file-names): Handle 'formatted-message?'. (check-derivation): Ditto. Ludovic Courtès 2020-06-28Add more missing (ice-9 format) imports....* gnu/machine/ssh.scm: Import (ice-9 format). * guix/scripts/graph.scm: Likewise. * guix/scripts/system/search.scm: Likewise. * guix/remote.scm: Likewise. Marius Bakke 2020-06-18machine: ssh: Fix guix deploy hang when using non-DCE UUIDs....Fixes <https://issues.guix.gnu.org/issue/41237>. The UUID type information was lost when passing to the lower gexp code strata, which led to not being able to recreate the UUID in the generated script. This occurred for non-DCE type UUIDs such as that of a FAT file system. A following commit will prevent the find-partition-by-uuid procedure from entering a loop when its UUID argument is invalid. Reported-by: Brice Waegeneire <brice@waegenei.re> * gnu/machine/ssh.scm (machine-check-file-system-availability): Replace the STRING->UUID procedure by the UUID macro, and provide the UUID type as its second argument. Maxim Cournoyer 2020-03-23machine: ssh: Better report missing initrd modules....* gnu/machine/ssh.scm (machine-check-initrd-modules): Improve message upon module mismatch. Ludovic Courtès 2020-03-23machine: ssh: Make sanity checks in a single round trip....* gnu/machine/ssh.scm (<remote-assertion>): New record type. (remote-let): New macro. (machine-check-file-system-availability): Rewrite to use 'remote-let' instead of 'mlet' and 'machine-remote-eval'. (machine-check-initrd-modules): Likewise. (machine-check-building-for-appropriate-system): Make non-monadic. (check-deployment-sanity): Rewrite to gather all the assertions as a single gexp and pass it to 'machine-remote-eval'. Ludovic Courtès 2020-02-09Update e-mail address for Jakob L. Kreuze....As requested here: <https://lists.gnu.org/archive/html/guix-devel/2020-02/msg00128.html>. * .mailmap: Add an entry for Jakob. * gnu/machine.scm, gnu/machine/digital-ocean.scm, gnu/machine/ssh.scm, gnu/packages/admin.scm, gnu/packages/i2p.scm, gnu/packages/music.scm, gnu/packages/web.scm, gnu/tests/reconfigure.scm, guix/scripts/deploy.scm, guix/scripts/system/reconfigure.scm: Update their e-mail address. Tobias Geerinckx-Rice 2019-12-08machine: Remove unnecessary record self-referencing bindings....'this-machine' and 'this-machine-ssh-configuration' were useless given that there are no thunked fields. * gnu/machine.scm (<machine>)[this-machine]: Remove. * gnu/machine/ssh.scm (<machine-ssh-configuration>) [this-machine-ssh-configuration]: Remove. Ludovic Courtès 2019-12-07machine: ssh: Deprecate missing 'host-key' field....* gnu/machine/ssh.scm (machine-ssh-session): Warn about missing host key. Ludovic Courtès 2019-12-04machine: ssh: <machine-ssh-configuration> can include the host key....* gnu/machine/ssh.scm (<machine-ssh-configuration>)[host-key]: New field. (machine-ssh-session): Pass #:host-key to 'open-ssh-session'. * doc/guix.texi (Invoking guix deploy): Document it. Ludovic Courtès 2019-10-22machine: digital-ocean: Rename 'enable-ipv6' to 'enable-ipv6?'....* gnu/machine/digital-ocean.scm (<digital-ocean-configuration>)[enable-ipv6]: Rename to... [enable-ipv6?]: ... this. (deploy-digital-ocean): Adjust accordingly. * doc/guix.texi (Invoking guix deploy): Adjust accordingly. Ludovic Courtès 2019-10-22machine: Implement 'digital-ocean-environment-type'....* gnu/machine/digital-ocean.scm: New file. * gnu/local.mk (GNU_SYSTEM_MODULES): Add it. * doc/guix.texi (Invoking guix deploy): Add documentation for 'digital-ocean-configuration'. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Jakob L. Kreuze 2019-08-17machine: ssh: Fix sanity checks....* gnu/machine/ssh.scm (machine-check-file-system-availability)[check-labeled-file-system]: Use 'source-module-closure' for '(gnu build file-systems)'. (machine-check-initrd-modules): Unquote 'file-system-label->string' instead of 'device'. 宋文武 2019-08-16machine: Use 'become-command'....* gnu/machine/ssh.scm (managed-host-remote-eval): Pass an appropriate 'become-command' to 'remote-eval'. * guix/ssh.scm (remote-authorize-signing-key): Add optional 'become-command' argument. All callers changed. Jakob L. Kreuze 2019-08-15machine: Automatically authorize the coordinator's signing key....* guix/ssh.scm (remote-authorize-signing-key): New variable. * gnu/machine/ssh.scm (deploy-managed-host): Authorize coordinator's signing key before any invocations of 'remote-eval'. (deploy-managed-host): Display an error if a signing key does not exist. * doc/guix.texi (Invoking guix deploy): Remove section describing manual signing key authorization. (Invoking guix deploy): Add section describing the 'authorize?' field. Jakob L. Kreuze 2019-08-15machine: Implement 'roll-back-machine'....* gnu/machine.scm (roll-back-machine, &deploy-error, deploy-error?) (deploy-error-should-roll-back) (deploy-error-captured-args): New variable. * gnu/machine/ssh.scm (roll-back-managed-host): New variable. * guix/scripts/deploy.scm (guix-deploy): Roll-back systems when a deployment fails. Jakob L. Kreuze 2019-08-15machine: Allow non-root users to deploy....* doc/guix.texi (Invoking guix deploy): Add section describing prerequisites for deploying as a non-root user. * guix/remote.scm (remote-pipe-for-gexp): New optional 'become-command' argument. (%remote-eval): New optional 'become-command' argument. (remote-eval): New 'become-command' keyword argument. * guix/ssh.scm (remote-inferior): New optional 'become-command' argument. (inferior-remote-eval): New optional 'become-command' argument. (remote-authorize-signing-key): New optional 'become-command' argument. * gnu/machine/ssh.scm (machine-become-command): New variable. (managed-host-remote-eval): Invoke 'remote-eval' with the '#:become-command' keyword. (deploy-managed-host): Invoke 'remote-authorize-signing-key' with the '#:become-command' keyword. Jakob L. Kreuze 2019-08-14remote: Build derivations appropriate for the remote's...* gnu/machine/ssh.scm (machine-ssh-configuration): Add 'system' field. (managed-host-remote-eval): Pass 'system' field to 'remote-eval'. (machine-check-building-for-appropriate-system): New variable. (check-deployment-sanity): Add call to 'machine-check-building-for-appropriate-system'. * doc/guix.texi (Invoking guix deploy): Describe new 'system' field. * guix/ssh.scm (remote-system): New variable. * guix/remote.scm (remote-eval): Use result of 'remote-system' when lowering the G-Expression. (remote-eval): Add 'system' keyword argument. (trampoline): Return a <program-file> rather than a <scheme-file>. Jakob L. Kreuze 2019-08-07machine: Add 'build-locally?' field for managed hosts....* gnu/machine/ssh.scm (machine-ssh-configuration-build-locally?): New variable. (managed-host-remote-eval): Pass 'build-locally?' to 'remote-eval'. Jakob L. Kreuze 2019-08-06machine: Implement safety checks....* gnu/machine/ssh.scm (machine-check-file-system-availability) (machine-check-initrd-modules, check-deployment-sanity): New variable. (deploy-managed-host): Perform safety checks before deploying. Jakob L. Kreuze 2019-08-06machine: Rename 'system' field....* gnu/machine.scm (machine-system): Delete variable. (machine-operating-system): New variable. All callers changed. * doc/guix.texi (Invoking guix deploy): Use the 'machine-operating-system' accessor rather than 'machine-system'. Jakob L. Kreuze 2019-07-26guix system: Add 'reconfigure' module....* guix/scripts/system/reconfigure.scm: New file. * Makefile.am (MODULES): Add it. * guix/scripts/system.scm (bootloader-installer-script): Export variable. * gnu/machine/ssh.scm (switch-to-system, upgrade-shepherd-services) (install-bootloader): Delete variable. * gnu/machine/ssh.scm (deploy-managed-host): Rewrite procedure. * gnu/services/herd.scm (live-service): Export variable. * gnu/services/herd.scm (live-service-canonical-name): New variable. * tests/services.scm (live-service): Delete variable. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Jakob L. Kreuze 2019-07-18machine: Fix typo....* gnu/machine/ssh.scm (managed-host-environment-type)[description]: Fix typo. Tobias Geerinckx-Rice 2019-07-06gnu: Add machine type for deployment specifications....* gnu/machine.scm: New file. * gnu/machine/ssh.scm: New file. * gnu/local.mk (GNU_SYSTEM_MODULES): Add it. Jakob L. Kreuze