Debian Codegraph @

src/tests/lxc-test-autostart

raw
1 #!/bin/sh
2
3 # SPDX-License-Identifier: LGPL-2.1+
4
5 # lxc: linux Container library
6
7 DONE=0
8 cleanup() {
9 lxc-destroy -n $CONTAINER_NAME >/dev/null 2>&1 || true
10
11 if [ $DONE -eq 0 ]; then
12 echo "FAIL"
13 exit 1
14 fi
15 echo "PASS"
16 }
17
18 trap cleanup EXIT HUP INT TERM
19 set -eu
20
21 # Create a container
22 CONTAINER_NAME=lxc-test-auto
23
24 lxc-create -t busybox -n $CONTAINER_NAME -B dir
25 CONTAINER_PATH=$(dirname $(lxc-info -n $CONTAINER_NAME -c lxc.rootfs.path -H) | sed -e 's/dir://')
26 cp $CONTAINER_PATH/config $CONTAINER_PATH/config.bak
27
28 # Ensure it's not in lxc-autostart
29 lxc-autostart -L | grep -q $CONTAINER_NAME && \
30 (echo "Container shouldn't be auto-started" && exit 1)
31
32 # Mark it as auto-started and re-check
33 echo "lxc.start.auto = 1" >> $CONTAINER_PATH/config
34 lxc-autostart -L | grep -q $CONTAINER_NAME || \
35 (echo "Container should be auto-started" && exit 1)
36
37 # Put it in a test group and set a delay
38 echo "lxc.group = lxc-auto-test" >> $CONTAINER_PATH/config
39 echo "lxc.start.delay = 5" >> $CONTAINER_PATH/config
40
41 # Check that everything looks good
42 if [ "$(lxc-autostart -L -g lxc-auto-test | grep $CONTAINER_NAME)" != "$CONTAINER_NAME 5" ]; then
43 echo "Invalid autostart setting" && exit 1
44 fi
45
46 # Start it
47 lxc-autostart -g lxc-auto-test
48 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't start" && exit 1)
49 sleep 5
50
51 # Restart it
52 lxc-autostart -g lxc-auto-test -r
53 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't restart" && exit 1)
54 sleep 5
55
56 # When shutting down, the container calls sync. If the machine running
57 # the test has a massive backlog, it can take minutes for the sync call to
58 # finish, causing a test failure.
59 # So try to reduce that by flushing everything to disk before we attempt
60 # a container shutdown.
61 sync
62
63 # Shut it down
64 lxc-autostart -g lxc-auto-test -s -t 120
65 lxc-wait -n $CONTAINER_NAME -t 120 -s STOPPED || (echo "Container didn't stop" && exit 1)
66
67 # Kill it
68 lxc-autostart -g lxc-auto-test -k
69 lxc-wait -n $CONTAINER_NAME -t 60 -s STOPPED || (echo "Container didn't die" && exit 1)
70
71 DONE=1