]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-cloneconfig
Merge pull request #4003 from brauner/2021-10-19.fixes
[mirror_lxc.git] / src / tests / lxc-test-cloneconfig
CommitLineData
67702c21
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
22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
24set -e
25
26s=`mktemp -d /tmp/lxctest-XXXXXX`
27
28DONE=0
29
30verify_unchanged_number() {
31 key=$1
32 desc=$2
33 n1=`grep ^$key $CONTAINER_PATH/config | wc -l`
34 n2=`grep ^$key $CONTAINER2_PATH/config | wc -l`
35 if [ $n1 -ne $n2 ]; then
36 echo "Test $testnum failed"
37 echo "unequal number of $desc"
38 echo "Original has $n1, clone has $n2"
39 false
40 fi
41}
42
43cleanup() {
44 lxc-destroy -n lxctestb || true
45 lxc-destroy -n lxctestb2 || true
46 rm -rf $s
47 [ $DONE -eq 1 ] && echo "PASS" || echo "FAIL"
48}
49
50verify_numnics() {
7fa3f2e9 51 verify_unchanged_number lxc.net.0.type "network defs"
67702c21
SH
52}
53
54verify_hwaddr() {
7fa3f2e9 55 verify_unchanged_number lxc.net.0.hwaddr "hwaddr defs"
56 grep ^lxc.net.0.hwaddr $CONTAINER_PATH/config | while read line; do
67702c21
SH
57 addr=`echo $line | awk -F= { print $2 }`
58 echo "looking for $addr in $CONTAINER2_PATH/config"
59 if grep -q $addr $CONTAINER2_PATH/config; then
60 echo "Test $testnum failed"
61 echo "hwaddr $addr was not changed"
62 false
63 fi
64 done
65}
66
67verify_hooks() {
68 verify_unchanged_number lxc.hook "hooks"
69 grep ^lxc.hook $CONTAINER_PATH/config | while read line; do
70 nline=${line/$CONTAINER_PATH/$CONTAINER2_PATH}
71 if ! grep -q "$nline" $CONTAINER2_PATH/config; then
72 echo "Test $testnum failed"
73 echo "Failed to find $nline in:"
74 cat $CONTAINER2_PATH/config
75 fi
76 done
77}
78
79trap cleanup EXIT
80
81# Simple nic
82cat > $s/1.conf << EOF
7fa3f2e9 83lxc.net.0.type = veth
84lxc.net.0.link = lxcbr0
67702c21
SH
85EOF
86
87# Simple nic with hwaddr; verify hwaddr changed
88cat > $s/2.conf << EOF
7fa3f2e9 89lxc.net.0.type = veth
90lxc.net.0.link = lxcbr0
67702c21
SH
91EOF
92
93# No nics, but nic from include
94cat > $s/1.include << EOF
7fa3f2e9 95lxc.net.0.type = veth
96lxc.net.0.link = lxcbr0
67702c21
SH
97lxc.hook.start = /bin/bash
98EOF
99cat > $s/3.conf << EOF
100lxc.include = $s/1.include
101EOF
102
103# No nics, one clone hook in /bin
104cat > $s/4.conf << EOF
105lxc.hook.start = /bin/bash
106EOF
107
108# Figure out container dirname
109# We need this in 5.conf
110lxc-destroy -n lxctestb || true
4b19d742 111lxc-create -t busybox -n lxctestb -B dir
7a96a068 112CONTAINER_PATH=$(dirname $(lxc-info -n lxctestb -c lxc.rootfs.path -H) | sed 's/dir://')
67702c21
SH
113lxc-destroy -n lxctestb
114
115# No nics, one clone hook in $container
116cat > $s/5.conf << EOF
117lxc.hook.start = $CONTAINER_PATH/x1
118EOF
119
120CONTAINER2_PATH="${CONTAINER_PATH}2"
121
122testnum=1
123for f in $s/*.conf; do
124 echo "Test $testnum starting ($f)"
125 lxc-create -t busybox -f $f -n lxctestb
126 touch $CONTAINER_PATH/x1
d0a6bd39 127 lxc-copy -s -n lxctestb -N lxctestb2
67702c21
SH
128 # verify that # nics did not change
129 verify_numnics
130 # verify hwaddr, if any changed
131 verify_hwaddr
132 # verify hooks are correct
133 verify_hooks
134 lxc-destroy -n lxctestb2 || true
135 lxc-destroy -n lxctestb || true
136 testnum=$((testnum+1))
137done
138
139DONE=1