singularity.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu tests singularity)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system vm)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services docker)
  25. #:use-module (gnu packages bash)
  26. #:use-module (gnu packages guile)
  27. #:use-module (gnu packages linux) ;singularity
  28. #:use-module (guix gexp)
  29. #:use-module (guix store)
  30. #:use-module (guix grafts)
  31. #:use-module (guix monads)
  32. #:use-module (guix packages)
  33. #:use-module (guix profiles)
  34. #:use-module (guix scripts pack)
  35. #:export (%test-singularity))
  36. (define %singularity-os
  37. (simple-operating-system
  38. (service singularity-service-type)
  39. (simple-service 'guest-account
  40. account-service-type
  41. (list (user-account (name "guest") (uid 1000) (group "guest"))
  42. (user-group (name "guest") (id 1000))))))
  43. (define (run-singularity-test image)
  44. "Load IMAGE, a Squashfs image, as a Singularity image and run it inside
  45. %SINGULARITY-OS."
  46. (define os
  47. (marionette-operating-system %singularity-os))
  48. (define singularity-exec
  49. #~(begin
  50. (use-modules (ice-9 popen) (rnrs io ports))
  51. (let* ((pipe (open-pipe* OPEN_READ
  52. #$(file-append singularity
  53. "/bin/singularity")
  54. "exec" #$image "/bin/guile"
  55. "-c" "(display \"hello, world\")"))
  56. (str (get-string-all pipe))
  57. (status (close-pipe pipe)))
  58. (and (zero? status)
  59. (string=? str "hello, world")))))
  60. (define test
  61. (with-imported-modules '((gnu build marionette))
  62. #~(begin
  63. (use-modules (srfi srfi-11) (srfi srfi-64)
  64. (gnu build marionette))
  65. (define marionette
  66. (make-marionette (list #$(virtual-machine os))))
  67. (test-runner-current (system-test-runner #$output))
  68. (test-begin "singularity")
  69. (test-assert "singularity exec /bin/guile (as root)"
  70. (marionette-eval '#$singularity-exec
  71. marionette))
  72. (test-equal "singularity exec /bin/guile (unprivileged)"
  73. 0
  74. (marionette-eval
  75. `(begin
  76. (use-modules (ice-9 match))
  77. (match (primitive-fork)
  78. (0
  79. (dynamic-wind
  80. (const #f)
  81. (lambda ()
  82. (setgid 1000)
  83. (setuid 1000)
  84. (execl #$(program-file "singularity-exec-test"
  85. #~(exit #$singularity-exec))
  86. "test"))
  87. (lambda ()
  88. (primitive-exit 127))))
  89. (pid
  90. (cdr (waitpid pid)))))
  91. marionette))
  92. (test-equal "singularity run" ;test the entry point
  93. 42
  94. (marionette-eval
  95. `(status:exit-val
  96. (system* #$(file-append singularity "/bin/singularity")
  97. "run" #$image "-c" "(exit 42)"))
  98. marionette))
  99. ;; FIXME: Singularity 2.x doesn't directly honor
  100. ;; /.singularity.d/env/*.sh. Instead, you have to load those files
  101. ;; manually, which we don't do. Remove 'test-skip' call once we've
  102. ;; switch to Singularity 3.x.
  103. (test-skip 1)
  104. (test-equal "singularity run, with environment"
  105. 0
  106. (marionette-eval
  107. ;; Check whether GUILE_LOAD_PATH is properly set, allowing us to
  108. ;; find the (json) module.
  109. `(status:exit-val
  110. (system* #$(file-append singularity "/bin/singularity")
  111. "--debug" "run" #$image "-c" "(use-modules (json))"))
  112. marionette))
  113. (test-end))))
  114. (gexp->derivation "singularity-test" test))
  115. (define (build-tarball&run-singularity-test)
  116. (mlet* %store-monad
  117. ((_ (set-grafting #f))
  118. (guile (set-guile-for-build (default-guile)))
  119. ;; 'singularity exec' insists on having /bin/sh in the image.
  120. (profile (profile-derivation (packages->manifest
  121. (list bash-minimal
  122. guile-2.2 guile-json-3))
  123. #:hooks '()
  124. #:locales? #f))
  125. (tarball (squashfs-image "singularity-pack" profile
  126. #:entry-point "bin/guile"
  127. #:symlinks '(("/bin" -> "bin")))))
  128. (run-singularity-test tarball)))
  129. (define %test-singularity
  130. (system-test
  131. (name "singularity")
  132. (description "Test Singularity container of Guix.")
  133. (value (build-tarball&run-singularity-test))))