processes.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-processes)
  20. #:use-module (guix scripts processes)
  21. #:use-module (guix store)
  22. #:use-module (guix derivations)
  23. #:use-module (guix packages)
  24. #:use-module (guix gexp)
  25. #:use-module ((guix utils) #:select (call-with-temporary-directory))
  26. #:use-module (gnu packages bootstrap)
  27. #:use-module (guix tests)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-64)
  30. #:use-module (rnrs bytevectors)
  31. #:use-module (rnrs io ports)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 threads))
  34. ;; When using --system argument, binfmt-misc mechanism may be used. In that
  35. ;; case, (guix script processes) won't work because:
  36. ;;
  37. ;; * ARGV0 is qemu-user and not guix-daemon.
  38. ;; * Guix-daemon won't be able to stuff client PID in ARGV1 of forked
  39. ;; processes.
  40. ;;
  41. ;; See: https://lists.gnu.org/archive/html/bug-guix/2019-12/msg00017.html.
  42. ;;
  43. ;; If we detect that we are running with binfmt emulation, all the following
  44. ;; tests must be skipped.
  45. (define (binfmt-misc?)
  46. (let ((pid (getpid))
  47. (cmdline (call-with-input-file "/proc/self/cmdline" get-string-all)))
  48. (match (primitive-fork)
  49. (0 (dynamic-wind
  50. (const #t)
  51. (lambda ()
  52. (exit
  53. (not (equal?
  54. (call-with-input-file (format #f "/proc/~a/cmdline" pid)
  55. get-string-all)
  56. cmdline))))
  57. (const #t)))
  58. (x (zero? (cdr (waitpid x)))))))
  59. (define-syntax-rule (test-assert* description exp)
  60. (begin
  61. (when (binfmt-misc?)
  62. (test-skip 1))
  63. (test-assert description exp)))
  64. (test-begin "processes")
  65. (test-assert* "not a client"
  66. (not (find (lambda (session)
  67. (= (getpid)
  68. (process-id (daemon-session-client session))))
  69. (daemon-sessions))))
  70. (test-assert* "client"
  71. (with-store store
  72. (let* ((session (find (lambda (session)
  73. (= (getpid)
  74. (process-id (daemon-session-client session))))
  75. (daemon-sessions)))
  76. (daemon (daemon-session-process session)))
  77. (and (kill (process-id daemon) 0)
  78. (string-suffix? "guix-daemon" (first (process-command daemon)))))))
  79. (test-assert* "client + lock"
  80. (with-store store
  81. (call-with-temporary-directory
  82. (lambda (directory)
  83. (let* ((token1 (string-append directory "/token1"))
  84. (token2 (string-append directory "/token2"))
  85. (exp #~(begin #$(random-text)
  86. (mkdir #$token1)
  87. (let loop ()
  88. (unless (file-exists? #$token2)
  89. (sleep 1)
  90. (loop)))
  91. (mkdir #$output)))
  92. (guile (package-derivation store %bootstrap-guile))
  93. (drv (run-with-store store
  94. (gexp->derivation "foo" exp
  95. #:guile-for-build guile)))
  96. (thread (call-with-new-thread
  97. (lambda ()
  98. (build-derivations store (list drv)))))
  99. (_ (let loop ()
  100. (unless (file-exists? token1)
  101. (usleep 200)
  102. (loop))))
  103. (session (find (lambda (session)
  104. (= (getpid)
  105. (process-id (daemon-session-client session))))
  106. (daemon-sessions)))
  107. (locks (daemon-session-locks-held (pk 'session session))))
  108. (call-with-output-file token2 (const #t))
  109. (equal? (list (string-append (derivation->output-path drv) ".lock"))
  110. locks))))))
  111. (test-end "processes")