45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# lxc: linux Container library
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Stéphane Graber <stgraber@ubuntu.com>
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# This is a test script for lxc-autostart
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# This library is free software; you can redistribute it and/or
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# modify it under the terms of the GNU Lesser General Public
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# License as published by the Free Software Foundation; either
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# version 2.1 of the License, or (at your option) any later version.
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# This library is distributed in the hope that it will be useful,
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# but WITHOUT ANY WARRANTY; without even the implied warranty of
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Lesser General Public License for more details.
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# You should have received a copy of the GNU Lesser General Public
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# License along with this library; if not, write to the Free Software
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3a5495cf2f6c1806f5a91d699448b15b510f146ePo-Hsu LinKNOWN_RELEASES="precise trusty xenial yakkety zesty"
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber lxc-destroy -n $CONTAINER_NAME >/dev/null 2>&1 || true
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber if [ $DONE -eq 0 ]; then
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Create a container
01c05c821093dc854def146d4bab62885d8eb664Po-Hsu Lin# default release is trusty, or the systems release if recognized
01c05c821093dc854def146d4bab62885d8eb664Po-Hsu Lin rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
01c05c821093dc854def146d4bab62885d8eb664Po-Hsu Linlxc-create -t download -n $CONTAINER_NAME -- -d ubuntu -r $release -a $ARCH
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane GraberCONTAINER_PATH=$(dirname $(lxc-info -n $CONTAINER_NAME -c lxc.rootfs -H))
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Grabercp $CONTAINER_PATH/config $CONTAINER_PATH/config.bak
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Ensure it's not in lxc-autostart
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberlxc-autostart -L | grep -q $CONTAINER_NAME && \
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber (echo "Container shouldn't be auto-started" && exit 1)
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Mark it as auto-started and re-check
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberecho "lxc.start.auto = 1" >> $CONTAINER_PATH/config
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberlxc-autostart -L | grep -q $CONTAINER_NAME || \
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber (echo "Container should be auto-started" && exit 1)
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Put it in a test group and set a delay
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberecho "lxc.group = lxc-auto-test" >> $CONTAINER_PATH/config
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberecho "lxc.start.delay = 5" >> $CONTAINER_PATH/config
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Check that everything looks good
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberif [ "$(lxc-autostart -L -g lxc-auto-test | grep $CONTAINER_NAME)" != "$CONTAINER_NAME 5" ]; then
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberlxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't start" && exit 1)
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberlxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't restart" && exit 1)
c6b861ba646bd8f4d651b74e733256a7a3b81a32Stéphane Graber# When shutting down, the container calls sync. If the machine running
c6b861ba646bd8f4d651b74e733256a7a3b81a32Stéphane Graber# the test has a massive backlog, it can take minutes for the sync call to
c6b861ba646bd8f4d651b74e733256a7a3b81a32Stéphane Graber# finish, causing a test failure.
c6b861ba646bd8f4d651b74e733256a7a3b81a32Stéphane Graber# So try to reduce that by flushing everything to disk before we attempt
c6b861ba646bd8f4d651b74e733256a7a3b81a32Stéphane Graber# a container shutdown.
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graber# Shut it down
48356bbfdf8f277c5fe65287d76b7d891ca40964Stéphane Graberlxc-wait -n $CONTAINER_NAME -t 120 -s STOPPED || (echo "Container didn't stop" && exit 1)
45794802f13e5db18b5d7acb45169e1f4e8b70b2Stéphane Graberlxc-wait -n $CONTAINER_NAME -t 60 -s STOPPED || (echo "Container didn't die" && exit 1)