Debian Codegraph @

src/tests/lxc-test-symlink

raw
1 #!/bin/bash
2
3 # SPDX-License-Identifier: LGPL-2.1+
4
5 # This is a regression test for symbolic links
6
7 set -ex
8
9 dirname=$(mktemp -d)
10 fname=$(mktemp)
11 fname2=$(mktemp)
12
13 lxcpath=/var/lib/lxcsym1
14
15 cleanup() {
16 lxc-destroy -P $lxcpath -f -n symtest1 || true
17 rm -f $lxcpath
18 rmdir $dirname || true
19 rm -f $fname || true
20 rm -f $fname2 || true
21 }
22
23 trap cleanup EXIT SIGHUP SIGINT SIGTERM
24
25 testrun() {
26 expected=$1
27 run=$2
28 pass="pass"
29 lxc-start -P $lxcpath -n symtest1 -l trace -o $lxcpath/log || pass="fail"
30 [ $pass = "pass" ] && lxc-wait -P $lxcpath -n symtest1 -t 10 -s RUNNING || pass="fail"
31 if [ "$pass" != "$expected" ]; then
32 echo "Test $run: expected $expected but container did not. Start log:"
33 cat $lxcpath/log
34 echo "FAIL: Test $run: expected $expected but container did not."
35 false
36 fi
37 lxc-stop -P $lxcpath -n symtest1 -k || true
38 }
39
40 # make lxcpath a symlink - this should NOT cause failure
41 ln -s /var/lib/lxc $lxcpath
42
43 lxc-destroy -P $lxcpath -f -n symtest1 || true
44 lxc-create -P $lxcpath -t busybox -n symtest1
45
46 cat >> /var/lib/lxc/symtest1/config << EOF
47 lxc.mount.entry = $dirname opt/xxx/dir none bind,create=dir
48 lxc.mount.entry = $fname opt/xxx/file none bind,create=file
49 lxc.mount.entry = $fname2 opt/xxx/file2 none bind
50 lxc.mount.entry = $dirname /var/lib/lxc/symtest1/rootfs/opt/xxx/dir2 none bind,create=dir
51 EOF
52
53 # Regular - should succeed
54 mkdir -p /var/lib/lxc/symtest1/rootfs/opt/xxx
55 touch /var/lib/lxc/symtest1/rootfs/opt/xxx/file2
56 testrun pass 1
57
58 # symlink - should fail
59 rm -rf /var/lib/lxc/symtest1/rootfs/opt/xxx
60 mkdir -p /var/lib/lxc/symtest1/rootfs/opt/xxx2
61 ln -s /var/lib/lxc/symtest1/rootfs/opt/xxx2 /var/lib/lxc/symtest1/rootfs/opt/xxx
62 touch /var/lib/lxc/symtest1/rootfs/opt/xxx/file2
63 testrun fail 2
64
65 # final final symlink - should fail
66 rm -rf $lxcpath/symtest1/rootfs/opt/xxx
67 mkdir -p $lxcpath/symtest1/rootfs/opt/xxx
68 mkdir -p $lxcpath/symtest1/rootfs/opt/xxx/dir
69 touch $lxcpath/symtest1/rootfs/opt/xxx/file
70 touch $lxcpath/symtest1/rootfs/opt/xxx/file2src
71 ln -s $lxcpath/symtest1/rootfs/opt/xxx/file2src $lxcpath/symtest1/rootfs/opt/xxx/file2
72 testrun fail 3
73
74 # Ideally we'd also try a loop device, but that won't work in nested containers
75 # anyway - TODO
76
77 # what about /proc itself
78
79 rm -rf $lxcpath/symtest1/rootfs/opt/xxx
80 mkdir -p $lxcpath/symtest1/rootfs/opt/xxx
81 touch $lxcpath/symtest1/rootfs/opt/xxx/file2
82 mv $lxcpath/symtest1/rootfs/proc $lxcpath/symtest1/rootfs/proc1
83 ln -s $lxcpath/symtest1/rootfs/proc1 $lxcpath/symtest1/rootfs/proc
84 testrun fail 4
85
86 echo "all tests passed"