]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-usernic.in
Merge pull request #882 from hallyn/2016-03-08/cgfsng.2
[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
8cb8e496 27LXC_USER_NIC="@LIBEXECDIR@/lxc/lxc-user-nic"
0e17b9c6 28
8befa924 29cleanup() {
0e17b9c6
SG
30 (
31 set +e
32
c85a40bc 33 lxc-stop -n usernic-c1 -k
0e17b9c6
SG
34 lxc-destroy -n usernic-c1
35
061ba5d0 36 sed -i '/usernic-user/d' /run/lxc/nics /etc/lxc/lxc-usernet
0e17b9c6
SG
37 ifconfig usernic-br0 down
38 ifconfig usernic-br1 down
39 brctl delbr usernic-br0
40 brctl delbr usernic-br1
41
c85a40bc 42 run_cmd "lxc-stop -n b1 -k"
0e17b9c6
SG
43 pkill -u $(id -u usernic-user) -9
44
45 rm -rf /tmp/usernic-test /home/usernic-user /run/user/$(id -u usernic-user)
46
47 deluser usernic-user
48 ) >/dev/null 2>&1
49
50 if [ "$DONE" = "1" ]; then
51 echo "PASS"
52 exit 0
53 fi
54
55 echo "FAIL"
56 exit 1
57}
58
59run_cmd() {
198a3f10
SG
60 sudo -i -u usernic-user \
61 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
62 XDG_RUNTIME_DIR=/run/user/$(id -u usernic-user) $*
8befa924 63}
20ab58c7 64
fd2b7320
SG
65ARCH=i386
66if type dpkg >/dev/null 2>&1; then
67 ARCH=$(dpkg --print-architecture)
68fi
69
0e17b9c6
SG
70set -eu
71trap cleanup EXIT SIGHUP SIGINT SIGTERM
72
8befa924
SH
73# create a test user
74deluser usernic-user || true
75useradd usernic-user
76sudo mkdir -p /home/usernic-user
79d88b03 77sudo chown usernic-user: /home/usernic-user
8befa924 78usermod -v 910000-919999 -w 910000-919999 usernic-user
8befa924 79
0e17b9c6
SG
80mkdir -p /home/usernic-user/.config/lxc/
81cat > /home/usernic-user/.config/lxc/default.conf << EOF
8befa924 82lxc.network.type = empty
0e17b9c6
SG
83lxc.id_map = u 0 910000 10000
84lxc.id_map = g 0 910000 10000
8befa924 85EOF
20ab58c7 86
42e5c987 87if which cgm >/dev/null 2>&1; then
2ba5eb93
SG
88 cgm create all usernic-user
89 cgm chown all usernic-user $(id -u usernic-user) $(id -g usernic-user)
90 cgm movepid all usernic-user $$
42e5c987 91elif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
ef4deb7f 92 for d in $(cut -d : -f 2 /proc/self/cgroup); do
3f458ed0
SG
93 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
94 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
95 string:$d string:usernic-user >/dev/null
96
97 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
98 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
99 string:$d string:usernic-user int32:$(id -u usernic-user) int32:$(id -g usernic-user) >/dev/null
100
101 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
102 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
103 string:$d string:usernic-user int32:$$ >/dev/null
104 done
105else
106 for d in /sys/fs/cgroup/*; do
107 [ ! -d $d/lxctest ] && mkdir $d/lxctest
79d88b03 108 chown -R usernic-user: $d/lxctest
3f458ed0
SG
109 echo $$ > $d/lxctest/tasks
110 done
111fi
0e17b9c6
SG
112
113mkdir -p /run/user/$(id -u usernic-user)
79d88b03 114chown -R usernic-user: /run/user/$(id -u usernic-user) /home/usernic-user
20ab58c7 115
57221f67
SG
116# Copy the download template cache if available
117run_cmd "mkdir -p /home/usernic-user/.cache/lxc"
118[ -d /var/cache/lxc/download ] && \
119 cp -R /var/cache/lxc/download /home/usernic-user/.cache/lxc && \
120 chown -R usernic-user: /home/usernic-user/.cache/lxc
121
122
0e17b9c6 123# Create two test bridges
8befa924
SH
124brctl addbr usernic-br0
125brctl addbr usernic-br1
126ifconfig usernic-br0 0.0.0.0 up
127ifconfig usernic-br1 0.0.0.0 up
128
fd2b7320
SG
129ARCH=i386
130if type dpkg >/dev/null 2>&1; then
131 ARCH=$(dpkg --print-architecture)
132fi
133
8befa924 134# Create three containers
fd2b7320 135run_cmd "lxc-create -t download -n b1 -- -d ubuntu -r trusty -a $ARCH"
0e17b9c6
SG
136run_cmd "lxc-start -n b1 -d"
137p1=$(run_cmd "lxc-info -n b1 -p -H")
8befa924 138
615af4ac
SH
139lxcpath=/home/usernic-user/.local/share/lxc
140lxcname=b1
141
8befa924 142# Assign one veth, should fail as no allowed entries yet
615af4ac 143if run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p1 veth usernic-br0 xx1"; then
8befa924 144 echo "FAIL: able to create nic with no entries"
0e17b9c6 145 exit 1
20ab58c7
SH
146fi
147
8befa924 148# Give him a quota of two
d08363af 149touch /etc/lxc/lxc-usernet
0e17b9c6
SG
150sed -i '/^usernic-user/d' /etc/lxc/lxc-usernet
151echo "usernic-user veth usernic-br0 2" >> /etc/lxc/lxc-usernet
8befa924
SH
152
153# Assign one veth to second bridge, should fail
615af4ac 154if run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p1 veth usernic-br1 xx1"; then
8befa924 155 echo "FAIL: able to create nic with no entries"
0e17b9c6 156 exit 1
8befa924 157fi
20ab58c7 158
8befa924 159# Assign two veths, should succeed
615af4ac 160if ! run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p1 veth usernic-br0 xx2"; then
8befa924 161 echo "FAIL: unable to create first nic"
0e17b9c6 162 exit 1
8befa924 163fi
0e17b9c6 164
615af4ac 165if ! run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p1 veth usernic-br0 xx3"; then
8befa924 166 echo "FAIL: unable to create second nic"
0e17b9c6 167 exit 1
20ab58c7
SH
168fi
169
8befa924 170# Assign one more veth, should fail.
615af4ac 171if run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p1 veth usernic-br0 xx4"; then
8befa924 172 echo "FAIL: able to create third nic"
0e17b9c6 173 exit 1
20ab58c7
SH
174fi
175
8befa924 176# Shut down and restart the container, should be able to assign more nics
c85a40bc 177run_cmd "lxc-stop -n b1 -k"
0e17b9c6
SG
178run_cmd "lxc-start -n b1 -d"
179p1=$(run_cmd "lxc-info -n b1 -p -H")
180
615af4ac 181if ! run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p1 veth usernic-br0 xx5"; then
8befa924
SH
182 echo "FAIL: unable to create nic after destroying the old"
183 cleanup 1
184fi
185
c85a40bc 186run_cmd "lxc-stop -n b1 -k"
8befa924
SH
187
188# Create a root-owned ns
189lxc-create -t busybox -n usernic-c1
190lxc-start -n usernic-c1 -d
0e17b9c6 191p2=$(lxc-info -n usernic-c1 -p -H)
8befa924
SH
192
193# assign veth to it - should fail
615af4ac 194if run_cmd "$LXC_USER_NIC $lxcpath $lxcname $p2 veth usernic-br0 xx6"; then
8befa924
SH
195 echo "FAIL: able to attach nic to root-owned container"
196 cleanup 1
20ab58c7
SH
197fi
198
199echo "All tests passed"
0e17b9c6 200DONE=1