docker.scm 12 KB

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