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