67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn#!/bin/bash
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# lxc: linux Container library
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Authors:
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Serge Hallyn <serge.hallyn@ubuntu.com>
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn#
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# This is a test script for the lxc-user-nic program
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# This library is free software; you can redistribute it and/or
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# modify it under the terms of the GNU Lesser General Public
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# License as published by the Free Software Foundation; either
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# version 2.1 of the License, or (at your option) any later version.
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# This library is distributed in the hope that it will be useful,
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# but WITHOUT ANY WARRANTY; without even the implied warranty of
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Lesser General Public License for more details.
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# You should have received a copy of the GNU Lesser General Public
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# License along with this library; if not, write to the Free Software
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynset -e
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyns=`mktemp -d /tmp/lxctest-XXXXXX`
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynDONE=0
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynverify_unchanged_number() {
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn key=$1
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn desc=$2
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn n1=`grep ^$key $CONTAINER_PATH/config | wc -l`
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn n2=`grep ^$key $CONTAINER2_PATH/config | wc -l`
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn if [ $n1 -ne $n2 ]; then
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "Test $testnum failed"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "unequal number of $desc"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "Original has $n1, clone has $n2"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn false
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn fi
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn}
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncleanup() {
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn lxc-destroy -n lxctestb || true
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn lxc-destroy -n lxctestb2 || true
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn rm -rf $s
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn [ $DONE -eq 1 ] && echo "PASS" || echo "FAIL"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn}
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynverify_numnics() {
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn verify_unchanged_number lxc.network.type "network defs"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn}
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynverify_hwaddr() {
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn verify_unchanged_number lxc.network.hwaddr "hwaddr defs"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn grep ^lxc.network.hwaddr $CONTAINER_PATH/config | while read line; do
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn addr=`echo $line | awk -F= { print $2 }`
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "looking for $addr in $CONTAINER2_PATH/config"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn if grep -q $addr $CONTAINER2_PATH/config; then
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "Test $testnum failed"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "hwaddr $addr was not changed"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn false
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn fi
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn done
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn}
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynverify_hooks() {
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn verify_unchanged_number lxc.hook "hooks"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn grep ^lxc.hook $CONTAINER_PATH/config | while read line; do
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn nline=${line/$CONTAINER_PATH/$CONTAINER2_PATH}
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn if ! grep -q "$nline" $CONTAINER2_PATH/config; then
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "Test $testnum failed"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "Failed to find $nline in:"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn cat $CONTAINER2_PATH/config
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn fi
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn done
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn}
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyntrap cleanup EXIT
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Simple nic
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncat > $s/1.conf << EOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.network.type = veth
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.network.link = lxcbr0
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynEOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Simple nic with hwaddr; verify hwaddr changed
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncat > $s/2.conf << EOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.network.type = veth
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.network.link = lxcbr0
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynEOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# No nics, but nic from include
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncat > $s/1.include << EOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.network.type = veth
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.network.link = lxcbr0
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.hook.start = /bin/bash
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynEOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncat > $s/3.conf << EOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.include = $s/1.include
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynEOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# No nics, one clone hook in /bin
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncat > $s/4.conf << EOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.hook.start = /bin/bash
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynEOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# Figure out container dirname
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# We need this in 5.conf
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc-destroy -n lxctestb || true
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc-create -t busybox -n lxctestb
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynCONTAINER_PATH=$(dirname $(lxc-info -n lxctestb -c lxc.rootfs -H))
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc-destroy -n lxctestb
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn# No nics, one clone hook in $container
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyncat > $s/5.conf << EOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynlxc.hook.start = $CONTAINER_PATH/x1
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynEOF
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynCONTAINER2_PATH="${CONTAINER_PATH}2"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyntestnum=1
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallynfor f in $s/*.conf; do
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn echo "Test $testnum starting ($f)"
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn lxc-create -t busybox -f $f -n lxctestb
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn touch $CONTAINER_PATH/x1
d0a6bd39400a6d14cfec94ad647f3af1bda1e321Christian Brauner lxc-copy -s -n lxctestb -N lxctestb2
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn # verify that # nics did not change
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn verify_numnics
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn # verify hwaddr, if any changed
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn verify_hwaddr
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn # verify hooks are correct
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn verify_hooks
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn lxc-destroy -n lxctestb2 || true
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn lxc-destroy -n lxctestb || true
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn testnum=$((testnum+1))
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyndone
67702c2129c462b5e8124020a496fbf6b7ae5540Serge Hallyn
67702c2129c462b5e8124020a496fbf6b7ae5540Serge HallynDONE=1