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 (srfi srfi-64)
24 #:use-module (ice-9 match))
26 (define (assert-exit x)
27 (primitive-exit (if x 0 1)))
29 ;; Skip these tests unless user namespaces are available and the setgroups
30 ;; file (introduced in Linux 3.19 to address a security issue) exists.
31 (unless (and (file-exists? "/proc/self/ns/user")
32 (file-exists? "/proc/self/setgroups"))
35 (test-begin "containers")
37 (test-assert "call-with-container, user namespace"
39 (call-with-container '()
41 ;; The user is root within the new user namespace.
42 (assert-exit (and (zero? (getuid)) (zero? (getgid)))))
43 #:namespaces '(user))))
45 (test-assert "call-with-container, uts namespace"
47 (call-with-container '()
49 ;; The user is root within the container and should be able to change
50 ;; the hostname of that container.
51 (sethostname "test-container")
53 #:namespaces '(user uts))))
55 (test-assert "call-with-container, pid namespace"
57 (call-with-container '()
59 (match (primitive-fork)
61 ;; The first forked process in the new pid namespace is pid 2.
62 (assert-exit (= 2 (getpid))))
67 (status:exit-val status)))))))
68 #:namespaces '(user pid))))
70 (test-assert "call-with-container, mnt namespace"
72 (call-with-container '(("none" device "/testing" "tmpfs" () #f #f))
74 (assert-exit (file-exists? "/testing")))
75 #:namespaces '(user mnt))))
77 (test-assert "call-with-container, all namespaces"
79 (call-with-container '()
81 (primitive-exit 0)))))
83 (test-assert "container-excursion"
84 (call-with-temporary-directory
86 ;; Two pipes: One for the container to signal that the test can begin,
87 ;; and one for the parent to signal to the container that the test is
89 (match (list (pipe) (pipe))
90 (((start-in . start-out) (end-in . end-out))
94 ;; Signal for the test to start.
95 (write 'ready start-out)
97 ;; Wait for test completion.
101 (define (namespaces pid)
102 (let ((pid (number->string pid)))
104 (readlink (string-append "/proc/" pid "/ns/" ns)))
105 '("user" "ipc" "uts" "net" "pid" "mnt"))))
107 (let* ((pid (run-container root '() %namespaces 1 container))
108 (container-namespaces (namespaces pid))
112 ;; Wait for container to be ready.
115 (container-excursion pid
117 ;; Fork again so that the pid is within the context of
118 ;; the joined pid namespace instead of the original pid
120 (match (primitive-fork)
122 ;; Check that all of the namespace identifiers are
123 ;; the same as the container process.
125 (equal? container-namespaces
126 (namespaces (getpid)))))
128 (match (waitpid fork-pid)
131 (status:exit-val status)))))))))))
133 ;; Stop the container.
134 (write 'done end-out)
142 (exit (= (test-runner-fail-count (test-runner-current)) 0))