docker.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
  3. ;;; Copyright © 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  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 (gnu tests docker)
  20. #:use-module (gnu tests)
  21. #:use-module (gnu system)
  22. #:use-module (gnu system file-systems)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services dbus)
  26. #:use-module (gnu services networking)
  27. #:use-module (gnu services docker)
  28. #:use-module (gnu services desktop)
  29. #:use-module ((gnu packages base) #:select (glibc))
  30. #:use-module (gnu packages guile)
  31. #:use-module (gnu packages docker)
  32. #:use-module (guix gexp)
  33. #:use-module (guix grafts)
  34. #:use-module (guix monads)
  35. #:use-module (guix packages)
  36. #:use-module (guix profiles)
  37. #:use-module (guix scripts pack)
  38. #:use-module (guix store)
  39. #:use-module (guix tests)
  40. #:use-module (guix build-system trivial)
  41. #:use-module ((guix licenses) #:prefix license:)
  42. #:export (%test-docker
  43. %test-docker-system))
  44. (define %docker-os
  45. (simple-operating-system
  46. (service dhcp-client-service-type)
  47. (dbus-service)
  48. (polkit-service)
  49. (service elogind-service-type)
  50. (service docker-service-type)))
  51. (define (run-docker-test docker-tarball)
  52. "Load DOCKER-TARBALL as Docker image and run it in a Docker container,
  53. inside %DOCKER-OS."
  54. (define os
  55. (marionette-operating-system
  56. %docker-os
  57. #:imported-modules '((gnu services herd)
  58. (guix combinators))))
  59. (define vm
  60. (virtual-machine
  61. (operating-system os)
  62. (memory-size 700)
  63. (disk-image-size (* 1500 (expt 2 20)))
  64. (port-forwardings '())))
  65. (define test
  66. (with-imported-modules '((gnu build marionette))
  67. #~(begin
  68. (use-modules (srfi srfi-11) (srfi srfi-64)
  69. (gnu build marionette))
  70. (define marionette
  71. (make-marionette (list #$vm)))
  72. (mkdir #$output)
  73. (chdir #$output)
  74. (test-begin "docker")
  75. (test-assert "service running"
  76. (marionette-eval
  77. '(begin
  78. (use-modules (gnu services herd))
  79. (match (start-service 'dockerd)
  80. (#f #f)
  81. (('service response-parts ...)
  82. (match (assq-ref response-parts 'running)
  83. ((pid) (number? pid))))))
  84. marionette))
  85. (test-eq "fetch version"
  86. 0
  87. (marionette-eval
  88. `(begin
  89. (system* ,(string-append #$docker-cli "/bin/docker")
  90. "version"))
  91. marionette))
  92. (test-equal "Load docker image and run it"
  93. '("hello world" "hi!" "JSON!" #o1777)
  94. (marionette-eval
  95. `(begin
  96. (define slurp
  97. (lambda args
  98. (let* ((port (apply open-pipe* OPEN_READ args))
  99. (output (read-line port))
  100. (status (close-pipe port)))
  101. output)))
  102. (let* ((raw-line (slurp ,(string-append #$docker-cli
  103. "/bin/docker")
  104. "load" "-i"
  105. ,#$docker-tarball))
  106. (repository&tag (string-drop raw-line
  107. (string-length
  108. "Loaded image: ")))
  109. (response1 (slurp
  110. ,(string-append #$docker-cli "/bin/docker")
  111. "run" "--entrypoint" "bin/Guile"
  112. repository&tag
  113. "/aa.scm"))
  114. (response2 (slurp ;default entry point
  115. ,(string-append #$docker-cli "/bin/docker")
  116. "run" repository&tag
  117. "-c" "(display \"hi!\")"))
  118. ;; Check whether (json) is in $GUILE_LOAD_PATH.
  119. (response3 (slurp ;default entry point + environment
  120. ,(string-append #$docker-cli "/bin/docker")
  121. "run" repository&tag
  122. "-c" "(use-modules (json))
  123. (display (json-string->scm (scm->json-string \"JSON!\")))"))
  124. ;; Check whether /tmp exists.
  125. (response4 (slurp
  126. ,(string-append #$docker-cli "/bin/docker")
  127. "run" repository&tag "-c"
  128. "(display (stat:perms (lstat \"/tmp\")))")))
  129. (list response1 response2 response3
  130. (string->number response4))))
  131. marionette))
  132. (test-end)
  133. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  134. (gexp->derivation "docker-test" test))
  135. (define (build-tarball&run-docker-test)
  136. (mlet* %store-monad
  137. ((_ (set-grafting #f))
  138. (guile (set-guile-for-build (default-guile)))
  139. (guest-script-package ->
  140. (package
  141. (name "guest-script")
  142. (version "0")
  143. (source #f)
  144. (build-system trivial-build-system)
  145. (arguments `(#:guile ,guile-3.0
  146. #:builder
  147. (let ((out (assoc-ref %outputs "out")))
  148. (mkdir out)
  149. (call-with-output-file (string-append out "/a.scm")
  150. (lambda (port)
  151. (display "(display \"hello world\n\")" port)))
  152. #t)))
  153. (synopsis "Display hello world using Guile")
  154. (description "This package displays the text \"hello world\" on the
  155. standard output device and then enters a new line.")
  156. (home-page #f)
  157. (license license:public-domain)))
  158. (profile (profile-derivation (packages->manifest
  159. (list guile-3.0 guile-json-3
  160. guest-script-package))
  161. #:hooks '()
  162. #:locales? #f))
  163. (tarball (docker-image "docker-pack" profile
  164. #:symlinks '(("/bin/Guile" -> "bin/guile")
  165. ("aa.scm" -> "a.scm"))
  166. #:entry-point "bin/guile"
  167. #:localstatedir? #t)))
  168. (run-docker-test tarball)))
  169. (define %test-docker
  170. (system-test
  171. (name "docker")
  172. (description "Test Docker container of Guix.")
  173. (value (build-tarball&run-docker-test))))
  174. (define (run-docker-system-test tarball)
  175. "Load DOCKER-TARBALL as Docker image and run it in a Docker container,
  176. inside %DOCKER-OS."
  177. (define os
  178. (marionette-operating-system
  179. %docker-os
  180. #:imported-modules '((gnu services herd)
  181. (guix combinators))))
  182. (define vm
  183. (virtual-machine
  184. (operating-system os)
  185. ;; FIXME: Because we're using the volatile-root setup where the root file
  186. ;; system is a tmpfs overlaid over a small root file system, 'docker
  187. ;; load' must be able to store the whole image into memory, hence the
  188. ;; huge memory requirements. We should avoid the volatile-root setup
  189. ;; instead.
  190. (memory-size 4500)
  191. (port-forwardings '())))
  192. (define test
  193. (with-imported-modules '((gnu build marionette)
  194. (guix build utils))
  195. #~(begin
  196. (use-modules (srfi srfi-11) (srfi srfi-64)
  197. (gnu build marionette)
  198. (guix build utils))
  199. (define marionette
  200. (make-marionette (list #$vm)))
  201. (mkdir #$output)
  202. (chdir #$output)
  203. (test-begin "docker")
  204. (test-assert "service running"
  205. (marionette-eval
  206. '(begin
  207. (use-modules (gnu services herd))
  208. (match (start-service 'dockerd)
  209. (#f #f)
  210. (('service response-parts ...)
  211. (match (assq-ref response-parts 'running)
  212. ((pid) (number? pid))))))
  213. marionette))
  214. (test-assert "load system image and run it"
  215. (marionette-eval
  216. `(begin
  217. (define (slurp command . args)
  218. ;; Return the output from COMMAND.
  219. (let* ((port (apply open-pipe* OPEN_READ command args))
  220. (output (read-line port))
  221. (status (close-pipe port)))
  222. output))
  223. (define (docker-cli command . args)
  224. ;; Run the given Docker COMMAND.
  225. (apply invoke #$(file-append docker-cli "/bin/docker")
  226. command args))
  227. (define (wait-for-container-file container file)
  228. ;; Wait for FILE to show up in CONTAINER.
  229. (docker-cli "exec" container
  230. #$(file-append guile-3.0 "/bin/guile")
  231. "-c"
  232. (object->string
  233. `(let loop ((n 15))
  234. (when (zero? n)
  235. (error "file didn't show up" ,file))
  236. (unless (file-exists? ,file)
  237. (sleep 1)
  238. (loop (- n 1)))))))
  239. (let* ((line (slurp #$(file-append docker-cli "/bin/docker")
  240. "load" "-i" #$tarball))
  241. (repository&tag (string-drop line
  242. (string-length
  243. "Loaded image: ")))
  244. (container (slurp
  245. #$(file-append docker-cli "/bin/docker")
  246. "create" repository&tag)))
  247. (docker-cli "start" container)
  248. ;; Wait for shepherd to be ready.
  249. (wait-for-container-file container
  250. "/var/run/shepherd/socket")
  251. (docker-cli "exec" container
  252. "/run/current-system/profile/bin/herd"
  253. "status")
  254. (slurp #$(file-append docker-cli "/bin/docker")
  255. "exec" container
  256. "/run/current-system/profile/bin/herd"
  257. "status" "guix-daemon")))
  258. marionette))
  259. (test-end)
  260. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  261. (gexp->derivation "docker-system-test" test))
  262. (define %test-docker-system
  263. (system-test
  264. (name "docker-system")
  265. (description "Run a system image as produced by @command{guix system
  266. docker-image} inside Docker.")
  267. (value (with-monad %store-monad
  268. (>>= (system-docker-image (operating-system
  269. (inherit (simple-operating-system))
  270. ;; Use locales for a single libc to
  271. ;; reduce space requirements.
  272. (locale-libcs (list glibc)))
  273. #:memory-size 1024)
  274. run-docker-system-test)))))