build: container: Fix call-with-clean-exit.
[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 (srfi srfi-64)
24 #:use-module (ice-9 match))
25
26 (define (assert-exit x)
27 (primitive-exit (if x 0 1)))
28
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"))
33 (exit 77))
34
35 (test-begin "containers")
36
37 (test-assert "call-with-container, exit with 0 when there is no error"
38 (zero?
39 (call-with-container '() (const #t) #:namespaces '(user))))
40
41 (test-assert "call-with-container, user namespace"
42 (zero?
43 (call-with-container '()
44 (lambda ()
45 ;; The user is root within the new user namespace.
46 (assert-exit (and (zero? (getuid)) (zero? (getgid)))))
47 #:namespaces '(user))))
48
49 (test-assert "call-with-container, uts namespace"
50 (zero?
51 (call-with-container '()
52 (lambda ()
53 ;; The user is root within the container and should be able to change
54 ;; the hostname of that container.
55 (sethostname "test-container")
56 (primitive-exit 0))
57 #:namespaces '(user uts))))
58
59 (test-assert "call-with-container, pid namespace"
60 (zero?
61 (call-with-container '()
62 (lambda ()
63 (match (primitive-fork)
64 (0
65 ;; The first forked process in the new pid namespace is pid 2.
66 (assert-exit (= 2 (getpid))))
67 (pid
68 (primitive-exit
69 (match (waitpid pid)
70 ((_ . status)
71 (status:exit-val status)))))))
72 #:namespaces '(user pid))))
73
74 (test-assert "call-with-container, mnt namespace"
75 (zero?
76 (call-with-container '(("none" device "/testing" "tmpfs" () #f #f))
77 (lambda ()
78 (assert-exit (file-exists? "/testing")))
79 #:namespaces '(user mnt))))
80
81 (test-assert "call-with-container, all namespaces"
82 (zero?
83 (call-with-container '()
84 (lambda ()
85 (primitive-exit 0)))))
86
87 (test-assert "container-excursion"
88 (call-with-temporary-directory
89 (lambda (root)
90 ;; Two pipes: One for the container to signal that the test can begin,
91 ;; and one for the parent to signal to the container that the test is
92 ;; over.
93 (match (list (pipe) (pipe))
94 (((start-in . start-out) (end-in . end-out))
95 (define (container)
96 (close end-out)
97 (close start-in)
98 ;; Signal for the test to start.
99 (write 'ready start-out)
100 (close start-out)
101 ;; Wait for test completion.
102 (read end-in)
103 (close end-in))
104
105 (define (namespaces pid)
106 (let ((pid (number->string pid)))
107 (map (lambda (ns)
108 (readlink (string-append "/proc/" pid "/ns/" ns)))
109 '("user" "ipc" "uts" "net" "pid" "mnt"))))
110
111 (let* ((pid (run-container root '() %namespaces 1 container))
112 (container-namespaces (namespaces pid))
113 (result
114 (begin
115 (close start-out)
116 ;; Wait for container to be ready.
117 (read start-in)
118 (close start-in)
119 (container-excursion pid
120 (lambda ()
121 ;; Fork again so that the pid is within the context of
122 ;; the joined pid namespace instead of the original pid
123 ;; namespace.
124 (match (primitive-fork)
125 (0
126 ;; Check that all of the namespace identifiers are
127 ;; the same as the container process.
128 (assert-exit
129 (equal? container-namespaces
130 (namespaces (getpid)))))
131 (fork-pid
132 (match (waitpid fork-pid)
133 ((_ . status)
134 (primitive-exit
135 (status:exit-val status)))))))))))
136 (close end-in)
137 ;; Stop the container.
138 (write 'done end-out)
139 (close end-out)
140 (waitpid pid)
141 (zero? result)))))))
142
143 (test-end)
144
145 \f
146 (exit (= (test-runner-fail-count (test-runner-current)) 0))