Merge branch 'core-updates'
[jackhill/guix/guix.git] / tests / containers.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
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))
26
27 (define (assert-exit x)
28 (primitive-exit (if x 0 1)))
29
30 (test-begin "containers")
31
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?))
38 (test-skip 1)))
39
40 (skip-if-unsupported)
41 (test-assert "call-with-container, exit with 0 when there is no error"
42 (zero?
43 (call-with-container '() (const #t) #:namespaces '(user))))
44
45 (skip-if-unsupported)
46 (test-assert "call-with-container, user namespace"
47 (zero?
48 (call-with-container '()
49 (lambda ()
50 ;; The user is root within the new user namespace.
51 (assert-exit (and (zero? (getuid)) (zero? (getgid)))))
52 #:namespaces '(user))))
53
54 (skip-if-unsupported)
55 (test-assert "call-with-container, uts namespace"
56 (zero?
57 (call-with-container '()
58 (lambda ()
59 ;; The user is root within the container and should be able to change
60 ;; the hostname of that container.
61 (sethostname "test-container")
62 (primitive-exit 0))
63 #:namespaces '(user uts))))
64
65 (skip-if-unsupported)
66 (test-assert "call-with-container, pid namespace"
67 (zero?
68 (call-with-container '()
69 (lambda ()
70 (match (primitive-fork)
71 (0
72 ;; The first forked process in the new pid namespace is pid 2.
73 (assert-exit (= 2 (getpid))))
74 (pid
75 (primitive-exit
76 (match (waitpid pid)
77 ((_ . status)
78 (status:exit-val status)))))))
79 #:namespaces '(user pid))))
80
81 (skip-if-unsupported)
82 (test-assert "call-with-container, mnt namespace"
83 (zero?
84 (call-with-container (list (file-system
85 (device "none")
86 (mount-point "/testing")
87 (type "tmpfs")))
88 (lambda ()
89 (assert-exit (file-exists? "/testing")))
90 #:namespaces '(user mnt))))
91
92 (skip-if-unsupported)
93 (test-equal "call-with-container, mnt namespace, wrong bind mount"
94 `(system-error ,ENOENT)
95 ;; An exception should be raised; see <http://bugs.gnu.org/23306>.
96 (catch 'system-error
97 (lambda ()
98 (call-with-container (list (file-system
99 (device "/does-not-exist")
100 (mount-point "/foo")
101 (type "none")
102 (flags '(bind-mount))))
103 (const #t)
104 #:namespaces '(user mnt)))
105 (lambda args
106 (list 'system-error (system-error-errno args)))))
107
108 (skip-if-unsupported)
109 (test-assert "call-with-container, all namespaces"
110 (zero?
111 (call-with-container '()
112 (lambda ()
113 (primitive-exit 0)))))
114
115 (skip-if-unsupported)
116 (test-assert "container-excursion"
117 (call-with-temporary-directory
118 (lambda (root)
119 ;; Two pipes: One for the container to signal that the test can begin,
120 ;; and one for the parent to signal to the container that the test is
121 ;; over.
122 (match (list (pipe) (pipe))
123 (((start-in . start-out) (end-in . end-out))
124 (define (container)
125 (close end-out)
126 (close start-in)
127 ;; Signal for the test to start.
128 (write 'ready start-out)
129 (close start-out)
130 ;; Wait for test completion.
131 (read end-in)
132 (close end-in))
133
134 (define (namespaces pid)
135 (let ((pid (number->string pid)))
136 (map (lambda (ns)
137 (readlink (string-append "/proc/" pid "/ns/" ns)))
138 '("user" "ipc" "uts" "net" "pid" "mnt"))))
139
140 (let* ((pid (run-container root '() %namespaces 1 container))
141 (container-namespaces (namespaces pid))
142 (result
143 (begin
144 (close start-out)
145 ;; Wait for container to be ready.
146 (read start-in)
147 (close start-in)
148 (container-excursion pid
149 (lambda ()
150 ;; Fork again so that the pid is within the context of
151 ;; the joined pid namespace instead of the original pid
152 ;; namespace.
153 (match (primitive-fork)
154 (0
155 ;; Check that all of the namespace identifiers are
156 ;; the same as the container process.
157 (assert-exit
158 (equal? container-namespaces
159 (namespaces (getpid)))))
160 (fork-pid
161 (match (waitpid fork-pid)
162 ((_ . status)
163 (primitive-exit
164 (status:exit-val status)))))))))))
165 (close end-in)
166 ;; Stop the container.
167 (write 'done end-out)
168 (close end-out)
169 (waitpid pid)
170 (zero? result)))))))
171
172 (skip-if-unsupported)
173 (test-equal "container-excursion, same namespaces"
174 42
175 ;; The parent and child are in the same namespaces. 'container-excursion'
176 ;; should notice that and avoid calling 'setns' since that would fail.
177 (container-excursion (getpid)
178 (lambda ()
179 (primitive-exit 42))))
180
181 (test-end)