]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
tests: add tests making sure the exit code is appropriate. 3175/head
authorFlorian Margaine <florian@platform.sh>
Tue, 29 Oct 2019 09:53:17 +0000 (10:53 +0100)
committerFlorian Margaine <florian@platform.sh>
Tue, 29 Oct 2019 19:52:03 +0000 (20:52 +0100)
lxc2 broke this feature for lxc-execute, and lxc3 broke it for
lxc-attach. This adds a test making sure we don't do the same mistake
a third time.

Signed-off-by: Florian Margaine <florian@platform.sh>
src/tests/Makefile.am
src/tests/lxc-test-exit-code [new file with mode: 0755]

index 309212e9c93ac0e83a9e31d7190285802eff409b..ac0e2f3127ba725fe108e7f08451772508af500c 100644 (file)
@@ -108,6 +108,7 @@ bin_SCRIPTS += lxc-test-automount \
               lxc-test-autostart \
               lxc-test-cloneconfig \
               lxc-test-createconfig \
+              lxc-test-exit-code \
               lxc-test-no-new-privs \
               lxc-test-rootfs
 
diff --git a/src/tests/lxc-test-exit-code b/src/tests/lxc-test-exit-code
new file mode 100755 (executable)
index 0000000..b74ab08
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# lxc: linux Container library
+
+# Authors:
+# Florian Margaine <florian@platform.sh>
+#
+# This is a test script for the lxc-attach and lxc-execute
+# programs. It tests whether the exit code is not 0 when a script
+# fails to execute.
+
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+set -e
+
+FAIL() {
+       echo -n "Failed " >&2
+       echo "$*" >&2
+       lxc-destroy -n busy -f
+       exit 1
+}
+
+# Create a container
+lxc-create -t busybox -n busy || FAIL "creating busybox container"
+
+# Run lxc-execute to make sure it fails when the command fails, and
+# succeed when the command succeeds.
+lxc-execute -n busy -- sh -c 'exit 1' && FAIL "should be failing" || true
+lxc-execute -n busy -- sh -c 'exit 0' || FAIL "should be succeeding"
+
+# Now, start the container and wait for it to be in running state.
+lxc-start -n busy -d || FAIL "starting busybox container"
+lxc-wait -n busy -s RUNNING || FAIL "waiting for busybox container to run"
+
+# And run the same tests on lxc-attach.
+lxc-attach -n busy -- sh -c 'exit 1' && FAIL "should be failing" || true
+lxc-attach -n busy -- sh -c 'exit 0' || FAIL "should be succeeding"
+
+lxc-destroy -n busy -f
+
+exit 0