]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-usernic.in
spelling: timeout
[mirror_lxc.git] / src / tests / lxc-test-usernic.in
CommitLineData
20ab58c7
SH
1#!/bin/bash
2
3# lxc: linux Container library
4
5# Authors:
6# Serge Hallyn <serge.hallyn@ubuntu.com>
7#
8# This is a test script for the lxc-user-nic program
9
10# This library is free software; you can redistribute it and/or
11# modify it under the terms of the GNU Lesser General Public
12# License as published by the Free Software Foundation; either
13# version 2.1 of the License, or (at your option) any later version.
14
15# This library is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Lesser General Public License for more details.
19
20# You should have received a copy of the GNU Lesser General Public
21# License along with this library; if not, write to the Free Software
250b1eec 22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20ab58c7 23
061ba5d0
SG
24# This test assumes an Ubuntu host
25
0e17b9c6 26DONE=0
0815a592 27KNOWN_RELEASES="precise trusty xenial yakkety zesty"
8cb8e496 28LXC_USER_NIC="@LIBEXECDIR@/lxc/lxc-user-nic"
0e17b9c6 29
8befa924 30cleanup() {
9c50a526 31 set +e
0e17b9c6 32
9c50a526 33 (
c85a40bc 34 lxc-stop -n usernic-c1 -k
0e17b9c6
SG
35 lxc-destroy -n usernic-c1
36
061ba5d0 37 sed -i '/usernic-user/d' /run/lxc/nics /etc/lxc/lxc-usernet
0e17b9c6
SG
38 ifconfig usernic-br0 down
39 ifconfig usernic-br1 down
40 brctl delbr usernic-br0
41 brctl delbr usernic-br1
42
c85a40bc 43 run_cmd "lxc-stop -n b1 -k"
0e17b9c6
SG
44 pkill -u $(id -u usernic-user) -9
45
46 rm -rf /tmp/usernic-test /home/usernic-user /run/user/$(id -u usernic-user)
47
48 deluser usernic-user
49 ) >/dev/null 2>&1
50
51 if [ "$DONE" = "1" ]; then
52 echo "PASS"
53 exit 0
54 fi
55
56 echo "FAIL"
57 exit 1
58}
59
60run_cmd() {
198a3f10
SG
61 sudo -i -u usernic-user \
62 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
63 XDG_RUNTIME_DIR=/run/user/$(id -u usernic-user) $*
8befa924 64}
20ab58c7 65
fd2b7320
SG
66ARCH=i386
67if type dpkg >/dev/null 2>&1; then
68 ARCH=$(dpkg --print-architecture)
69fi
70
0e17b9c6
SG
71set -eu
72trap cleanup EXIT SIGHUP SIGINT SIGTERM
73
8befa924
SH
74# create a test user
75deluser usernic-user || true
76useradd usernic-user
77sudo mkdir -p /home/usernic-user
79d88b03 78sudo chown usernic-user: /home/usernic-user
8befa924 79usermod -v 910000-919999 -w 910000-919999 usernic-user
8befa924 80
0e17b9c6
SG
81mkdir -p /home/usernic-user/.config/lxc/
82cat > /home/usernic-user/.config/lxc/default.conf << EOF
7fa3f2e9 83lxc.net.0.type = empty
bdcbb6b3
CB
84lxc.idmap = u 0 910000 10000
85lxc.idmap = g 0 910000 10000
8befa924 86EOF
20ab58c7 87
42e5c987 88if which cgm >/dev/null 2>&1; then
2ba5eb93
SG
89 cgm create all usernic-user
90 cgm chown all usernic-user $(id -u usernic-user) $(id -g usernic-user)
91 cgm movepid all usernic-user $$
42e5c987 92elif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
ef4deb7f 93 for d in $(cut -d : -f 2 /proc/self/cgroup); do
3f458ed0
SG
94 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
95 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
96 string:$d string:usernic-user >/dev/null
97
98 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
99 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
100 string:$d string:usernic-user int32:$(id -u usernic-user) int32:$(id -g usernic-user) >/dev/null
101
102 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
103 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
104 string:$d string:usernic-user int32:$$ >/dev/null
105 done
106else
107 for d in /sys/fs/cgroup/*; do
8d5a91fc 108 [ "$d" = "/sys/fs/cgroup/unified" ] && continue
177f793a 109 [ -f $d/cgroup.clone_children ] && echo 1 > $d/cgroup.clone_children
3f458ed0 110 [ ! -d $d/lxctest ] && mkdir $d/lxctest
79d88b03 111 chown -R usernic-user: $d/lxctest
3f458ed0
SG
112 echo $$ > $d/lxctest/tasks
113 done
114fi
0e17b9c6
SG
115
116mkdir -p /run/user/$(id -u usernic-user)
79d88b03 117chown -R usernic-user: /run/user/$(id -u usernic-user) /home/usernic-user
20ab58c7 118
57221f67
SG
119# Copy the download template cache if available
120run_cmd "mkdir -p /home/usernic-user/.cache/lxc"
121[ -d /var/cache/lxc/download ] && \
122 cp -R /var/cache/lxc/download /home/usernic-user/.cache/lxc && \
123 chown -R usernic-user: /home/usernic-user/.cache/lxc
124
125
0e17b9c6 126# Create two test bridges
8befa924
SH
127brctl addbr usernic-br0
128brctl addbr usernic-br1
129ifconfig usernic-br0 0.0.0.0 up
130ifconfig usernic-br1 0.0.0.0 up
131
fd2b7320
SG
132ARCH=i386
133if type dpkg >/dev/null 2>&1; then
134 ARCH=$(dpkg --print-architecture)
135fi
136
64ea46c7
PHL
137# default release is trusty, or the systems release if recognized
138release=trusty
139if [ -f /etc/lsb-release ]; then
140 . /etc/lsb-release
141 rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
142 rels="$KNOWN_RELEASES"
143 for r in $rels; do
144 [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
145 done
146fi
147
8befa924 148# Create three containers
64ea46c7 149run_cmd "lxc-create -t download -n b1 -- -d ubuntu -r $release -a $ARCH"
0e17b9c6
SG
150run_cmd "lxc-start -n b1 -d"
151p1=$(run_cmd "lxc-info -n b1 -p -H")
8befa924 152
615af4ac
SH
153lxcpath=/home/usernic-user/.local/share/lxc
154lxcname=b1
155
8befa924 156# Assign one veth, should fail as no allowed entries yet
f703d990 157if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx1"; then
8befa924 158 echo "FAIL: able to create nic with no entries"
0e17b9c6 159 exit 1
20ab58c7
SH
160fi
161
8befa924 162# Give him a quota of two
d08363af 163touch /etc/lxc/lxc-usernet
0e17b9c6
SG
164sed -i '/^usernic-user/d' /etc/lxc/lxc-usernet
165echo "usernic-user veth usernic-br0 2" >> /etc/lxc/lxc-usernet
8befa924
SH
166
167# Assign one veth to second bridge, should fail
f703d990 168if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br1 xx1"; then
8befa924 169 echo "FAIL: able to create nic with no entries"
0e17b9c6 170 exit 1
8befa924 171fi
20ab58c7 172
8befa924 173# Assign two veths, should succeed
f703d990 174if ! run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx2"; then
8befa924 175 echo "FAIL: unable to create first nic"
0e17b9c6 176 exit 1
8befa924 177fi
0e17b9c6 178
f703d990 179if ! run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx3"; then
8befa924 180 echo "FAIL: unable to create second nic"
0e17b9c6 181 exit 1
20ab58c7
SH
182fi
183
8befa924 184# Assign one more veth, should fail.
f703d990 185if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx4"; then
8befa924 186 echo "FAIL: able to create third nic"
0e17b9c6 187 exit 1
20ab58c7
SH
188fi
189
8befa924 190# Shut down and restart the container, should be able to assign more nics
c85a40bc 191run_cmd "lxc-stop -n b1 -k"
0e17b9c6
SG
192run_cmd "lxc-start -n b1 -d"
193p1=$(run_cmd "lxc-info -n b1 -p -H")
194
f703d990 195if ! run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx5"; then
8befa924
SH
196 echo "FAIL: unable to create nic after destroying the old"
197 cleanup 1
198fi
199
c85a40bc 200run_cmd "lxc-stop -n b1 -k"
8befa924
SH
201
202# Create a root-owned ns
203lxc-create -t busybox -n usernic-c1
204lxc-start -n usernic-c1 -d
0e17b9c6 205p2=$(lxc-info -n usernic-c1 -p -H)
8befa924
SH
206
207# assign veth to it - should fail
f703d990 208if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p2 veth usernic-br0 xx6"; then
8befa924
SH
209 echo "FAIL: able to attach nic to root-owned container"
210 cleanup 1
20ab58c7
SH
211fi
212
213echo "All tests passed"
0e17b9c6 214DONE=1