lxc-test-autostart revision 01c05c821093dc854def146d4bab62885d8eb664
653N/A#!/bin/sh
653N/A
653N/A# lxc: linux Container library
653N/A
911N/A# Authors:
810N/A# Stéphane Graber <stgraber@ubuntu.com>
653N/A#
653N/A# This is a test script for lxc-autostart
919N/A
919N/A# This library is free software; you can redistribute it and/or
919N/A# modify it under the terms of the GNU Lesser General Public
919N/A# License as published by the Free Software Foundation; either
919N/A# version 2.1 of the License, or (at your option) any later version.
919N/A
919N/A# This library is distributed in the hope that it will be useful,
919N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
919N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
919N/A# Lesser General Public License for more details.
919N/A
919N/A# You should have received a copy of the GNU Lesser General Public
919N/A# License along with this library; if not, write to the Free Software
919N/A# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
919N/A
919N/ADONE=0
919N/AKNOWN_RELEASES="precise trusty wily xenial yakkety"
653N/Acleanup() {
653N/A lxc-destroy -n $CONTAINER_NAME >/dev/null 2>&1 || true
653N/A
653N/A if [ $DONE -eq 0 ]; then
653N/A echo "FAIL"
653N/A exit 1
653N/A fi
911N/A echo "PASS"
911N/A}
911N/A
911N/AARCH=i386
911N/Aif type dpkg >/dev/null 2>&1; then
653N/A ARCH=$(dpkg --print-architecture)
653N/Afi
653N/A
653N/Atrap cleanup EXIT HUP INT TERM
653N/Aset -eu
653N/A
653N/A# Create a container
653N/ACONTAINER_NAME=lxc-test-auto
653N/A
653N/A# default release is trusty, or the systems release if recognized
911N/Arelease=trusty
911N/Aif [ -f /etc/lsb-release ]; then
911N/A . /etc/lsb-release
911N/A rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
911N/A rels="$KNOWN_RELEASES"
911N/A for r in $rels; do
653N/A [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
653N/A done
653N/Afi
653N/A
653N/Alxc-create -t download -n $CONTAINER_NAME -- -d ubuntu -r $release -a $ARCH
653N/ACONTAINER_PATH=$(dirname $(lxc-info -n $CONTAINER_NAME -c lxc.rootfs -H))
653N/Acp $CONTAINER_PATH/config $CONTAINER_PATH/config.bak
653N/A
653N/A# Ensure it's not in lxc-autostart
653N/Alxc-autostart -L | grep -q $CONTAINER_NAME && \
837N/A (echo "Container shouldn't be auto-started" && exit 1)
837N/A
653N/A# Mark it as auto-started and re-check
653N/Aecho "lxc.start.auto = 1" >> $CONTAINER_PATH/config
653N/Alxc-autostart -L | grep -q $CONTAINER_NAME || \
653N/A (echo "Container should be auto-started" && exit 1)
653N/A
653N/A# Put it in a test group and set a delay
653N/Aecho "lxc.group = lxc-auto-test" >> $CONTAINER_PATH/config
653N/Aecho "lxc.start.delay = 5" >> $CONTAINER_PATH/config
653N/A
837N/A# Check that everything looks good
837N/Aif [ "$(lxc-autostart -L -g lxc-auto-test | grep $CONTAINER_NAME)" != "$CONTAINER_NAME 5" ]; then
653N/A echo "Invalid autostart setting" && exit 1
837N/Afi
837N/A
# Start it
lxc-autostart -g lxc-auto-test
lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't start" && exit 1)
sleep 5
# Restart it
lxc-autostart -g lxc-auto-test -r
lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't restart" && exit 1)
sleep 5
# When shutting down, the container calls sync. If the machine running
# the test has a massive backlog, it can take minutes for the sync call to
# finish, causing a test failure.
# So try to reduce that by flushing everything to disk before we attempt
# a container shutdown.
sync
# Shut it down
lxc-autostart -g lxc-auto-test -s -t 120
lxc-wait -n $CONTAINER_NAME -t 120 -s STOPPED || (echo "Container didn't stop" && exit 1)
# Kill it
lxc-autostart -g lxc-auto-test -k
lxc-wait -n $CONTAINER_NAME -t 60 -s STOPPED || (echo "Container didn't die" && exit 1)
DONE=1