1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (test-containers)
20 #:use-module (guix utils)
21 #:use-module (guix build syscalls)
22 #:use-module (gnu build linux-container)
23 #:use-module (gnu system file-systems)
24 #:use-module (srfi srfi-64)
25 #:use-module (ice-9 match))
27 (define (assert-exit x)
28 (primitive-exit (if x 0 1)))
30 (test-begin "containers")
32 ;; Skip these tests unless user namespaces are available and the setgroups
33 ;; file (introduced in Linux 3.19 to address a security issue) exists.
34 (define (skip-if-unsupported)
35 (unless (and (user-namespace-supported?)
36 (unprivileged-user-namespace-supported?)
37 (setgroups-supported?))
41 (test-assert "call-with-container, exit with 0 when there is no error"
43 (call-with-container '() (const #t) #:namespaces '(user))))
46 (test-assert "call-with-container, user namespace"
48 (call-with-container '()
50 ;; The user is root within the new user namespace.
51 (assert-exit (and (zero? (getuid)) (zero? (getgid)))))
52 #:namespaces '(user))))
55 (test-assert "call-with-container, uts namespace"
57 (call-with-container '()
59 ;; The user is root within the container and should be able to change
60 ;; the hostname of that container.
61 (sethostname "test-container")
63 #:namespaces '(user uts))))
66 (test-assert "call-with-container, pid namespace"
68 (call-with-container '()
70 (match (primitive-fork)
72 ;; The first forked process in the new pid namespace is pid 2.
73 (assert-exit (= 2 (getpid))))
78 (status:exit-val status)))))))
79 #:namespaces '(user pid))))
82 (test-assert "call-with-container, mnt namespace"
84 (call-with-container (list (file-system
86 (mount-point "/testing")
90 (assert-exit (file-exists? "/testing")))
91 #:namespaces '(user mnt))))
94 (test-equal "call-with-container, mnt namespace, wrong bind mount"
95 `(system-error ,ENOENT)
96 ;; An exception should be raised; see <http://bugs.gnu.org/23306>.
99 (call-with-container (list (file-system
100 (device "/does-not-exist")
103 (flags '(bind-mount))
106 #:namespaces '(user mnt)))
108 (list 'system-error (system-error-errno args)))))
110 (skip-if-unsupported)
111 (test-assert "call-with-container, all namespaces"
113 (call-with-container '()
115 (primitive-exit 0)))))
117 (skip-if-unsupported)
118 (test-assert "container-excursion"
119 (call-with-temporary-directory
121 ;; Two pipes: One for the container to signal that the test can begin,
122 ;; and one for the parent to signal to the container that the test is
124 (match (list (pipe) (pipe))
125 (((start-in . start-out) (end-in . end-out))
129 ;; Signal for the test to start.
130 (write 'ready start-out)
132 ;; Wait for test completion.
136 (define (namespaces pid)
137 (let ((pid (number->string pid)))
139 (readlink (string-append "/proc/" pid "/ns/" ns)))
140 '("user" "ipc" "uts" "net" "pid" "mnt"))))
142 (let* ((pid (run-container root '() %namespaces 1 container))
143 (container-namespaces (namespaces pid))
147 ;; Wait for container to be ready.
150 (container-excursion pid
152 ;; Fork again so that the pid is within the context of
153 ;; the joined pid namespace instead of the original pid
155 (match (primitive-fork)
157 ;; Check that all of the namespace identifiers are
158 ;; the same as the container process.
160 (equal? container-namespaces
161 (namespaces (getpid)))))
163 (match (waitpid fork-pid)
166 (status:exit-val status)))))))))))
168 ;; Stop the container.
169 (write 'done end-out)
174 (skip-if-unsupported)
175 (test-equal "container-excursion, same namespaces"
177 ;; The parent and child are in the same namespaces. 'container-excursion'
178 ;; should notice that and avoid calling 'setns' since that would fail.
179 (container-excursion (getpid)
181 (primitive-exit 42))))
183 (skip-if-unsupported)
184 (test-assert "container-excursion*"
185 (call-with-temporary-directory
187 (define (namespaces pid)
188 (let ((pid (number->string pid)))
190 (readlink (string-append "/proc/" pid "/ns/" ns)))
191 '("user" "ipc" "uts" "net" "pid" "mnt"))))
193 (let* ((pid (run-container root '()
197 (expected (namespaces pid))
198 (result (container-excursion* pid
202 (equal? result expected)))))
204 (skip-if-unsupported)
205 (test-equal "container-excursion*, same namespaces"
207 (container-excursion* (getpid)