]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc-test-autostart
spelling: userns
[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 KNOWN_RELEASES="precise trusty xenial yakkety zesty"
26 cleanup() {
27 lxc-destroy -n $CONTAINER_NAME >/dev/null 2>&1 || true
28
29 if [ $DONE -eq 0 ]; then
30 echo "FAIL"
31 exit 1
32 fi
33 echo "PASS"
34 }
35
36 ARCH=i386
37 if type dpkg >/dev/null 2>&1; then
38 ARCH=$(dpkg --print-architecture)
39 fi
40
41 trap cleanup EXIT HUP INT TERM
42 set -eu
43
44 # Create a container
45 CONTAINER_NAME=lxc-test-auto
46
47 # default release is trusty, or the systems release if recognized
48 release=trusty
49 if [ -f /etc/lsb-release ]; then
50 . /etc/lsb-release
51 rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
52 rels="$KNOWN_RELEASES"
53 for r in $rels; do
54 [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
55 done
56 fi
57
58 lxc-create -t download -n $CONTAINER_NAME -B dir -- -d ubuntu -r $release -a $ARCH
59 CONTAINER_PATH=$(dirname $(lxc-info -n $CONTAINER_NAME -c lxc.rootfs.path -H) | sed -e 's/dir://')
60 cp $CONTAINER_PATH/config $CONTAINER_PATH/config.bak
61
62 # Ensure it's not in lxc-autostart
63 lxc-autostart -L | grep -q $CONTAINER_NAME && \
64 (echo "Container shouldn't be auto-started" && exit 1)
65
66 # Mark it as auto-started and re-check
67 echo "lxc.start.auto = 1" >> $CONTAINER_PATH/config
68 lxc-autostart -L | grep -q $CONTAINER_NAME || \
69 (echo "Container should be auto-started" && exit 1)
70
71 # Put it in a test group and set a delay
72 echo "lxc.group = lxc-auto-test" >> $CONTAINER_PATH/config
73 echo "lxc.start.delay = 5" >> $CONTAINER_PATH/config
74
75 # Check that everything looks good
76 if [ "$(lxc-autostart -L -g lxc-auto-test | grep $CONTAINER_NAME)" != "$CONTAINER_NAME 5" ]; then
77 echo "Invalid autostart setting" && exit 1
78 fi
79
80 # Start it
81 lxc-autostart -g lxc-auto-test
82 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't start" && exit 1)
83 sleep 5
84
85 # Restart it
86 lxc-autostart -g lxc-auto-test -r
87 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't restart" && exit 1)
88 sleep 5
89
90 # When shutting down, the container calls sync. If the machine running
91 # the test has a massive backlog, it can take minutes for the sync call to
92 # finish, causing a test failure.
93 # So try to reduce that by flushing everything to disk before we attempt
94 # a container shutdown.
95 sync
96
97 # Shut it down
98 lxc-autostart -g lxc-auto-test -s -t 120
99 lxc-wait -n $CONTAINER_NAME -t 120 -s STOPPED || (echo "Container didn't stop" && exit 1)
100
101 # Kill it
102 lxc-autostart -g lxc-auto-test -k
103 lxc-wait -n $CONTAINER_NAME -t 60 -s STOPPED || (echo "Container didn't die" && exit 1)
104
105 DONE=1