1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (test-containers)
21 #:use-module (guix utils)
22 #:use-module (guix build syscalls)
23 #:use-module (gnu build linux-container)
24 #:use-module (gnu system file-systems)
25 #:use-module (srfi srfi-64)
26 #:use-module (ice-9 match))
28 (define (assert-exit x)
29 (primitive-exit (if x 0 1)))
31 (test-begin "containers")
33 ;; Skip these tests unless user namespaces are available and the setgroups
34 ;; file (introduced in Linux 3.19 to address a security issue) exists.
35 (define (skip-if-unsupported)
36 (unless (and (user-namespace-supported?)
37 (unprivileged-user-namespace-supported?)
38 (setgroups-supported?))
42 (test-assert "call-with-container, exit with 0 when there is no error"
44 (call-with-container '() (const #t) #:namespaces '(user))))
47 (test-assert "call-with-container, user namespace"
49 (call-with-container '()
51 ;; The user is root within the new user namespace.
52 (assert-exit (and (zero? (getuid)) (zero? (getgid)))))
53 #:namespaces '(user))))
56 (test-assert "call-with-container, user namespace, guest UID/GID"
58 (call-with-container '()
60 (assert-exit (and (= 42 (getuid)) (= 77 (getgid)))))
63 #:namespaces '(user))))
66 (test-assert "call-with-container, uts namespace"
68 (call-with-container '()
70 ;; The user is root within the container and should be able to change
71 ;; the hostname of that container.
72 (sethostname "test-container")
74 #:namespaces '(user uts))))
77 (test-assert "call-with-container, pid namespace"
79 (call-with-container '()
81 (match (primitive-fork)
83 ;; The first forked process in the new pid namespace is pid 2.
84 (assert-exit (= 2 (getpid))))
89 (status:exit-val status)))))))
90 #:namespaces '(user pid))))
93 (test-assert "call-with-container, mnt namespace"
95 (call-with-container (list (file-system
97 (mount-point "/testing")
101 (assert-exit (file-exists? "/testing")))
102 #:namespaces '(user mnt))))
104 (skip-if-unsupported)
105 (test-equal "call-with-container, mnt namespace, wrong bind mount"
106 `(system-error ,ENOENT)
107 ;; An exception should be raised; see <http://bugs.gnu.org/23306>.
110 (call-with-container (list (file-system
111 (device "/does-not-exist")
114 (flags '(bind-mount))
117 #:namespaces '(user mnt)))
119 (list 'system-error (system-error-errno args)))))
121 (skip-if-unsupported)
122 (test-assert "call-with-container, all namespaces"
124 (call-with-container '()
126 (primitive-exit 0)))))
128 (skip-if-unsupported)
129 (test-assert "container-excursion"
130 (call-with-temporary-directory
132 ;; Two pipes: One for the container to signal that the test can begin,
133 ;; and one for the parent to signal to the container that the test is
135 (match (list (pipe) (pipe))
136 (((start-in . start-out) (end-in . end-out))
140 ;; Signal for the test to start.
141 (write 'ready start-out)
143 ;; Wait for test completion.
147 (define (namespaces pid)
148 (let ((pid (number->string pid)))
150 (readlink (string-append "/proc/" pid "/ns/" ns)))
151 '("user" "ipc" "uts" "net" "pid" "mnt"))))
153 (let* ((pid (run-container root '() %namespaces 1 container))
154 (container-namespaces (namespaces pid))
158 ;; Wait for container to be ready.
161 (container-excursion pid
163 ;; Fork again so that the pid is within the context of
164 ;; the joined pid namespace instead of the original pid
166 (match (primitive-fork)
168 ;; Check that all of the namespace identifiers are
169 ;; the same as the container process.
171 (equal? container-namespaces
172 (namespaces (getpid)))))
174 (match (waitpid fork-pid)
177 (status:exit-val status)))))))))))
179 ;; Stop the container.
180 (write 'done end-out)
185 (skip-if-unsupported)
186 (test-equal "container-excursion, same namespaces"
188 ;; The parent and child are in the same namespaces. 'container-excursion'
189 ;; should notice that and avoid calling 'setns' since that would fail.
190 (container-excursion (getpid)
192 (primitive-exit 42))))
194 (skip-if-unsupported)
195 (test-assert "container-excursion*"
196 (call-with-temporary-directory
198 (define (namespaces pid)
199 (let ((pid (number->string pid)))
201 (readlink (string-append "/proc/" pid "/ns/" ns)))
202 '("user" "ipc" "uts" "net" "pid" "mnt"))))
204 (let* ((pid (run-container root '()
208 (expected (namespaces pid))
209 (result (container-excursion* pid
213 (equal? result expected)))))
215 (skip-if-unsupported)
216 (test-equal "container-excursion*, same namespaces"
218 (container-excursion* (getpid)