;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2019 Danny Milosavljevic ;;; Copyright © 2019-2023 Ludovic Courtès ;;; Copyright © 2024 Giacomo Leidi ;;; ;;; 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 . (define-module (gnu tests docker) #:use-module (gnu image) #:use-module (gnu tests) #:use-module (gnu system) #:use-module (gnu system file
aboutsummaryrefslogtreecommitdiff
blob: 042887ea9b326c22ed732fde4b32a8ad8d2e212b (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
# GNU Guix --- Functional package management for GNU
# Copyright © 2018, 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/>.

#
# Test the 'guix pack --localstatedir' command-line utility.
#

guix pack --version

# 'guix pack --localstatedir' produces derivations that depend on
# guile-sqlite3 and guile-gcrypt.  To make that relatively inexpensive, run
# the test in the user's global store if possible, on the grounds that
# binaries may already be there or can be built or downloaded inexpensively.

storedir="`guile -c '(use-modules (guix config))(display %storedir)'`"
localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`"
NIX_STORE_DIR="$storedir"
GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
export NIX_STORE_DIR GUIX_DAEMON_SOCKET

if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
then
    exit 77
fi

# Build a tarball with '--localstatedir'
the_pack="`guix pack -C none --localstatedir --profile-name=current-guix \
            guile-bootstrap`"
test_directory="`mktemp -d`"
trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT

cd "$test_directory"
tar -xf "$the_pack"

profile="`find -name current-guix`"
test "`readlink $profile`" = "current-guix-1-link"
test -s "`dirname $profile`/../../../db/db.sqlite"
test -x ".`guix build guile-bootstrap`/bin/guile"
cd -

# Make sure the store database is not completely bogus.
guile -c "(use-modules (sqlite3) (guix config) (ice-9 match))

  (define db
    (sqlite-open (string-append \"$test_directory\"
                                %localstatedir
                               \"/guix/db/db.sqlite\")
                 SQLITE_OPEN_READONLY))

  (define stmt
    (sqlite-prepare db \"SELECT * FROM ValidPaths;\"))

  (match (sqlite-fold cons '() stmt)
    ((#(ids paths hashes times derivers sizes) ...)
     (exit (member \"`guix build guile-bootstrap`\" paths))))"
stem-test tarball) "Load DOCKER-TARBALL as Docker image and run it in a Docker container, inside %DOCKER-OS." (define os (marionette-operating-system (operating-system-with-gc-roots %docker-os (list tarball)) #:imported-modules '((gnu services herd) (guix combinators)))) (define vm (virtual-machine (operating-system os) (volatile? #f) (disk-image-size (* 6000 (expt 2 20))) (memory-size 2048) (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 ;; Relax timeout to accommodate older systems. (make-marionette (list #$vm) #:timeout 60)) (test-runner-current (system-test-runner #$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 (use-modules (ice-9 popen) (ice-9 rdelim) (guix build utils)) (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-3.0 "/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)))) (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 (>>= (lower-object (system-image (os->image (operating-system (inherit (simple-operating-system)) ;; Use locales for a single libc to ;; reduce space requirements. (locale-libcs (list glibc))) #:type docker-image-type))) run-docker-system-test))))) (define %oci-os (simple-operating-system (service dhcp-client-service-type) (service dbus-root-service-type) (service polkit-service-type) (service elogind-service-type) (service docker-service-type) (extra-special-file "/shared.txt" (plain-file "shared.txt" "hello")) (service oci-container-service-type (list (oci-container-configuration (image (oci-image (repository "guile") (value (specifications->manifest '("guile"))) (pack-options '(#:symlinks (("/bin" -> "bin")))))) (entrypoint "/bin/guile") (command '("-c" "(let l ((c 300))(display c)(sleep 1)(when(positive? c)(l (- c 1))))")) (host-environment '(("VARIABLE" . "value"))) (volumes '(("/shared.txt" . "/shared.txt:ro"))) (extra-arguments '("--env" "VARIABLE"))))))) (define (run-oci-container-test) "Run IMAGE as an OCI backed Shepherd service, inside OS." (define os (marionette-operating-system (operating-system-with-gc-roots %oci-os (list)) #:imported-modules '((gnu services herd) (guix combinators)))) (define vm (virtual-machine (operating-system os) (volatile? #f) (memory-size 1024) (disk-image-size (* 3000 (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 ;; Relax timeout to accommodate older systems and ;; allow for pulling the image. (make-marionette (list #$vm) #:timeout 60)) (test-runner-current (system-test-runner #$output)) (test-begin "oci-container") (test-assert "dockerd 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)) (sleep 10) ; let service start (test-assert "docker-guile running" (marionette-eval '(begin (use-modules (gnu services herd)) (match (start-service 'docker-guile) (#f #f) (('service response-parts ...) (match (assq-ref response-parts 'running) ((pid) (number? pid)))))) marionette)) (test-equal "passing host environment variables and volumes" '("value" "hello") (marionette-eval `(begin (use-modules (ice-9 popen) (ice-9 rdelim)) (define slurp (lambda args (let* ((port (apply open-pipe* OPEN_READ args)) (output (let ((line (read-line port))) (if (eof-object? line) "" line))) (status (close-pipe port))) output))) (let* ((response1 (slurp ,(string-append #$docker-cli "/bin/docker") "exec" "docker-guile" "/bin/guile" "-c" "(display (getenv \"VARIABLE\"))")) (response2 (slurp ,(string-append #$docker-cli "/bin/docker") "exec" "docker-guile" "/bin/guile" "-c" "(begin (use-modules (ice-9 popen) (ice-9 rdelim)) (display (call-with-input-file \"/shared.txt\" read-line)))"))) (list response1 response2))) marionette)) (test-end)))) (gexp->derivation "oci-container-test" test)) (define %test-oci-container (system-test (name "oci-container") (description "Test OCI backed Shepherd service.") (value (run-oci-container-test))))