docker.scm 12 KB

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