aboutsummaryrefslogtreecommitdiff
path: root/gnu/tests/docker.scm
blob: 10882b9d1fd0a83f1973f1d83f77471e121ac027 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu tests docker)
  #:use-module (gnu tests)
  #:use-module (gnu system)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system vm)
  #:use-module (gnu services)
  #:use-module (gnu services dbus)
  #:use-module (gnu services networking)
  #:use-module (gnu services docker)
  #:use-module (gnu services desktop)
  #:use-module (gnu packages docker)
  #:use-module (gnu packages guile)
  #:use-module (guix gexp)
  #:use-module (guix grafts)
  #:use-module (guix monads)
  #:use-module (guix packages)
  #:use-module (guix profiles)
  #:use-module (guix scripts pack)
  #:use-module (guix store)
  #:use-module (guix tests)
  #:use-module (guix build-system trivial)
  #:use-module ((guix licenses) #:prefix license:)
  #:export (%test-docker
            %test-docker-system))

(define %docker-os
  (simple-operating-system
   (service dhcp-client-service-type)
   (dbus-service)
   (polkit-service)
   (service elogind-service-type)
   (service docker-service-type)))

(define (run-docker-test docker-tarball)
  "Load DOCKER-TARBALL as Docker image and run it in a Docker container,
inside %DOCKER-OS."
  (define os
    (marionette-operating-system
     %docker-os
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm
    (virtual-machine
     (operating-system os)
     (memory-size 700)
     (disk-image-size (* 1500 (expt 2 20)))
     (port-forwardings '())))

  (define test
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (srfi srfi-11) (srfi srfi-64)
                       (gnu build marionette))

          (define marionette
            (make-marionette (list #$vm)))

          (mkdir #$output)
          (chdir #$output)

          (test-begin "docker")

          (test-assert "service running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (match (start-service 'dockerd)
                  (#f #f)
                  (('service response-parts ...)
                   (match (assq-ref response-parts 'running)
                     ((pid) (number? pid))))))
             marionette))

          (test-eq "fetch version"
            0
            (marionette-eval
             `(begin
                (system* ,(string-append #$docker-cli "/bin/docker")
                         "version"))
             marionette))

          (test-equal "Load docker image and run it"
            '("hello world" "hi!" "JSON!" #o1777)
            (marionette-eval
             `(begin
                (define slurp
                  (lambda args
                    (let* ((port (apply open-pipe* OPEN_READ args))
                           (output (read-line port))
                           (status (close-pipe port)))
                      output)))
                (let* ((raw-line (slurp ,(string-append #$docker-cli
                                                        "/bin/docker")
                                                        "load" "-i"
                                                        ,#$docker-tarball))
                       (repository&tag (string-drop raw-line
                                                    (string-length
                                                     "Loaded image: ")))
                       (response1 (slurp
                                   ,(string-append #$docker-cli "/bin/docker")
                                   "run" "--entrypoint" "bin/Guile"
                                   repository&tag
                                   "/aa.scm"))
                       (response2 (slurp          ;default entry point
                                   ,(string-append #$docker-cli "/bin/docker")
                                   "run" repository&tag
                                   "-c" "(display \"hi!\")"))

                       ;; Check whether (json) is in $GUILE_LOAD_PATH.
                       (response3 (slurp    ;default entry point + environment
                                   ,(string-append #$docker-cli "/bin/docker")
                                   "run" repository&tag
                                   "-c" "(use-modules (json))
  (display (json-string->scm (scm->json-string \"JSON!\")))"))

                       ;; Check whether /tmp exists.
                       (response4 (slurp
                                   ,(string-append #$docker-cli "/bin/docker")
                                   "run" repository&tag "-c"
                                   "(display (stat:perms (lstat \"/tmp\")))")))
                  (list response1 response2 response3
                        (string->number response4))))
             marionette))

          (test-end)
          (exit (= (test-runner-fail-count (test-runner-current)) 0)))))

  (gexp->derivation "docker-test" test))

(define (build-tarball&run-docker-test)
  (mlet* %store-monad
      ((_ (set-grafting #f))
       (guile (set-guile-for-build (default-guile)))
       (guest-script-package ->
        (package
          (name "guest-script")
          (version "0")
          (source #f)
          (build-system trivial-build-system)
          (arguments `(#:guile ,guile-2.2
                       #:builder
                       (let ((out (assoc-ref %outputs "out")))
                         (mkdir out)
                         (call-with-output-file (string-append out "/a.scm")
                           (lambda (port)
                             (display "(display \"hello world\n\")" port)))
                         #t)))
          (synopsis "Display hello world using Guile")
          (description "This package displays the text \"hello world\" on the
standard output device and then enters a new line.")
          (home-page #f)
          (license license:public-domain)))
       (profile (profile-derivation (packages->manifest
                                     (list guile-2.2 guile-json-3
                                           guest-script-package))
                                    #:hooks '()
                                    #:locales? #f))
       (tarball (docker-image "docker-pack" profile
                              #:symlinks '(("/bin/Guile" -> "bin/guile")
                                           ("aa.scm" -> "a.scm"))
                              #:entry-point "bin/guile"
                              #:localstatedir? #t)))
    (run-docker-test tarball)))

(define %test-docker
  (system-test
   (name "docker")
   (description "Test Docker container of Guix.")
   (value (build-tarball&run-docker-test))))


(define (run-docker-system-test tarball)
  "Load DOCKER-TARBALL as Docker image and run it in a Docker container,
inside %DOCKER-OS."
  (define os
    (marionette-operating-system
     %docker-os
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm
    (virtual-machine
     (operating-system os)
     ;; FIXME: Because we're using the volatile-root setup where the root file
     ;; system is a tmpfs overlaid over a small root file system, 'docker
     ;; load' must be able to store the whole image into memory, hence the
     ;; huge memory requirements.  We should avoid the volatile-root setup
     ;; instead.
     (memory-size 3000)
     (port-forwardings '())))

  (define test
    (with-imported-modules '((gnu build marionette)
                             (guix build utils))
      #~(begin
          (use-modules (srfi srfi-11) (srfi srfi-64)
                       (gnu build marionette)
                       (guix build utils))

          (define marionette
            (make-marionette (list #$vm)))

          (mkdir #$output)
          (chdir #$output)

          (test-begin "docker")

          (test-assert "service running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (match (start-service 'dockerd)
                  (#f #f)
                  (('service response-parts ...)
                   (match (assq-ref response-parts 'running)
                     ((pid) (number? pid))))))
             marionette))

          (test-assert "load system image and run it"
            (marionette-eval
             `(begin
                (define (slurp command . args)
                  ;; Return the output from COMMAND.
                  (let* ((port (apply open-pipe* OPEN_READ command args))
                         (output (read-line port))
                         (status (close-pipe port)))
                    output))

                (define (docker-cli command . args)
                  ;; Run the given Docker COMMAND.
                  (apply invoke #$(file-append docker-cli "/bin/docker")
                         command args))

                (define (wait-for-container-file container file)
                  ;; Wait for FILE to show up in CONTAINER.
                  (docker-cli "exec" container
                              #$(file-append guile-2.2 "/bin/guile")
                              "-c"
                              (object->string
                               `(let loop ((n 15))
                                  (when (zero? n)
                                    (error "file didn't show up" ,file))
                                  (unless (file-exists? ,file)
                                    (sleep 1)
                                    (loop (- n 1)))))))

                (let* ((line (slurp #$(file-append docker-cli "/bin/docker")
                                    "load" "-i" #$tarball))
                       (repository&tag (string-drop line
                                                    (string-length
                                                     "Loaded image: ")))
                       (container (slurp
                                   #$(file-append docker-cli "/bin/docker")
                                   "create" repository&tag)))
                  (docker-cli "start" container)

                  ;; Wait for shepherd to be ready.
                  (wait-for-container-file container
                                           "/var/run/shepherd/socket")

                  (docker-cli "exec" container
                              "/run/current-system/profile/bin/herd"
                              "status")
                  (slurp #$(file-append docker-cli "/bin/docker")
                         "exec" container
                         "/run/current-system/profile/bin/herd"
                         "status" "guix-daemon")))
             marionette))

          (test-end)
          (exit (= (test-runner-fail-count (test-runner-current)) 0)))))

  (gexp->derivation "docker-system-test" test))

(define %test-docker-system
  (system-test
   (name "docker-system")
   (description "Run a system image as produced by @command{guix system
docker-image} inside Docker.")
   (value (with-monad %store-monad
            (>>= (system-docker-image (simple-operating-system))
                 run-docker-system-test)))))
"/include/lzo" "')")))))))) (inputs (list lzo)) (home-page "https://github.com/jd-boyd/python-lzo") (synopsis "Python bindings for the LZO data compression library") (description "Python-LZO provides Python bindings for LZO, i.e. you can access the LZO library from your Python scripts thereby compressing ordinary Python strings.") (license license:gpl2+))) (define-public python-lz4 (package (name "python-lz4") (version "4.3.2") (source (origin (method url-fetch) (uri (pypi-uri "lz4" version)) (sha256 (base32 "1nmc36j5xnk7mvwwpm0nb1sddjk5iv77h877fdkkxcngm621shz1")) (modules '((guix build utils))) (snippet '(begin ;; Remove bundled copy of lz4. (delete-file-recursively "lz4libs"))))) (build-system python-build-system) (arguments (list #:phases #~(modify-phases %standard-phases (replace 'check (lambda* (#:key tests? #:allow-other-keys) (when tests? ;; Taken from tox.ini (excludes experimental tests). (invoke "pytest" "-vv" "tests/block" "tests/frame"))))))) (native-inputs (list pkg-config python-pytest python-pkgconfig python-setuptools-scm ;; For tests. python-psutil)) (inputs (list lz4)) (home-page "https://github.com/python-lz4/python-lz4") (synopsis "LZ4 bindings for Python") (description "This package provides python bindings for the lz4 compression library by Yann Collet. The project contains bindings for the LZ4 block format and the LZ4 frame format.") (license license:bsd-3))) (define-public python-lzstring (package (name "python-lzstring") (version "1.0.4") (source (origin (method url-fetch) (uri (pypi-uri "lzstring" version)) (sha256 (base32 "18ly9pppy2yspxzw7k1b23wk77k7m44rz2g0271bqgqrk3jn3yhs")))) (build-system python-build-system) (propagated-inputs (list python-future)) (home-page "https://github.com/gkovacs/lz-string-python") (synopsis "String compression") (description "Lz-string is a string compressor library for Python.") (license license:expat))) (define-public python-python-snappy (package ;; PyPI contains both `snappy' and `python-snappy' as completely distinct ;; packages. To avoid a name collision in Guix, we use the variable name ;; `python-python-snappy' for the package called `python-snappy' on PyPI. (name "python-python-snappy") (version "0.6.1") (source (origin (method url-fetch) (uri (pypi-uri "python-snappy" version)) (sha256 (base32 "0amv12w0ybn6n1lk36x70a3l8bdjv4mn7iflb59wqsi00smhg8dn")))) (build-system python-build-system) (arguments '(#:phases (modify-phases %standard-phases (replace 'check (lambda* (#:key tests? #:allow-other-keys) (when tests? (invoke "pytest" "-vv" "-k" ;; CFFI is only supported for PyPy builds. (string-append "not test_snappy_cffi_enum " "and not test_snappy_all_cffi")))))))) (inputs (list snappy)) (native-inputs (list python-pytest)) (home-page "https://github.com/andrix/python-snappy") (synopsis "Python bindings for the Snappy compression library") (description "@code{python-python-snappy} provides bindings to the Snappy library and can be used to compress and decompress files and streams. It can also be used directly from the command line.") (license license:bsd-3))) (define-public bitshuffle (package (name "bitshuffle") (version "0.3.5") (source (origin (method url-fetch) (uri (pypi-uri "bitshuffle" version)) (sha256 (base32 "1823x61kyax4dc2hjmc1xraskxi1193y8lvxd03vqv029jrj8cjy")) (modules '((guix build utils))) (snippet '(begin ;; Remove generated Cython files. (delete-file "bitshuffle/h5.c") (delete-file "bitshuffle/ext.c") #t)))) (build-system python-build-system) (arguments `(#:tests? #f ; fail: https://github.com/h5py/h5py/issues/769 #:phases (modify-phases %standard-phases (add-after 'unpack 'fix-neon-detection ;; Neon is only for aarch64 ATM ;; see: https://github.com/kiyo-masui/bitshuffle/pull/73 (lambda _ (substitute* "src/bitshuffle_core.c" (("#define USEARMNEON") "#ifdef __aarch64__\n#define USEARMNEON\n#endif")) #t)) (add-after 'unpack 'dont-build-native (lambda _ (substitute* "setup.py" (("'-march=native', ") "")) #t))))) (inputs `(("numpy" ,python-numpy) ("h5py" ,python-h5py) ("hdf5" ,hdf5))) (native-inputs `(("cython" ,python-cython))) (home-page "https://github.com/kiyo-masui/bitshuffle") (synopsis "Filter for improving compression of typed binary data") (description "Bitshuffle is an algorithm that rearranges typed, binary data for improving compression, as well as a python/C package that implements this algorithm within the Numpy framework.") (license license:expat))) (define-public bitshuffle-for-snappy (package/inherit bitshuffle (name "bitshuffle-for-snappy") (build-system gnu-build-system) (arguments (substitute-keyword-arguments (package-arguments bitshuffle) ((#:tests? _ #f) #f) ((#:phases phases) `(modify-phases %standard-phases (replace 'configure (lambda* (#:key outputs #:allow-other-keys) (with-output-to-file "Makefile" (lambda _ (format #t "\ libbitshuffle.so: src/bitshuffle.o src/bitshuffle_core.o src/iochain.o lz4/lz4.o \tgcc -O3 -ffast-math -std=c99 -o $@ -shared -fPIC $^ %.o: %.c \tgcc -O3 -ffast-math -std=c99 -fPIC -Isrc -Ilz4 -c $< -o $@ PREFIX:=~a LIBDIR:=$(PREFIX)/lib INCLUDEDIR:=$(PREFIX)/include install: libbitshuffle.so \tinstall -dm755 $(LIBDIR) \tinstall -dm755 $(INCLUDEDIR) \tinstall -m755 libbitshuffle.so $(LIBDIR) \tinstall -m644 src/bitshuffle.h $(INCLUDEDIR) \tinstall -m644 src/bitshuffle_core.h $(INCLUDEDIR) \tinstall -m644 src/iochain.h $(INCLUDEDIR) \tinstall -m644 lz4/lz4.h $(INCLUDEDIR) " (assoc-ref outputs "out")))) #t)))))) (inputs '()) (native-inputs '()))) (define-public python-zipp (package (name "python-zipp") (version "1.0.0") (source (origin (method url-fetch) (uri (pypi-uri "zipp" version)) (sha256 (base32 "0v3qayhqv7vyzydpydwcp51bqciw8p2ajddw68x5k8zppc0vx3yk")))) (build-system python-build-system) (propagated-inputs (list python-more-itertools)) (native-inputs (list python-setuptools-scm)) (home-page "https://github.com/jaraco/zipp") (synopsis "Backport of pathlib-compatible object wrapper for zip files") (description "This package provides a @code{pathlib}-compatible @code{Zipfile} object wrapper. It provides a backport of the @code{Path} object.") (license license:expat))) (define-public python-zipstream-ng (package (name "python-zipstream-ng") (version "1.7.1") (source (origin (method url-fetch) (uri (pypi-uri "zipstream-ng" version)) (sha256 (base32 "1z4zdqqs2rg3z36khgj96bpggv34av337isfv7yxg32prawj687r")))) (build-system pyproject-build-system) (native-inputs (list python-pytest python-pytest-cov python-setuptools python-wheel)) (home-page "https://github.com/pR0Ps/zipstream-ng") (synopsis "Streamable zip file generator") (description "This package provides a modern and easy to use streamable zip file generator") (license license:lgpl3))) (define-public python-zopfli (package (name "python-zopfli") (version "0.2.2") (source (origin (method url-fetch) (uri (pypi-uri "zopfli" version ".zip")) (sha256 (base32 "1z1akqx3fjnwa75insch9p08hafikqdvqkj6mxv1k6fr81sxnj9d")))) (build-system python-build-system) (arguments (list #:phases #~(modify-phases %standard-phases (add-after 'unpack 'use-system-zopfli (lambda _ (setenv "USE_SYSTEM_ZOPFLI" "1"))) (add-before 'build 'set-version (lambda _ (setenv "SETUPTOOLS_SCM_PRETEND_VERSION" #$version))) (replace 'check (lambda* (#:key tests? #:allow-other-keys) (when tests? (invoke "pytest" "-vv"))))))) (native-inputs (list unzip python-pytest python-setuptools-scm)) (inputs (list zopfli)) (home-page "https://github.com/fonttools/py-zopfli") (synopsis "Python bindings for Zopfli") (description "@code{pyzopfli} is a straight forward wrapper around the @code{ZlibCompress} method of the the @code{zopfli} library.") (license license:asl2.0))) (define-public python-zstandard (package (name "python-zstandard") (version "0.19.0") (source (origin (method url-fetch) (uri (pypi-uri "zstandard" version)) (sha256 (base32 "0qvqhs121spk7yc1l20samflxx47waxv3xm55ksxpn1djk6jzl9i")))) (build-system python-build-system) (propagated-inputs (list python-cffi)) (native-inputs (list python-hypothesis)) (home-page "https://github.com/indygreg/python-zstandard") (synopsis "Zstandard bindings for Python") (description "This project provides Python bindings for interfacing with the Zstandard compression library. A C extension and CFFI interface are provided.") (license license:bsd-3))) (define-public python-pyzstd (package (name "python-pyzstd") (version "0.15.9") (source (origin (method url-fetch) (uri (pypi-uri "pyzstd" version)) (sha256 (base32 "1iycfmif15v1jhv0gsza1hyd1hn3sz0vn9s1y79abzv8axndxzfb")) (modules '((guix build utils))) (snippet '(begin ;; Remove a bundled copy of the zstd sources. (delete-file-recursively "zstd"))))) (build-system python-build-system) (arguments (list #:configure-flags #~(list "--dynamic-link-zstd") #:phases #~(modify-phases %standard-phases (replace 'build ;; The python-build-system's phase doesn't honour configure-flags. (lambda* (#:key configure-flags #:allow-other-keys) (apply invoke "python" "./setup.py" "build" configure-flags))) (replace 'check ;; The python-build-system's phase doesn't honour configure-flags. (lambda* (#:key tests? test-target configure-flags #:allow-other-keys) (when tests? (apply invoke "python" "./setup.py" test-target configure-flags))))))) (inputs (list `(,zstd "lib"))) (home-page "https://github.com/animalize/pyzstd") (synopsis "Zstandard bindings for Python") (description "This package provides Python bindings to the Zstandard (zstd) compression library. The API is similar to Python's bz2/lzma/zlib module.") (license license:bsd-3)))