aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2024 Richard Sent <richard@freakingpenguin.com>.
;;;
;;; 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 (gnu tests sddm)
  #:use-module (gnu bootloader)
  #:use-module (gnu bootloader grub)
  #:use-module (gnu packages)
  #:use-module (gnu packages ocr)
  #:use-module (gnu packages xorg)
  #:use-module (gnu services)
  #:use-module (gnu services desktop)
  #:use-module (gnu services sddm)
  #:use-module (gnu services xorg)
  #:use-module (gnu system)
  #:use-module (gnu system vm)
  #:use-module (gnu tests)
  #:use-module (guix gexp)
  #:use-module (guix modules)
  #:use-module (srfi srfi-1)
  #:export (%test-sddm))

(define %sddm-os
  (operating-system
    (inherit %simple-os)
    (services
     (cons* (service sddm-service-type)
            (modify-services %desktop-services
              (delete gdm-service-type))))))

(define (run-sddm-test)
  "Run tests in %SDDM-OS."

  (define os (marionette-operating-system
              %sddm-os
              #:imported-modules (source-module-closure
                                  '((gnu services herd)))))

  (define vm (virtual-machine
              (operating-system os)
              ;; Test may nondeterministically fail with default memory size.
              (memory-size 1024)))

  (define test
    (with-imported-modules (source-module-closure
                            '((gnu build marionette)))
      #~(begin
          (use-modules (gnu build marionette)
                       (srfi srfi-26)
                       (srfi srfi-64))

          (let ((marionette (make-marionette (list #$vm))))

            (test-runner-current (system-test-runner #$output))
            (test-begin "sddm")

            (test-assert "service is running"
              (marionette-eval
               '(begin
                  (use-modules (gnu services herd))
                  ;; sddm's shepherd service is called xorg-server
                  (start-service 'xorg-server))
               marionette))

            (test-assert "service can be stopped"
              (marionette-eval
               '(begin
                  (use-modules (gnu services herd))
                  (stop-service 'xorg-server))
               marionette))

            (test-assert "service can be restarted"
              (marionette-eval
               '(begin
                  (use-modules (gnu services herd))
                  (restart-service 'xorg-server))
               marionette))

            (test-assert "login screen up"
              ;; GNU Ocrad fails to recognize any text, so use Tesseract.
              (wait-for-screen-text marionette
                                    ;; Login button not recognized, use SDDM's
                                    ;; welcome message.
                                    (cut string-contains <> (string-append "Welcome to "
                                                                           #$(operating-system-host-name %sddm-os)))
                                    #:ocr #$(file-append tesseract-ocr
                                                         "/bin/tesseract")
                                    #:timeout 60))

            (test-end)))))

  (gexp->derivation "sddm-test" test))

(define %test-sddm
  (system-test
   (name "sddm")
   (description "Basic tests for the SDDM service.")
   (value (run-sddm-test))))
xecute PROC in the context of a temporary git worktree created from COMMIT. PROC receives the temporary directory file name as an argument." (call-with-temporary-directory (lambda (tmp-directory) (dynamic-wind (lambda () #t) (lambda () (git-add-worktree tmp-directory commit) (proc tmp-directory)) (lambda () (invoke "git" "worktree" "remove" "--force" tmp-directory)))))) (define %savannah-guix-git-repo-push-url-regexp "git.(savannah|sv).gnu.org:?/srv/git/guix.git \\(push\\)") (define-syntax-rule (with-input-pipe-to-string prog arg ...) (let* ((input-pipe (open-pipe* OPEN_READ prog arg ...)) (output (get-string-all input-pipe)) (exit-val (status:exit-val (close-pipe input-pipe)))) (unless (zero? exit-val) (error (format #f "Command ~s exited with non-zero exit status: ~s" (string-join (list prog arg ...)) exit-val))) (string-trim-both output))) (define (find-origin-remote) "Find the name of the git remote with the Savannah Guix git repo URL." (and-let* ((remotes (string-split (with-input-pipe-to-string "git" "remote" "-v") #\newline)) (origin-entry (find (cut string-match %savannah-guix-git-repo-push-url-regexp <>) remotes))) (first (string-split origin-entry #\tab)))) (define (commit-already-pushed? remote commit) "True if COMMIT is found in the REMOTE repository." (not (string-null? (with-input-pipe-to-string "git" "branch" "-r" "--contains" commit (string-append remote "/master"))))) (define (keep-source-in-store store source) "Add SOURCE to the store under the name that the 'guix' package expects." ;; Add SOURCE to the store, but this time under the real name used in the ;; 'origin'. This allows us to build the package without having to make a ;; real checkout; thus, it also works when working on a private branch. (reload-module (resolve-module '(gnu packages package-management))) (let* ((source (add-to-store store (origin-file-name (package-source guix)) #t "sha256" source #:select? (git-predicate source))) (root (store-path-package-name source))) ;; Add an indirect GC root for SOURCE in the current directory. (false-if-exception (delete-file root)) (symlink source root) (add-indirect-root store (string-append (getcwd) "/" root)) (info (G_ "source code kept in ~a (GC root: ~a)~%") source root))) (define (main . args) (match args ((commit version) (with-directory-excursion %top-srcdir (or (getenv "GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT") (let ((remote (find-origin-remote))) (unless remote (leave (G_ "Failed to find the origin git remote.~%"))) (commit-already-pushed? remote commit)) (leave (G_ "Commit ~a is not pushed upstream. Aborting.~%") commit)) (call-with-temporary-git-worktree commit (lambda (tmp-directory) (let* ((hash (nix-base32-string->bytevector (string-trim-both (with-output-to-string (lambda () (guix-hash "-rx" tmp-directory)))))) (location (package-definition-location)) (old-hash (content-hash-value (origin-hash (package-source guix))))) (edit-expression location (update-definition commit hash #:old-hash old-hash #:version version)) ;; When GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT is set, the sources are ;; added to the store. This is used as part of 'make release'. (when (getenv "GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT") (with-store store (keep-source-in-store store tmp-directory)))))))) ((commit) ;; Automatically deduce the version and revision numbers. (main commit #f)))) (apply main (cdr (command-line)))