docker.scm 12 KB

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