singularity.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 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. (mkdir #$output)
  68. (chdir #$output)
  69. (test-begin "singularity")
  70. (test-assert "singularity exec /bin/guile (as root)"
  71. (marionette-eval '#$singularity-exec
  72. marionette))
  73. (test-equal "singularity exec /bin/guile (unprivileged)"
  74. 0
  75. (marionette-eval
  76. `(begin
  77. (use-modules (ice-9 match))
  78. (match (primitive-fork)
  79. (0
  80. (dynamic-wind
  81. (const #f)
  82. (lambda ()
  83. (setgid 1000)
  84. (setuid 1000)
  85. (execl #$(program-file "singularity-exec-test"
  86. #~(exit #$singularity-exec))
  87. "test"))
  88. (lambda ()
  89. (primitive-exit 127))))
  90. (pid
  91. (cdr (waitpid pid)))))
  92. marionette))
  93. (test-equal "singularity run" ;test the entry point
  94. 42
  95. (marionette-eval
  96. `(status:exit-val
  97. (system* #$(file-append singularity "/bin/singularity")
  98. "run" #$image "-c" "(exit 42)"))
  99. marionette))
  100. ;; FIXME: Singularity 2.x doesn't directly honor
  101. ;; /.singularity.d/env/*.sh. Instead, you have to load those files
  102. ;; manually, which we don't do. Remove 'test-skip' call once we've
  103. ;; switch to Singularity 3.x.
  104. (test-skip 1)
  105. (test-equal "singularity run, with environment"
  106. 0
  107. (marionette-eval
  108. ;; Check whether GUILE_LOAD_PATH is properly set, allowing us to
  109. ;; find the (json) module.
  110. `(status:exit-val
  111. (system* #$(file-append singularity "/bin/singularity")
  112. "--debug" "run" #$image "-c" "(use-modules (json))"))
  113. marionette))
  114. (test-end)
  115. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  116. (gexp->derivation "singularity-test" test))
  117. (define (build-tarball&run-singularity-test)
  118. (mlet* %store-monad
  119. ((_ (set-grafting #f))
  120. (guile (set-guile-for-build (default-guile)))
  121. ;; 'singularity exec' insists on having /bin/sh in the image.
  122. (profile (profile-derivation (packages->manifest
  123. (list bash-minimal
  124. guile-2.2 guile-json-3))
  125. #:hooks '()
  126. #:locales? #f))
  127. (tarball (squashfs-image "singularity-pack" profile
  128. #:entry-point "bin/guile"
  129. #:symlinks '(("/bin" -> "bin")))))
  130. (run-singularity-test tarball)))
  131. (define %test-singularity
  132. (system-test
  133. (name "singularity")
  134. (description "Test Singularity container of Guix.")
  135. (value (build-tarball&run-singularity-test))))