]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc-test-autostart
tests: fix get_item tests
[mirror_lxc.git] / src / tests / lxc-test-autostart
1 #!/bin/sh
2
3 # lxc: linux Container library
4
5 # Authors:
6 # Stéphane Graber <stgraber@ubuntu.com>
7 #
8 # This is a test script for lxc-autostart
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
24 DONE=0
25 cleanup() {
26 lxc-destroy -n $CONTAINER_NAME >/dev/null 2>&1 || true
27
28 if [ $DONE -eq 0 ]; then
29 echo "FAIL"
30 exit 1
31 fi
32 echo "PASS"
33 }
34
35 ARCH=i386
36 if type dpkg >/dev/null 2>&1; then
37 ARCH=$(dpkg --print-architecture)
38 fi
39
40 trap cleanup EXIT HUP INT TERM
41 set -eu
42
43 # Create a container
44 CONTAINER_NAME=lxc-test-auto
45 lxc-create -t download -n $CONTAINER_NAME -- -d ubuntu -r trusty -a $ARCH
46 CONTAINER_PATH=$(dirname $(lxc-info -n $CONTAINER_NAME -c lxc.rootfs -H))
47 cp $CONTAINER_PATH/config $CONTAINER_PATH/config.bak
48
49 # Ensure it's not in lxc-autostart
50 lxc-autostart -L | grep -q $CONTAINER_NAME && \
51 (echo "Container shouldn't be auto-started" && exit 1)
52
53 # Mark it as auto-started and re-check
54 echo "lxc.start.auto = 1" >> $CONTAINER_PATH/config
55 lxc-autostart -L | grep -q $CONTAINER_NAME || \
56 (echo "Container should be auto-started" && exit 1)
57
58 # Put it in a test group and set a delay
59 echo "lxc.group = lxc-auto-test" >> $CONTAINER_PATH/config
60 echo "lxc.start.delay = 5" >> $CONTAINER_PATH/config
61
62 # Check that everything looks good
63 if [ "$(lxc-autostart -L -g lxc-auto-test | grep $CONTAINER_NAME)" != "$CONTAINER_NAME 5" ]; then
64 echo "Invalid autostart setting" && exit 1
65 fi
66
67 # Start it
68 lxc-autostart -g lxc-auto-test
69 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't start" && exit 1)
70 sleep 5
71
72 # Restart it
73 lxc-autostart -g lxc-auto-test -r
74 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't restart" && exit 1)
75 sleep 5
76
77 # When shutting down, the container calls sync. If the machine running
78 # the test has a massive backlog, it can take minutes for the sync call to
79 # finish, causing a test failure.
80 # So try to reduce that by flushing everything to disk before we attempt
81 # a container shutdown.
82 sync
83
84 # Shut it down
85 lxc-autostart -g lxc-auto-test -s -t 120
86 lxc-wait -n $CONTAINER_NAME -t 120 -s STOPPED || (echo "Container didn't stop" && exit 1)
87
88 # Kill it
89 lxc-autostart -g lxc-auto-test -k
90 lxc-wait -n $CONTAINER_NAME -t 60 -s STOPPED || (echo "Container didn't die" && exit 1)
91
92 DONE=1